diff --git a/sys/cddl/contrib/opensolaris/uts/common/dtrace/dtrace.c b/sys/cddl/contrib/opensolaris/uts/common/dtrace/dtrace.c
index b2e7b01b125c..76aaa0920f8a 100644
--- a/sys/cddl/contrib/opensolaris/uts/common/dtrace/dtrace.c
+++ b/sys/cddl/contrib/opensolaris/uts/common/dtrace/dtrace.c
@@ -1,18452 +1,18452 @@
 /*
  * CDDL HEADER START
  *
  * The contents of this file are subject to the terms of the
  * Common Development and Distribution License (the "License").
  * You may not use this file except in compliance with the License.
  *
  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  * or http://www.opensolaris.org/os/licensing.
  * See the License for the specific language governing permissions
  * and limitations under the License.
  *
  * When distributing Covered Code, include this CDDL HEADER in each
  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  * If applicable, add the following below this CDDL HEADER, with the
  * fields enclosed by brackets "[]" replaced with your own identifying
  * information: Portions Copyright [yyyy] [name of copyright owner]
  *
  * CDDL HEADER END
  *
  * $FreeBSD$
  */
 
 /*
  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
  * Copyright (c) 2016, Joyent, Inc. All rights reserved.
  * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
  */
 
 /*
  * DTrace - Dynamic Tracing for Solaris
  *
  * This is the implementation of the Solaris Dynamic Tracing framework
  * (DTrace).  The user-visible interface to DTrace is described at length in
  * the "Solaris Dynamic Tracing Guide".  The interfaces between the libdtrace
  * library, the in-kernel DTrace framework, and the DTrace providers are
  * described in the block comments in the <sys/dtrace.h> header file.  The
  * internal architecture of DTrace is described in the block comments in the
  * <sys/dtrace_impl.h> header file.  The comments contained within the DTrace
  * implementation very much assume mastery of all of these sources; if one has
  * an unanswered question about the implementation, one should consult them
  * first.
  *
  * The functions here are ordered roughly as follows:
  *
  *   - Probe context functions
  *   - Probe hashing functions
  *   - Non-probe context utility functions
  *   - Matching functions
  *   - Provider-to-Framework API functions
  *   - Probe management functions
  *   - DIF object functions
  *   - Format functions
  *   - Predicate functions
  *   - ECB functions
  *   - Buffer functions
  *   - Enabling functions
  *   - DOF functions
  *   - Anonymous enabling functions
  *   - Consumer state functions
  *   - Helper functions
  *   - Hook functions
  *   - Driver cookbook functions
  *
  * Each group of functions begins with a block comment labelled the "DTrace
  * [Group] Functions", allowing one to find each block by searching forward
  * on capital-f functions.
  */
 #include <sys/errno.h>
 #include <sys/param.h>
 #include <sys/types.h>
 #ifndef illumos
 #include <sys/time.h>
 #endif
 #include <sys/stat.h>
 #include <sys/conf.h>
 #include <sys/systm.h>
 #include <sys/endian.h>
 #ifdef illumos
 #include <sys/ddi.h>
 #include <sys/sunddi.h>
 #endif
 #include <sys/cpuvar.h>
 #include <sys/kmem.h>
 #ifdef illumos
 #include <sys/strsubr.h>
 #endif
 #include <sys/sysmacros.h>
 #include <sys/dtrace_impl.h>
 #include <sys/atomic.h>
 #include <sys/cmn_err.h>
 #ifdef illumos
 #include <sys/mutex_impl.h>
 #include <sys/rwlock_impl.h>
 #endif
 #include <sys/ctf_api.h>
 #ifdef illumos
 #include <sys/panic.h>
 #include <sys/priv_impl.h>
 #endif
 #ifdef illumos
 #include <sys/cred_impl.h>
 #include <sys/procfs_isa.h>
 #endif
 #include <sys/taskq.h>
 #ifdef illumos
 #include <sys/mkdev.h>
 #include <sys/kdi.h>
 #endif
 #include <sys/zone.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include "strtolctype.h"
 
 /* FreeBSD includes: */
 #ifndef illumos
 #include <sys/callout.h>
 #include <sys/ctype.h>
 #include <sys/eventhandler.h>
 #include <sys/limits.h>
 #include <sys/linker.h>
 #include <sys/kdb.h>
 #include <sys/jail.h>
 #include <sys/kernel.h>
 #include <sys/malloc.h>
 #include <sys/lock.h>
 #include <sys/mutex.h>
 #include <sys/ptrace.h>
 #include <sys/random.h>
 #include <sys/rwlock.h>
 #include <sys/sx.h>
 #include <sys/sysctl.h>
 
 
 #include <sys/mount.h>
 #undef AT_UID
 #undef AT_GID
 #include <sys/vnode.h>
 #include <sys/cred.h>
 
 #include <sys/dtrace_bsd.h>
 
 #include <netinet/in.h>
 
 #include "dtrace_cddl.h"
 #include "dtrace_debug.c"
 #endif
 
 #include "dtrace_xoroshiro128_plus.h"
 
 /*
  * DTrace Tunable Variables
  *
  * The following variables may be tuned by adding a line to /etc/system that
  * includes both the name of the DTrace module ("dtrace") and the name of the
  * variable.  For example:
  *
  *   set dtrace:dtrace_destructive_disallow = 1
  *
  * In general, the only variables that one should be tuning this way are those
  * that affect system-wide DTrace behavior, and for which the default behavior
  * is undesirable.  Most of these variables are tunable on a per-consumer
  * basis using DTrace options, and need not be tuned on a system-wide basis.
  * When tuning these variables, avoid pathological values; while some attempt
  * is made to verify the integrity of these variables, they are not considered
  * part of the supported interface to DTrace, and they are therefore not
  * checked comprehensively.  Further, these variables should not be tuned
  * dynamically via "mdb -kw" or other means; they should only be tuned via
  * /etc/system.
  */
 int		dtrace_destructive_disallow = 0;
 #ifndef illumos
 /* Positive logic version of dtrace_destructive_disallow for loader tunable */
 int		dtrace_allow_destructive = 1;
 #endif
 dtrace_optval_t	dtrace_nonroot_maxsize = (16 * 1024 * 1024);
 size_t		dtrace_difo_maxsize = (256 * 1024);
 dtrace_optval_t	dtrace_dof_maxsize = (8 * 1024 * 1024);
 size_t		dtrace_statvar_maxsize = (16 * 1024);
 size_t		dtrace_actions_max = (16 * 1024);
 size_t		dtrace_retain_max = 1024;
 dtrace_optval_t	dtrace_helper_actions_max = 128;
 dtrace_optval_t	dtrace_helper_providers_max = 32;
 dtrace_optval_t	dtrace_dstate_defsize = (1 * 1024 * 1024);
 size_t		dtrace_strsize_default = 256;
 dtrace_optval_t	dtrace_cleanrate_default = 9900990;		/* 101 hz */
 dtrace_optval_t	dtrace_cleanrate_min = 200000;			/* 5000 hz */
 dtrace_optval_t	dtrace_cleanrate_max = (uint64_t)60 * NANOSEC;	/* 1/minute */
 dtrace_optval_t	dtrace_aggrate_default = NANOSEC;		/* 1 hz */
 dtrace_optval_t	dtrace_statusrate_default = NANOSEC;		/* 1 hz */
 dtrace_optval_t dtrace_statusrate_max = (hrtime_t)10 * NANOSEC;	 /* 6/minute */
 dtrace_optval_t	dtrace_switchrate_default = NANOSEC;		/* 1 hz */
 dtrace_optval_t	dtrace_nspec_default = 1;
 dtrace_optval_t	dtrace_specsize_default = 32 * 1024;
 dtrace_optval_t dtrace_stackframes_default = 20;
 dtrace_optval_t dtrace_ustackframes_default = 20;
 dtrace_optval_t dtrace_jstackframes_default = 50;
 dtrace_optval_t dtrace_jstackstrsize_default = 512;
 int		dtrace_msgdsize_max = 128;
 hrtime_t	dtrace_chill_max = MSEC2NSEC(500);		/* 500 ms */
 hrtime_t	dtrace_chill_interval = NANOSEC;		/* 1000 ms */
 int		dtrace_devdepth_max = 32;
 int		dtrace_err_verbose;
 hrtime_t	dtrace_deadman_interval = NANOSEC;
 hrtime_t	dtrace_deadman_timeout = (hrtime_t)10 * NANOSEC;
 hrtime_t	dtrace_deadman_user = (hrtime_t)30 * NANOSEC;
 hrtime_t	dtrace_unregister_defunct_reap = (hrtime_t)60 * NANOSEC;
 #ifndef illumos
 int		dtrace_memstr_max = 4096;
 int		dtrace_bufsize_max_frac = 128;
 #endif
 
 /*
  * DTrace External Variables
  *
  * As dtrace(7D) is a kernel module, any DTrace variables are obviously
  * available to DTrace consumers via the backtick (`) syntax.  One of these,
  * dtrace_zero, is made deliberately so:  it is provided as a source of
  * well-known, zero-filled memory.  While this variable is not documented,
  * it is used by some translators as an implementation detail.
  */
 const char	dtrace_zero[256] = { 0 };	/* zero-filled memory */
 
 /*
  * DTrace Internal Variables
  */
 #ifdef illumos
 static dev_info_t	*dtrace_devi;		/* device info */
 #endif
 #ifdef illumos
 static vmem_t		*dtrace_arena;		/* probe ID arena */
 static vmem_t		*dtrace_minor;		/* minor number arena */
 #else
 static taskq_t		*dtrace_taskq;		/* task queue */
 static struct unrhdr	*dtrace_arena;		/* Probe ID number.     */
 #endif
 static dtrace_probe_t	**dtrace_probes;	/* array of all probes */
 static int		dtrace_nprobes;		/* number of probes */
 static dtrace_provider_t *dtrace_provider;	/* provider list */
 static dtrace_meta_t	*dtrace_meta_pid;	/* user-land meta provider */
 static int		dtrace_opens;		/* number of opens */
 static int		dtrace_helpers;		/* number of helpers */
 static int		dtrace_getf;		/* number of unpriv getf()s */
 #ifdef illumos
 static void		*dtrace_softstate;	/* softstate pointer */
 #endif
 static dtrace_hash_t	*dtrace_bymod;		/* probes hashed by module */
 static dtrace_hash_t	*dtrace_byfunc;		/* probes hashed by function */
 static dtrace_hash_t	*dtrace_byname;		/* probes hashed by name */
 static dtrace_toxrange_t *dtrace_toxrange;	/* toxic range array */
 static int		dtrace_toxranges;	/* number of toxic ranges */
 static int		dtrace_toxranges_max;	/* size of toxic range array */
 static dtrace_anon_t	dtrace_anon;		/* anonymous enabling */
 static kmem_cache_t	*dtrace_state_cache;	/* cache for dynamic state */
 static uint64_t		dtrace_vtime_references; /* number of vtimestamp refs */
 static kthread_t	*dtrace_panicked;	/* panicking thread */
 static dtrace_ecb_t	*dtrace_ecb_create_cache; /* cached created ECB */
 static dtrace_genid_t	dtrace_probegen;	/* current probe generation */
 static dtrace_helpers_t *dtrace_deferred_pid;	/* deferred helper list */
 static dtrace_enabling_t *dtrace_retained;	/* list of retained enablings */
 static dtrace_genid_t	dtrace_retained_gen;	/* current retained enab gen */
 static dtrace_dynvar_t	dtrace_dynhash_sink;	/* end of dynamic hash chains */
 static int		dtrace_dynvar_failclean; /* dynvars failed to clean */
 #ifndef illumos
 static struct mtx	dtrace_unr_mtx;
 MTX_SYSINIT(dtrace_unr_mtx, &dtrace_unr_mtx, "Unique resource identifier", MTX_DEF);
 static eventhandler_tag	dtrace_kld_load_tag;
 static eventhandler_tag	dtrace_kld_unload_try_tag;
 #endif
 
 /*
  * DTrace Locking
  * DTrace is protected by three (relatively coarse-grained) locks:
  *
  * (1) dtrace_lock is required to manipulate essentially any DTrace state,
  *     including enabling state, probes, ECBs, consumer state, helper state,
  *     etc.  Importantly, dtrace_lock is _not_ required when in probe context;
  *     probe context is lock-free -- synchronization is handled via the
  *     dtrace_sync() cross call mechanism.
  *
  * (2) dtrace_provider_lock is required when manipulating provider state, or
  *     when provider state must be held constant.
  *
  * (3) dtrace_meta_lock is required when manipulating meta provider state, or
  *     when meta provider state must be held constant.
  *
  * The lock ordering between these three locks is dtrace_meta_lock before
  * dtrace_provider_lock before dtrace_lock.  (In particular, there are
  * several places where dtrace_provider_lock is held by the framework as it
  * calls into the providers -- which then call back into the framework,
  * grabbing dtrace_lock.)
  *
  * There are two other locks in the mix:  mod_lock and cpu_lock.  With respect
  * to dtrace_provider_lock and dtrace_lock, cpu_lock continues its historical
  * role as a coarse-grained lock; it is acquired before both of these locks.
  * With respect to dtrace_meta_lock, its behavior is stranger:  cpu_lock must
  * be acquired _between_ dtrace_meta_lock and any other DTrace locks.
  * mod_lock is similar with respect to dtrace_provider_lock in that it must be
  * acquired _between_ dtrace_provider_lock and dtrace_lock.
  */
 static kmutex_t		dtrace_lock;		/* probe state lock */
 static kmutex_t		dtrace_provider_lock;	/* provider state lock */
 static kmutex_t		dtrace_meta_lock;	/* meta-provider state lock */
 
 #ifndef illumos
 /* XXX FreeBSD hacks. */
 #define cr_suid		cr_svuid
 #define cr_sgid		cr_svgid
 #define	ipaddr_t	in_addr_t
 #define mod_modname	pathname
 #define vuprintf	vprintf
 #ifndef crgetzoneid
 #define crgetzoneid(_a)        0
 #endif
 #define ttoproc(_a)	((_a)->td_proc)
 #define SNOCD		0
 #define CPU_ON_INTR(_a)	0
 
 #define PRIV_EFFECTIVE		(1 << 0)
 #define PRIV_DTRACE_KERNEL	(1 << 1)
 #define PRIV_DTRACE_PROC	(1 << 2)
 #define PRIV_DTRACE_USER	(1 << 3)
 #define PRIV_PROC_OWNER		(1 << 4)
 #define PRIV_PROC_ZONE		(1 << 5)
 #define PRIV_ALL		~0
 
 SYSCTL_DECL(_debug_dtrace);
 SYSCTL_DECL(_kern_dtrace);
 #endif
 
 #ifdef illumos
 #define curcpu	CPU->cpu_id
 #endif
 
 
 /*
  * DTrace Provider Variables
  *
  * These are the variables relating to DTrace as a provider (that is, the
  * provider of the BEGIN, END, and ERROR probes).
  */
 static dtrace_pattr_t	dtrace_provider_attr = {
 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON },
 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON },
 { DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON },
 };
 
 static void
 dtrace_nullop(void)
 {}
 
 static dtrace_pops_t dtrace_provider_ops = {
 	.dtps_provide =	(void (*)(void *, dtrace_probedesc_t *))dtrace_nullop,
 	.dtps_provide_module =	(void (*)(void *, modctl_t *))dtrace_nullop,
 	.dtps_enable =	(void (*)(void *, dtrace_id_t, void *))dtrace_nullop,
 	.dtps_disable =	(void (*)(void *, dtrace_id_t, void *))dtrace_nullop,
 	.dtps_suspend =	(void (*)(void *, dtrace_id_t, void *))dtrace_nullop,
 	.dtps_resume =	(void (*)(void *, dtrace_id_t, void *))dtrace_nullop,
 	.dtps_getargdesc =	NULL,
 	.dtps_getargval =	NULL,
 	.dtps_usermode =	NULL,
 	.dtps_destroy =	(void (*)(void *, dtrace_id_t, void *))dtrace_nullop,
 };
 
 static dtrace_id_t	dtrace_probeid_begin;	/* special BEGIN probe */
 static dtrace_id_t	dtrace_probeid_end;	/* special END probe */
 dtrace_id_t		dtrace_probeid_error;	/* special ERROR probe */
 
 /*
  * DTrace Helper Tracing Variables
  *
  * These variables should be set dynamically to enable helper tracing.  The
  * only variables that should be set are dtrace_helptrace_enable (which should
  * be set to a non-zero value to allocate helper tracing buffers on the next
  * open of /dev/dtrace) and dtrace_helptrace_disable (which should be set to a
  * non-zero value to deallocate helper tracing buffers on the next close of
  * /dev/dtrace).  When (and only when) helper tracing is disabled, the
  * buffer size may also be set via dtrace_helptrace_bufsize.
  */
 int			dtrace_helptrace_enable = 0;
 int			dtrace_helptrace_disable = 0;
 int			dtrace_helptrace_bufsize = 16 * 1024 * 1024;
 uint32_t		dtrace_helptrace_nlocals;
 static dtrace_helptrace_t *dtrace_helptrace_buffer;
 static uint32_t		dtrace_helptrace_next = 0;
 static int		dtrace_helptrace_wrapped = 0;
 
 /*
  * DTrace Error Hashing
  *
  * On DEBUG kernels, DTrace will track the errors that has seen in a hash
  * table.  This is very useful for checking coverage of tests that are
  * expected to induce DIF or DOF processing errors, and may be useful for
  * debugging problems in the DIF code generator or in DOF generation .  The
  * error hash may be examined with the ::dtrace_errhash MDB dcmd.
  */
 #ifdef DEBUG
 static dtrace_errhash_t	dtrace_errhash[DTRACE_ERRHASHSZ];
 static const char *dtrace_errlast;
 static kthread_t *dtrace_errthread;
 static kmutex_t dtrace_errlock;
 #endif
 
 /*
  * DTrace Macros and Constants
  *
  * These are various macros that are useful in various spots in the
  * implementation, along with a few random constants that have no meaning
  * outside of the implementation.  There is no real structure to this cpp
  * mishmash -- but is there ever?
  */
 #define	DTRACE_HASHSTR(hash, probe)	\
 	dtrace_hash_str(*((char **)((uintptr_t)(probe) + (hash)->dth_stroffs)))
 
 #define	DTRACE_HASHNEXT(hash, probe)	\
 	(dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_nextoffs)
 
 #define	DTRACE_HASHPREV(hash, probe)	\
 	(dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_prevoffs)
 
 #define	DTRACE_HASHEQ(hash, lhs, rhs)	\
 	(strcmp(*((char **)((uintptr_t)(lhs) + (hash)->dth_stroffs)), \
 	    *((char **)((uintptr_t)(rhs) + (hash)->dth_stroffs))) == 0)
 
 #define	DTRACE_AGGHASHSIZE_SLEW		17
 
 #define	DTRACE_V4MAPPED_OFFSET		(sizeof (uint32_t) * 3)
 
 /*
  * The key for a thread-local variable consists of the lower 61 bits of the
  * t_did, plus the 3 bits of the highest active interrupt above LOCK_LEVEL.
  * We add DIF_VARIABLE_MAX to t_did to assure that the thread key is never
  * equal to a variable identifier.  This is necessary (but not sufficient) to
  * assure that global associative arrays never collide with thread-local
  * variables.  To guarantee that they cannot collide, we must also define the
  * order for keying dynamic variables.  That order is:
  *
  *   [ key0 ] ... [ keyn ] [ variable-key ] [ tls-key ]
  *
  * Because the variable-key and the tls-key are in orthogonal spaces, there is
  * no way for a global variable key signature to match a thread-local key
  * signature.
  */
 #ifdef illumos
 #define	DTRACE_TLS_THRKEY(where) { \
 	uint_t intr = 0; \
 	uint_t actv = CPU->cpu_intr_actv >> (LOCK_LEVEL + 1); \
 	for (; actv; actv >>= 1) \
 		intr++; \
 	ASSERT(intr < (1 << 3)); \
 	(where) = ((curthread->t_did + DIF_VARIABLE_MAX) & \
 	    (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \
 }
 #else
 #define	DTRACE_TLS_THRKEY(where) { \
 	solaris_cpu_t *_c = &solaris_cpu[curcpu]; \
 	uint_t intr = 0; \
 	uint_t actv = _c->cpu_intr_actv; \
 	for (; actv; actv >>= 1) \
 		intr++; \
 	ASSERT(intr < (1 << 3)); \
 	(where) = ((curthread->td_tid + DIF_VARIABLE_MAX) & \
 	    (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \
 }
 #endif
 
 #define	DT_BSWAP_8(x)	((x) & 0xff)
 #define	DT_BSWAP_16(x)	((DT_BSWAP_8(x) << 8) | DT_BSWAP_8((x) >> 8))
 #define	DT_BSWAP_32(x)	((DT_BSWAP_16(x) << 16) | DT_BSWAP_16((x) >> 16))
 #define	DT_BSWAP_64(x)	((DT_BSWAP_32(x) << 32) | DT_BSWAP_32((x) >> 32))
 
 #define	DT_MASK_LO 0x00000000FFFFFFFFULL
 
 #define	DTRACE_STORE(type, tomax, offset, what) \
 	*((type *)((uintptr_t)(tomax) + (uintptr_t)offset)) = (type)(what);
 
 #if !defined(__x86) && !defined(__aarch64__)
 #define	DTRACE_ALIGNCHECK(addr, size, flags)				\
 	if (addr & (size - 1)) {					\
 		*flags |= CPU_DTRACE_BADALIGN;				\
 		cpu_core[curcpu].cpuc_dtrace_illval = addr;	\
 		return (0);						\
 	}
 #else
 #define	DTRACE_ALIGNCHECK(addr, size, flags)
 #endif
 
 /*
  * Test whether a range of memory starting at testaddr of size testsz falls
  * within the range of memory described by addr, sz.  We take care to avoid
  * problems with overflow and underflow of the unsigned quantities, and
  * disallow all negative sizes.  Ranges of size 0 are allowed.
  */
 #define	DTRACE_INRANGE(testaddr, testsz, baseaddr, basesz) \
 	((testaddr) - (uintptr_t)(baseaddr) < (basesz) && \
 	(testaddr) + (testsz) - (uintptr_t)(baseaddr) <= (basesz) && \
 	(testaddr) + (testsz) >= (testaddr))
 
 #define	DTRACE_RANGE_REMAIN(remp, addr, baseaddr, basesz)		\
 do {									\
 	if ((remp) != NULL) {						\
 		*(remp) = (uintptr_t)(baseaddr) + (basesz) - (addr);	\
 	}								\
 } while (0)
 
 
 /*
  * Test whether alloc_sz bytes will fit in the scratch region.  We isolate
  * alloc_sz on the righthand side of the comparison in order to avoid overflow
  * or underflow in the comparison with it.  This is simpler than the INRANGE
  * check above, because we know that the dtms_scratch_ptr is valid in the
  * range.  Allocations of size zero are allowed.
  */
 #define	DTRACE_INSCRATCH(mstate, alloc_sz) \
 	((mstate)->dtms_scratch_base + (mstate)->dtms_scratch_size - \
 	(mstate)->dtms_scratch_ptr >= (alloc_sz))
 
 #define	DTRACE_LOADFUNC(bits)						\
 /*CSTYLED*/								\
 uint##bits##_t								\
 dtrace_load##bits(uintptr_t addr)					\
 {									\
 	size_t size = bits / NBBY;					\
 	/*CSTYLED*/							\
 	uint##bits##_t rval;						\
 	int i;								\
 	volatile uint16_t *flags = (volatile uint16_t *)		\
 	    &cpu_core[curcpu].cpuc_dtrace_flags;			\
 									\
 	DTRACE_ALIGNCHECK(addr, size, flags);				\
 									\
 	for (i = 0; i < dtrace_toxranges; i++) {			\
 		if (addr >= dtrace_toxrange[i].dtt_limit)		\
 			continue;					\
 									\
 		if (addr + size <= dtrace_toxrange[i].dtt_base)		\
 			continue;					\
 									\
 		/*							\
 		 * This address falls within a toxic region; return 0.	\
 		 */							\
 		*flags |= CPU_DTRACE_BADADDR;				\
 		cpu_core[curcpu].cpuc_dtrace_illval = addr;		\
 		return (0);						\
 	}								\
 									\
 	*flags |= CPU_DTRACE_NOFAULT;					\
 	/*CSTYLED*/							\
 	rval = *((volatile uint##bits##_t *)addr);			\
 	*flags &= ~CPU_DTRACE_NOFAULT;					\
 									\
 	return (!(*flags & CPU_DTRACE_FAULT) ? rval : 0);		\
 }
 
 #ifdef _LP64
 #define	dtrace_loadptr	dtrace_load64
 #else
 #define	dtrace_loadptr	dtrace_load32
 #endif
 
 #define	DTRACE_DYNHASH_FREE	0
 #define	DTRACE_DYNHASH_SINK	1
 #define	DTRACE_DYNHASH_VALID	2
 
 #define	DTRACE_MATCH_NEXT	0
 #define	DTRACE_MATCH_DONE	1
 #define	DTRACE_ANCHORED(probe)	((probe)->dtpr_func[0] != '\0')
 #define	DTRACE_STATE_ALIGN	64
 
 #define	DTRACE_FLAGS2FLT(flags)						\
 	(((flags) & CPU_DTRACE_BADADDR) ? DTRACEFLT_BADADDR :		\
 	((flags) & CPU_DTRACE_ILLOP) ? DTRACEFLT_ILLOP :		\
 	((flags) & CPU_DTRACE_DIVZERO) ? DTRACEFLT_DIVZERO :		\
 	((flags) & CPU_DTRACE_KPRIV) ? DTRACEFLT_KPRIV :		\
 	((flags) & CPU_DTRACE_UPRIV) ? DTRACEFLT_UPRIV :		\
 	((flags) & CPU_DTRACE_TUPOFLOW) ?  DTRACEFLT_TUPOFLOW :		\
 	((flags) & CPU_DTRACE_BADALIGN) ?  DTRACEFLT_BADALIGN :		\
 	((flags) & CPU_DTRACE_NOSCRATCH) ?  DTRACEFLT_NOSCRATCH :	\
 	((flags) & CPU_DTRACE_BADSTACK) ?  DTRACEFLT_BADSTACK :		\
 	DTRACEFLT_UNKNOWN)
 
 #define	DTRACEACT_ISSTRING(act)						\
 	((act)->dta_kind == DTRACEACT_DIFEXPR &&			\
 	(act)->dta_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING)
 
 /* Function prototype definitions: */
 static size_t dtrace_strlen(const char *, size_t);
 static dtrace_probe_t *dtrace_probe_lookup_id(dtrace_id_t id);
 static void dtrace_enabling_provide(dtrace_provider_t *);
 static int dtrace_enabling_match(dtrace_enabling_t *, int *);
 static void dtrace_enabling_matchall(void);
 static void dtrace_enabling_reap(void);
 static dtrace_state_t *dtrace_anon_grab(void);
 static uint64_t dtrace_helper(int, dtrace_mstate_t *,
     dtrace_state_t *, uint64_t, uint64_t);
 static dtrace_helpers_t *dtrace_helpers_create(proc_t *);
 static void dtrace_buffer_drop(dtrace_buffer_t *);
 static int dtrace_buffer_consumed(dtrace_buffer_t *, hrtime_t when);
 static intptr_t dtrace_buffer_reserve(dtrace_buffer_t *, size_t, size_t,
     dtrace_state_t *, dtrace_mstate_t *);
 static int dtrace_state_option(dtrace_state_t *, dtrace_optid_t,
     dtrace_optval_t);
 static int dtrace_ecb_create_enable(dtrace_probe_t *, void *);
 static void dtrace_helper_provider_destroy(dtrace_helper_provider_t *);
 uint16_t dtrace_load16(uintptr_t);
 uint32_t dtrace_load32(uintptr_t);
 uint64_t dtrace_load64(uintptr_t);
 uint8_t dtrace_load8(uintptr_t);
 void dtrace_dynvar_clean(dtrace_dstate_t *);
 dtrace_dynvar_t *dtrace_dynvar(dtrace_dstate_t *, uint_t, dtrace_key_t *,
     size_t, dtrace_dynvar_op_t, dtrace_mstate_t *, dtrace_vstate_t *);
 uintptr_t dtrace_dif_varstr(uintptr_t, dtrace_state_t *, dtrace_mstate_t *);
 static int dtrace_priv_proc(dtrace_state_t *);
 static void dtrace_getf_barrier(void);
 static int dtrace_canload_remains(uint64_t, size_t, size_t *,
     dtrace_mstate_t *, dtrace_vstate_t *);
 static int dtrace_canstore_remains(uint64_t, size_t, size_t *,
     dtrace_mstate_t *, dtrace_vstate_t *);
 
 /*
  * DTrace Probe Context Functions
  *
  * These functions are called from probe context.  Because probe context is
  * any context in which C may be called, arbitrarily locks may be held,
  * interrupts may be disabled, we may be in arbitrary dispatched state, etc.
  * As a result, functions called from probe context may only call other DTrace
  * support functions -- they may not interact at all with the system at large.
  * (Note that the ASSERT macro is made probe-context safe by redefining it in
  * terms of dtrace_assfail(), a probe-context safe function.) If arbitrary
  * loads are to be performed from probe context, they _must_ be in terms of
  * the safe dtrace_load*() variants.
  *
  * Some functions in this block are not actually called from probe context;
  * for these functions, there will be a comment above the function reading
  * "Note:  not called from probe context."
  */
 void
 dtrace_panic(const char *format, ...)
 {
 	va_list alist;
 
 	va_start(alist, format);
 #ifdef __FreeBSD__
 	vpanic(format, alist);
 #else
 	dtrace_vpanic(format, alist);
 #endif
 	va_end(alist);
 }
 
 int
 dtrace_assfail(const char *a, const char *f, int l)
 {
 	dtrace_panic("assertion failed: %s, file: %s, line: %d", a, f, l);
 
 	/*
 	 * We just need something here that even the most clever compiler
 	 * cannot optimize away.
 	 */
 	return (a[(uintptr_t)f]);
 }
 
 /*
  * Atomically increment a specified error counter from probe context.
  */
 static void
 dtrace_error(uint32_t *counter)
 {
 	/*
 	 * Most counters stored to in probe context are per-CPU counters.
 	 * However, there are some error conditions that are sufficiently
 	 * arcane that they don't merit per-CPU storage.  If these counters
 	 * are incremented concurrently on different CPUs, scalability will be
 	 * adversely affected -- but we don't expect them to be white-hot in a
 	 * correctly constructed enabling...
 	 */
 	uint32_t oval, nval;
 
 	do {
 		oval = *counter;
 
 		if ((nval = oval + 1) == 0) {
 			/*
 			 * If the counter would wrap, set it to 1 -- assuring
 			 * that the counter is never zero when we have seen
 			 * errors.  (The counter must be 32-bits because we
 			 * aren't guaranteed a 64-bit compare&swap operation.)
 			 * To save this code both the infamy of being fingered
 			 * by a priggish news story and the indignity of being
 			 * the target of a neo-puritan witch trial, we're
 			 * carefully avoiding any colorful description of the
 			 * likelihood of this condition -- but suffice it to
 			 * say that it is only slightly more likely than the
 			 * overflow of predicate cache IDs, as discussed in
 			 * dtrace_predicate_create().
 			 */
 			nval = 1;
 		}
 	} while (dtrace_cas32(counter, oval, nval) != oval);
 }
 
 /*
  * Use the DTRACE_LOADFUNC macro to define functions for each of loading a
  * uint8_t, a uint16_t, a uint32_t and a uint64_t.
  */
 /* BEGIN CSTYLED */
 DTRACE_LOADFUNC(8)
 DTRACE_LOADFUNC(16)
 DTRACE_LOADFUNC(32)
 DTRACE_LOADFUNC(64)
 /* END CSTYLED */
 
 static int
 dtrace_inscratch(uintptr_t dest, size_t size, dtrace_mstate_t *mstate)
 {
 	if (dest < mstate->dtms_scratch_base)
 		return (0);
 
 	if (dest + size < dest)
 		return (0);
 
 	if (dest + size > mstate->dtms_scratch_ptr)
 		return (0);
 
 	return (1);
 }
 
 static int
 dtrace_canstore_statvar(uint64_t addr, size_t sz, size_t *remain,
     dtrace_statvar_t **svars, int nsvars)
 {
 	int i;
 	size_t maxglobalsize, maxlocalsize;
 
 	if (nsvars == 0)
 		return (0);
 
 	maxglobalsize = dtrace_statvar_maxsize + sizeof (uint64_t);
 	maxlocalsize = maxglobalsize * NCPU;
 
 	for (i = 0; i < nsvars; i++) {
 		dtrace_statvar_t *svar = svars[i];
 		uint8_t scope;
 		size_t size;
 
 		if (svar == NULL || (size = svar->dtsv_size) == 0)
 			continue;
 
 		scope = svar->dtsv_var.dtdv_scope;
 
 		/*
 		 * We verify that our size is valid in the spirit of providing
 		 * defense in depth:  we want to prevent attackers from using
 		 * DTrace to escalate an orthogonal kernel heap corruption bug
 		 * into the ability to store to arbitrary locations in memory.
 		 */
 		VERIFY((scope == DIFV_SCOPE_GLOBAL && size <= maxglobalsize) ||
 		    (scope == DIFV_SCOPE_LOCAL && size <= maxlocalsize));
 
 		if (DTRACE_INRANGE(addr, sz, svar->dtsv_data,
 		    svar->dtsv_size)) {
 			DTRACE_RANGE_REMAIN(remain, addr, svar->dtsv_data,
 			    svar->dtsv_size);
 			return (1);
 		}
 	}
 
 	return (0);
 }
 
 /*
  * Check to see if the address is within a memory region to which a store may
  * be issued.  This includes the DTrace scratch areas, and any DTrace variable
  * region.  The caller of dtrace_canstore() is responsible for performing any
  * alignment checks that are needed before stores are actually executed.
  */
 static int
 dtrace_canstore(uint64_t addr, size_t sz, dtrace_mstate_t *mstate,
     dtrace_vstate_t *vstate)
 {
 	return (dtrace_canstore_remains(addr, sz, NULL, mstate, vstate));
 }
 
 /*
  * Implementation of dtrace_canstore which communicates the upper bound of the
  * allowed memory region.
  */
 static int
 dtrace_canstore_remains(uint64_t addr, size_t sz, size_t *remain,
     dtrace_mstate_t *mstate, dtrace_vstate_t *vstate)
 {
 	/*
 	 * First, check to see if the address is in scratch space...
 	 */
 	if (DTRACE_INRANGE(addr, sz, mstate->dtms_scratch_base,
 	    mstate->dtms_scratch_size)) {
 		DTRACE_RANGE_REMAIN(remain, addr, mstate->dtms_scratch_base,
 		    mstate->dtms_scratch_size);
 		return (1);
 	}
 
 	/*
 	 * Now check to see if it's a dynamic variable.  This check will pick
 	 * up both thread-local variables and any global dynamically-allocated
 	 * variables.
 	 */
 	if (DTRACE_INRANGE(addr, sz, vstate->dtvs_dynvars.dtds_base,
 	    vstate->dtvs_dynvars.dtds_size)) {
 		dtrace_dstate_t *dstate = &vstate->dtvs_dynvars;
 		uintptr_t base = (uintptr_t)dstate->dtds_base +
 		    (dstate->dtds_hashsize * sizeof (dtrace_dynhash_t));
 		uintptr_t chunkoffs;
 		dtrace_dynvar_t *dvar;
 
 		/*
 		 * Before we assume that we can store here, we need to make
 		 * sure that it isn't in our metadata -- storing to our
 		 * dynamic variable metadata would corrupt our state.  For
 		 * the range to not include any dynamic variable metadata,
 		 * it must:
 		 *
 		 *	(1) Start above the hash table that is at the base of
 		 *	the dynamic variable space
 		 *
 		 *	(2) Have a starting chunk offset that is beyond the
 		 *	dtrace_dynvar_t that is at the base of every chunk
 		 *
 		 *	(3) Not span a chunk boundary
 		 *
 		 *	(4) Not be in the tuple space of a dynamic variable
 		 *
 		 */
 		if (addr < base)
 			return (0);
 
 		chunkoffs = (addr - base) % dstate->dtds_chunksize;
 
 		if (chunkoffs < sizeof (dtrace_dynvar_t))
 			return (0);
 
 		if (chunkoffs + sz > dstate->dtds_chunksize)
 			return (0);
 
 		dvar = (dtrace_dynvar_t *)((uintptr_t)addr - chunkoffs);
 
 		if (dvar->dtdv_hashval == DTRACE_DYNHASH_FREE)
 			return (0);
 
 		if (chunkoffs < sizeof (dtrace_dynvar_t) +
 		    ((dvar->dtdv_tuple.dtt_nkeys - 1) * sizeof (dtrace_key_t)))
 			return (0);
 
 		DTRACE_RANGE_REMAIN(remain, addr, dvar, dstate->dtds_chunksize);
 		return (1);
 	}
 
 	/*
 	 * Finally, check the static local and global variables.  These checks
 	 * take the longest, so we perform them last.
 	 */
 	if (dtrace_canstore_statvar(addr, sz, remain,
 	    vstate->dtvs_locals, vstate->dtvs_nlocals))
 		return (1);
 
 	if (dtrace_canstore_statvar(addr, sz, remain,
 	    vstate->dtvs_globals, vstate->dtvs_nglobals))
 		return (1);
 
 	return (0);
 }
 
 
 /*
  * Convenience routine to check to see if the address is within a memory
  * region in which a load may be issued given the user's privilege level;
  * if not, it sets the appropriate error flags and loads 'addr' into the
  * illegal value slot.
  *
  * DTrace subroutines (DIF_SUBR_*) should use this helper to implement
  * appropriate memory access protection.
  */
 static int
 dtrace_canload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate,
     dtrace_vstate_t *vstate)
 {
 	return (dtrace_canload_remains(addr, sz, NULL, mstate, vstate));
 }
 
 /*
  * Implementation of dtrace_canload which communicates the uppoer bound of the
  * allowed memory region.
  */
 static int
 dtrace_canload_remains(uint64_t addr, size_t sz, size_t *remain,
     dtrace_mstate_t *mstate, dtrace_vstate_t *vstate)
 {
 	volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval;
 	file_t *fp;
 
 	/*
 	 * If we hold the privilege to read from kernel memory, then
 	 * everything is readable.
 	 */
 	if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) {
 		DTRACE_RANGE_REMAIN(remain, addr, addr, sz);
 		return (1);
 	}
 
 	/*
 	 * You can obviously read that which you can store.
 	 */
 	if (dtrace_canstore_remains(addr, sz, remain, mstate, vstate))
 		return (1);
 
 	/*
 	 * We're allowed to read from our own string table.
 	 */
 	if (DTRACE_INRANGE(addr, sz, mstate->dtms_difo->dtdo_strtab,
 	    mstate->dtms_difo->dtdo_strlen)) {
 		DTRACE_RANGE_REMAIN(remain, addr,
 		    mstate->dtms_difo->dtdo_strtab,
 		    mstate->dtms_difo->dtdo_strlen);
 		return (1);
 	}
 
 	if (vstate->dtvs_state != NULL &&
 	    dtrace_priv_proc(vstate->dtvs_state)) {
 		proc_t *p;
 
 		/*
 		 * When we have privileges to the current process, there are
 		 * several context-related kernel structures that are safe to
 		 * read, even absent the privilege to read from kernel memory.
 		 * These reads are safe because these structures contain only
 		 * state that (1) we're permitted to read, (2) is harmless or
 		 * (3) contains pointers to additional kernel state that we're
 		 * not permitted to read (and as such, do not present an
 		 * opportunity for privilege escalation).  Finally (and
 		 * critically), because of the nature of their relation with
 		 * the current thread context, the memory associated with these
 		 * structures cannot change over the duration of probe context,
 		 * and it is therefore impossible for this memory to be
 		 * deallocated and reallocated as something else while it's
 		 * being operated upon.
 		 */
 		if (DTRACE_INRANGE(addr, sz, curthread, sizeof (kthread_t))) {
 			DTRACE_RANGE_REMAIN(remain, addr, curthread,
 			    sizeof (kthread_t));
 			return (1);
 		}
 
 		if ((p = curthread->t_procp) != NULL && DTRACE_INRANGE(addr,
 		    sz, curthread->t_procp, sizeof (proc_t))) {
 			DTRACE_RANGE_REMAIN(remain, addr, curthread->t_procp,
 			    sizeof (proc_t));
 			return (1);
 		}
 
 		if (curthread->t_cred != NULL && DTRACE_INRANGE(addr, sz,
 		    curthread->t_cred, sizeof (cred_t))) {
 			DTRACE_RANGE_REMAIN(remain, addr, curthread->t_cred,
 			    sizeof (cred_t));
 			return (1);
 		}
 
 #ifdef illumos
 		if (p != NULL && p->p_pidp != NULL && DTRACE_INRANGE(addr, sz,
 		    &(p->p_pidp->pid_id), sizeof (pid_t))) {
 			DTRACE_RANGE_REMAIN(remain, addr, &(p->p_pidp->pid_id),
 			    sizeof (pid_t));
 			return (1);
 		}
 
 		if (curthread->t_cpu != NULL && DTRACE_INRANGE(addr, sz,
 		    curthread->t_cpu, offsetof(cpu_t, cpu_pause_thread))) {
 			DTRACE_RANGE_REMAIN(remain, addr, curthread->t_cpu,
 			    offsetof(cpu_t, cpu_pause_thread));
 			return (1);
 		}
 #endif
 	}
 
 	if ((fp = mstate->dtms_getf) != NULL) {
 		uintptr_t psz = sizeof (void *);
 		vnode_t *vp;
 		vnodeops_t *op;
 
 		/*
 		 * When getf() returns a file_t, the enabling is implicitly
 		 * granted the (transient) right to read the returned file_t
 		 * as well as the v_path and v_op->vnop_name of the underlying
 		 * vnode.  These accesses are allowed after a successful
 		 * getf() because the members that they refer to cannot change
 		 * once set -- and the barrier logic in the kernel's closef()
 		 * path assures that the file_t and its referenced vode_t
 		 * cannot themselves be stale (that is, it impossible for
 		 * either dtms_getf itself or its f_vnode member to reference
 		 * freed memory).
 		 */
 		if (DTRACE_INRANGE(addr, sz, fp, sizeof (file_t))) {
 			DTRACE_RANGE_REMAIN(remain, addr, fp, sizeof (file_t));
 			return (1);
 		}
 
 		if ((vp = fp->f_vnode) != NULL) {
 			size_t slen;
 #ifdef illumos
 			if (DTRACE_INRANGE(addr, sz, &vp->v_path, psz)) {
 				DTRACE_RANGE_REMAIN(remain, addr, &vp->v_path,
 				    psz);
 				return (1);
 			}
 			slen = strlen(vp->v_path) + 1;
 			if (DTRACE_INRANGE(addr, sz, vp->v_path, slen)) {
 				DTRACE_RANGE_REMAIN(remain, addr, vp->v_path,
 				    slen);
 				return (1);
 			}
 #endif
 
 			if (DTRACE_INRANGE(addr, sz, &vp->v_op, psz)) {
 				DTRACE_RANGE_REMAIN(remain, addr, &vp->v_op,
 				    psz);
 				return (1);
 			}
 
 #ifdef illumos
 			if ((op = vp->v_op) != NULL &&
 			    DTRACE_INRANGE(addr, sz, &op->vnop_name, psz)) {
 				DTRACE_RANGE_REMAIN(remain, addr,
 				    &op->vnop_name, psz);
 				return (1);
 			}
 
 			if (op != NULL && op->vnop_name != NULL &&
 			    DTRACE_INRANGE(addr, sz, op->vnop_name,
 			    (slen = strlen(op->vnop_name) + 1))) {
 				DTRACE_RANGE_REMAIN(remain, addr,
 				    op->vnop_name, slen);
 				return (1);
 			}
 #endif
 		}
 	}
 
 	DTRACE_CPUFLAG_SET(CPU_DTRACE_KPRIV);
 	*illval = addr;
 	return (0);
 }
 
 /*
  * Convenience routine to check to see if a given string is within a memory
  * region in which a load may be issued given the user's privilege level;
  * this exists so that we don't need to issue unnecessary dtrace_strlen()
  * calls in the event that the user has all privileges.
  */
 static int
 dtrace_strcanload(uint64_t addr, size_t sz, size_t *remain,
     dtrace_mstate_t *mstate, dtrace_vstate_t *vstate)
 {
 	size_t rsize;
 
 	/*
 	 * If we hold the privilege to read from kernel memory, then
 	 * everything is readable.
 	 */
 	if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) {
 		DTRACE_RANGE_REMAIN(remain, addr, addr, sz);
 		return (1);
 	}
 
 	/*
 	 * Even if the caller is uninterested in querying the remaining valid
 	 * range, it is required to ensure that the access is allowed.
 	 */
 	if (remain == NULL) {
 		remain = &rsize;
 	}
 	if (dtrace_canload_remains(addr, 0, remain, mstate, vstate)) {
 		size_t strsz;
 		/*
 		 * Perform the strlen after determining the length of the
 		 * memory region which is accessible.  This prevents timing
 		 * information from being used to find NULs in memory which is
 		 * not accessible to the caller.
 		 */
 		strsz = 1 + dtrace_strlen((char *)(uintptr_t)addr,
 		    MIN(sz, *remain));
 		if (strsz <= *remain) {
 			return (1);
 		}
 	}
 
 	return (0);
 }
 
 /*
  * Convenience routine to check to see if a given variable is within a memory
  * region in which a load may be issued given the user's privilege level.
  */
 static int
 dtrace_vcanload(void *src, dtrace_diftype_t *type, size_t *remain,
     dtrace_mstate_t *mstate, dtrace_vstate_t *vstate)
 {
 	size_t sz;
 	ASSERT(type->dtdt_flags & DIF_TF_BYREF);
 
 	/*
 	 * Calculate the max size before performing any checks since even
 	 * DTRACE_ACCESS_KERNEL-credentialed callers expect that this function
 	 * return the max length via 'remain'.
 	 */
 	if (type->dtdt_kind == DIF_TYPE_STRING) {
 		dtrace_state_t *state = vstate->dtvs_state;
 
 		if (state != NULL) {
 			sz = state->dts_options[DTRACEOPT_STRSIZE];
 		} else {
 			/*
 			 * In helper context, we have a NULL state; fall back
 			 * to using the system-wide default for the string size
 			 * in this case.
 			 */
 			sz = dtrace_strsize_default;
 		}
 	} else {
 		sz = type->dtdt_size;
 	}
 
 	/*
 	 * If we hold the privilege to read from kernel memory, then
 	 * everything is readable.
 	 */
 	if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) {
 		DTRACE_RANGE_REMAIN(remain, (uintptr_t)src, src, sz);
 		return (1);
 	}
 
 	if (type->dtdt_kind == DIF_TYPE_STRING) {
 		return (dtrace_strcanload((uintptr_t)src, sz, remain, mstate,
 		    vstate));
 	}
 	return (dtrace_canload_remains((uintptr_t)src, sz, remain, mstate,
 	    vstate));
 }
 
 /*
  * Convert a string to a signed integer using safe loads.
  *
  * NOTE: This function uses various macros from strtolctype.h to manipulate
  * digit values, etc -- these have all been checked to ensure they make
  * no additional function calls.
  */
 static int64_t
 dtrace_strtoll(char *input, int base, size_t limit)
 {
 	uintptr_t pos = (uintptr_t)input;
 	int64_t val = 0;
 	int x;
 	boolean_t neg = B_FALSE;
 	char c, cc, ccc;
 	uintptr_t end = pos + limit;
 
 	/*
 	 * Consume any whitespace preceding digits.
 	 */
 	while ((c = dtrace_load8(pos)) == ' ' || c == '\t')
 		pos++;
 
 	/*
 	 * Handle an explicit sign if one is present.
 	 */
 	if (c == '-' || c == '+') {
 		if (c == '-')
 			neg = B_TRUE;
 		c = dtrace_load8(++pos);
 	}
 
 	/*
 	 * Check for an explicit hexadecimal prefix ("0x" or "0X") and skip it
 	 * if present.
 	 */
 	if (base == 16 && c == '0' && ((cc = dtrace_load8(pos + 1)) == 'x' ||
 	    cc == 'X') && isxdigit(ccc = dtrace_load8(pos + 2))) {
 		pos += 2;
 		c = ccc;
 	}
 
 	/*
 	 * Read in contiguous digits until the first non-digit character.
 	 */
 	for (; pos < end && c != '\0' && lisalnum(c) && (x = DIGIT(c)) < base;
 	    c = dtrace_load8(++pos))
 		val = val * base + x;
 
 	return (neg ? -val : val);
 }
 
 /*
  * Compare two strings using safe loads.
  */
 static int
 dtrace_strncmp(char *s1, char *s2, size_t limit)
 {
 	uint8_t c1, c2;
 	volatile uint16_t *flags;
 
 	if (s1 == s2 || limit == 0)
 		return (0);
 
 	flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags;
 
 	do {
 		if (s1 == NULL) {
 			c1 = '\0';
 		} else {
 			c1 = dtrace_load8((uintptr_t)s1++);
 		}
 
 		if (s2 == NULL) {
 			c2 = '\0';
 		} else {
 			c2 = dtrace_load8((uintptr_t)s2++);
 		}
 
 		if (c1 != c2)
 			return (c1 - c2);
 	} while (--limit && c1 != '\0' && !(*flags & CPU_DTRACE_FAULT));
 
 	return (0);
 }
 
 /*
  * Compute strlen(s) for a string using safe memory accesses.  The additional
  * len parameter is used to specify a maximum length to ensure completion.
  */
 static size_t
 dtrace_strlen(const char *s, size_t lim)
 {
 	uint_t len;
 
 	for (len = 0; len != lim; len++) {
 		if (dtrace_load8((uintptr_t)s++) == '\0')
 			break;
 	}
 
 	return (len);
 }
 
 /*
  * Check if an address falls within a toxic region.
  */
 static int
 dtrace_istoxic(uintptr_t kaddr, size_t size)
 {
 	uintptr_t taddr, tsize;
 	int i;
 
 	for (i = 0; i < dtrace_toxranges; i++) {
 		taddr = dtrace_toxrange[i].dtt_base;
 		tsize = dtrace_toxrange[i].dtt_limit - taddr;
 
 		if (kaddr - taddr < tsize) {
 			DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
 			cpu_core[curcpu].cpuc_dtrace_illval = kaddr;
 			return (1);
 		}
 
 		if (taddr - kaddr < size) {
 			DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
 			cpu_core[curcpu].cpuc_dtrace_illval = taddr;
 			return (1);
 		}
 	}
 
 	return (0);
 }
 
 /*
  * Copy src to dst using safe memory accesses.  The src is assumed to be unsafe
  * memory specified by the DIF program.  The dst is assumed to be safe memory
  * that we can store to directly because it is managed by DTrace.  As with
  * standard bcopy, overlapping copies are handled properly.
  */
 static void
 dtrace_bcopy(const void *src, void *dst, size_t len)
 {
 	if (len != 0) {
 		uint8_t *s1 = dst;
 		const uint8_t *s2 = src;
 
 		if (s1 <= s2) {
 			do {
 				*s1++ = dtrace_load8((uintptr_t)s2++);
 			} while (--len != 0);
 		} else {
 			s2 += len;
 			s1 += len;
 
 			do {
 				*--s1 = dtrace_load8((uintptr_t)--s2);
 			} while (--len != 0);
 		}
 	}
 }
 
 /*
  * Copy src to dst using safe memory accesses, up to either the specified
  * length, or the point that a nul byte is encountered.  The src is assumed to
  * be unsafe memory specified by the DIF program.  The dst is assumed to be
  * safe memory that we can store to directly because it is managed by DTrace.
  * Unlike dtrace_bcopy(), overlapping regions are not handled.
  */
 static void
 dtrace_strcpy(const void *src, void *dst, size_t len)
 {
 	if (len != 0) {
 		uint8_t *s1 = dst, c;
 		const uint8_t *s2 = src;
 
 		do {
 			*s1++ = c = dtrace_load8((uintptr_t)s2++);
 		} while (--len != 0 && c != '\0');
 	}
 }
 
 /*
  * Copy src to dst, deriving the size and type from the specified (BYREF)
  * variable type.  The src is assumed to be unsafe memory specified by the DIF
  * program.  The dst is assumed to be DTrace variable memory that is of the
  * specified type; we assume that we can store to directly.
  */
 static void
 dtrace_vcopy(void *src, void *dst, dtrace_diftype_t *type, size_t limit)
 {
 	ASSERT(type->dtdt_flags & DIF_TF_BYREF);
 
 	if (type->dtdt_kind == DIF_TYPE_STRING) {
 		dtrace_strcpy(src, dst, MIN(type->dtdt_size, limit));
 	} else {
 		dtrace_bcopy(src, dst, MIN(type->dtdt_size, limit));
 	}
 }
 
 /*
  * Compare s1 to s2 using safe memory accesses.  The s1 data is assumed to be
  * unsafe memory specified by the DIF program.  The s2 data is assumed to be
  * safe memory that we can access directly because it is managed by DTrace.
  */
 static int
 dtrace_bcmp(const void *s1, const void *s2, size_t len)
 {
 	volatile uint16_t *flags;
 
 	flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags;
 
 	if (s1 == s2)
 		return (0);
 
 	if (s1 == NULL || s2 == NULL)
 		return (1);
 
 	if (s1 != s2 && len != 0) {
 		const uint8_t *ps1 = s1;
 		const uint8_t *ps2 = s2;
 
 		do {
 			if (dtrace_load8((uintptr_t)ps1++) != *ps2++)
 				return (1);
 		} while (--len != 0 && !(*flags & CPU_DTRACE_FAULT));
 	}
 	return (0);
 }
 
 /*
  * Zero the specified region using a simple byte-by-byte loop.  Note that this
  * is for safe DTrace-managed memory only.
  */
 static void
 dtrace_bzero(void *dst, size_t len)
 {
 	uchar_t *cp;
 
 	for (cp = dst; len != 0; len--)
 		*cp++ = 0;
 }
 
 static void
 dtrace_add_128(uint64_t *addend1, uint64_t *addend2, uint64_t *sum)
 {
 	uint64_t result[2];
 
 	result[0] = addend1[0] + addend2[0];
 	result[1] = addend1[1] + addend2[1] +
 	    (result[0] < addend1[0] || result[0] < addend2[0] ? 1 : 0);
 
 	sum[0] = result[0];
 	sum[1] = result[1];
 }
 
 /*
  * Shift the 128-bit value in a by b. If b is positive, shift left.
  * If b is negative, shift right.
  */
 static void
 dtrace_shift_128(uint64_t *a, int b)
 {
 	uint64_t mask;
 
 	if (b == 0)
 		return;
 
 	if (b < 0) {
 		b = -b;
 		if (b >= 64) {
 			a[0] = a[1] >> (b - 64);
 			a[1] = 0;
 		} else {
 			a[0] >>= b;
 			mask = 1LL << (64 - b);
 			mask -= 1;
 			a[0] |= ((a[1] & mask) << (64 - b));
 			a[1] >>= b;
 		}
 	} else {
 		if (b >= 64) {
 			a[1] = a[0] << (b - 64);
 			a[0] = 0;
 		} else {
 			a[1] <<= b;
 			mask = a[0] >> (64 - b);
 			a[1] |= mask;
 			a[0] <<= b;
 		}
 	}
 }
 
 /*
  * The basic idea is to break the 2 64-bit values into 4 32-bit values,
  * use native multiplication on those, and then re-combine into the
  * resulting 128-bit value.
  *
  * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) =
  *     hi1 * hi2 << 64 +
  *     hi1 * lo2 << 32 +
  *     hi2 * lo1 << 32 +
  *     lo1 * lo2
  */
 static void
 dtrace_multiply_128(uint64_t factor1, uint64_t factor2, uint64_t *product)
 {
 	uint64_t hi1, hi2, lo1, lo2;
 	uint64_t tmp[2];
 
 	hi1 = factor1 >> 32;
 	hi2 = factor2 >> 32;
 
 	lo1 = factor1 & DT_MASK_LO;
 	lo2 = factor2 & DT_MASK_LO;
 
 	product[0] = lo1 * lo2;
 	product[1] = hi1 * hi2;
 
 	tmp[0] = hi1 * lo2;
 	tmp[1] = 0;
 	dtrace_shift_128(tmp, 32);
 	dtrace_add_128(product, tmp, product);
 
 	tmp[0] = hi2 * lo1;
 	tmp[1] = 0;
 	dtrace_shift_128(tmp, 32);
 	dtrace_add_128(product, tmp, product);
 }
 
 /*
  * This privilege check should be used by actions and subroutines to
  * verify that the user credentials of the process that enabled the
  * invoking ECB match the target credentials
  */
 static int
 dtrace_priv_proc_common_user(dtrace_state_t *state)
 {
 	cred_t *cr, *s_cr = state->dts_cred.dcr_cred;
 
 	/*
 	 * We should always have a non-NULL state cred here, since if cred
 	 * is null (anonymous tracing), we fast-path bypass this routine.
 	 */
 	ASSERT(s_cr != NULL);
 
 	if ((cr = CRED()) != NULL &&
 	    s_cr->cr_uid == cr->cr_uid &&
 	    s_cr->cr_uid == cr->cr_ruid &&
 	    s_cr->cr_uid == cr->cr_suid &&
 	    s_cr->cr_gid == cr->cr_gid &&
 	    s_cr->cr_gid == cr->cr_rgid &&
 	    s_cr->cr_gid == cr->cr_sgid)
 		return (1);
 
 	return (0);
 }
 
 /*
  * This privilege check should be used by actions and subroutines to
  * verify that the zone of the process that enabled the invoking ECB
  * matches the target credentials
  */
 static int
 dtrace_priv_proc_common_zone(dtrace_state_t *state)
 {
 #ifdef illumos
 	cred_t *cr, *s_cr = state->dts_cred.dcr_cred;
 
 	/*
 	 * We should always have a non-NULL state cred here, since if cred
 	 * is null (anonymous tracing), we fast-path bypass this routine.
 	 */
 	ASSERT(s_cr != NULL);
 
 	if ((cr = CRED()) != NULL && s_cr->cr_zone == cr->cr_zone)
 		return (1);
 
 	return (0);
 #else
 	return (1);
 #endif
 }
 
 /*
  * This privilege check should be used by actions and subroutines to
  * verify that the process has not setuid or changed credentials.
  */
 static int
 dtrace_priv_proc_common_nocd(void)
 {
 	proc_t *proc;
 
 	if ((proc = ttoproc(curthread)) != NULL &&
 	    !(proc->p_flag & SNOCD))
 		return (1);
 
 	return (0);
 }
 
 static int
 dtrace_priv_proc_destructive(dtrace_state_t *state)
 {
 	int action = state->dts_cred.dcr_action;
 
 	if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE) == 0) &&
 	    dtrace_priv_proc_common_zone(state) == 0)
 		goto bad;
 
 	if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER) == 0) &&
 	    dtrace_priv_proc_common_user(state) == 0)
 		goto bad;
 
 	if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG) == 0) &&
 	    dtrace_priv_proc_common_nocd() == 0)
 		goto bad;
 
 	return (1);
 
 bad:
 	cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV;
 
 	return (0);
 }
 
 static int
 dtrace_priv_proc_control(dtrace_state_t *state)
 {
 	if (state->dts_cred.dcr_action & DTRACE_CRA_PROC_CONTROL)
 		return (1);
 
 	if (dtrace_priv_proc_common_zone(state) &&
 	    dtrace_priv_proc_common_user(state) &&
 	    dtrace_priv_proc_common_nocd())
 		return (1);
 
 	cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV;
 
 	return (0);
 }
 
 static int
 dtrace_priv_proc(dtrace_state_t *state)
 {
 	if (state->dts_cred.dcr_action & DTRACE_CRA_PROC)
 		return (1);
 
 	cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV;
 
 	return (0);
 }
 
 static int
 dtrace_priv_kernel(dtrace_state_t *state)
 {
 	if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL)
 		return (1);
 
 	cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV;
 
 	return (0);
 }
 
 static int
 dtrace_priv_kernel_destructive(dtrace_state_t *state)
 {
 	if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL_DESTRUCTIVE)
 		return (1);
 
 	cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV;
 
 	return (0);
 }
 
 /*
  * Determine if the dte_cond of the specified ECB allows for processing of
  * the current probe to continue.  Note that this routine may allow continued
  * processing, but with access(es) stripped from the mstate's dtms_access
  * field.
  */
 static int
 dtrace_priv_probe(dtrace_state_t *state, dtrace_mstate_t *mstate,
     dtrace_ecb_t *ecb)
 {
 	dtrace_probe_t *probe = ecb->dte_probe;
 	dtrace_provider_t *prov = probe->dtpr_provider;
 	dtrace_pops_t *pops = &prov->dtpv_pops;
 	int mode = DTRACE_MODE_NOPRIV_DROP;
 
 	ASSERT(ecb->dte_cond);
 
 #ifdef illumos
 	if (pops->dtps_mode != NULL) {
 		mode = pops->dtps_mode(prov->dtpv_arg,
 		    probe->dtpr_id, probe->dtpr_arg);
 
 		ASSERT((mode & DTRACE_MODE_USER) ||
 		    (mode & DTRACE_MODE_KERNEL));
 		ASSERT((mode & DTRACE_MODE_NOPRIV_RESTRICT) ||
 		    (mode & DTRACE_MODE_NOPRIV_DROP));
 	}
 
 	/*
 	 * If the dte_cond bits indicate that this consumer is only allowed to
 	 * see user-mode firings of this probe, call the provider's dtps_mode()
 	 * entry point to check that the probe was fired while in a user
 	 * context.  If that's not the case, use the policy specified by the
 	 * provider to determine if we drop the probe or merely restrict
 	 * operation.
 	 */
 	if (ecb->dte_cond & DTRACE_COND_USERMODE) {
 		ASSERT(mode != DTRACE_MODE_NOPRIV_DROP);
 
 		if (!(mode & DTRACE_MODE_USER)) {
 			if (mode & DTRACE_MODE_NOPRIV_DROP)
 				return (0);
 
 			mstate->dtms_access &= ~DTRACE_ACCESS_ARGS;
 		}
 	}
 #endif
 
 	/*
 	 * This is more subtle than it looks. We have to be absolutely certain
 	 * that CRED() isn't going to change out from under us so it's only
 	 * legit to examine that structure if we're in constrained situations.
 	 * Currently, the only times we'll this check is if a non-super-user
 	 * has enabled the profile or syscall providers -- providers that
 	 * allow visibility of all processes. For the profile case, the check
 	 * above will ensure that we're examining a user context.
 	 */
 	if (ecb->dte_cond & DTRACE_COND_OWNER) {
 		cred_t *cr;
 		cred_t *s_cr = state->dts_cred.dcr_cred;
 		proc_t *proc;
 
 		ASSERT(s_cr != NULL);
 
 		if ((cr = CRED()) == NULL ||
 		    s_cr->cr_uid != cr->cr_uid ||
 		    s_cr->cr_uid != cr->cr_ruid ||
 		    s_cr->cr_uid != cr->cr_suid ||
 		    s_cr->cr_gid != cr->cr_gid ||
 		    s_cr->cr_gid != cr->cr_rgid ||
 		    s_cr->cr_gid != cr->cr_sgid ||
 		    (proc = ttoproc(curthread)) == NULL ||
 		    (proc->p_flag & SNOCD)) {
 			if (mode & DTRACE_MODE_NOPRIV_DROP)
 				return (0);
 
 #ifdef illumos
 			mstate->dtms_access &= ~DTRACE_ACCESS_PROC;
 #endif
 		}
 	}
 
 #ifdef illumos
 	/*
 	 * If our dte_cond is set to DTRACE_COND_ZONEOWNER and we are not
 	 * in our zone, check to see if our mode policy is to restrict rather
 	 * than to drop; if to restrict, strip away both DTRACE_ACCESS_PROC
 	 * and DTRACE_ACCESS_ARGS
 	 */
 	if (ecb->dte_cond & DTRACE_COND_ZONEOWNER) {
 		cred_t *cr;
 		cred_t *s_cr = state->dts_cred.dcr_cred;
 
 		ASSERT(s_cr != NULL);
 
 		if ((cr = CRED()) == NULL ||
 		    s_cr->cr_zone->zone_id != cr->cr_zone->zone_id) {
 			if (mode & DTRACE_MODE_NOPRIV_DROP)
 				return (0);
 
 			mstate->dtms_access &=
 			    ~(DTRACE_ACCESS_PROC | DTRACE_ACCESS_ARGS);
 		}
 	}
 #endif
 
 	return (1);
 }
 
 /*
  * Note:  not called from probe context.  This function is called
  * asynchronously (and at a regular interval) from outside of probe context to
  * clean the dirty dynamic variable lists on all CPUs.  Dynamic variable
  * cleaning is explained in detail in <sys/dtrace_impl.h>.
  */
 void
 dtrace_dynvar_clean(dtrace_dstate_t *dstate)
 {
 	dtrace_dynvar_t *dirty;
 	dtrace_dstate_percpu_t *dcpu;
 	dtrace_dynvar_t **rinsep;
 	int i, j, work = 0;
 
 	for (i = 0; i < NCPU; i++) {
 		dcpu = &dstate->dtds_percpu[i];
 		rinsep = &dcpu->dtdsc_rinsing;
 
 		/*
 		 * If the dirty list is NULL, there is no dirty work to do.
 		 */
 		if (dcpu->dtdsc_dirty == NULL)
 			continue;
 
 		if (dcpu->dtdsc_rinsing != NULL) {
 			/*
 			 * If the rinsing list is non-NULL, then it is because
 			 * this CPU was selected to accept another CPU's
 			 * dirty list -- and since that time, dirty buffers
 			 * have accumulated.  This is a highly unlikely
 			 * condition, but we choose to ignore the dirty
 			 * buffers -- they'll be picked up a future cleanse.
 			 */
 			continue;
 		}
 
 		if (dcpu->dtdsc_clean != NULL) {
 			/*
 			 * If the clean list is non-NULL, then we're in a
 			 * situation where a CPU has done deallocations (we
 			 * have a non-NULL dirty list) but no allocations (we
 			 * also have a non-NULL clean list).  We can't simply
 			 * move the dirty list into the clean list on this
 			 * CPU, yet we also don't want to allow this condition
 			 * to persist, lest a short clean list prevent a
 			 * massive dirty list from being cleaned (which in
 			 * turn could lead to otherwise avoidable dynamic
 			 * drops).  To deal with this, we look for some CPU
 			 * with a NULL clean list, NULL dirty list, and NULL
 			 * rinsing list -- and then we borrow this CPU to
 			 * rinse our dirty list.
 			 */
 			for (j = 0; j < NCPU; j++) {
 				dtrace_dstate_percpu_t *rinser;
 
 				rinser = &dstate->dtds_percpu[j];
 
 				if (rinser->dtdsc_rinsing != NULL)
 					continue;
 
 				if (rinser->dtdsc_dirty != NULL)
 					continue;
 
 				if (rinser->dtdsc_clean != NULL)
 					continue;
 
 				rinsep = &rinser->dtdsc_rinsing;
 				break;
 			}
 
 			if (j == NCPU) {
 				/*
 				 * We were unable to find another CPU that
 				 * could accept this dirty list -- we are
 				 * therefore unable to clean it now.
 				 */
 				dtrace_dynvar_failclean++;
 				continue;
 			}
 		}
 
 		work = 1;
 
 		/*
 		 * Atomically move the dirty list aside.
 		 */
 		do {
 			dirty = dcpu->dtdsc_dirty;
 
 			/*
 			 * Before we zap the dirty list, set the rinsing list.
 			 * (This allows for a potential assertion in
 			 * dtrace_dynvar():  if a free dynamic variable appears
 			 * on a hash chain, either the dirty list or the
 			 * rinsing list for some CPU must be non-NULL.)
 			 */
 			*rinsep = dirty;
 			dtrace_membar_producer();
 		} while (dtrace_casptr(&dcpu->dtdsc_dirty,
 		    dirty, NULL) != dirty);
 	}
 
 	if (!work) {
 		/*
 		 * We have no work to do; we can simply return.
 		 */
 		return;
 	}
 
 	dtrace_sync();
 
 	for (i = 0; i < NCPU; i++) {
 		dcpu = &dstate->dtds_percpu[i];
 
 		if (dcpu->dtdsc_rinsing == NULL)
 			continue;
 
 		/*
 		 * We are now guaranteed that no hash chain contains a pointer
 		 * into this dirty list; we can make it clean.
 		 */
 		ASSERT(dcpu->dtdsc_clean == NULL);
 		dcpu->dtdsc_clean = dcpu->dtdsc_rinsing;
 		dcpu->dtdsc_rinsing = NULL;
 	}
 
 	/*
 	 * Before we actually set the state to be DTRACE_DSTATE_CLEAN, make
 	 * sure that all CPUs have seen all of the dtdsc_clean pointers.
 	 * This prevents a race whereby a CPU incorrectly decides that
 	 * the state should be something other than DTRACE_DSTATE_CLEAN
 	 * after dtrace_dynvar_clean() has completed.
 	 */
 	dtrace_sync();
 
 	dstate->dtds_state = DTRACE_DSTATE_CLEAN;
 }
 
 /*
  * Depending on the value of the op parameter, this function looks-up,
  * allocates or deallocates an arbitrarily-keyed dynamic variable.  If an
  * allocation is requested, this function will return a pointer to a
  * dtrace_dynvar_t corresponding to the allocated variable -- or NULL if no
  * variable can be allocated.  If NULL is returned, the appropriate counter
  * will be incremented.
  */
 dtrace_dynvar_t *
 dtrace_dynvar(dtrace_dstate_t *dstate, uint_t nkeys,
     dtrace_key_t *key, size_t dsize, dtrace_dynvar_op_t op,
     dtrace_mstate_t *mstate, dtrace_vstate_t *vstate)
 {
 	uint64_t hashval = DTRACE_DYNHASH_VALID;
 	dtrace_dynhash_t *hash = dstate->dtds_hash;
 	dtrace_dynvar_t *free, *new_free, *next, *dvar, *start, *prev = NULL;
 	processorid_t me = curcpu, cpu = me;
 	dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[me];
 	size_t bucket, ksize;
 	size_t chunksize = dstate->dtds_chunksize;
 	uintptr_t kdata, lock, nstate;
 	uint_t i;
 
 	ASSERT(nkeys != 0);
 
 	/*
 	 * Hash the key.  As with aggregations, we use Jenkins' "One-at-a-time"
 	 * algorithm.  For the by-value portions, we perform the algorithm in
 	 * 16-bit chunks (as opposed to 8-bit chunks).  This speeds things up a
 	 * bit, and seems to have only a minute effect on distribution.  For
 	 * the by-reference data, we perform "One-at-a-time" iterating (safely)
 	 * over each referenced byte.  It's painful to do this, but it's much
 	 * better than pathological hash distribution.  The efficacy of the
 	 * hashing algorithm (and a comparison with other algorithms) may be
 	 * found by running the ::dtrace_dynstat MDB dcmd.
 	 */
 	for (i = 0; i < nkeys; i++) {
 		if (key[i].dttk_size == 0) {
 			uint64_t val = key[i].dttk_value;
 
 			hashval += (val >> 48) & 0xffff;
 			hashval += (hashval << 10);
 			hashval ^= (hashval >> 6);
 
 			hashval += (val >> 32) & 0xffff;
 			hashval += (hashval << 10);
 			hashval ^= (hashval >> 6);
 
 			hashval += (val >> 16) & 0xffff;
 			hashval += (hashval << 10);
 			hashval ^= (hashval >> 6);
 
 			hashval += val & 0xffff;
 			hashval += (hashval << 10);
 			hashval ^= (hashval >> 6);
 		} else {
 			/*
 			 * This is incredibly painful, but it beats the hell
 			 * out of the alternative.
 			 */
 			uint64_t j, size = key[i].dttk_size;
 			uintptr_t base = (uintptr_t)key[i].dttk_value;
 
 			if (!dtrace_canload(base, size, mstate, vstate))
 				break;
 
 			for (j = 0; j < size; j++) {
 				hashval += dtrace_load8(base + j);
 				hashval += (hashval << 10);
 				hashval ^= (hashval >> 6);
 			}
 		}
 	}
 
 	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT))
 		return (NULL);
 
 	hashval += (hashval << 3);
 	hashval ^= (hashval >> 11);
 	hashval += (hashval << 15);
 
 	/*
 	 * There is a remote chance (ideally, 1 in 2^31) that our hashval
 	 * comes out to be one of our two sentinel hash values.  If this
 	 * actually happens, we set the hashval to be a value known to be a
 	 * non-sentinel value.
 	 */
 	if (hashval == DTRACE_DYNHASH_FREE || hashval == DTRACE_DYNHASH_SINK)
 		hashval = DTRACE_DYNHASH_VALID;
 
 	/*
 	 * Yes, it's painful to do a divide here.  If the cycle count becomes
 	 * important here, tricks can be pulled to reduce it.  (However, it's
 	 * critical that hash collisions be kept to an absolute minimum;
 	 * they're much more painful than a divide.)  It's better to have a
 	 * solution that generates few collisions and still keeps things
 	 * relatively simple.
 	 */
 	bucket = hashval % dstate->dtds_hashsize;
 
 	if (op == DTRACE_DYNVAR_DEALLOC) {
 		volatile uintptr_t *lockp = &hash[bucket].dtdh_lock;
 
 		for (;;) {
 			while ((lock = *lockp) & 1)
 				continue;
 
 			if (dtrace_casptr((volatile void *)lockp,
 			    (volatile void *)lock, (volatile void *)(lock + 1)) == (void *)lock)
 				break;
 		}
 
 		dtrace_membar_producer();
 	}
 
 top:
 	prev = NULL;
 	lock = hash[bucket].dtdh_lock;
 
 	dtrace_membar_consumer();
 
 	start = hash[bucket].dtdh_chain;
 	ASSERT(start != NULL && (start->dtdv_hashval == DTRACE_DYNHASH_SINK ||
 	    start->dtdv_hashval != DTRACE_DYNHASH_FREE ||
 	    op != DTRACE_DYNVAR_DEALLOC));
 
 	for (dvar = start; dvar != NULL; dvar = dvar->dtdv_next) {
 		dtrace_tuple_t *dtuple = &dvar->dtdv_tuple;
 		dtrace_key_t *dkey = &dtuple->dtt_key[0];
 
 		if (dvar->dtdv_hashval != hashval) {
 			if (dvar->dtdv_hashval == DTRACE_DYNHASH_SINK) {
 				/*
 				 * We've reached the sink, and therefore the
 				 * end of the hash chain; we can kick out of
 				 * the loop knowing that we have seen a valid
 				 * snapshot of state.
 				 */
 				ASSERT(dvar->dtdv_next == NULL);
 				ASSERT(dvar == &dtrace_dynhash_sink);
 				break;
 			}
 
 			if (dvar->dtdv_hashval == DTRACE_DYNHASH_FREE) {
 				/*
 				 * We've gone off the rails:  somewhere along
 				 * the line, one of the members of this hash
 				 * chain was deleted.  Note that we could also
 				 * detect this by simply letting this loop run
 				 * to completion, as we would eventually hit
 				 * the end of the dirty list.  However, we
 				 * want to avoid running the length of the
 				 * dirty list unnecessarily (it might be quite
 				 * long), so we catch this as early as
 				 * possible by detecting the hash marker.  In
 				 * this case, we simply set dvar to NULL and
 				 * break; the conditional after the loop will
 				 * send us back to top.
 				 */
 				dvar = NULL;
 				break;
 			}
 
 			goto next;
 		}
 
 		if (dtuple->dtt_nkeys != nkeys)
 			goto next;
 
 		for (i = 0; i < nkeys; i++, dkey++) {
 			if (dkey->dttk_size != key[i].dttk_size)
 				goto next; /* size or type mismatch */
 
 			if (dkey->dttk_size != 0) {
 				if (dtrace_bcmp(
 				    (void *)(uintptr_t)key[i].dttk_value,
 				    (void *)(uintptr_t)dkey->dttk_value,
 				    dkey->dttk_size))
 					goto next;
 			} else {
 				if (dkey->dttk_value != key[i].dttk_value)
 					goto next;
 			}
 		}
 
 		if (op != DTRACE_DYNVAR_DEALLOC)
 			return (dvar);
 
 		ASSERT(dvar->dtdv_next == NULL ||
 		    dvar->dtdv_next->dtdv_hashval != DTRACE_DYNHASH_FREE);
 
 		if (prev != NULL) {
 			ASSERT(hash[bucket].dtdh_chain != dvar);
 			ASSERT(start != dvar);
 			ASSERT(prev->dtdv_next == dvar);
 			prev->dtdv_next = dvar->dtdv_next;
 		} else {
 			if (dtrace_casptr(&hash[bucket].dtdh_chain,
 			    start, dvar->dtdv_next) != start) {
 				/*
 				 * We have failed to atomically swing the
 				 * hash table head pointer, presumably because
 				 * of a conflicting allocation on another CPU.
 				 * We need to reread the hash chain and try
 				 * again.
 				 */
 				goto top;
 			}
 		}
 
 		dtrace_membar_producer();
 
 		/*
 		 * Now set the hash value to indicate that it's free.
 		 */
 		ASSERT(hash[bucket].dtdh_chain != dvar);
 		dvar->dtdv_hashval = DTRACE_DYNHASH_FREE;
 
 		dtrace_membar_producer();
 
 		/*
 		 * Set the next pointer to point at the dirty list, and
 		 * atomically swing the dirty pointer to the newly freed dvar.
 		 */
 		do {
 			next = dcpu->dtdsc_dirty;
 			dvar->dtdv_next = next;
 		} while (dtrace_casptr(&dcpu->dtdsc_dirty, next, dvar) != next);
 
 		/*
 		 * Finally, unlock this hash bucket.
 		 */
 		ASSERT(hash[bucket].dtdh_lock == lock);
 		ASSERT(lock & 1);
 		hash[bucket].dtdh_lock++;
 
 		return (NULL);
 next:
 		prev = dvar;
 		continue;
 	}
 
 	if (dvar == NULL) {
 		/*
 		 * If dvar is NULL, it is because we went off the rails:
 		 * one of the elements that we traversed in the hash chain
 		 * was deleted while we were traversing it.  In this case,
 		 * we assert that we aren't doing a dealloc (deallocs lock
 		 * the hash bucket to prevent themselves from racing with
 		 * one another), and retry the hash chain traversal.
 		 */
 		ASSERT(op != DTRACE_DYNVAR_DEALLOC);
 		goto top;
 	}
 
 	if (op != DTRACE_DYNVAR_ALLOC) {
 		/*
 		 * If we are not to allocate a new variable, we want to
 		 * return NULL now.  Before we return, check that the value
 		 * of the lock word hasn't changed.  If it has, we may have
 		 * seen an inconsistent snapshot.
 		 */
 		if (op == DTRACE_DYNVAR_NOALLOC) {
 			if (hash[bucket].dtdh_lock != lock)
 				goto top;
 		} else {
 			ASSERT(op == DTRACE_DYNVAR_DEALLOC);
 			ASSERT(hash[bucket].dtdh_lock == lock);
 			ASSERT(lock & 1);
 			hash[bucket].dtdh_lock++;
 		}
 
 		return (NULL);
 	}
 
 	/*
 	 * We need to allocate a new dynamic variable.  The size we need is the
 	 * size of dtrace_dynvar plus the size of nkeys dtrace_key_t's plus the
 	 * size of any auxiliary key data (rounded up to 8-byte alignment) plus
 	 * the size of any referred-to data (dsize).  We then round the final
 	 * size up to the chunksize for allocation.
 	 */
 	for (ksize = 0, i = 0; i < nkeys; i++)
 		ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t));
 
 	/*
 	 * This should be pretty much impossible, but could happen if, say,
 	 * strange DIF specified the tuple.  Ideally, this should be an
 	 * assertion and not an error condition -- but that requires that the
 	 * chunksize calculation in dtrace_difo_chunksize() be absolutely
 	 * bullet-proof.  (That is, it must not be able to be fooled by
 	 * malicious DIF.)  Given the lack of backwards branches in DIF,
 	 * solving this would presumably not amount to solving the Halting
 	 * Problem -- but it still seems awfully hard.
 	 */
 	if (sizeof (dtrace_dynvar_t) + sizeof (dtrace_key_t) * (nkeys - 1) +
 	    ksize + dsize > chunksize) {
 		dcpu->dtdsc_drops++;
 		return (NULL);
 	}
 
 	nstate = DTRACE_DSTATE_EMPTY;
 
 	do {
 retry:
 		free = dcpu->dtdsc_free;
 
 		if (free == NULL) {
 			dtrace_dynvar_t *clean = dcpu->dtdsc_clean;
 			void *rval;
 
 			if (clean == NULL) {
 				/*
 				 * We're out of dynamic variable space on
 				 * this CPU.  Unless we have tried all CPUs,
 				 * we'll try to allocate from a different
 				 * CPU.
 				 */
 				switch (dstate->dtds_state) {
 				case DTRACE_DSTATE_CLEAN: {
 					void *sp = &dstate->dtds_state;
 
 					if (++cpu >= NCPU)
 						cpu = 0;
 
 					if (dcpu->dtdsc_dirty != NULL &&
 					    nstate == DTRACE_DSTATE_EMPTY)
 						nstate = DTRACE_DSTATE_DIRTY;
 
 					if (dcpu->dtdsc_rinsing != NULL)
 						nstate = DTRACE_DSTATE_RINSING;
 
 					dcpu = &dstate->dtds_percpu[cpu];
 
 					if (cpu != me)
 						goto retry;
 
 					(void) dtrace_cas32(sp,
 					    DTRACE_DSTATE_CLEAN, nstate);
 
 					/*
 					 * To increment the correct bean
 					 * counter, take another lap.
 					 */
 					goto retry;
 				}
 
 				case DTRACE_DSTATE_DIRTY:
 					dcpu->dtdsc_dirty_drops++;
 					break;
 
 				case DTRACE_DSTATE_RINSING:
 					dcpu->dtdsc_rinsing_drops++;
 					break;
 
 				case DTRACE_DSTATE_EMPTY:
 					dcpu->dtdsc_drops++;
 					break;
 				}
 
 				DTRACE_CPUFLAG_SET(CPU_DTRACE_DROP);
 				return (NULL);
 			}
 
 			/*
 			 * The clean list appears to be non-empty.  We want to
 			 * move the clean list to the free list; we start by
 			 * moving the clean pointer aside.
 			 */
 			if (dtrace_casptr(&dcpu->dtdsc_clean,
 			    clean, NULL) != clean) {
 				/*
 				 * We are in one of two situations:
 				 *
 				 *  (a)	The clean list was switched to the
 				 *	free list by another CPU.
 				 *
 				 *  (b)	The clean list was added to by the
 				 *	cleansing cyclic.
 				 *
 				 * In either of these situations, we can
 				 * just reattempt the free list allocation.
 				 */
 				goto retry;
 			}
 
 			ASSERT(clean->dtdv_hashval == DTRACE_DYNHASH_FREE);
 
 			/*
 			 * Now we'll move the clean list to our free list.
 			 * It's impossible for this to fail:  the only way
 			 * the free list can be updated is through this
 			 * code path, and only one CPU can own the clean list.
 			 * Thus, it would only be possible for this to fail if
 			 * this code were racing with dtrace_dynvar_clean().
 			 * (That is, if dtrace_dynvar_clean() updated the clean
 			 * list, and we ended up racing to update the free
 			 * list.)  This race is prevented by the dtrace_sync()
 			 * in dtrace_dynvar_clean() -- which flushes the
 			 * owners of the clean lists out before resetting
 			 * the clean lists.
 			 */
 			dcpu = &dstate->dtds_percpu[me];
 			rval = dtrace_casptr(&dcpu->dtdsc_free, NULL, clean);
 			ASSERT(rval == NULL);
 			goto retry;
 		}
 
 		dvar = free;
 		new_free = dvar->dtdv_next;
 	} while (dtrace_casptr(&dcpu->dtdsc_free, free, new_free) != free);
 
 	/*
 	 * We have now allocated a new chunk.  We copy the tuple keys into the
 	 * tuple array and copy any referenced key data into the data space
 	 * following the tuple array.  As we do this, we relocate dttk_value
 	 * in the final tuple to point to the key data address in the chunk.
 	 */
 	kdata = (uintptr_t)&dvar->dtdv_tuple.dtt_key[nkeys];
 	dvar->dtdv_data = (void *)(kdata + ksize);
 	dvar->dtdv_tuple.dtt_nkeys = nkeys;
 
 	for (i = 0; i < nkeys; i++) {
 		dtrace_key_t *dkey = &dvar->dtdv_tuple.dtt_key[i];
 		size_t kesize = key[i].dttk_size;
 
 		if (kesize != 0) {
 			dtrace_bcopy(
 			    (const void *)(uintptr_t)key[i].dttk_value,
 			    (void *)kdata, kesize);
 			dkey->dttk_value = kdata;
 			kdata += P2ROUNDUP(kesize, sizeof (uint64_t));
 		} else {
 			dkey->dttk_value = key[i].dttk_value;
 		}
 
 		dkey->dttk_size = kesize;
 	}
 
 	ASSERT(dvar->dtdv_hashval == DTRACE_DYNHASH_FREE);
 	dvar->dtdv_hashval = hashval;
 	dvar->dtdv_next = start;
 
 	if (dtrace_casptr(&hash[bucket].dtdh_chain, start, dvar) == start)
 		return (dvar);
 
 	/*
 	 * The cas has failed.  Either another CPU is adding an element to
 	 * this hash chain, or another CPU is deleting an element from this
 	 * hash chain.  The simplest way to deal with both of these cases
 	 * (though not necessarily the most efficient) is to free our
 	 * allocated block and re-attempt it all.  Note that the free is
 	 * to the dirty list and _not_ to the free list.  This is to prevent
 	 * races with allocators, above.
 	 */
 	dvar->dtdv_hashval = DTRACE_DYNHASH_FREE;
 
 	dtrace_membar_producer();
 
 	do {
 		free = dcpu->dtdsc_dirty;
 		dvar->dtdv_next = free;
 	} while (dtrace_casptr(&dcpu->dtdsc_dirty, free, dvar) != free);
 
 	goto top;
 }
 
 /*ARGSUSED*/
 static void
 dtrace_aggregate_min(uint64_t *oval, uint64_t nval, uint64_t arg)
 {
 	if ((int64_t)nval < (int64_t)*oval)
 		*oval = nval;
 }
 
 /*ARGSUSED*/
 static void
 dtrace_aggregate_max(uint64_t *oval, uint64_t nval, uint64_t arg)
 {
 	if ((int64_t)nval > (int64_t)*oval)
 		*oval = nval;
 }
 
 static void
 dtrace_aggregate_quantize(uint64_t *quanta, uint64_t nval, uint64_t incr)
 {
 	int i, zero = DTRACE_QUANTIZE_ZEROBUCKET;
 	int64_t val = (int64_t)nval;
 
 	if (val < 0) {
 		for (i = 0; i < zero; i++) {
 			if (val <= DTRACE_QUANTIZE_BUCKETVAL(i)) {
 				quanta[i] += incr;
 				return;
 			}
 		}
 	} else {
 		for (i = zero + 1; i < DTRACE_QUANTIZE_NBUCKETS; i++) {
 			if (val < DTRACE_QUANTIZE_BUCKETVAL(i)) {
 				quanta[i - 1] += incr;
 				return;
 			}
 		}
 
 		quanta[DTRACE_QUANTIZE_NBUCKETS - 1] += incr;
 		return;
 	}
 
 	ASSERT(0);
 }
 
 static void
 dtrace_aggregate_lquantize(uint64_t *lquanta, uint64_t nval, uint64_t incr)
 {
 	uint64_t arg = *lquanta++;
 	int32_t base = DTRACE_LQUANTIZE_BASE(arg);
 	uint16_t step = DTRACE_LQUANTIZE_STEP(arg);
 	uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg);
 	int32_t val = (int32_t)nval, level;
 
 	ASSERT(step != 0);
 	ASSERT(levels != 0);
 
 	if (val < base) {
 		/*
 		 * This is an underflow.
 		 */
 		lquanta[0] += incr;
 		return;
 	}
 
 	level = (val - base) / step;
 
 	if (level < levels) {
 		lquanta[level + 1] += incr;
 		return;
 	}
 
 	/*
 	 * This is an overflow.
 	 */
 	lquanta[levels + 1] += incr;
 }
 
 static int
 dtrace_aggregate_llquantize_bucket(uint16_t factor, uint16_t low,
     uint16_t high, uint16_t nsteps, int64_t value)
 {
 	int64_t this = 1, last, next;
 	int base = 1, order;
 
 	ASSERT(factor <= nsteps);
 	ASSERT(nsteps % factor == 0);
 
 	for (order = 0; order < low; order++)
 		this *= factor;
 
 	/*
 	 * If our value is less than our factor taken to the power of the
 	 * low order of magnitude, it goes into the zeroth bucket.
 	 */
 	if (value < (last = this))
 		return (0);
 
 	for (this *= factor; order <= high; order++) {
 		int nbuckets = this > nsteps ? nsteps : this;
 
 		if ((next = this * factor) < this) {
 			/*
 			 * We should not generally get log/linear quantizations
 			 * with a high magnitude that allows 64-bits to
 			 * overflow, but we nonetheless protect against this
 			 * by explicitly checking for overflow, and clamping
 			 * our value accordingly.
 			 */
 			value = this - 1;
 		}
 
 		if (value < this) {
 			/*
 			 * If our value lies within this order of magnitude,
 			 * determine its position by taking the offset within
 			 * the order of magnitude, dividing by the bucket
 			 * width, and adding to our (accumulated) base.
 			 */
 			return (base + (value - last) / (this / nbuckets));
 		}
 
 		base += nbuckets - (nbuckets / factor);
 		last = this;
 		this = next;
 	}
 
 	/*
 	 * Our value is greater than or equal to our factor taken to the
 	 * power of one plus the high magnitude -- return the top bucket.
 	 */
 	return (base);
 }
 
 static void
 dtrace_aggregate_llquantize(uint64_t *llquanta, uint64_t nval, uint64_t incr)
 {
 	uint64_t arg = *llquanta++;
 	uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(arg);
 	uint16_t low = DTRACE_LLQUANTIZE_LOW(arg);
 	uint16_t high = DTRACE_LLQUANTIZE_HIGH(arg);
 	uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(arg);
 
 	llquanta[dtrace_aggregate_llquantize_bucket(factor,
 	    low, high, nsteps, nval)] += incr;
 }
 
 /*ARGSUSED*/
 static void
 dtrace_aggregate_avg(uint64_t *data, uint64_t nval, uint64_t arg)
 {
 	data[0]++;
 	data[1] += nval;
 }
 
 /*ARGSUSED*/
 static void
 dtrace_aggregate_stddev(uint64_t *data, uint64_t nval, uint64_t arg)
 {
 	int64_t snval = (int64_t)nval;
 	uint64_t tmp[2];
 
 	data[0]++;
 	data[1] += nval;
 
 	/*
 	 * What we want to say here is:
 	 *
 	 * data[2] += nval * nval;
 	 *
 	 * But given that nval is 64-bit, we could easily overflow, so
 	 * we do this as 128-bit arithmetic.
 	 */
 	if (snval < 0)
 		snval = -snval;
 
 	dtrace_multiply_128((uint64_t)snval, (uint64_t)snval, tmp);
 	dtrace_add_128(data + 2, tmp, data + 2);
 }
 
 /*ARGSUSED*/
 static void
 dtrace_aggregate_count(uint64_t *oval, uint64_t nval, uint64_t arg)
 {
 	*oval = *oval + 1;
 }
 
 /*ARGSUSED*/
 static void
 dtrace_aggregate_sum(uint64_t *oval, uint64_t nval, uint64_t arg)
 {
 	*oval += nval;
 }
 
 /*
  * Aggregate given the tuple in the principal data buffer, and the aggregating
  * action denoted by the specified dtrace_aggregation_t.  The aggregation
  * buffer is specified as the buf parameter.  This routine does not return
  * failure; if there is no space in the aggregation buffer, the data will be
  * dropped, and a corresponding counter incremented.
  */
 static void
 dtrace_aggregate(dtrace_aggregation_t *agg, dtrace_buffer_t *dbuf,
     intptr_t offset, dtrace_buffer_t *buf, uint64_t expr, uint64_t arg)
 {
 	dtrace_recdesc_t *rec = &agg->dtag_action.dta_rec;
 	uint32_t i, ndx, size, fsize;
 	uint32_t align = sizeof (uint64_t) - 1;
 	dtrace_aggbuffer_t *agb;
 	dtrace_aggkey_t *key;
 	uint32_t hashval = 0, limit, isstr;
 	caddr_t tomax, data, kdata;
 	dtrace_actkind_t action;
 	dtrace_action_t *act;
 	uintptr_t offs;
 
 	if (buf == NULL)
 		return;
 
 	if (!agg->dtag_hasarg) {
 		/*
 		 * Currently, only quantize() and lquantize() take additional
 		 * arguments, and they have the same semantics:  an increment
 		 * value that defaults to 1 when not present.  If additional
 		 * aggregating actions take arguments, the setting of the
 		 * default argument value will presumably have to become more
 		 * sophisticated...
 		 */
 		arg = 1;
 	}
 
 	action = agg->dtag_action.dta_kind - DTRACEACT_AGGREGATION;
 	size = rec->dtrd_offset - agg->dtag_base;
 	fsize = size + rec->dtrd_size;
 
 	ASSERT(dbuf->dtb_tomax != NULL);
 	data = dbuf->dtb_tomax + offset + agg->dtag_base;
 
 	if ((tomax = buf->dtb_tomax) == NULL) {
 		dtrace_buffer_drop(buf);
 		return;
 	}
 
 	/*
 	 * The metastructure is always at the bottom of the buffer.
 	 */
 	agb = (dtrace_aggbuffer_t *)(tomax + buf->dtb_size -
 	    sizeof (dtrace_aggbuffer_t));
 
 	if (buf->dtb_offset == 0) {
 		/*
 		 * We just kludge up approximately 1/8th of the size to be
 		 * buckets.  If this guess ends up being routinely
 		 * off-the-mark, we may need to dynamically readjust this
 		 * based on past performance.
 		 */
 		uintptr_t hashsize = (buf->dtb_size >> 3) / sizeof (uintptr_t);
 
 		if ((uintptr_t)agb - hashsize * sizeof (dtrace_aggkey_t *) <
 		    (uintptr_t)tomax || hashsize == 0) {
 			/*
 			 * We've been given a ludicrously small buffer;
 			 * increment our drop count and leave.
 			 */
 			dtrace_buffer_drop(buf);
 			return;
 		}
 
 		/*
 		 * And now, a pathetic attempt to try to get a an odd (or
 		 * perchance, a prime) hash size for better hash distribution.
 		 */
 		if (hashsize > (DTRACE_AGGHASHSIZE_SLEW << 3))
 			hashsize -= DTRACE_AGGHASHSIZE_SLEW;
 
 		agb->dtagb_hashsize = hashsize;
 		agb->dtagb_hash = (dtrace_aggkey_t **)((uintptr_t)agb -
 		    agb->dtagb_hashsize * sizeof (dtrace_aggkey_t *));
 		agb->dtagb_free = (uintptr_t)agb->dtagb_hash;
 
 		for (i = 0; i < agb->dtagb_hashsize; i++)
 			agb->dtagb_hash[i] = NULL;
 	}
 
 	ASSERT(agg->dtag_first != NULL);
 	ASSERT(agg->dtag_first->dta_intuple);
 
 	/*
 	 * Calculate the hash value based on the key.  Note that we _don't_
 	 * include the aggid in the hashing (but we will store it as part of
 	 * the key).  The hashing algorithm is Bob Jenkins' "One-at-a-time"
 	 * algorithm: a simple, quick algorithm that has no known funnels, and
 	 * gets good distribution in practice.  The efficacy of the hashing
 	 * algorithm (and a comparison with other algorithms) may be found by
 	 * running the ::dtrace_aggstat MDB dcmd.
 	 */
 	for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) {
 		i = act->dta_rec.dtrd_offset - agg->dtag_base;
 		limit = i + act->dta_rec.dtrd_size;
 		ASSERT(limit <= size);
 		isstr = DTRACEACT_ISSTRING(act);
 
 		for (; i < limit; i++) {
 			hashval += data[i];
 			hashval += (hashval << 10);
 			hashval ^= (hashval >> 6);
 
 			if (isstr && data[i] == '\0')
 				break;
 		}
 	}
 
 	hashval += (hashval << 3);
 	hashval ^= (hashval >> 11);
 	hashval += (hashval << 15);
 
 	/*
 	 * Yes, the divide here is expensive -- but it's generally the least
 	 * of the performance issues given the amount of data that we iterate
 	 * over to compute hash values, compare data, etc.
 	 */
 	ndx = hashval % agb->dtagb_hashsize;
 
 	for (key = agb->dtagb_hash[ndx]; key != NULL; key = key->dtak_next) {
 		ASSERT((caddr_t)key >= tomax);
 		ASSERT((caddr_t)key < tomax + buf->dtb_size);
 
 		if (hashval != key->dtak_hashval || key->dtak_size != size)
 			continue;
 
 		kdata = key->dtak_data;
 		ASSERT(kdata >= tomax && kdata < tomax + buf->dtb_size);
 
 		for (act = agg->dtag_first; act->dta_intuple;
 		    act = act->dta_next) {
 			i = act->dta_rec.dtrd_offset - agg->dtag_base;
 			limit = i + act->dta_rec.dtrd_size;
 			ASSERT(limit <= size);
 			isstr = DTRACEACT_ISSTRING(act);
 
 			for (; i < limit; i++) {
 				if (kdata[i] != data[i])
 					goto next;
 
 				if (isstr && data[i] == '\0')
 					break;
 			}
 		}
 
 		if (action != key->dtak_action) {
 			/*
 			 * We are aggregating on the same value in the same
 			 * aggregation with two different aggregating actions.
 			 * (This should have been picked up in the compiler,
 			 * so we may be dealing with errant or devious DIF.)
 			 * This is an error condition; we indicate as much,
 			 * and return.
 			 */
 			DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
 			return;
 		}
 
 		/*
 		 * This is a hit:  we need to apply the aggregator to
 		 * the value at this key.
 		 */
 		agg->dtag_aggregate((uint64_t *)(kdata + size), expr, arg);
 		return;
 next:
 		continue;
 	}
 
 	/*
 	 * We didn't find it.  We need to allocate some zero-filled space,
 	 * link it into the hash table appropriately, and apply the aggregator
 	 * to the (zero-filled) value.
 	 */
 	offs = buf->dtb_offset;
 	while (offs & (align - 1))
 		offs += sizeof (uint32_t);
 
 	/*
 	 * If we don't have enough room to both allocate a new key _and_
 	 * its associated data, increment the drop count and return.
 	 */
 	if ((uintptr_t)tomax + offs + fsize >
 	    agb->dtagb_free - sizeof (dtrace_aggkey_t)) {
 		dtrace_buffer_drop(buf);
 		return;
 	}
 
 	/*CONSTCOND*/
 	ASSERT(!(sizeof (dtrace_aggkey_t) & (sizeof (uintptr_t) - 1)));
 	key = (dtrace_aggkey_t *)(agb->dtagb_free - sizeof (dtrace_aggkey_t));
 	agb->dtagb_free -= sizeof (dtrace_aggkey_t);
 
 	key->dtak_data = kdata = tomax + offs;
 	buf->dtb_offset = offs + fsize;
 
 	/*
 	 * Now copy the data across.
 	 */
 	*((dtrace_aggid_t *)kdata) = agg->dtag_id;
 
 	for (i = sizeof (dtrace_aggid_t); i < size; i++)
 		kdata[i] = data[i];
 
 	/*
 	 * Because strings are not zeroed out by default, we need to iterate
 	 * looking for actions that store strings, and we need to explicitly
 	 * pad these strings out with zeroes.
 	 */
 	for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) {
 		int nul;
 
 		if (!DTRACEACT_ISSTRING(act))
 			continue;
 
 		i = act->dta_rec.dtrd_offset - agg->dtag_base;
 		limit = i + act->dta_rec.dtrd_size;
 		ASSERT(limit <= size);
 
 		for (nul = 0; i < limit; i++) {
 			if (nul) {
 				kdata[i] = '\0';
 				continue;
 			}
 
 			if (data[i] != '\0')
 				continue;
 
 			nul = 1;
 		}
 	}
 
 	for (i = size; i < fsize; i++)
 		kdata[i] = 0;
 
 	key->dtak_hashval = hashval;
 	key->dtak_size = size;
 	key->dtak_action = action;
 	key->dtak_next = agb->dtagb_hash[ndx];
 	agb->dtagb_hash[ndx] = key;
 
 	/*
 	 * Finally, apply the aggregator.
 	 */
 	*((uint64_t *)(key->dtak_data + size)) = agg->dtag_initial;
 	agg->dtag_aggregate((uint64_t *)(key->dtak_data + size), expr, arg);
 }
 
 /*
  * Given consumer state, this routine finds a speculation in the INACTIVE
  * state and transitions it into the ACTIVE state.  If there is no speculation
  * in the INACTIVE state, 0 is returned.  In this case, no error counter is
  * incremented -- it is up to the caller to take appropriate action.
  */
 static int
 dtrace_speculation(dtrace_state_t *state)
 {
 	int i = 0;
 	dtrace_speculation_state_t curstate;
 	uint32_t *stat = &state->dts_speculations_unavail, count;
 
 	while (i < state->dts_nspeculations) {
 		dtrace_speculation_t *spec = &state->dts_speculations[i];
 
 		curstate = spec->dtsp_state;
 
 		if (curstate != DTRACESPEC_INACTIVE) {
 			if (curstate == DTRACESPEC_COMMITTINGMANY ||
 			    curstate == DTRACESPEC_COMMITTING ||
 			    curstate == DTRACESPEC_DISCARDING)
 				stat = &state->dts_speculations_busy;
 			i++;
 			continue;
 		}
 
 		if (dtrace_cas32((uint32_t *)&spec->dtsp_state,
 		    curstate, DTRACESPEC_ACTIVE) == curstate)
 			return (i + 1);
 	}
 
 	/*
 	 * We couldn't find a speculation.  If we found as much as a single
 	 * busy speculation buffer, we'll attribute this failure as "busy"
 	 * instead of "unavail".
 	 */
 	do {
 		count = *stat;
 	} while (dtrace_cas32(stat, count, count + 1) != count);
 
 	return (0);
 }
 
 /*
  * This routine commits an active speculation.  If the specified speculation
  * is not in a valid state to perform a commit(), this routine will silently do
  * nothing.  The state of the specified speculation is transitioned according
  * to the state transition diagram outlined in <sys/dtrace_impl.h>
  */
 static void
 dtrace_speculation_commit(dtrace_state_t *state, processorid_t cpu,
     dtrace_specid_t which)
 {
 	dtrace_speculation_t *spec;
 	dtrace_buffer_t *src, *dest;
 	uintptr_t daddr, saddr, dlimit, slimit;
 	dtrace_speculation_state_t curstate, new = 0;
 	intptr_t offs;
 	uint64_t timestamp;
 
 	if (which == 0)
 		return;
 
 	if (which > state->dts_nspeculations) {
 		cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
 		return;
 	}
 
 	spec = &state->dts_speculations[which - 1];
 	src = &spec->dtsp_buffer[cpu];
 	dest = &state->dts_buffer[cpu];
 
 	do {
 		curstate = spec->dtsp_state;
 
 		if (curstate == DTRACESPEC_COMMITTINGMANY)
 			break;
 
 		switch (curstate) {
 		case DTRACESPEC_INACTIVE:
 		case DTRACESPEC_DISCARDING:
 			return;
 
 		case DTRACESPEC_COMMITTING:
 			/*
 			 * This is only possible if we are (a) commit()'ing
 			 * without having done a prior speculate() on this CPU
 			 * and (b) racing with another commit() on a different
 			 * CPU.  There's nothing to do -- we just assert that
 			 * our offset is 0.
 			 */
 			ASSERT(src->dtb_offset == 0);
 			return;
 
 		case DTRACESPEC_ACTIVE:
 			new = DTRACESPEC_COMMITTING;
 			break;
 
 		case DTRACESPEC_ACTIVEONE:
 			/*
 			 * This speculation is active on one CPU.  If our
 			 * buffer offset is non-zero, we know that the one CPU
 			 * must be us.  Otherwise, we are committing on a
 			 * different CPU from the speculate(), and we must
 			 * rely on being asynchronously cleaned.
 			 */
 			if (src->dtb_offset != 0) {
 				new = DTRACESPEC_COMMITTING;
 				break;
 			}
 			/*FALLTHROUGH*/
 
 		case DTRACESPEC_ACTIVEMANY:
 			new = DTRACESPEC_COMMITTINGMANY;
 			break;
 
 		default:
 			ASSERT(0);
 		}
 	} while (dtrace_cas32((uint32_t *)&spec->dtsp_state,
 	    curstate, new) != curstate);
 
 	/*
 	 * We have set the state to indicate that we are committing this
 	 * speculation.  Now reserve the necessary space in the destination
 	 * buffer.
 	 */
 	if ((offs = dtrace_buffer_reserve(dest, src->dtb_offset,
 	    sizeof (uint64_t), state, NULL)) < 0) {
 		dtrace_buffer_drop(dest);
 		goto out;
 	}
 
 	/*
 	 * We have sufficient space to copy the speculative buffer into the
 	 * primary buffer.  First, modify the speculative buffer, filling
 	 * in the timestamp of all entries with the curstate time.  The data
 	 * must have the commit() time rather than the time it was traced,
 	 * so that all entries in the primary buffer are in timestamp order.
 	 */
 	timestamp = dtrace_gethrtime();
 	saddr = (uintptr_t)src->dtb_tomax;
 	slimit = saddr + src->dtb_offset;
 	while (saddr < slimit) {
 		size_t size;
 		dtrace_rechdr_t *dtrh = (dtrace_rechdr_t *)saddr;
 
 		if (dtrh->dtrh_epid == DTRACE_EPIDNONE) {
 			saddr += sizeof (dtrace_epid_t);
 			continue;
 		}
 		ASSERT3U(dtrh->dtrh_epid, <=, state->dts_necbs);
 		size = state->dts_ecbs[dtrh->dtrh_epid - 1]->dte_size;
 
 		ASSERT3U(saddr + size, <=, slimit);
 		ASSERT3U(size, >=, sizeof (dtrace_rechdr_t));
 		ASSERT3U(DTRACE_RECORD_LOAD_TIMESTAMP(dtrh), ==, UINT64_MAX);
 
 		DTRACE_RECORD_STORE_TIMESTAMP(dtrh, timestamp);
 
 		saddr += size;
 	}
 
 	/*
 	 * Copy the buffer across.  (Note that this is a
 	 * highly subobtimal bcopy(); in the unlikely event that this becomes
 	 * a serious performance issue, a high-performance DTrace-specific
 	 * bcopy() should obviously be invented.)
 	 */
 	daddr = (uintptr_t)dest->dtb_tomax + offs;
 	dlimit = daddr + src->dtb_offset;
 	saddr = (uintptr_t)src->dtb_tomax;
 
 	/*
 	 * First, the aligned portion.
 	 */
 	while (dlimit - daddr >= sizeof (uint64_t)) {
 		*((uint64_t *)daddr) = *((uint64_t *)saddr);
 
 		daddr += sizeof (uint64_t);
 		saddr += sizeof (uint64_t);
 	}
 
 	/*
 	 * Now any left-over bit...
 	 */
 	while (dlimit - daddr)
 		*((uint8_t *)daddr++) = *((uint8_t *)saddr++);
 
 	/*
 	 * Finally, commit the reserved space in the destination buffer.
 	 */
 	dest->dtb_offset = offs + src->dtb_offset;
 
 out:
 	/*
 	 * If we're lucky enough to be the only active CPU on this speculation
 	 * buffer, we can just set the state back to DTRACESPEC_INACTIVE.
 	 */
 	if (curstate == DTRACESPEC_ACTIVE ||
 	    (curstate == DTRACESPEC_ACTIVEONE && new == DTRACESPEC_COMMITTING)) {
 		uint32_t rval = dtrace_cas32((uint32_t *)&spec->dtsp_state,
 		    DTRACESPEC_COMMITTING, DTRACESPEC_INACTIVE);
 
 		ASSERT(rval == DTRACESPEC_COMMITTING);
 	}
 
 	src->dtb_offset = 0;
 	src->dtb_xamot_drops += src->dtb_drops;
 	src->dtb_drops = 0;
 }
 
 /*
  * This routine discards an active speculation.  If the specified speculation
  * is not in a valid state to perform a discard(), this routine will silently
  * do nothing.  The state of the specified speculation is transitioned
  * according to the state transition diagram outlined in <sys/dtrace_impl.h>
  */
 static void
 dtrace_speculation_discard(dtrace_state_t *state, processorid_t cpu,
     dtrace_specid_t which)
 {
 	dtrace_speculation_t *spec;
 	dtrace_speculation_state_t curstate, new = 0;
 	dtrace_buffer_t *buf;
 
 	if (which == 0)
 		return;
 
 	if (which > state->dts_nspeculations) {
 		cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
 		return;
 	}
 
 	spec = &state->dts_speculations[which - 1];
 	buf = &spec->dtsp_buffer[cpu];
 
 	do {
 		curstate = spec->dtsp_state;
 
 		switch (curstate) {
 		case DTRACESPEC_INACTIVE:
 		case DTRACESPEC_COMMITTINGMANY:
 		case DTRACESPEC_COMMITTING:
 		case DTRACESPEC_DISCARDING:
 			return;
 
 		case DTRACESPEC_ACTIVE:
 		case DTRACESPEC_ACTIVEMANY:
 			new = DTRACESPEC_DISCARDING;
 			break;
 
 		case DTRACESPEC_ACTIVEONE:
 			if (buf->dtb_offset != 0) {
 				new = DTRACESPEC_INACTIVE;
 			} else {
 				new = DTRACESPEC_DISCARDING;
 			}
 			break;
 
 		default:
 			ASSERT(0);
 		}
 	} while (dtrace_cas32((uint32_t *)&spec->dtsp_state,
 	    curstate, new) != curstate);
 
 	buf->dtb_offset = 0;
 	buf->dtb_drops = 0;
 }
 
 /*
  * Note:  not called from probe context.  This function is called
  * asynchronously from cross call context to clean any speculations that are
  * in the COMMITTINGMANY or DISCARDING states.  These speculations may not be
  * transitioned back to the INACTIVE state until all CPUs have cleaned the
  * speculation.
  */
 static void
 dtrace_speculation_clean_here(dtrace_state_t *state)
 {
 	dtrace_icookie_t cookie;
 	processorid_t cpu = curcpu;
 	dtrace_buffer_t *dest = &state->dts_buffer[cpu];
 	dtrace_specid_t i;
 
 	cookie = dtrace_interrupt_disable();
 
 	if (dest->dtb_tomax == NULL) {
 		dtrace_interrupt_enable(cookie);
 		return;
 	}
 
 	for (i = 0; i < state->dts_nspeculations; i++) {
 		dtrace_speculation_t *spec = &state->dts_speculations[i];
 		dtrace_buffer_t *src = &spec->dtsp_buffer[cpu];
 
 		if (src->dtb_tomax == NULL)
 			continue;
 
 		if (spec->dtsp_state == DTRACESPEC_DISCARDING) {
 			src->dtb_offset = 0;
 			continue;
 		}
 
 		if (spec->dtsp_state != DTRACESPEC_COMMITTINGMANY)
 			continue;
 
 		if (src->dtb_offset == 0)
 			continue;
 
 		dtrace_speculation_commit(state, cpu, i + 1);
 	}
 
 	dtrace_interrupt_enable(cookie);
 }
 
 /*
  * Note:  not called from probe context.  This function is called
  * asynchronously (and at a regular interval) to clean any speculations that
  * are in the COMMITTINGMANY or DISCARDING states.  If it discovers that there
  * is work to be done, it cross calls all CPUs to perform that work;
  * COMMITMANY and DISCARDING speculations may not be transitioned back to the
  * INACTIVE state until they have been cleaned by all CPUs.
  */
 static void
 dtrace_speculation_clean(dtrace_state_t *state)
 {
 	int work = 0, rv;
 	dtrace_specid_t i;
 
 	for (i = 0; i < state->dts_nspeculations; i++) {
 		dtrace_speculation_t *spec = &state->dts_speculations[i];
 
 		ASSERT(!spec->dtsp_cleaning);
 
 		if (spec->dtsp_state != DTRACESPEC_DISCARDING &&
 		    spec->dtsp_state != DTRACESPEC_COMMITTINGMANY)
 			continue;
 
 		work++;
 		spec->dtsp_cleaning = 1;
 	}
 
 	if (!work)
 		return;
 
 	dtrace_xcall(DTRACE_CPUALL,
 	    (dtrace_xcall_t)dtrace_speculation_clean_here, state);
 
 	/*
 	 * We now know that all CPUs have committed or discarded their
 	 * speculation buffers, as appropriate.  We can now set the state
 	 * to inactive.
 	 */
 	for (i = 0; i < state->dts_nspeculations; i++) {
 		dtrace_speculation_t *spec = &state->dts_speculations[i];
 		dtrace_speculation_state_t curstate, new;
 
 		if (!spec->dtsp_cleaning)
 			continue;
 
 		curstate = spec->dtsp_state;
 		ASSERT(curstate == DTRACESPEC_DISCARDING ||
 		    curstate == DTRACESPEC_COMMITTINGMANY);
 
 		new = DTRACESPEC_INACTIVE;
 
 		rv = dtrace_cas32((uint32_t *)&spec->dtsp_state, curstate, new);
 		ASSERT(rv == curstate);
 		spec->dtsp_cleaning = 0;
 	}
 }
 
 /*
  * Called as part of a speculate() to get the speculative buffer associated
  * with a given speculation.  Returns NULL if the specified speculation is not
  * in an ACTIVE state.  If the speculation is in the ACTIVEONE state -- and
  * the active CPU is not the specified CPU -- the speculation will be
  * atomically transitioned into the ACTIVEMANY state.
  */
 static dtrace_buffer_t *
 dtrace_speculation_buffer(dtrace_state_t *state, processorid_t cpuid,
     dtrace_specid_t which)
 {
 	dtrace_speculation_t *spec;
 	dtrace_speculation_state_t curstate, new = 0;
 	dtrace_buffer_t *buf;
 
 	if (which == 0)
 		return (NULL);
 
 	if (which > state->dts_nspeculations) {
 		cpu_core[cpuid].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
 		return (NULL);
 	}
 
 	spec = &state->dts_speculations[which - 1];
 	buf = &spec->dtsp_buffer[cpuid];
 
 	do {
 		curstate = spec->dtsp_state;
 
 		switch (curstate) {
 		case DTRACESPEC_INACTIVE:
 		case DTRACESPEC_COMMITTINGMANY:
 		case DTRACESPEC_DISCARDING:
 			return (NULL);
 
 		case DTRACESPEC_COMMITTING:
 			ASSERT(buf->dtb_offset == 0);
 			return (NULL);
 
 		case DTRACESPEC_ACTIVEONE:
 			/*
 			 * This speculation is currently active on one CPU.
 			 * Check the offset in the buffer; if it's non-zero,
 			 * that CPU must be us (and we leave the state alone).
 			 * If it's zero, assume that we're starting on a new
 			 * CPU -- and change the state to indicate that the
 			 * speculation is active on more than one CPU.
 			 */
 			if (buf->dtb_offset != 0)
 				return (buf);
 
 			new = DTRACESPEC_ACTIVEMANY;
 			break;
 
 		case DTRACESPEC_ACTIVEMANY:
 			return (buf);
 
 		case DTRACESPEC_ACTIVE:
 			new = DTRACESPEC_ACTIVEONE;
 			break;
 
 		default:
 			ASSERT(0);
 		}
 	} while (dtrace_cas32((uint32_t *)&spec->dtsp_state,
 	    curstate, new) != curstate);
 
 	ASSERT(new == DTRACESPEC_ACTIVEONE || new == DTRACESPEC_ACTIVEMANY);
 	return (buf);
 }
 
 /*
  * Return a string.  In the event that the user lacks the privilege to access
  * arbitrary kernel memory, we copy the string out to scratch memory so that we
  * don't fail access checking.
  *
  * dtrace_dif_variable() uses this routine as a helper for various
  * builtin values such as 'execname' and 'probefunc.'
  */
 uintptr_t
 dtrace_dif_varstr(uintptr_t addr, dtrace_state_t *state,
     dtrace_mstate_t *mstate)
 {
 	uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
 	uintptr_t ret;
 	size_t strsz;
 
 	/*
 	 * The easy case: this probe is allowed to read all of memory, so
 	 * we can just return this as a vanilla pointer.
 	 */
 	if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0)
 		return (addr);
 
 	/*
 	 * This is the tougher case: we copy the string in question from
 	 * kernel memory into scratch memory and return it that way: this
 	 * ensures that we won't trip up when access checking tests the
 	 * BYREF return value.
 	 */
 	strsz = dtrace_strlen((char *)addr, size) + 1;
 
 	if (mstate->dtms_scratch_ptr + strsz >
 	    mstate->dtms_scratch_base + mstate->dtms_scratch_size) {
 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
 		return (0);
 	}
 
 	dtrace_strcpy((const void *)addr, (void *)mstate->dtms_scratch_ptr,
 	    strsz);
 	ret = mstate->dtms_scratch_ptr;
 	mstate->dtms_scratch_ptr += strsz;
 	return (ret);
 }
 
 /*
  * Return a string from a memoy address which is known to have one or
  * more concatenated, individually zero terminated, sub-strings.
  * In the event that the user lacks the privilege to access
  * arbitrary kernel memory, we copy the string out to scratch memory so that we
  * don't fail access checking.
  *
  * dtrace_dif_variable() uses this routine as a helper for various
  * builtin values such as 'execargs'.
  */
 static uintptr_t
 dtrace_dif_varstrz(uintptr_t addr, size_t strsz, dtrace_state_t *state,
     dtrace_mstate_t *mstate)
 {
 	char *p;
 	size_t i;
 	uintptr_t ret;
 
 	if (mstate->dtms_scratch_ptr + strsz >
 	    mstate->dtms_scratch_base + mstate->dtms_scratch_size) {
 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
 		return (0);
 	}
 
 	dtrace_bcopy((const void *)addr, (void *)mstate->dtms_scratch_ptr,
 	    strsz);
 
 	/* Replace sub-string termination characters with a space. */
 	for (p = (char *) mstate->dtms_scratch_ptr, i = 0; i < strsz - 1;
 	    p++, i++)
 		if (*p == '\0')
 			*p = ' ';
 
 	ret = mstate->dtms_scratch_ptr;
 	mstate->dtms_scratch_ptr += strsz;
 	return (ret);
 }
 
 /*
  * This function implements the DIF emulator's variable lookups.  The emulator
  * passes a reserved variable identifier and optional built-in array index.
  */
 static uint64_t
 dtrace_dif_variable(dtrace_mstate_t *mstate, dtrace_state_t *state, uint64_t v,
     uint64_t ndx)
 {
 	/*
 	 * If we're accessing one of the uncached arguments, we'll turn this
 	 * into a reference in the args array.
 	 */
 	if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) {
 		ndx = v - DIF_VAR_ARG0;
 		v = DIF_VAR_ARGS;
 	}
 
 	switch (v) {
 	case DIF_VAR_ARGS:
 		ASSERT(mstate->dtms_present & DTRACE_MSTATE_ARGS);
 		if (ndx >= sizeof (mstate->dtms_arg) /
 		    sizeof (mstate->dtms_arg[0])) {
 			int aframes = mstate->dtms_probe->dtpr_aframes + 2;
 			dtrace_provider_t *pv;
 			uint64_t val;
 
 			pv = mstate->dtms_probe->dtpr_provider;
 			if (pv->dtpv_pops.dtps_getargval != NULL)
 				val = pv->dtpv_pops.dtps_getargval(pv->dtpv_arg,
 				    mstate->dtms_probe->dtpr_id,
 				    mstate->dtms_probe->dtpr_arg, ndx, aframes);
 			else
 				val = dtrace_getarg(ndx, aframes);
 
 			/*
 			 * This is regrettably required to keep the compiler
 			 * from tail-optimizing the call to dtrace_getarg().
 			 * The condition always evaluates to true, but the
 			 * compiler has no way of figuring that out a priori.
 			 * (None of this would be necessary if the compiler
 			 * could be relied upon to _always_ tail-optimize
 			 * the call to dtrace_getarg() -- but it can't.)
 			 */
 			if (mstate->dtms_probe != NULL)
 				return (val);
 
 			ASSERT(0);
 		}
 
 		return (mstate->dtms_arg[ndx]);
 
 #ifdef illumos
 	case DIF_VAR_UREGS: {
 		klwp_t *lwp;
 
 		if (!dtrace_priv_proc(state))
 			return (0);
 
 		if ((lwp = curthread->t_lwp) == NULL) {
 			DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
 			cpu_core[curcpu].cpuc_dtrace_illval = NULL;
 			return (0);
 		}
 
 		return (dtrace_getreg(lwp->lwp_regs, ndx));
 		return (0);
 	}
 #else
 	case DIF_VAR_UREGS: {
 		struct trapframe *tframe;
 
 		if (!dtrace_priv_proc(state))
 			return (0);
 
 		if ((tframe = curthread->td_frame) == NULL) {
 			DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
 			cpu_core[curcpu].cpuc_dtrace_illval = 0;
 			return (0);
 		}
 
 		return (dtrace_getreg(tframe, ndx));
 	}
 #endif
 
 	case DIF_VAR_CURTHREAD:
 		if (!dtrace_priv_proc(state))
 			return (0);
 		return ((uint64_t)(uintptr_t)curthread);
 
 	case DIF_VAR_TIMESTAMP:
 		if (!(mstate->dtms_present & DTRACE_MSTATE_TIMESTAMP)) {
 			mstate->dtms_timestamp = dtrace_gethrtime();
 			mstate->dtms_present |= DTRACE_MSTATE_TIMESTAMP;
 		}
 		return (mstate->dtms_timestamp);
 
 	case DIF_VAR_VTIMESTAMP:
 		ASSERT(dtrace_vtime_references != 0);
 		return (curthread->t_dtrace_vtime);
 
 	case DIF_VAR_WALLTIMESTAMP:
 		if (!(mstate->dtms_present & DTRACE_MSTATE_WALLTIMESTAMP)) {
 			mstate->dtms_walltimestamp = dtrace_gethrestime();
 			mstate->dtms_present |= DTRACE_MSTATE_WALLTIMESTAMP;
 		}
 		return (mstate->dtms_walltimestamp);
 
 #ifdef illumos
 	case DIF_VAR_IPL:
 		if (!dtrace_priv_kernel(state))
 			return (0);
 		if (!(mstate->dtms_present & DTRACE_MSTATE_IPL)) {
 			mstate->dtms_ipl = dtrace_getipl();
 			mstate->dtms_present |= DTRACE_MSTATE_IPL;
 		}
 		return (mstate->dtms_ipl);
 #endif
 
 	case DIF_VAR_EPID:
 		ASSERT(mstate->dtms_present & DTRACE_MSTATE_EPID);
 		return (mstate->dtms_epid);
 
 	case DIF_VAR_ID:
 		ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
 		return (mstate->dtms_probe->dtpr_id);
 
 	case DIF_VAR_STACKDEPTH:
 		if (!dtrace_priv_kernel(state))
 			return (0);
 		if (!(mstate->dtms_present & DTRACE_MSTATE_STACKDEPTH)) {
 			int aframes = mstate->dtms_probe->dtpr_aframes + 2;
 
 			mstate->dtms_stackdepth = dtrace_getstackdepth(aframes);
 			mstate->dtms_present |= DTRACE_MSTATE_STACKDEPTH;
 		}
 		return (mstate->dtms_stackdepth);
 
 	case DIF_VAR_USTACKDEPTH:
 		if (!dtrace_priv_proc(state))
 			return (0);
 		if (!(mstate->dtms_present & DTRACE_MSTATE_USTACKDEPTH)) {
 			/*
 			 * See comment in DIF_VAR_PID.
 			 */
 			if (DTRACE_ANCHORED(mstate->dtms_probe) &&
 			    CPU_ON_INTR(CPU)) {
 				mstate->dtms_ustackdepth = 0;
 			} else {
 				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
 				mstate->dtms_ustackdepth =
 				    dtrace_getustackdepth();
 				DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
 			}
 			mstate->dtms_present |= DTRACE_MSTATE_USTACKDEPTH;
 		}
 		return (mstate->dtms_ustackdepth);
 
 	case DIF_VAR_CALLER:
 		if (!dtrace_priv_kernel(state))
 			return (0);
 		if (!(mstate->dtms_present & DTRACE_MSTATE_CALLER)) {
 			int aframes = mstate->dtms_probe->dtpr_aframes + 2;
 
 			if (!DTRACE_ANCHORED(mstate->dtms_probe)) {
 				/*
 				 * If this is an unanchored probe, we are
 				 * required to go through the slow path:
 				 * dtrace_caller() only guarantees correct
 				 * results for anchored probes.
 				 */
 				pc_t caller[2] = {0, 0};
 
 				dtrace_getpcstack(caller, 2, aframes,
 				    (uint32_t *)(uintptr_t)mstate->dtms_arg[0]);
 				mstate->dtms_caller = caller[1];
 			} else if ((mstate->dtms_caller =
 			    dtrace_caller(aframes)) == -1) {
 				/*
 				 * We have failed to do this the quick way;
 				 * we must resort to the slower approach of
 				 * calling dtrace_getpcstack().
 				 */
 				pc_t caller = 0;
 
 				dtrace_getpcstack(&caller, 1, aframes, NULL);
 				mstate->dtms_caller = caller;
 			}
 
 			mstate->dtms_present |= DTRACE_MSTATE_CALLER;
 		}
 		return (mstate->dtms_caller);
 
 	case DIF_VAR_UCALLER:
 		if (!dtrace_priv_proc(state))
 			return (0);
 
 		if (!(mstate->dtms_present & DTRACE_MSTATE_UCALLER)) {
 			uint64_t ustack[3];
 
 			/*
 			 * dtrace_getupcstack() fills in the first uint64_t
 			 * with the current PID.  The second uint64_t will
 			 * be the program counter at user-level.  The third
 			 * uint64_t will contain the caller, which is what
 			 * we're after.
 			 */
 			ustack[2] = 0;
 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
 			dtrace_getupcstack(ustack, 3);
 			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
 			mstate->dtms_ucaller = ustack[2];
 			mstate->dtms_present |= DTRACE_MSTATE_UCALLER;
 		}
 
 		return (mstate->dtms_ucaller);
 
 	case DIF_VAR_PROBEPROV:
 		ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
 		return (dtrace_dif_varstr(
 		    (uintptr_t)mstate->dtms_probe->dtpr_provider->dtpv_name,
 		    state, mstate));
 
 	case DIF_VAR_PROBEMOD:
 		ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
 		return (dtrace_dif_varstr(
 		    (uintptr_t)mstate->dtms_probe->dtpr_mod,
 		    state, mstate));
 
 	case DIF_VAR_PROBEFUNC:
 		ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
 		return (dtrace_dif_varstr(
 		    (uintptr_t)mstate->dtms_probe->dtpr_func,
 		    state, mstate));
 
 	case DIF_VAR_PROBENAME:
 		ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE);
 		return (dtrace_dif_varstr(
 		    (uintptr_t)mstate->dtms_probe->dtpr_name,
 		    state, mstate));
 
 	case DIF_VAR_PID:
 		if (!dtrace_priv_proc(state))
 			return (0);
 
 #ifdef illumos
 		/*
 		 * Note that we are assuming that an unanchored probe is
 		 * always due to a high-level interrupt.  (And we're assuming
 		 * that there is only a single high level interrupt.)
 		 */
 		if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
 			return (pid0.pid_id);
 
 		/*
 		 * It is always safe to dereference one's own t_procp pointer:
 		 * it always points to a valid, allocated proc structure.
 		 * Further, it is always safe to dereference the p_pidp member
 		 * of one's own proc structure.  (These are truisms becuase
 		 * threads and processes don't clean up their own state --
 		 * they leave that task to whomever reaps them.)
 		 */
 		return ((uint64_t)curthread->t_procp->p_pidp->pid_id);
 #else
 		return ((uint64_t)curproc->p_pid);
 #endif
 
 	case DIF_VAR_PPID:
 		if (!dtrace_priv_proc(state))
 			return (0);
 
 #ifdef illumos
 		/*
 		 * See comment in DIF_VAR_PID.
 		 */
 		if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
 			return (pid0.pid_id);
 
 		/*
 		 * It is always safe to dereference one's own t_procp pointer:
 		 * it always points to a valid, allocated proc structure.
 		 * (This is true because threads don't clean up their own
 		 * state -- they leave that task to whomever reaps them.)
 		 */
 		return ((uint64_t)curthread->t_procp->p_ppid);
 #else
 		if (curproc->p_pid == proc0.p_pid)
 			return (curproc->p_pid);
 		else
 			return (curproc->p_pptr->p_pid);
 #endif
 
 	case DIF_VAR_TID:
 #ifdef illumos
 		/*
 		 * See comment in DIF_VAR_PID.
 		 */
 		if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
 			return (0);
 #endif
 
 		return ((uint64_t)curthread->t_tid);
 
 	case DIF_VAR_EXECARGS: {
 		struct pargs *p_args = curthread->td_proc->p_args;
 
 		if (p_args == NULL)
 			return(0);
 
 		return (dtrace_dif_varstrz(
 		    (uintptr_t) p_args->ar_args, p_args->ar_length, state, mstate));
 	}
 
 	case DIF_VAR_EXECNAME:
 #ifdef illumos
 		if (!dtrace_priv_proc(state))
 			return (0);
 
 		/*
 		 * See comment in DIF_VAR_PID.
 		 */
 		if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
 			return ((uint64_t)(uintptr_t)p0.p_user.u_comm);
 
 		/*
 		 * It is always safe to dereference one's own t_procp pointer:
 		 * it always points to a valid, allocated proc structure.
 		 * (This is true because threads don't clean up their own
 		 * state -- they leave that task to whomever reaps them.)
 		 */
 		return (dtrace_dif_varstr(
 		    (uintptr_t)curthread->t_procp->p_user.u_comm,
 		    state, mstate));
 #else
 		return (dtrace_dif_varstr(
 		    (uintptr_t) curthread->td_proc->p_comm, state, mstate));
 #endif
 
 	case DIF_VAR_ZONENAME:
 #ifdef illumos
 		if (!dtrace_priv_proc(state))
 			return (0);
 
 		/*
 		 * See comment in DIF_VAR_PID.
 		 */
 		if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
 			return ((uint64_t)(uintptr_t)p0.p_zone->zone_name);
 
 		/*
 		 * It is always safe to dereference one's own t_procp pointer:
 		 * it always points to a valid, allocated proc structure.
 		 * (This is true because threads don't clean up their own
 		 * state -- they leave that task to whomever reaps them.)
 		 */
 		return (dtrace_dif_varstr(
 		    (uintptr_t)curthread->t_procp->p_zone->zone_name,
 		    state, mstate));
 #elif defined(__FreeBSD__)
 	/*
 	 * On FreeBSD, we introduce compatibility to zonename by falling through
 	 * into jailname.
 	 */
 	case DIF_VAR_JAILNAME:
 		if (!dtrace_priv_kernel(state))
 			return (0);
 
 		return (dtrace_dif_varstr(
 		    (uintptr_t)curthread->td_ucred->cr_prison->pr_name,
 		    state, mstate));
 
 	case DIF_VAR_JID:
 		if (!dtrace_priv_kernel(state))
 			return (0);
 
 		return ((uint64_t)curthread->td_ucred->cr_prison->pr_id);
 #else
 		return (0);
 #endif
 
 	case DIF_VAR_UID:
 		if (!dtrace_priv_proc(state))
 			return (0);
 
 #ifdef illumos
 		/*
 		 * See comment in DIF_VAR_PID.
 		 */
 		if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
 			return ((uint64_t)p0.p_cred->cr_uid);
 
 		/*
 		 * It is always safe to dereference one's own t_procp pointer:
 		 * it always points to a valid, allocated proc structure.
 		 * (This is true because threads don't clean up their own
 		 * state -- they leave that task to whomever reaps them.)
 		 *
 		 * Additionally, it is safe to dereference one's own process
 		 * credential, since this is never NULL after process birth.
 		 */
 		return ((uint64_t)curthread->t_procp->p_cred->cr_uid);
 #else
 		return ((uint64_t)curthread->td_ucred->cr_uid);
 #endif
 
 	case DIF_VAR_GID:
 		if (!dtrace_priv_proc(state))
 			return (0);
 
 #ifdef illumos
 		/*
 		 * See comment in DIF_VAR_PID.
 		 */
 		if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
 			return ((uint64_t)p0.p_cred->cr_gid);
 
 		/*
 		 * It is always safe to dereference one's own t_procp pointer:
 		 * it always points to a valid, allocated proc structure.
 		 * (This is true because threads don't clean up their own
 		 * state -- they leave that task to whomever reaps them.)
 		 *
 		 * Additionally, it is safe to dereference one's own process
 		 * credential, since this is never NULL after process birth.
 		 */
 		return ((uint64_t)curthread->t_procp->p_cred->cr_gid);
 #else
 		return ((uint64_t)curthread->td_ucred->cr_gid);
 #endif
 
 	case DIF_VAR_ERRNO: {
 #ifdef illumos
 		klwp_t *lwp;
 		if (!dtrace_priv_proc(state))
 			return (0);
 
 		/*
 		 * See comment in DIF_VAR_PID.
 		 */
 		if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU))
 			return (0);
 
 		/*
 		 * It is always safe to dereference one's own t_lwp pointer in
 		 * the event that this pointer is non-NULL.  (This is true
 		 * because threads and lwps don't clean up their own state --
 		 * they leave that task to whomever reaps them.)
 		 */
 		if ((lwp = curthread->t_lwp) == NULL)
 			return (0);
 
 		return ((uint64_t)lwp->lwp_errno);
 #else
 		return (curthread->td_errno);
 #endif
 	}
 #ifndef illumos
 	case DIF_VAR_CPU: {
 		return curcpu;
 	}
 #endif
 	default:
 		DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
 		return (0);
 	}
 }
 
 
 typedef enum dtrace_json_state {
 	DTRACE_JSON_REST = 1,
 	DTRACE_JSON_OBJECT,
 	DTRACE_JSON_STRING,
 	DTRACE_JSON_STRING_ESCAPE,
 	DTRACE_JSON_STRING_ESCAPE_UNICODE,
 	DTRACE_JSON_COLON,
 	DTRACE_JSON_COMMA,
 	DTRACE_JSON_VALUE,
 	DTRACE_JSON_IDENTIFIER,
 	DTRACE_JSON_NUMBER,
 	DTRACE_JSON_NUMBER_FRAC,
 	DTRACE_JSON_NUMBER_EXP,
 	DTRACE_JSON_COLLECT_OBJECT
 } dtrace_json_state_t;
 
 /*
  * This function possesses just enough knowledge about JSON to extract a single
  * value from a JSON string and store it in the scratch buffer.  It is able
  * to extract nested object values, and members of arrays by index.
  *
  * elemlist is a list of JSON keys, stored as packed NUL-terminated strings, to
  * be looked up as we descend into the object tree.  e.g.
  *
  *    foo[0].bar.baz[32] --> "foo" NUL "0" NUL "bar" NUL "baz" NUL "32" NUL
  *       with nelems = 5.
  *
  * The run time of this function must be bounded above by strsize to limit the
  * amount of work done in probe context.  As such, it is implemented as a
  * simple state machine, reading one character at a time using safe loads
  * until we find the requested element, hit a parsing error or run off the
  * end of the object or string.
  *
  * As there is no way for a subroutine to return an error without interrupting
  * clause execution, we simply return NULL in the event of a missing key or any
  * other error condition.  Each NULL return in this function is commented with
  * the error condition it represents -- parsing or otherwise.
  *
  * The set of states for the state machine closely matches the JSON
  * specification (http://json.org/).  Briefly:
  *
  *   DTRACE_JSON_REST:
  *     Skip whitespace until we find either a top-level Object, moving
  *     to DTRACE_JSON_OBJECT; or an Array, moving to DTRACE_JSON_VALUE.
  *
  *   DTRACE_JSON_OBJECT:
  *     Locate the next key String in an Object.  Sets a flag to denote
  *     the next String as a key string and moves to DTRACE_JSON_STRING.
  *
  *   DTRACE_JSON_COLON:
  *     Skip whitespace until we find the colon that separates key Strings
  *     from their values.  Once found, move to DTRACE_JSON_VALUE.
  *
  *   DTRACE_JSON_VALUE:
  *     Detects the type of the next value (String, Number, Identifier, Object
  *     or Array) and routes to the states that process that type.  Here we also
  *     deal with the element selector list if we are requested to traverse down
  *     into the object tree.
  *
  *   DTRACE_JSON_COMMA:
  *     Skip whitespace until we find the comma that separates key-value pairs
  *     in Objects (returning to DTRACE_JSON_OBJECT) or values in Arrays
  *     (similarly DTRACE_JSON_VALUE).  All following literal value processing
  *     states return to this state at the end of their value, unless otherwise
  *     noted.
  *
  *   DTRACE_JSON_NUMBER, DTRACE_JSON_NUMBER_FRAC, DTRACE_JSON_NUMBER_EXP:
  *     Processes a Number literal from the JSON, including any exponent
  *     component that may be present.  Numbers are returned as strings, which
  *     may be passed to strtoll() if an integer is required.
  *
  *   DTRACE_JSON_IDENTIFIER:
  *     Processes a "true", "false" or "null" literal in the JSON.
  *
  *   DTRACE_JSON_STRING, DTRACE_JSON_STRING_ESCAPE,
  *   DTRACE_JSON_STRING_ESCAPE_UNICODE:
  *     Processes a String literal from the JSON, whether the String denotes
  *     a key, a value or part of a larger Object.  Handles all escape sequences
  *     present in the specification, including four-digit unicode characters,
  *     but merely includes the escape sequence without converting it to the
  *     actual escaped character.  If the String is flagged as a key, we
  *     move to DTRACE_JSON_COLON rather than DTRACE_JSON_COMMA.
  *
  *   DTRACE_JSON_COLLECT_OBJECT:
  *     This state collects an entire Object (or Array), correctly handling
  *     embedded strings.  If the full element selector list matches this nested
  *     object, we return the Object in full as a string.  If not, we use this
  *     state to skip to the next value at this level and continue processing.
  *
  * NOTE: This function uses various macros from strtolctype.h to manipulate
  * digit values, etc -- these have all been checked to ensure they make
  * no additional function calls.
  */
 static char *
 dtrace_json(uint64_t size, uintptr_t json, char *elemlist, int nelems,
     char *dest)
 {
 	dtrace_json_state_t state = DTRACE_JSON_REST;
 	int64_t array_elem = INT64_MIN;
 	int64_t array_pos = 0;
 	uint8_t escape_unicount = 0;
 	boolean_t string_is_key = B_FALSE;
 	boolean_t collect_object = B_FALSE;
 	boolean_t found_key = B_FALSE;
 	boolean_t in_array = B_FALSE;
 	uint32_t braces = 0, brackets = 0;
 	char *elem = elemlist;
 	char *dd = dest;
 	uintptr_t cur;
 
 	for (cur = json; cur < json + size; cur++) {
 		char cc = dtrace_load8(cur);
 		if (cc == '\0')
 			return (NULL);
 
 		switch (state) {
 		case DTRACE_JSON_REST:
 			if (isspace(cc))
 				break;
 
 			if (cc == '{') {
 				state = DTRACE_JSON_OBJECT;
 				break;
 			}
 
 			if (cc == '[') {
 				in_array = B_TRUE;
 				array_pos = 0;
 				array_elem = dtrace_strtoll(elem, 10, size);
 				found_key = array_elem == 0 ? B_TRUE : B_FALSE;
 				state = DTRACE_JSON_VALUE;
 				break;
 			}
 
 			/*
 			 * ERROR: expected to find a top-level object or array.
 			 */
 			return (NULL);
 		case DTRACE_JSON_OBJECT:
 			if (isspace(cc))
 				break;
 
 			if (cc == '"') {
 				state = DTRACE_JSON_STRING;
 				string_is_key = B_TRUE;
 				break;
 			}
 
 			/*
 			 * ERROR: either the object did not start with a key
 			 * string, or we've run off the end of the object
 			 * without finding the requested key.
 			 */
 			return (NULL);
 		case DTRACE_JSON_STRING:
 			if (cc == '\\') {
 				*dd++ = '\\';
 				state = DTRACE_JSON_STRING_ESCAPE;
 				break;
 			}
 
 			if (cc == '"') {
 				if (collect_object) {
 					/*
 					 * We don't reset the dest here, as
 					 * the string is part of a larger
 					 * object being collected.
 					 */
 					*dd++ = cc;
 					collect_object = B_FALSE;
 					state = DTRACE_JSON_COLLECT_OBJECT;
 					break;
 				}
 				*dd = '\0';
 				dd = dest; /* reset string buffer */
 				if (string_is_key) {
 					if (dtrace_strncmp(dest, elem,
 					    size) == 0)
 						found_key = B_TRUE;
 				} else if (found_key) {
 					if (nelems > 1) {
 						/*
 						 * We expected an object, not
 						 * this string.
 						 */
 						return (NULL);
 					}
 					return (dest);
 				}
 				state = string_is_key ? DTRACE_JSON_COLON :
 				    DTRACE_JSON_COMMA;
 				string_is_key = B_FALSE;
 				break;
 			}
 
 			*dd++ = cc;
 			break;
 		case DTRACE_JSON_STRING_ESCAPE:
 			*dd++ = cc;
 			if (cc == 'u') {
 				escape_unicount = 0;
 				state = DTRACE_JSON_STRING_ESCAPE_UNICODE;
 			} else {
 				state = DTRACE_JSON_STRING;
 			}
 			break;
 		case DTRACE_JSON_STRING_ESCAPE_UNICODE:
 			if (!isxdigit(cc)) {
 				/*
 				 * ERROR: invalid unicode escape, expected
 				 * four valid hexidecimal digits.
 				 */
 				return (NULL);
 			}
 
 			*dd++ = cc;
 			if (++escape_unicount == 4)
 				state = DTRACE_JSON_STRING;
 			break;
 		case DTRACE_JSON_COLON:
 			if (isspace(cc))
 				break;
 
 			if (cc == ':') {
 				state = DTRACE_JSON_VALUE;
 				break;
 			}
 
 			/*
 			 * ERROR: expected a colon.
 			 */
 			return (NULL);
 		case DTRACE_JSON_COMMA:
 			if (isspace(cc))
 				break;
 
 			if (cc == ',') {
 				if (in_array) {
 					state = DTRACE_JSON_VALUE;
 					if (++array_pos == array_elem)
 						found_key = B_TRUE;
 				} else {
 					state = DTRACE_JSON_OBJECT;
 				}
 				break;
 			}
 
 			/*
 			 * ERROR: either we hit an unexpected character, or
 			 * we reached the end of the object or array without
 			 * finding the requested key.
 			 */
 			return (NULL);
 		case DTRACE_JSON_IDENTIFIER:
 			if (islower(cc)) {
 				*dd++ = cc;
 				break;
 			}
 
 			*dd = '\0';
 			dd = dest; /* reset string buffer */
 
 			if (dtrace_strncmp(dest, "true", 5) == 0 ||
 			    dtrace_strncmp(dest, "false", 6) == 0 ||
 			    dtrace_strncmp(dest, "null", 5) == 0) {
 				if (found_key) {
 					if (nelems > 1) {
 						/*
 						 * ERROR: We expected an object,
 						 * not this identifier.
 						 */
 						return (NULL);
 					}
 					return (dest);
 				} else {
 					cur--;
 					state = DTRACE_JSON_COMMA;
 					break;
 				}
 			}
 
 			/*
 			 * ERROR: we did not recognise the identifier as one
 			 * of those in the JSON specification.
 			 */
 			return (NULL);
 		case DTRACE_JSON_NUMBER:
 			if (cc == '.') {
 				*dd++ = cc;
 				state = DTRACE_JSON_NUMBER_FRAC;
 				break;
 			}
 
 			if (cc == 'x' || cc == 'X') {
 				/*
 				 * ERROR: specification explicitly excludes
 				 * hexidecimal or octal numbers.
 				 */
 				return (NULL);
 			}
 
 			/* FALLTHRU */
 		case DTRACE_JSON_NUMBER_FRAC:
 			if (cc == 'e' || cc == 'E') {
 				*dd++ = cc;
 				state = DTRACE_JSON_NUMBER_EXP;
 				break;
 			}
 
 			if (cc == '+' || cc == '-') {
 				/*
 				 * ERROR: expect sign as part of exponent only.
 				 */
 				return (NULL);
 			}
 			/* FALLTHRU */
 		case DTRACE_JSON_NUMBER_EXP:
 			if (isdigit(cc) || cc == '+' || cc == '-') {
 				*dd++ = cc;
 				break;
 			}
 
 			*dd = '\0';
 			dd = dest; /* reset string buffer */
 			if (found_key) {
 				if (nelems > 1) {
 					/*
 					 * ERROR: We expected an object, not
 					 * this number.
 					 */
 					return (NULL);
 				}
 				return (dest);
 			}
 
 			cur--;
 			state = DTRACE_JSON_COMMA;
 			break;
 		case DTRACE_JSON_VALUE:
 			if (isspace(cc))
 				break;
 
 			if (cc == '{' || cc == '[') {
 				if (nelems > 1 && found_key) {
 					in_array = cc == '[' ? B_TRUE : B_FALSE;
 					/*
 					 * If our element selector directs us
 					 * to descend into this nested object,
 					 * then move to the next selector
 					 * element in the list and restart the
 					 * state machine.
 					 */
 					while (*elem != '\0')
 						elem++;
 					elem++; /* skip the inter-element NUL */
 					nelems--;
 					dd = dest;
 					if (in_array) {
 						state = DTRACE_JSON_VALUE;
 						array_pos = 0;
 						array_elem = dtrace_strtoll(
 						    elem, 10, size);
 						found_key = array_elem == 0 ?
 						    B_TRUE : B_FALSE;
 					} else {
 						found_key = B_FALSE;
 						state = DTRACE_JSON_OBJECT;
 					}
 					break;
 				}
 
 				/*
 				 * Otherwise, we wish to either skip this
 				 * nested object or return it in full.
 				 */
 				if (cc == '[')
 					brackets = 1;
 				else
 					braces = 1;
 				*dd++ = cc;
 				state = DTRACE_JSON_COLLECT_OBJECT;
 				break;
 			}
 
 			if (cc == '"') {
 				state = DTRACE_JSON_STRING;
 				break;
 			}
 
 			if (islower(cc)) {
 				/*
 				 * Here we deal with true, false and null.
 				 */
 				*dd++ = cc;
 				state = DTRACE_JSON_IDENTIFIER;
 				break;
 			}
 
 			if (cc == '-' || isdigit(cc)) {
 				*dd++ = cc;
 				state = DTRACE_JSON_NUMBER;
 				break;
 			}
 
 			/*
 			 * ERROR: unexpected character at start of value.
 			 */
 			return (NULL);
 		case DTRACE_JSON_COLLECT_OBJECT:
 			if (cc == '\0')
 				/*
 				 * ERROR: unexpected end of input.
 				 */
 				return (NULL);
 
 			*dd++ = cc;
 			if (cc == '"') {
 				collect_object = B_TRUE;
 				state = DTRACE_JSON_STRING;
 				break;
 			}
 
 			if (cc == ']') {
 				if (brackets-- == 0) {
 					/*
 					 * ERROR: unbalanced brackets.
 					 */
 					return (NULL);
 				}
 			} else if (cc == '}') {
 				if (braces-- == 0) {
 					/*
 					 * ERROR: unbalanced braces.
 					 */
 					return (NULL);
 				}
 			} else if (cc == '{') {
 				braces++;
 			} else if (cc == '[') {
 				brackets++;
 			}
 
 			if (brackets == 0 && braces == 0) {
 				if (found_key) {
 					*dd = '\0';
 					return (dest);
 				}
 				dd = dest; /* reset string buffer */
 				state = DTRACE_JSON_COMMA;
 			}
 			break;
 		}
 	}
 	return (NULL);
 }
 
 /*
  * Emulate the execution of DTrace ID subroutines invoked by the call opcode.
  * Notice that we don't bother validating the proper number of arguments or
  * their types in the tuple stack.  This isn't needed because all argument
  * interpretation is safe because of our load safety -- the worst that can
  * happen is that a bogus program can obtain bogus results.
  */
 static void
 dtrace_dif_subr(uint_t subr, uint_t rd, uint64_t *regs,
     dtrace_key_t *tupregs, int nargs,
     dtrace_mstate_t *mstate, dtrace_state_t *state)
 {
 	volatile uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags;
 	volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval;
 	dtrace_vstate_t *vstate = &state->dts_vstate;
 
 #ifdef illumos
 	union {
 		mutex_impl_t mi;
 		uint64_t mx;
 	} m;
 
 	union {
 		krwlock_t ri;
 		uintptr_t rw;
 	} r;
 #else
 	struct thread *lowner;
 	union {
 		struct lock_object *li;
 		uintptr_t lx;
 	} l;
 #endif
 
 	switch (subr) {
 	case DIF_SUBR_RAND:
 		regs[rd] = dtrace_xoroshiro128_plus_next(
 		    state->dts_rstate[curcpu]);
 		break;
 
 #ifdef illumos
 	case DIF_SUBR_MUTEX_OWNED:
 		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t),
 		    mstate, vstate)) {
 			regs[rd] = 0;
 			break;
 		}
 
 		m.mx = dtrace_load64(tupregs[0].dttk_value);
 		if (MUTEX_TYPE_ADAPTIVE(&m.mi))
 			regs[rd] = MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER;
 		else
 			regs[rd] = LOCK_HELD(&m.mi.m_spin.m_spinlock);
 		break;
 
 	case DIF_SUBR_MUTEX_OWNER:
 		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t),
 		    mstate, vstate)) {
 			regs[rd] = 0;
 			break;
 		}
 
 		m.mx = dtrace_load64(tupregs[0].dttk_value);
 		if (MUTEX_TYPE_ADAPTIVE(&m.mi) &&
 		    MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER)
 			regs[rd] = (uintptr_t)MUTEX_OWNER(&m.mi);
 		else
 			regs[rd] = 0;
 		break;
 
 	case DIF_SUBR_MUTEX_TYPE_ADAPTIVE:
 		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t),
 		    mstate, vstate)) {
 			regs[rd] = 0;
 			break;
 		}
 
 		m.mx = dtrace_load64(tupregs[0].dttk_value);
 		regs[rd] = MUTEX_TYPE_ADAPTIVE(&m.mi);
 		break;
 
 	case DIF_SUBR_MUTEX_TYPE_SPIN:
 		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t),
 		    mstate, vstate)) {
 			regs[rd] = 0;
 			break;
 		}
 
 		m.mx = dtrace_load64(tupregs[0].dttk_value);
 		regs[rd] = MUTEX_TYPE_SPIN(&m.mi);
 		break;
 
 	case DIF_SUBR_RW_READ_HELD: {
 		uintptr_t tmp;
 
 		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t),
 		    mstate, vstate)) {
 			regs[rd] = 0;
 			break;
 		}
 
 		r.rw = dtrace_loadptr(tupregs[0].dttk_value);
 		regs[rd] = _RW_READ_HELD(&r.ri, tmp);
 		break;
 	}
 
 	case DIF_SUBR_RW_WRITE_HELD:
 		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t),
 		    mstate, vstate)) {
 			regs[rd] = 0;
 			break;
 		}
 
 		r.rw = dtrace_loadptr(tupregs[0].dttk_value);
 		regs[rd] = _RW_WRITE_HELD(&r.ri);
 		break;
 
 	case DIF_SUBR_RW_ISWRITER:
 		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t),
 		    mstate, vstate)) {
 			regs[rd] = 0;
 			break;
 		}
 
 		r.rw = dtrace_loadptr(tupregs[0].dttk_value);
 		regs[rd] = _RW_ISWRITER(&r.ri);
 		break;
 
 #else /* !illumos */
 	case DIF_SUBR_MUTEX_OWNED:
 		if (!dtrace_canload(tupregs[0].dttk_value,
 			sizeof (struct lock_object), mstate, vstate)) {
 			regs[rd] = 0;
 			break;
 		}
 		l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value);
 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
 		regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner);
 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
 		break;
 
 	case DIF_SUBR_MUTEX_OWNER:
 		if (!dtrace_canload(tupregs[0].dttk_value,
 			sizeof (struct lock_object), mstate, vstate)) {
 			regs[rd] = 0;
 			break;
 		}
 		l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value);
 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
 		LOCK_CLASS(l.li)->lc_owner(l.li, &lowner);
 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
 		regs[rd] = (uintptr_t)lowner;
 		break;
 
 	case DIF_SUBR_MUTEX_TYPE_ADAPTIVE:
 		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (struct mtx),
 		    mstate, vstate)) {
 			regs[rd] = 0;
 			break;
 		}
 		l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value);
 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
 		regs[rd] = (LOCK_CLASS(l.li)->lc_flags & LC_SLEEPLOCK) != 0;
 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
 		break;
 
 	case DIF_SUBR_MUTEX_TYPE_SPIN:
 		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (struct mtx),
 		    mstate, vstate)) {
 			regs[rd] = 0;
 			break;
 		}
 		l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value);
 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
 		regs[rd] = (LOCK_CLASS(l.li)->lc_flags & LC_SPINLOCK) != 0;
 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
 		break;
 
 	case DIF_SUBR_RW_READ_HELD: 
 	case DIF_SUBR_SX_SHARED_HELD: 
 		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t),
 		    mstate, vstate)) {
 			regs[rd] = 0;
 			break;
 		}
 		l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value);
 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
 		regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner) &&
 		    lowner == NULL;
 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
 		break;
 
 	case DIF_SUBR_RW_WRITE_HELD:
 	case DIF_SUBR_SX_EXCLUSIVE_HELD:
 		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t),
 		    mstate, vstate)) {
 			regs[rd] = 0;
 			break;
 		}
 		l.lx = dtrace_loadptr(tupregs[0].dttk_value);
 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
 		regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner) &&
 		    lowner != NULL;
 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
 		break;
 
 	case DIF_SUBR_RW_ISWRITER:
 	case DIF_SUBR_SX_ISEXCLUSIVE:
 		if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t),
 		    mstate, vstate)) {
 			regs[rd] = 0;
 			break;
 		}
 		l.lx = dtrace_loadptr(tupregs[0].dttk_value);
 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
 		LOCK_CLASS(l.li)->lc_owner(l.li, &lowner);
 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
 		regs[rd] = (lowner == curthread);
 		break;
 #endif /* illumos */
 
 	case DIF_SUBR_BCOPY: {
 		/*
 		 * We need to be sure that the destination is in the scratch
 		 * region -- no other region is allowed.
 		 */
 		uintptr_t src = tupregs[0].dttk_value;
 		uintptr_t dest = tupregs[1].dttk_value;
 		size_t size = tupregs[2].dttk_value;
 
 		if (!dtrace_inscratch(dest, size, mstate)) {
 			*flags |= CPU_DTRACE_BADADDR;
 			*illval = regs[rd];
 			break;
 		}
 
 		if (!dtrace_canload(src, size, mstate, vstate)) {
 			regs[rd] = 0;
 			break;
 		}
 
 		dtrace_bcopy((void *)src, (void *)dest, size);
 		break;
 	}
 
 	case DIF_SUBR_ALLOCA:
 	case DIF_SUBR_COPYIN: {
 		uintptr_t dest = P2ROUNDUP(mstate->dtms_scratch_ptr, 8);
 		uint64_t size =
 		    tupregs[subr == DIF_SUBR_ALLOCA ? 0 : 1].dttk_value;
 		size_t scratch_size = (dest - mstate->dtms_scratch_ptr) + size;
 
 		/*
 		 * This action doesn't require any credential checks since
 		 * probes will not activate in user contexts to which the
 		 * enabling user does not have permissions.
 		 */
 
 		/*
 		 * Rounding up the user allocation size could have overflowed
 		 * a large, bogus allocation (like -1ULL) to 0.
 		 */
 		if (scratch_size < size ||
 		    !DTRACE_INSCRATCH(mstate, scratch_size)) {
 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
 			regs[rd] = 0;
 			break;
 		}
 
 		if (subr == DIF_SUBR_COPYIN) {
 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
 			dtrace_copyin(tupregs[0].dttk_value, dest, size, flags);
 			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
 		}
 
 		mstate->dtms_scratch_ptr += scratch_size;
 		regs[rd] = dest;
 		break;
 	}
 
 	case DIF_SUBR_COPYINTO: {
 		uint64_t size = tupregs[1].dttk_value;
 		uintptr_t dest = tupregs[2].dttk_value;
 
 		/*
 		 * This action doesn't require any credential checks since
 		 * probes will not activate in user contexts to which the
 		 * enabling user does not have permissions.
 		 */
 		if (!dtrace_inscratch(dest, size, mstate)) {
 			*flags |= CPU_DTRACE_BADADDR;
 			*illval = regs[rd];
 			break;
 		}
 
 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
 		dtrace_copyin(tupregs[0].dttk_value, dest, size, flags);
 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
 		break;
 	}
 
 	case DIF_SUBR_COPYINSTR: {
 		uintptr_t dest = mstate->dtms_scratch_ptr;
 		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
 
 		if (nargs > 1 && tupregs[1].dttk_value < size)
 			size = tupregs[1].dttk_value + 1;
 
 		/*
 		 * This action doesn't require any credential checks since
 		 * probes will not activate in user contexts to which the
 		 * enabling user does not have permissions.
 		 */
 		if (!DTRACE_INSCRATCH(mstate, size)) {
 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
 			regs[rd] = 0;
 			break;
 		}
 
 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
 		dtrace_copyinstr(tupregs[0].dttk_value, dest, size, flags);
 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
 
 		((char *)dest)[size - 1] = '\0';
 		mstate->dtms_scratch_ptr += size;
 		regs[rd] = dest;
 		break;
 	}
 
 #ifdef illumos
 	case DIF_SUBR_MSGSIZE:
 	case DIF_SUBR_MSGDSIZE: {
 		uintptr_t baddr = tupregs[0].dttk_value, daddr;
 		uintptr_t wptr, rptr;
 		size_t count = 0;
 		int cont = 0;
 
 		while (baddr != 0 && !(*flags & CPU_DTRACE_FAULT)) {
 
 			if (!dtrace_canload(baddr, sizeof (mblk_t), mstate,
 			    vstate)) {
 				regs[rd] = 0;
 				break;
 			}
 
 			wptr = dtrace_loadptr(baddr +
 			    offsetof(mblk_t, b_wptr));
 
 			rptr = dtrace_loadptr(baddr +
 			    offsetof(mblk_t, b_rptr));
 
 			if (wptr < rptr) {
 				*flags |= CPU_DTRACE_BADADDR;
 				*illval = tupregs[0].dttk_value;
 				break;
 			}
 
 			daddr = dtrace_loadptr(baddr +
 			    offsetof(mblk_t, b_datap));
 
 			baddr = dtrace_loadptr(baddr +
 			    offsetof(mblk_t, b_cont));
 
 			/*
 			 * We want to prevent against denial-of-service here,
 			 * so we're only going to search the list for
 			 * dtrace_msgdsize_max mblks.
 			 */
 			if (cont++ > dtrace_msgdsize_max) {
 				*flags |= CPU_DTRACE_ILLOP;
 				break;
 			}
 
 			if (subr == DIF_SUBR_MSGDSIZE) {
 				if (dtrace_load8(daddr +
 				    offsetof(dblk_t, db_type)) != M_DATA)
 					continue;
 			}
 
 			count += wptr - rptr;
 		}
 
 		if (!(*flags & CPU_DTRACE_FAULT))
 			regs[rd] = count;
 
 		break;
 	}
 #endif
 
 	case DIF_SUBR_PROGENYOF: {
 		pid_t pid = tupregs[0].dttk_value;
 		proc_t *p;
 		int rval = 0;
 
 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
 
 		for (p = curthread->t_procp; p != NULL; p = p->p_parent) {
 #ifdef illumos
 			if (p->p_pidp->pid_id == pid) {
 #else
 			if (p->p_pid == pid) {
 #endif
 				rval = 1;
 				break;
 			}
 		}
 
 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
 
 		regs[rd] = rval;
 		break;
 	}
 
 	case DIF_SUBR_SPECULATION:
 		regs[rd] = dtrace_speculation(state);
 		break;
 
 	case DIF_SUBR_COPYOUT: {
 		uintptr_t kaddr = tupregs[0].dttk_value;
 		uintptr_t uaddr = tupregs[1].dttk_value;
 		uint64_t size = tupregs[2].dttk_value;
 
 		if (!dtrace_destructive_disallow &&
 		    dtrace_priv_proc_control(state) &&
 		    !dtrace_istoxic(kaddr, size) &&
 		    dtrace_canload(kaddr, size, mstate, vstate)) {
 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
 			dtrace_copyout(kaddr, uaddr, size, flags);
 			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
 		}
 		break;
 	}
 
 	case DIF_SUBR_COPYOUTSTR: {
 		uintptr_t kaddr = tupregs[0].dttk_value;
 		uintptr_t uaddr = tupregs[1].dttk_value;
 		uint64_t size = tupregs[2].dttk_value;
 		size_t lim;
 
 		if (!dtrace_destructive_disallow &&
 		    dtrace_priv_proc_control(state) &&
 		    !dtrace_istoxic(kaddr, size) &&
 		    dtrace_strcanload(kaddr, size, &lim, mstate, vstate)) {
 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
 			dtrace_copyoutstr(kaddr, uaddr, lim, flags);
 			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
 		}
 		break;
 	}
 
 	case DIF_SUBR_STRLEN: {
 		size_t size = state->dts_options[DTRACEOPT_STRSIZE];
 		uintptr_t addr = (uintptr_t)tupregs[0].dttk_value;
 		size_t lim;
 
 		if (!dtrace_strcanload(addr, size, &lim, mstate, vstate)) {
 			regs[rd] = 0;
 			break;
 		}
 
 		regs[rd] = dtrace_strlen((char *)addr, lim);
 		break;
 	}
 
 	case DIF_SUBR_STRCHR:
 	case DIF_SUBR_STRRCHR: {
 		/*
 		 * We're going to iterate over the string looking for the
 		 * specified character.  We will iterate until we have reached
 		 * the string length or we have found the character.  If this
 		 * is DIF_SUBR_STRRCHR, we will look for the last occurrence
 		 * of the specified character instead of the first.
 		 */
 		uintptr_t addr = tupregs[0].dttk_value;
 		uintptr_t addr_limit;
 		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
 		size_t lim;
 		char c, target = (char)tupregs[1].dttk_value;
 
 		if (!dtrace_strcanload(addr, size, &lim, mstate, vstate)) {
 			regs[rd] = 0;
 			break;
 		}
 		addr_limit = addr + lim;
 
 		for (regs[rd] = 0; addr < addr_limit; addr++) {
 			if ((c = dtrace_load8(addr)) == target) {
 				regs[rd] = addr;
 
 				if (subr == DIF_SUBR_STRCHR)
 					break;
 			}
 
 			if (c == '\0')
 				break;
 		}
 		break;
 	}
 
 	case DIF_SUBR_STRSTR:
 	case DIF_SUBR_INDEX:
 	case DIF_SUBR_RINDEX: {
 		/*
 		 * We're going to iterate over the string looking for the
 		 * specified string.  We will iterate until we have reached
 		 * the string length or we have found the string.  (Yes, this
 		 * is done in the most naive way possible -- but considering
 		 * that the string we're searching for is likely to be
 		 * relatively short, the complexity of Rabin-Karp or similar
 		 * hardly seems merited.)
 		 */
 		char *addr = (char *)(uintptr_t)tupregs[0].dttk_value;
 		char *substr = (char *)(uintptr_t)tupregs[1].dttk_value;
 		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
 		size_t len = dtrace_strlen(addr, size);
 		size_t sublen = dtrace_strlen(substr, size);
 		char *limit = addr + len, *orig = addr;
 		int notfound = subr == DIF_SUBR_STRSTR ? 0 : -1;
 		int inc = 1;
 
 		regs[rd] = notfound;
 
 		if (!dtrace_canload((uintptr_t)addr, len + 1, mstate, vstate)) {
 			regs[rd] = 0;
 			break;
 		}
 
 		if (!dtrace_canload((uintptr_t)substr, sublen + 1, mstate,
 		    vstate)) {
 			regs[rd] = 0;
 			break;
 		}
 
 		/*
 		 * strstr() and index()/rindex() have similar semantics if
 		 * both strings are the empty string: strstr() returns a
 		 * pointer to the (empty) string, and index() and rindex()
 		 * both return index 0 (regardless of any position argument).
 		 */
 		if (sublen == 0 && len == 0) {
 			if (subr == DIF_SUBR_STRSTR)
 				regs[rd] = (uintptr_t)addr;
 			else
 				regs[rd] = 0;
 			break;
 		}
 
 		if (subr != DIF_SUBR_STRSTR) {
 			if (subr == DIF_SUBR_RINDEX) {
 				limit = orig - 1;
 				addr += len;
 				inc = -1;
 			}
 
 			/*
 			 * Both index() and rindex() take an optional position
 			 * argument that denotes the starting position.
 			 */
 			if (nargs == 3) {
 				int64_t pos = (int64_t)tupregs[2].dttk_value;
 
 				/*
 				 * If the position argument to index() is
 				 * negative, Perl implicitly clamps it at
 				 * zero.  This semantic is a little surprising
 				 * given the special meaning of negative
 				 * positions to similar Perl functions like
 				 * substr(), but it appears to reflect a
 				 * notion that index() can start from a
 				 * negative index and increment its way up to
 				 * the string.  Given this notion, Perl's
 				 * rindex() is at least self-consistent in
 				 * that it implicitly clamps positions greater
 				 * than the string length to be the string
 				 * length.  Where Perl completely loses
 				 * coherence, however, is when the specified
 				 * substring is the empty string ("").  In
 				 * this case, even if the position is
 				 * negative, rindex() returns 0 -- and even if
 				 * the position is greater than the length,
 				 * index() returns the string length.  These
 				 * semantics violate the notion that index()
 				 * should never return a value less than the
 				 * specified position and that rindex() should
 				 * never return a value greater than the
 				 * specified position.  (One assumes that
 				 * these semantics are artifacts of Perl's
 				 * implementation and not the results of
 				 * deliberate design -- it beggars belief that
 				 * even Larry Wall could desire such oddness.)
 				 * While in the abstract one would wish for
 				 * consistent position semantics across
 				 * substr(), index() and rindex() -- or at the
 				 * very least self-consistent position
 				 * semantics for index() and rindex() -- we
 				 * instead opt to keep with the extant Perl
 				 * semantics, in all their broken glory.  (Do
 				 * we have more desire to maintain Perl's
 				 * semantics than Perl does?  Probably.)
 				 */
 				if (subr == DIF_SUBR_RINDEX) {
 					if (pos < 0) {
 						if (sublen == 0)
 							regs[rd] = 0;
 						break;
 					}
 
 					if (pos > len)
 						pos = len;
 				} else {
 					if (pos < 0)
 						pos = 0;
 
 					if (pos >= len) {
 						if (sublen == 0)
 							regs[rd] = len;
 						break;
 					}
 				}
 
 				addr = orig + pos;
 			}
 		}
 
 		for (regs[rd] = notfound; addr != limit; addr += inc) {
 			if (dtrace_strncmp(addr, substr, sublen) == 0) {
 				if (subr != DIF_SUBR_STRSTR) {
 					/*
 					 * As D index() and rindex() are
 					 * modeled on Perl (and not on awk),
 					 * we return a zero-based (and not a
 					 * one-based) index.  (For you Perl
 					 * weenies: no, we're not going to add
 					 * $[ -- and shouldn't you be at a con
 					 * or something?)
 					 */
 					regs[rd] = (uintptr_t)(addr - orig);
 					break;
 				}
 
 				ASSERT(subr == DIF_SUBR_STRSTR);
 				regs[rd] = (uintptr_t)addr;
 				break;
 			}
 		}
 
 		break;
 	}
 
 	case DIF_SUBR_STRTOK: {
 		uintptr_t addr = tupregs[0].dttk_value;
 		uintptr_t tokaddr = tupregs[1].dttk_value;
 		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
 		uintptr_t limit, toklimit;
 		size_t clim;
 		uint8_t c = 0, tokmap[32];	 /* 256 / 8 */
 		char *dest = (char *)mstate->dtms_scratch_ptr;
 		int i;
 
 		/*
 		 * Check both the token buffer and (later) the input buffer,
 		 * since both could be non-scratch addresses.
 		 */
 		if (!dtrace_strcanload(tokaddr, size, &clim, mstate, vstate)) {
 			regs[rd] = 0;
 			break;
 		}
 		toklimit = tokaddr + clim;
 
 		if (!DTRACE_INSCRATCH(mstate, size)) {
 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
 			regs[rd] = 0;
 			break;
 		}
 
 		if (addr == 0) {
 			/*
 			 * If the address specified is NULL, we use our saved
 			 * strtok pointer from the mstate.  Note that this
 			 * means that the saved strtok pointer is _only_
 			 * valid within multiple enablings of the same probe --
 			 * it behaves like an implicit clause-local variable.
 			 */
 			addr = mstate->dtms_strtok;
 			limit = mstate->dtms_strtok_limit;
 		} else {
 			/*
 			 * If the user-specified address is non-NULL we must
 			 * access check it.  This is the only time we have
 			 * a chance to do so, since this address may reside
 			 * in the string table of this clause-- future calls
 			 * (when we fetch addr from mstate->dtms_strtok)
 			 * would fail this access check.
 			 */
 			if (!dtrace_strcanload(addr, size, &clim, mstate,
 			    vstate)) {
 				regs[rd] = 0;
 				break;
 			}
 			limit = addr + clim;
 		}
 
 		/*
 		 * First, zero the token map, and then process the token
 		 * string -- setting a bit in the map for every character
 		 * found in the token string.
 		 */
 		for (i = 0; i < sizeof (tokmap); i++)
 			tokmap[i] = 0;
 
 		for (; tokaddr < toklimit; tokaddr++) {
 			if ((c = dtrace_load8(tokaddr)) == '\0')
 				break;
 
 			ASSERT((c >> 3) < sizeof (tokmap));
 			tokmap[c >> 3] |= (1 << (c & 0x7));
 		}
 
 		for (; addr < limit; addr++) {
 			/*
 			 * We're looking for a character that is _not_
 			 * contained in the token string.
 			 */
 			if ((c = dtrace_load8(addr)) == '\0')
 				break;
 
 			if (!(tokmap[c >> 3] & (1 << (c & 0x7))))
 				break;
 		}
 
 		if (c == '\0') {
 			/*
 			 * We reached the end of the string without finding
 			 * any character that was not in the token string.
 			 * We return NULL in this case, and we set the saved
 			 * address to NULL as well.
 			 */
 			regs[rd] = 0;
 			mstate->dtms_strtok = 0;
 			mstate->dtms_strtok_limit = 0;
 			break;
 		}
 
 		/*
 		 * From here on, we're copying into the destination string.
 		 */
 		for (i = 0; addr < limit && i < size - 1; addr++) {
 			if ((c = dtrace_load8(addr)) == '\0')
 				break;
 
 			if (tokmap[c >> 3] & (1 << (c & 0x7)))
 				break;
 
 			ASSERT(i < size);
 			dest[i++] = c;
 		}
 
 		ASSERT(i < size);
 		dest[i] = '\0';
 		regs[rd] = (uintptr_t)dest;
 		mstate->dtms_scratch_ptr += size;
 		mstate->dtms_strtok = addr;
 		mstate->dtms_strtok_limit = limit;
 		break;
 	}
 
 	case DIF_SUBR_SUBSTR: {
 		uintptr_t s = tupregs[0].dttk_value;
 		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
 		char *d = (char *)mstate->dtms_scratch_ptr;
 		int64_t index = (int64_t)tupregs[1].dttk_value;
 		int64_t remaining = (int64_t)tupregs[2].dttk_value;
 		size_t len = dtrace_strlen((char *)s, size);
 		int64_t i;
 
 		if (!dtrace_canload(s, len + 1, mstate, vstate)) {
 			regs[rd] = 0;
 			break;
 		}
 
 		if (!DTRACE_INSCRATCH(mstate, size)) {
 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
 			regs[rd] = 0;
 			break;
 		}
 
 		if (nargs <= 2)
 			remaining = (int64_t)size;
 
 		if (index < 0) {
 			index += len;
 
 			if (index < 0 && index + remaining > 0) {
 				remaining += index;
 				index = 0;
 			}
 		}
 
 		if (index >= len || index < 0) {
 			remaining = 0;
 		} else if (remaining < 0) {
 			remaining += len - index;
 		} else if (index + remaining > size) {
 			remaining = size - index;
 		}
 
 		for (i = 0; i < remaining; i++) {
 			if ((d[i] = dtrace_load8(s + index + i)) == '\0')
 				break;
 		}
 
 		d[i] = '\0';
 
 		mstate->dtms_scratch_ptr += size;
 		regs[rd] = (uintptr_t)d;
 		break;
 	}
 
 	case DIF_SUBR_JSON: {
 		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
 		uintptr_t json = tupregs[0].dttk_value;
 		size_t jsonlen = dtrace_strlen((char *)json, size);
 		uintptr_t elem = tupregs[1].dttk_value;
 		size_t elemlen = dtrace_strlen((char *)elem, size);
 
 		char *dest = (char *)mstate->dtms_scratch_ptr;
 		char *elemlist = (char *)mstate->dtms_scratch_ptr + jsonlen + 1;
 		char *ee = elemlist;
 		int nelems = 1;
 		uintptr_t cur;
 
 		if (!dtrace_canload(json, jsonlen + 1, mstate, vstate) ||
 		    !dtrace_canload(elem, elemlen + 1, mstate, vstate)) {
 			regs[rd] = 0;
 			break;
 		}
 
 		if (!DTRACE_INSCRATCH(mstate, jsonlen + 1 + elemlen + 1)) {
 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
 			regs[rd] = 0;
 			break;
 		}
 
 		/*
 		 * Read the element selector and split it up into a packed list
 		 * of strings.
 		 */
 		for (cur = elem; cur < elem + elemlen; cur++) {
 			char cc = dtrace_load8(cur);
 
 			if (cur == elem && cc == '[') {
 				/*
 				 * If the first element selector key is
 				 * actually an array index then ignore the
 				 * bracket.
 				 */
 				continue;
 			}
 
 			if (cc == ']')
 				continue;
 
 			if (cc == '.' || cc == '[') {
 				nelems++;
 				cc = '\0';
 			}
 
 			*ee++ = cc;
 		}
 		*ee++ = '\0';
 
 		if ((regs[rd] = (uintptr_t)dtrace_json(size, json, elemlist,
 		    nelems, dest)) != 0)
 			mstate->dtms_scratch_ptr += jsonlen + 1;
 		break;
 	}
 
 	case DIF_SUBR_TOUPPER:
 	case DIF_SUBR_TOLOWER: {
 		uintptr_t s = tupregs[0].dttk_value;
 		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
 		char *dest = (char *)mstate->dtms_scratch_ptr, c;
 		size_t len = dtrace_strlen((char *)s, size);
 		char lower, upper, convert;
 		int64_t i;
 
 		if (subr == DIF_SUBR_TOUPPER) {
 			lower = 'a';
 			upper = 'z';
 			convert = 'A';
 		} else {
 			lower = 'A';
 			upper = 'Z';
 			convert = 'a';
 		}
 
 		if (!dtrace_canload(s, len + 1, mstate, vstate)) {
 			regs[rd] = 0;
 			break;
 		}
 
 		if (!DTRACE_INSCRATCH(mstate, size)) {
 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
 			regs[rd] = 0;
 			break;
 		}
 
 		for (i = 0; i < size - 1; i++) {
 			if ((c = dtrace_load8(s + i)) == '\0')
 				break;
 
 			if (c >= lower && c <= upper)
 				c = convert + (c - lower);
 
 			dest[i] = c;
 		}
 
 		ASSERT(i < size);
 		dest[i] = '\0';
 		regs[rd] = (uintptr_t)dest;
 		mstate->dtms_scratch_ptr += size;
 		break;
 	}
 
 #ifdef illumos
 	case DIF_SUBR_GETMAJOR:
 #ifdef _LP64
 		regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR64) & MAXMAJ64;
 #else
 		regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR) & MAXMAJ;
 #endif
 		break;
 
 	case DIF_SUBR_GETMINOR:
 #ifdef _LP64
 		regs[rd] = tupregs[0].dttk_value & MAXMIN64;
 #else
 		regs[rd] = tupregs[0].dttk_value & MAXMIN;
 #endif
 		break;
 
 	case DIF_SUBR_DDI_PATHNAME: {
 		/*
 		 * This one is a galactic mess.  We are going to roughly
 		 * emulate ddi_pathname(), but it's made more complicated
 		 * by the fact that we (a) want to include the minor name and
 		 * (b) must proceed iteratively instead of recursively.
 		 */
 		uintptr_t dest = mstate->dtms_scratch_ptr;
 		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
 		char *start = (char *)dest, *end = start + size - 1;
 		uintptr_t daddr = tupregs[0].dttk_value;
 		int64_t minor = (int64_t)tupregs[1].dttk_value;
 		char *s;
 		int i, len, depth = 0;
 
 		/*
 		 * Due to all the pointer jumping we do and context we must
 		 * rely upon, we just mandate that the user must have kernel
 		 * read privileges to use this routine.
 		 */
 		if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) == 0) {
 			*flags |= CPU_DTRACE_KPRIV;
 			*illval = daddr;
 			regs[rd] = 0;
 		}
 
 		if (!DTRACE_INSCRATCH(mstate, size)) {
 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
 			regs[rd] = 0;
 			break;
 		}
 
 		*end = '\0';
 
 		/*
 		 * We want to have a name for the minor.  In order to do this,
 		 * we need to walk the minor list from the devinfo.  We want
 		 * to be sure that we don't infinitely walk a circular list,
 		 * so we check for circularity by sending a scout pointer
 		 * ahead two elements for every element that we iterate over;
 		 * if the list is circular, these will ultimately point to the
 		 * same element.  You may recognize this little trick as the
 		 * answer to a stupid interview question -- one that always
 		 * seems to be asked by those who had to have it laboriously
 		 * explained to them, and who can't even concisely describe
 		 * the conditions under which one would be forced to resort to
 		 * this technique.  Needless to say, those conditions are
 		 * found here -- and probably only here.  Is this the only use
 		 * of this infamous trick in shipping, production code?  If it
 		 * isn't, it probably should be...
 		 */
 		if (minor != -1) {
 			uintptr_t maddr = dtrace_loadptr(daddr +
 			    offsetof(struct dev_info, devi_minor));
 
 			uintptr_t next = offsetof(struct ddi_minor_data, next);
 			uintptr_t name = offsetof(struct ddi_minor_data,
 			    d_minor) + offsetof(struct ddi_minor, name);
 			uintptr_t dev = offsetof(struct ddi_minor_data,
 			    d_minor) + offsetof(struct ddi_minor, dev);
 			uintptr_t scout;
 
 			if (maddr != NULL)
 				scout = dtrace_loadptr(maddr + next);
 
 			while (maddr != NULL && !(*flags & CPU_DTRACE_FAULT)) {
 				uint64_t m;
 #ifdef _LP64
 				m = dtrace_load64(maddr + dev) & MAXMIN64;
 #else
 				m = dtrace_load32(maddr + dev) & MAXMIN;
 #endif
 				if (m != minor) {
 					maddr = dtrace_loadptr(maddr + next);
 
 					if (scout == NULL)
 						continue;
 
 					scout = dtrace_loadptr(scout + next);
 
 					if (scout == NULL)
 						continue;
 
 					scout = dtrace_loadptr(scout + next);
 
 					if (scout == NULL)
 						continue;
 
 					if (scout == maddr) {
 						*flags |= CPU_DTRACE_ILLOP;
 						break;
 					}
 
 					continue;
 				}
 
 				/*
 				 * We have the minor data.  Now we need to
 				 * copy the minor's name into the end of the
 				 * pathname.
 				 */
 				s = (char *)dtrace_loadptr(maddr + name);
 				len = dtrace_strlen(s, size);
 
 				if (*flags & CPU_DTRACE_FAULT)
 					break;
 
 				if (len != 0) {
 					if ((end -= (len + 1)) < start)
 						break;
 
 					*end = ':';
 				}
 
 				for (i = 1; i <= len; i++)
 					end[i] = dtrace_load8((uintptr_t)s++);
 				break;
 			}
 		}
 
 		while (daddr != NULL && !(*flags & CPU_DTRACE_FAULT)) {
 			ddi_node_state_t devi_state;
 
 			devi_state = dtrace_load32(daddr +
 			    offsetof(struct dev_info, devi_node_state));
 
 			if (*flags & CPU_DTRACE_FAULT)
 				break;
 
 			if (devi_state >= DS_INITIALIZED) {
 				s = (char *)dtrace_loadptr(daddr +
 				    offsetof(struct dev_info, devi_addr));
 				len = dtrace_strlen(s, size);
 
 				if (*flags & CPU_DTRACE_FAULT)
 					break;
 
 				if (len != 0) {
 					if ((end -= (len + 1)) < start)
 						break;
 
 					*end = '@';
 				}
 
 				for (i = 1; i <= len; i++)
 					end[i] = dtrace_load8((uintptr_t)s++);
 			}
 
 			/*
 			 * Now for the node name...
 			 */
 			s = (char *)dtrace_loadptr(daddr +
 			    offsetof(struct dev_info, devi_node_name));
 
 			daddr = dtrace_loadptr(daddr +
 			    offsetof(struct dev_info, devi_parent));
 
 			/*
 			 * If our parent is NULL (that is, if we're the root
 			 * node), we're going to use the special path
 			 * "devices".
 			 */
 			if (daddr == 0)
 				s = "devices";
 
 			len = dtrace_strlen(s, size);
 			if (*flags & CPU_DTRACE_FAULT)
 				break;
 
 			if ((end -= (len + 1)) < start)
 				break;
 
 			for (i = 1; i <= len; i++)
 				end[i] = dtrace_load8((uintptr_t)s++);
 			*end = '/';
 
 			if (depth++ > dtrace_devdepth_max) {
 				*flags |= CPU_DTRACE_ILLOP;
 				break;
 			}
 		}
 
 		if (end < start)
 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
 
 		if (daddr == 0) {
 			regs[rd] = (uintptr_t)end;
 			mstate->dtms_scratch_ptr += size;
 		}
 
 		break;
 	}
 #endif
 
 	case DIF_SUBR_STRJOIN: {
 		char *d = (char *)mstate->dtms_scratch_ptr;
 		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
 		uintptr_t s1 = tupregs[0].dttk_value;
 		uintptr_t s2 = tupregs[1].dttk_value;
 		int i = 0, j = 0;
 		size_t lim1, lim2;
 		char c;
 
 		if (!dtrace_strcanload(s1, size, &lim1, mstate, vstate) ||
 		    !dtrace_strcanload(s2, size, &lim2, mstate, vstate)) {
 			regs[rd] = 0;
 			break;
 		}
 
 		if (!DTRACE_INSCRATCH(mstate, size)) {
 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
 			regs[rd] = 0;
 			break;
 		}
 
 		for (;;) {
 			if (i >= size) {
 				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
 				regs[rd] = 0;
 				break;
 			}
 			c = (i >= lim1) ? '\0' : dtrace_load8(s1++);
 			if ((d[i++] = c) == '\0') {
 				i--;
 				break;
 			}
 		}
 
 		for (;;) {
 			if (i >= size) {
 				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
 				regs[rd] = 0;
 				break;
 			}
 
 			c = (j++ >= lim2) ? '\0' : dtrace_load8(s2++);
 			if ((d[i++] = c) == '\0')
 				break;
 		}
 
 		if (i < size) {
 			mstate->dtms_scratch_ptr += i;
 			regs[rd] = (uintptr_t)d;
 		}
 
 		break;
 	}
 
 	case DIF_SUBR_STRTOLL: {
 		uintptr_t s = tupregs[0].dttk_value;
 		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
 		size_t lim;
 		int base = 10;
 
 		if (nargs > 1) {
 			if ((base = tupregs[1].dttk_value) <= 1 ||
 			    base > ('z' - 'a' + 1) + ('9' - '0' + 1)) {
 				*flags |= CPU_DTRACE_ILLOP;
 				break;
 			}
 		}
 
 		if (!dtrace_strcanload(s, size, &lim, mstate, vstate)) {
 			regs[rd] = INT64_MIN;
 			break;
 		}
 
 		regs[rd] = dtrace_strtoll((char *)s, base, lim);
 		break;
 	}
 
 	case DIF_SUBR_LLTOSTR: {
 		int64_t i = (int64_t)tupregs[0].dttk_value;
 		uint64_t val, digit;
 		uint64_t size = 65;	/* enough room for 2^64 in binary */
 		char *end = (char *)mstate->dtms_scratch_ptr + size - 1;
 		int base = 10;
 
 		if (nargs > 1) {
 			if ((base = tupregs[1].dttk_value) <= 1 ||
 			    base > ('z' - 'a' + 1) + ('9' - '0' + 1)) {
 				*flags |= CPU_DTRACE_ILLOP;
 				break;
 			}
 		}
 
 		val = (base == 10 && i < 0) ? i * -1 : i;
 
 		if (!DTRACE_INSCRATCH(mstate, size)) {
 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
 			regs[rd] = 0;
 			break;
 		}
 
 		for (*end-- = '\0'; val; val /= base) {
 			if ((digit = val % base) <= '9' - '0') {
 				*end-- = '0' + digit;
 			} else {
 				*end-- = 'a' + (digit - ('9' - '0') - 1);
 			}
 		}
 
 		if (i == 0 && base == 16)
 			*end-- = '0';
 
 		if (base == 16)
 			*end-- = 'x';
 
 		if (i == 0 || base == 8 || base == 16)
 			*end-- = '0';
 
 		if (i < 0 && base == 10)
 			*end-- = '-';
 
 		regs[rd] = (uintptr_t)end + 1;
 		mstate->dtms_scratch_ptr += size;
 		break;
 	}
 
 	case DIF_SUBR_HTONS:
 	case DIF_SUBR_NTOHS:
 #if BYTE_ORDER == BIG_ENDIAN
 		regs[rd] = (uint16_t)tupregs[0].dttk_value;
 #else
 		regs[rd] = DT_BSWAP_16((uint16_t)tupregs[0].dttk_value);
 #endif
 		break;
 
 
 	case DIF_SUBR_HTONL:
 	case DIF_SUBR_NTOHL:
 #if BYTE_ORDER == BIG_ENDIAN
 		regs[rd] = (uint32_t)tupregs[0].dttk_value;
 #else
 		regs[rd] = DT_BSWAP_32((uint32_t)tupregs[0].dttk_value);
 #endif
 		break;
 
 
 	case DIF_SUBR_HTONLL:
 	case DIF_SUBR_NTOHLL:
 #if BYTE_ORDER == BIG_ENDIAN
 		regs[rd] = (uint64_t)tupregs[0].dttk_value;
 #else
 		regs[rd] = DT_BSWAP_64((uint64_t)tupregs[0].dttk_value);
 #endif
 		break;
 
 
 	case DIF_SUBR_DIRNAME:
 	case DIF_SUBR_BASENAME: {
 		char *dest = (char *)mstate->dtms_scratch_ptr;
 		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
 		uintptr_t src = tupregs[0].dttk_value;
 		int i, j, len = dtrace_strlen((char *)src, size);
 		int lastbase = -1, firstbase = -1, lastdir = -1;
 		int start, end;
 
 		if (!dtrace_canload(src, len + 1, mstate, vstate)) {
 			regs[rd] = 0;
 			break;
 		}
 
 		if (!DTRACE_INSCRATCH(mstate, size)) {
 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
 			regs[rd] = 0;
 			break;
 		}
 
 		/*
 		 * The basename and dirname for a zero-length string is
 		 * defined to be "."
 		 */
 		if (len == 0) {
 			len = 1;
 			src = (uintptr_t)".";
 		}
 
 		/*
 		 * Start from the back of the string, moving back toward the
 		 * front until we see a character that isn't a slash.  That
 		 * character is the last character in the basename.
 		 */
 		for (i = len - 1; i >= 0; i--) {
 			if (dtrace_load8(src + i) != '/')
 				break;
 		}
 
 		if (i >= 0)
 			lastbase = i;
 
 		/*
 		 * Starting from the last character in the basename, move
 		 * towards the front until we find a slash.  The character
 		 * that we processed immediately before that is the first
 		 * character in the basename.
 		 */
 		for (; i >= 0; i--) {
 			if (dtrace_load8(src + i) == '/')
 				break;
 		}
 
 		if (i >= 0)
 			firstbase = i + 1;
 
 		/*
 		 * Now keep going until we find a non-slash character.  That
 		 * character is the last character in the dirname.
 		 */
 		for (; i >= 0; i--) {
 			if (dtrace_load8(src + i) != '/')
 				break;
 		}
 
 		if (i >= 0)
 			lastdir = i;
 
 		ASSERT(!(lastbase == -1 && firstbase != -1));
 		ASSERT(!(firstbase == -1 && lastdir != -1));
 
 		if (lastbase == -1) {
 			/*
 			 * We didn't find a non-slash character.  We know that
 			 * the length is non-zero, so the whole string must be
 			 * slashes.  In either the dirname or the basename
 			 * case, we return '/'.
 			 */
 			ASSERT(firstbase == -1);
 			firstbase = lastbase = lastdir = 0;
 		}
 
 		if (firstbase == -1) {
 			/*
 			 * The entire string consists only of a basename
 			 * component.  If we're looking for dirname, we need
 			 * to change our string to be just "."; if we're
 			 * looking for a basename, we'll just set the first
 			 * character of the basename to be 0.
 			 */
 			if (subr == DIF_SUBR_DIRNAME) {
 				ASSERT(lastdir == -1);
 				src = (uintptr_t)".";
 				lastdir = 0;
 			} else {
 				firstbase = 0;
 			}
 		}
 
 		if (subr == DIF_SUBR_DIRNAME) {
 			if (lastdir == -1) {
 				/*
 				 * We know that we have a slash in the name --
 				 * or lastdir would be set to 0, above.  And
 				 * because lastdir is -1, we know that this
 				 * slash must be the first character.  (That
 				 * is, the full string must be of the form
 				 * "/basename".)  In this case, the last
 				 * character of the directory name is 0.
 				 */
 				lastdir = 0;
 			}
 
 			start = 0;
 			end = lastdir;
 		} else {
 			ASSERT(subr == DIF_SUBR_BASENAME);
 			ASSERT(firstbase != -1 && lastbase != -1);
 			start = firstbase;
 			end = lastbase;
 		}
 
 		for (i = start, j = 0; i <= end && j < size - 1; i++, j++)
 			dest[j] = dtrace_load8(src + i);
 
 		dest[j] = '\0';
 		regs[rd] = (uintptr_t)dest;
 		mstate->dtms_scratch_ptr += size;
 		break;
 	}
 
 	case DIF_SUBR_GETF: {
 		uintptr_t fd = tupregs[0].dttk_value;
 		struct filedesc *fdp;
 		file_t *fp;
 
 		if (!dtrace_priv_proc(state)) {
 			regs[rd] = 0;
 			break;
 		}
 		fdp = curproc->p_fd;
 		FILEDESC_SLOCK(fdp);
 		/*
 		 * XXXMJG this looks broken as no ref is taken.
 		 */
 		fp = fget_noref(fdp, fd);
 		mstate->dtms_getf = fp;
 		regs[rd] = (uintptr_t)fp;
 		FILEDESC_SUNLOCK(fdp);
 		break;
 	}
 
 	case DIF_SUBR_CLEANPATH: {
 		char *dest = (char *)mstate->dtms_scratch_ptr, c;
 		uint64_t size = state->dts_options[DTRACEOPT_STRSIZE];
 		uintptr_t src = tupregs[0].dttk_value;
 		size_t lim;
 		int i = 0, j = 0;
 #ifdef illumos
 		zone_t *z;
 #endif
 
 		if (!dtrace_strcanload(src, size, &lim, mstate, vstate)) {
 			regs[rd] = 0;
 			break;
 		}
 
 		if (!DTRACE_INSCRATCH(mstate, size)) {
 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
 			regs[rd] = 0;
 			break;
 		}
 
 		/*
 		 * Move forward, loading each character.
 		 */
 		do {
 			c = (i >= lim) ? '\0' : dtrace_load8(src + i++);
 next:
 			if (j + 5 >= size)	/* 5 = strlen("/..c\0") */
 				break;
 
 			if (c != '/') {
 				dest[j++] = c;
 				continue;
 			}
 
 			c = (i >= lim) ? '\0' : dtrace_load8(src + i++);
 
 			if (c == '/') {
 				/*
 				 * We have two slashes -- we can just advance
 				 * to the next character.
 				 */
 				goto next;
 			}
 
 			if (c != '.') {
 				/*
 				 * This is not "." and it's not ".." -- we can
 				 * just store the "/" and this character and
 				 * drive on.
 				 */
 				dest[j++] = '/';
 				dest[j++] = c;
 				continue;
 			}
 
 			c = (i >= lim) ? '\0' : dtrace_load8(src + i++);
 
 			if (c == '/') {
 				/*
 				 * This is a "/./" component.  We're not going
 				 * to store anything in the destination buffer;
 				 * we're just going to go to the next component.
 				 */
 				goto next;
 			}
 
 			if (c != '.') {
 				/*
 				 * This is not ".." -- we can just store the
 				 * "/." and this character and continue
 				 * processing.
 				 */
 				dest[j++] = '/';
 				dest[j++] = '.';
 				dest[j++] = c;
 				continue;
 			}
 
 			c = (i >= lim) ? '\0' : dtrace_load8(src + i++);
 
 			if (c != '/' && c != '\0') {
 				/*
 				 * This is not ".." -- it's "..[mumble]".
 				 * We'll store the "/.." and this character
 				 * and continue processing.
 				 */
 				dest[j++] = '/';
 				dest[j++] = '.';
 				dest[j++] = '.';
 				dest[j++] = c;
 				continue;
 			}
 
 			/*
 			 * This is "/../" or "/..\0".  We need to back up
 			 * our destination pointer until we find a "/".
 			 */
 			i--;
 			while (j != 0 && dest[--j] != '/')
 				continue;
 
 			if (c == '\0')
 				dest[++j] = '/';
 		} while (c != '\0');
 
 		dest[j] = '\0';
 
 #ifdef illumos
 		if (mstate->dtms_getf != NULL &&
 		    !(mstate->dtms_access & DTRACE_ACCESS_KERNEL) &&
 		    (z = state->dts_cred.dcr_cred->cr_zone) != kcred->cr_zone) {
 			/*
 			 * If we've done a getf() as a part of this ECB and we
 			 * don't have kernel access (and we're not in the global
 			 * zone), check if the path we cleaned up begins with
 			 * the zone's root path, and trim it off if so.  Note
 			 * that this is an output cleanliness issue, not a
 			 * security issue: knowing one's zone root path does
 			 * not enable privilege escalation.
 			 */
 			if (strstr(dest, z->zone_rootpath) == dest)
 				dest += strlen(z->zone_rootpath) - 1;
 		}
 #endif
 
 		regs[rd] = (uintptr_t)dest;
 		mstate->dtms_scratch_ptr += size;
 		break;
 	}
 
 	case DIF_SUBR_INET_NTOA:
 	case DIF_SUBR_INET_NTOA6:
 	case DIF_SUBR_INET_NTOP: {
 		size_t size;
 		int af, argi, i;
 		char *base, *end;
 
 		if (subr == DIF_SUBR_INET_NTOP) {
 			af = (int)tupregs[0].dttk_value;
 			argi = 1;
 		} else {
 			af = subr == DIF_SUBR_INET_NTOA ? AF_INET: AF_INET6;
 			argi = 0;
 		}
 
 		if (af == AF_INET) {
 			ipaddr_t ip4;
 			uint8_t *ptr8, val;
 
 			if (!dtrace_canload(tupregs[argi].dttk_value,
 			    sizeof (ipaddr_t), mstate, vstate)) {
 				regs[rd] = 0;
 				break;
 			}
 
 			/*
 			 * Safely load the IPv4 address.
 			 */
 			ip4 = dtrace_load32(tupregs[argi].dttk_value);
 
 			/*
 			 * Check an IPv4 string will fit in scratch.
 			 */
 			size = INET_ADDRSTRLEN;
 			if (!DTRACE_INSCRATCH(mstate, size)) {
 				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
 				regs[rd] = 0;
 				break;
 			}
 			base = (char *)mstate->dtms_scratch_ptr;
 			end = (char *)mstate->dtms_scratch_ptr + size - 1;
 
 			/*
 			 * Stringify as a dotted decimal quad.
 			 */
 			*end-- = '\0';
 			ptr8 = (uint8_t *)&ip4;
 			for (i = 3; i >= 0; i--) {
 				val = ptr8[i];
 
 				if (val == 0) {
 					*end-- = '0';
 				} else {
 					for (; val; val /= 10) {
 						*end-- = '0' + (val % 10);
 					}
 				}
 
 				if (i > 0)
 					*end-- = '.';
 			}
 			ASSERT(end + 1 >= base);
 
 		} else if (af == AF_INET6) {
 			struct in6_addr ip6;
 			int firstzero, tryzero, numzero, v6end;
 			uint16_t val;
 			const char digits[] = "0123456789abcdef";
 
 			/*
 			 * Stringify using RFC 1884 convention 2 - 16 bit
 			 * hexadecimal values with a zero-run compression.
 			 * Lower case hexadecimal digits are used.
 			 * 	eg, fe80::214:4fff:fe0b:76c8.
 			 * The IPv4 embedded form is returned for inet_ntop,
 			 * just the IPv4 string is returned for inet_ntoa6.
 			 */
 
 			if (!dtrace_canload(tupregs[argi].dttk_value,
 			    sizeof (struct in6_addr), mstate, vstate)) {
 				regs[rd] = 0;
 				break;
 			}
 
 			/*
 			 * Safely load the IPv6 address.
 			 */
 			dtrace_bcopy(
 			    (void *)(uintptr_t)tupregs[argi].dttk_value,
 			    (void *)(uintptr_t)&ip6, sizeof (struct in6_addr));
 
 			/*
 			 * Check an IPv6 string will fit in scratch.
 			 */
 			size = INET6_ADDRSTRLEN;
 			if (!DTRACE_INSCRATCH(mstate, size)) {
 				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
 				regs[rd] = 0;
 				break;
 			}
 			base = (char *)mstate->dtms_scratch_ptr;
 			end = (char *)mstate->dtms_scratch_ptr + size - 1;
 			*end-- = '\0';
 
 			/*
 			 * Find the longest run of 16 bit zero values
 			 * for the single allowed zero compression - "::".
 			 */
 			firstzero = -1;
 			tryzero = -1;
 			numzero = 1;
 			for (i = 0; i < sizeof (struct in6_addr); i++) {
 #ifdef illumos
 				if (ip6._S6_un._S6_u8[i] == 0 &&
 #else
 				if (ip6.__u6_addr.__u6_addr8[i] == 0 &&
 #endif
 				    tryzero == -1 && i % 2 == 0) {
 					tryzero = i;
 					continue;
 				}
 
 				if (tryzero != -1 &&
 #ifdef illumos
 				    (ip6._S6_un._S6_u8[i] != 0 ||
 #else
 				    (ip6.__u6_addr.__u6_addr8[i] != 0 ||
 #endif
 				    i == sizeof (struct in6_addr) - 1)) {
 
 					if (i - tryzero <= numzero) {
 						tryzero = -1;
 						continue;
 					}
 
 					firstzero = tryzero;
 					numzero = i - i % 2 - tryzero;
 					tryzero = -1;
 
 #ifdef illumos
 					if (ip6._S6_un._S6_u8[i] == 0 &&
 #else
 					if (ip6.__u6_addr.__u6_addr8[i] == 0 &&
 #endif
 					    i == sizeof (struct in6_addr) - 1)
 						numzero += 2;
 				}
 			}
 			ASSERT(firstzero + numzero <= sizeof (struct in6_addr));
 
 			/*
 			 * Check for an IPv4 embedded address.
 			 */
 			v6end = sizeof (struct in6_addr) - 2;
 			if (IN6_IS_ADDR_V4MAPPED(&ip6) ||
 			    IN6_IS_ADDR_V4COMPAT(&ip6)) {
 				for (i = sizeof (struct in6_addr) - 1;
 				    i >= DTRACE_V4MAPPED_OFFSET; i--) {
 					ASSERT(end >= base);
 
 #ifdef illumos
 					val = ip6._S6_un._S6_u8[i];
 #else
 					val = ip6.__u6_addr.__u6_addr8[i];
 #endif
 
 					if (val == 0) {
 						*end-- = '0';
 					} else {
 						for (; val; val /= 10) {
 							*end-- = '0' + val % 10;
 						}
 					}
 
 					if (i > DTRACE_V4MAPPED_OFFSET)
 						*end-- = '.';
 				}
 
 				if (subr == DIF_SUBR_INET_NTOA6)
 					goto inetout;
 
 				/*
 				 * Set v6end to skip the IPv4 address that
 				 * we have already stringified.
 				 */
 				v6end = 10;
 			}
 
 			/*
 			 * Build the IPv6 string by working through the
 			 * address in reverse.
 			 */
 			for (i = v6end; i >= 0; i -= 2) {
 				ASSERT(end >= base);
 
 				if (i == firstzero + numzero - 2) {
 					*end-- = ':';
 					*end-- = ':';
 					i -= numzero - 2;
 					continue;
 				}
 
 				if (i < 14 && i != firstzero - 2)
 					*end-- = ':';
 
 #ifdef illumos
 				val = (ip6._S6_un._S6_u8[i] << 8) +
 				    ip6._S6_un._S6_u8[i + 1];
 #else
 				val = (ip6.__u6_addr.__u6_addr8[i] << 8) +
 				    ip6.__u6_addr.__u6_addr8[i + 1];
 #endif
 
 				if (val == 0) {
 					*end-- = '0';
 				} else {
 					for (; val; val /= 16) {
 						*end-- = digits[val % 16];
 					}
 				}
 			}
 			ASSERT(end + 1 >= base);
 
 		} else {
 			/*
 			 * The user didn't use AH_INET or AH_INET6.
 			 */
 			DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
 			regs[rd] = 0;
 			break;
 		}
 
 inetout:	regs[rd] = (uintptr_t)end + 1;
 		mstate->dtms_scratch_ptr += size;
 		break;
 	}
 
 	case DIF_SUBR_MEMREF: {
 		uintptr_t size = 2 * sizeof(uintptr_t);
 		uintptr_t *memref = (uintptr_t *) P2ROUNDUP(mstate->dtms_scratch_ptr, sizeof(uintptr_t));
 		size_t scratch_size = ((uintptr_t) memref - mstate->dtms_scratch_ptr) + size;
 
 		/* address and length */
 		memref[0] = tupregs[0].dttk_value;
 		memref[1] = tupregs[1].dttk_value;
 
 		regs[rd] = (uintptr_t) memref;
 		mstate->dtms_scratch_ptr += scratch_size;
 		break;
 	}
 
 #ifndef illumos
 	case DIF_SUBR_MEMSTR: {
 		char *str = (char *)mstate->dtms_scratch_ptr;
 		uintptr_t mem = tupregs[0].dttk_value;
 		char c = tupregs[1].dttk_value;
 		size_t size = tupregs[2].dttk_value;
 		uint8_t n;
 		int i;
 
 		regs[rd] = 0;
 
 		if (size == 0)
 			break;
 
 		if (!dtrace_canload(mem, size - 1, mstate, vstate))
 			break;
 
 		if (!DTRACE_INSCRATCH(mstate, size)) {
 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
 			break;
 		}
 
 		if (dtrace_memstr_max != 0 && size > dtrace_memstr_max) {
 			*flags |= CPU_DTRACE_ILLOP;
 			break;
 		}
 
 		for (i = 0; i < size - 1; i++) {
 			n = dtrace_load8(mem++);
 			str[i] = (n == 0) ? c : n;
 		}
 		str[size - 1] = 0;
 
 		regs[rd] = (uintptr_t)str;
 		mstate->dtms_scratch_ptr += size;
 		break;
 	}
 #endif
 	}
 }
 
 /*
  * Emulate the execution of DTrace IR instructions specified by the given
  * DIF object.  This function is deliberately void of assertions as all of
  * the necessary checks are handled by a call to dtrace_difo_validate().
  */
 static uint64_t
 dtrace_dif_emulate(dtrace_difo_t *difo, dtrace_mstate_t *mstate,
     dtrace_vstate_t *vstate, dtrace_state_t *state)
 {
 	const dif_instr_t *text = difo->dtdo_buf;
 	const uint_t textlen = difo->dtdo_len;
 	const char *strtab = difo->dtdo_strtab;
 	const uint64_t *inttab = difo->dtdo_inttab;
 
 	uint64_t rval = 0;
 	dtrace_statvar_t *svar;
 	dtrace_dstate_t *dstate = &vstate->dtvs_dynvars;
 	dtrace_difv_t *v;
 	volatile uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags;
 	volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval;
 
 	dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */
 	uint64_t regs[DIF_DIR_NREGS];
 	uint64_t *tmp;
 
 	uint8_t cc_n = 0, cc_z = 0, cc_v = 0, cc_c = 0;
 	int64_t cc_r;
 	uint_t pc = 0, id, opc = 0;
 	uint8_t ttop = 0;
 	dif_instr_t instr;
 	uint_t r1, r2, rd;
 
 	/*
 	 * We stash the current DIF object into the machine state: we need it
 	 * for subsequent access checking.
 	 */
 	mstate->dtms_difo = difo;
 
 	regs[DIF_REG_R0] = 0; 		/* %r0 is fixed at zero */
 
 	while (pc < textlen && !(*flags & CPU_DTRACE_FAULT)) {
 		opc = pc;
 
 		instr = text[pc++];
 		r1 = DIF_INSTR_R1(instr);
 		r2 = DIF_INSTR_R2(instr);
 		rd = DIF_INSTR_RD(instr);
 
 		switch (DIF_INSTR_OP(instr)) {
 		case DIF_OP_OR:
 			regs[rd] = regs[r1] | regs[r2];
 			break;
 		case DIF_OP_XOR:
 			regs[rd] = regs[r1] ^ regs[r2];
 			break;
 		case DIF_OP_AND:
 			regs[rd] = regs[r1] & regs[r2];
 			break;
 		case DIF_OP_SLL:
 			regs[rd] = regs[r1] << regs[r2];
 			break;
 		case DIF_OP_SRL:
 			regs[rd] = regs[r1] >> regs[r2];
 			break;
 		case DIF_OP_SUB:
 			regs[rd] = regs[r1] - regs[r2];
 			break;
 		case DIF_OP_ADD:
 			regs[rd] = regs[r1] + regs[r2];
 			break;
 		case DIF_OP_MUL:
 			regs[rd] = regs[r1] * regs[r2];
 			break;
 		case DIF_OP_SDIV:
 			if (regs[r2] == 0) {
 				regs[rd] = 0;
 				*flags |= CPU_DTRACE_DIVZERO;
 			} else {
 				regs[rd] = (int64_t)regs[r1] /
 				    (int64_t)regs[r2];
 			}
 			break;
 
 		case DIF_OP_UDIV:
 			if (regs[r2] == 0) {
 				regs[rd] = 0;
 				*flags |= CPU_DTRACE_DIVZERO;
 			} else {
 				regs[rd] = regs[r1] / regs[r2];
 			}
 			break;
 
 		case DIF_OP_SREM:
 			if (regs[r2] == 0) {
 				regs[rd] = 0;
 				*flags |= CPU_DTRACE_DIVZERO;
 			} else {
 				regs[rd] = (int64_t)regs[r1] %
 				    (int64_t)regs[r2];
 			}
 			break;
 
 		case DIF_OP_UREM:
 			if (regs[r2] == 0) {
 				regs[rd] = 0;
 				*flags |= CPU_DTRACE_DIVZERO;
 			} else {
 				regs[rd] = regs[r1] % regs[r2];
 			}
 			break;
 
 		case DIF_OP_NOT:
 			regs[rd] = ~regs[r1];
 			break;
 		case DIF_OP_MOV:
 			regs[rd] = regs[r1];
 			break;
 		case DIF_OP_CMP:
 			cc_r = regs[r1] - regs[r2];
 			cc_n = cc_r < 0;
 			cc_z = cc_r == 0;
 			cc_v = 0;
 			cc_c = regs[r1] < regs[r2];
 			break;
 		case DIF_OP_TST:
 			cc_n = cc_v = cc_c = 0;
 			cc_z = regs[r1] == 0;
 			break;
 		case DIF_OP_BA:
 			pc = DIF_INSTR_LABEL(instr);
 			break;
 		case DIF_OP_BE:
 			if (cc_z)
 				pc = DIF_INSTR_LABEL(instr);
 			break;
 		case DIF_OP_BNE:
 			if (cc_z == 0)
 				pc = DIF_INSTR_LABEL(instr);
 			break;
 		case DIF_OP_BG:
 			if ((cc_z | (cc_n ^ cc_v)) == 0)
 				pc = DIF_INSTR_LABEL(instr);
 			break;
 		case DIF_OP_BGU:
 			if ((cc_c | cc_z) == 0)
 				pc = DIF_INSTR_LABEL(instr);
 			break;
 		case DIF_OP_BGE:
 			if ((cc_n ^ cc_v) == 0)
 				pc = DIF_INSTR_LABEL(instr);
 			break;
 		case DIF_OP_BGEU:
 			if (cc_c == 0)
 				pc = DIF_INSTR_LABEL(instr);
 			break;
 		case DIF_OP_BL:
 			if (cc_n ^ cc_v)
 				pc = DIF_INSTR_LABEL(instr);
 			break;
 		case DIF_OP_BLU:
 			if (cc_c)
 				pc = DIF_INSTR_LABEL(instr);
 			break;
 		case DIF_OP_BLE:
 			if (cc_z | (cc_n ^ cc_v))
 				pc = DIF_INSTR_LABEL(instr);
 			break;
 		case DIF_OP_BLEU:
 			if (cc_c | cc_z)
 				pc = DIF_INSTR_LABEL(instr);
 			break;
 		case DIF_OP_RLDSB:
 			if (!dtrace_canload(regs[r1], 1, mstate, vstate))
 				break;
 			/*FALLTHROUGH*/
 		case DIF_OP_LDSB:
 			regs[rd] = (int8_t)dtrace_load8(regs[r1]);
 			break;
 		case DIF_OP_RLDSH:
 			if (!dtrace_canload(regs[r1], 2, mstate, vstate))
 				break;
 			/*FALLTHROUGH*/
 		case DIF_OP_LDSH:
 			regs[rd] = (int16_t)dtrace_load16(regs[r1]);
 			break;
 		case DIF_OP_RLDSW:
 			if (!dtrace_canload(regs[r1], 4, mstate, vstate))
 				break;
 			/*FALLTHROUGH*/
 		case DIF_OP_LDSW:
 			regs[rd] = (int32_t)dtrace_load32(regs[r1]);
 			break;
 		case DIF_OP_RLDUB:
 			if (!dtrace_canload(regs[r1], 1, mstate, vstate))
 				break;
 			/*FALLTHROUGH*/
 		case DIF_OP_LDUB:
 			regs[rd] = dtrace_load8(regs[r1]);
 			break;
 		case DIF_OP_RLDUH:
 			if (!dtrace_canload(regs[r1], 2, mstate, vstate))
 				break;
 			/*FALLTHROUGH*/
 		case DIF_OP_LDUH:
 			regs[rd] = dtrace_load16(regs[r1]);
 			break;
 		case DIF_OP_RLDUW:
 			if (!dtrace_canload(regs[r1], 4, mstate, vstate))
 				break;
 			/*FALLTHROUGH*/
 		case DIF_OP_LDUW:
 			regs[rd] = dtrace_load32(regs[r1]);
 			break;
 		case DIF_OP_RLDX:
 			if (!dtrace_canload(regs[r1], 8, mstate, vstate))
 				break;
 			/*FALLTHROUGH*/
 		case DIF_OP_LDX:
 			regs[rd] = dtrace_load64(regs[r1]);
 			break;
 		case DIF_OP_ULDSB:
 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
 			regs[rd] = (int8_t)
 			    dtrace_fuword8((void *)(uintptr_t)regs[r1]);
 			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
 			break;
 		case DIF_OP_ULDSH:
 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
 			regs[rd] = (int16_t)
 			    dtrace_fuword16((void *)(uintptr_t)regs[r1]);
 			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
 			break;
 		case DIF_OP_ULDSW:
 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
 			regs[rd] = (int32_t)
 			    dtrace_fuword32((void *)(uintptr_t)regs[r1]);
 			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
 			break;
 		case DIF_OP_ULDUB:
 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
 			regs[rd] =
 			    dtrace_fuword8((void *)(uintptr_t)regs[r1]);
 			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
 			break;
 		case DIF_OP_ULDUH:
 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
 			regs[rd] =
 			    dtrace_fuword16((void *)(uintptr_t)regs[r1]);
 			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
 			break;
 		case DIF_OP_ULDUW:
 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
 			regs[rd] =
 			    dtrace_fuword32((void *)(uintptr_t)regs[r1]);
 			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
 			break;
 		case DIF_OP_ULDX:
 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
 			regs[rd] =
 			    dtrace_fuword64((void *)(uintptr_t)regs[r1]);
 			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
 			break;
 		case DIF_OP_RET:
 			rval = regs[rd];
 			pc = textlen;
 			break;
 		case DIF_OP_NOP:
 			break;
 		case DIF_OP_SETX:
 			regs[rd] = inttab[DIF_INSTR_INTEGER(instr)];
 			break;
 		case DIF_OP_SETS:
 			regs[rd] = (uint64_t)(uintptr_t)
 			    (strtab + DIF_INSTR_STRING(instr));
 			break;
 		case DIF_OP_SCMP: {
 			size_t sz = state->dts_options[DTRACEOPT_STRSIZE];
 			uintptr_t s1 = regs[r1];
 			uintptr_t s2 = regs[r2];
 			size_t lim1, lim2;
 
 			/*
 			 * If one of the strings is NULL then the limit becomes
 			 * 0 which compares 0 characters in dtrace_strncmp()
 			 * resulting in a false positive.  dtrace_strncmp()
 			 * treats a NULL as an empty 1-char string.
 			 */
 			lim1 = lim2 = 1;
 
 			if (s1 != 0 &&
 			    !dtrace_strcanload(s1, sz, &lim1, mstate, vstate))
 				break;
 			if (s2 != 0 &&
 			    !dtrace_strcanload(s2, sz, &lim2, mstate, vstate))
 				break;
 
 			cc_r = dtrace_strncmp((char *)s1, (char *)s2,
 			    MIN(lim1, lim2));
 
 			cc_n = cc_r < 0;
 			cc_z = cc_r == 0;
 			cc_v = cc_c = 0;
 			break;
 		}
 		case DIF_OP_LDGA:
 			regs[rd] = dtrace_dif_variable(mstate, state,
 			    r1, regs[r2]);
 			break;
 		case DIF_OP_LDGS:
 			id = DIF_INSTR_VAR(instr);
 
 			if (id >= DIF_VAR_OTHER_UBASE) {
 				uintptr_t a;
 
 				id -= DIF_VAR_OTHER_UBASE;
 				svar = vstate->dtvs_globals[id];
 				ASSERT(svar != NULL);
 				v = &svar->dtsv_var;
 
 				if (!(v->dtdv_type.dtdt_flags & DIF_TF_BYREF)) {
 					regs[rd] = svar->dtsv_data;
 					break;
 				}
 
 				a = (uintptr_t)svar->dtsv_data;
 
 				if (*(uint8_t *)a == UINT8_MAX) {
 					/*
 					 * If the 0th byte is set to UINT8_MAX
 					 * then this is to be treated as a
 					 * reference to a NULL variable.
 					 */
 					regs[rd] = 0;
 				} else {
 					regs[rd] = a + sizeof (uint64_t);
 				}
 
 				break;
 			}
 
 			regs[rd] = dtrace_dif_variable(mstate, state, id, 0);
 			break;
 
 		case DIF_OP_STGS:
 			id = DIF_INSTR_VAR(instr);
 
 			ASSERT(id >= DIF_VAR_OTHER_UBASE);
 			id -= DIF_VAR_OTHER_UBASE;
 
 			VERIFY(id < vstate->dtvs_nglobals);
 			svar = vstate->dtvs_globals[id];
 			ASSERT(svar != NULL);
 			v = &svar->dtsv_var;
 
 			if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
 				uintptr_t a = (uintptr_t)svar->dtsv_data;
 				size_t lim;
 
 				ASSERT(a != 0);
 				ASSERT(svar->dtsv_size != 0);
 
 				if (regs[rd] == 0) {
 					*(uint8_t *)a = UINT8_MAX;
 					break;
 				} else {
 					*(uint8_t *)a = 0;
 					a += sizeof (uint64_t);
 				}
 				if (!dtrace_vcanload(
 				    (void *)(uintptr_t)regs[rd], &v->dtdv_type,
 				    &lim, mstate, vstate))
 					break;
 
 				dtrace_vcopy((void *)(uintptr_t)regs[rd],
 				    (void *)a, &v->dtdv_type, lim);
 				break;
 			}
 
 			svar->dtsv_data = regs[rd];
 			break;
 
 		case DIF_OP_LDTA:
 			/*
 			 * There are no DTrace built-in thread-local arrays at
 			 * present.  This opcode is saved for future work.
 			 */
 			*flags |= CPU_DTRACE_ILLOP;
 			regs[rd] = 0;
 			break;
 
 		case DIF_OP_LDLS:
 			id = DIF_INSTR_VAR(instr);
 
 			if (id < DIF_VAR_OTHER_UBASE) {
 				/*
 				 * For now, this has no meaning.
 				 */
 				regs[rd] = 0;
 				break;
 			}
 
 			id -= DIF_VAR_OTHER_UBASE;
 
 			ASSERT(id < vstate->dtvs_nlocals);
 			ASSERT(vstate->dtvs_locals != NULL);
 
 			svar = vstate->dtvs_locals[id];
 			ASSERT(svar != NULL);
 			v = &svar->dtsv_var;
 
 			if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
 				uintptr_t a = (uintptr_t)svar->dtsv_data;
 				size_t sz = v->dtdv_type.dtdt_size;
 				size_t lim;
 
 				sz += sizeof (uint64_t);
 				ASSERT(svar->dtsv_size == NCPU * sz);
 				a += curcpu * sz;
 
 				if (*(uint8_t *)a == UINT8_MAX) {
 					/*
 					 * If the 0th byte is set to UINT8_MAX
 					 * then this is to be treated as a
 					 * reference to a NULL variable.
 					 */
 					regs[rd] = 0;
 				} else {
 					regs[rd] = a + sizeof (uint64_t);
 				}
 
 				break;
 			}
 
 			ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t));
 			tmp = (uint64_t *)(uintptr_t)svar->dtsv_data;
 			regs[rd] = tmp[curcpu];
 			break;
 
 		case DIF_OP_STLS:
 			id = DIF_INSTR_VAR(instr);
 
 			ASSERT(id >= DIF_VAR_OTHER_UBASE);
 			id -= DIF_VAR_OTHER_UBASE;
 			VERIFY(id < vstate->dtvs_nlocals);
 
 			ASSERT(vstate->dtvs_locals != NULL);
 			svar = vstate->dtvs_locals[id];
 			ASSERT(svar != NULL);
 			v = &svar->dtsv_var;
 
 			if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
 				uintptr_t a = (uintptr_t)svar->dtsv_data;
 				size_t sz = v->dtdv_type.dtdt_size;
 				size_t lim;
 
 				sz += sizeof (uint64_t);
 				ASSERT(svar->dtsv_size == NCPU * sz);
 				a += curcpu * sz;
 
 				if (regs[rd] == 0) {
 					*(uint8_t *)a = UINT8_MAX;
 					break;
 				} else {
 					*(uint8_t *)a = 0;
 					a += sizeof (uint64_t);
 				}
 
 				if (!dtrace_vcanload(
 				    (void *)(uintptr_t)regs[rd], &v->dtdv_type,
 				    &lim, mstate, vstate))
 					break;
 
 				dtrace_vcopy((void *)(uintptr_t)regs[rd],
 				    (void *)a, &v->dtdv_type, lim);
 				break;
 			}
 
 			ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t));
 			tmp = (uint64_t *)(uintptr_t)svar->dtsv_data;
 			tmp[curcpu] = regs[rd];
 			break;
 
 		case DIF_OP_LDTS: {
 			dtrace_dynvar_t *dvar;
 			dtrace_key_t *key;
 
 			id = DIF_INSTR_VAR(instr);
 			ASSERT(id >= DIF_VAR_OTHER_UBASE);
 			id -= DIF_VAR_OTHER_UBASE;
 			v = &vstate->dtvs_tlocals[id];
 
 			key = &tupregs[DIF_DTR_NREGS];
 			key[0].dttk_value = (uint64_t)id;
 			key[0].dttk_size = 0;
 			DTRACE_TLS_THRKEY(key[1].dttk_value);
 			key[1].dttk_size = 0;
 
 			dvar = dtrace_dynvar(dstate, 2, key,
 			    sizeof (uint64_t), DTRACE_DYNVAR_NOALLOC,
 			    mstate, vstate);
 
 			if (dvar == NULL) {
 				regs[rd] = 0;
 				break;
 			}
 
 			if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
 				regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data;
 			} else {
 				regs[rd] = *((uint64_t *)dvar->dtdv_data);
 			}
 
 			break;
 		}
 
 		case DIF_OP_STTS: {
 			dtrace_dynvar_t *dvar;
 			dtrace_key_t *key;
 
 			id = DIF_INSTR_VAR(instr);
 			ASSERT(id >= DIF_VAR_OTHER_UBASE);
 			id -= DIF_VAR_OTHER_UBASE;
 			VERIFY(id < vstate->dtvs_ntlocals);
 
 			key = &tupregs[DIF_DTR_NREGS];
 			key[0].dttk_value = (uint64_t)id;
 			key[0].dttk_size = 0;
 			DTRACE_TLS_THRKEY(key[1].dttk_value);
 			key[1].dttk_size = 0;
 			v = &vstate->dtvs_tlocals[id];
 
 			dvar = dtrace_dynvar(dstate, 2, key,
 			    v->dtdv_type.dtdt_size > sizeof (uint64_t) ?
 			    v->dtdv_type.dtdt_size : sizeof (uint64_t),
 			    regs[rd] ? DTRACE_DYNVAR_ALLOC :
 			    DTRACE_DYNVAR_DEALLOC, mstate, vstate);
 
 			/*
 			 * Given that we're storing to thread-local data,
 			 * we need to flush our predicate cache.
 			 */
 			curthread->t_predcache = 0;
 
 			if (dvar == NULL)
 				break;
 
 			if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
 				size_t lim;
 
 				if (!dtrace_vcanload(
 				    (void *)(uintptr_t)regs[rd],
 				    &v->dtdv_type, &lim, mstate, vstate))
 					break;
 
 				dtrace_vcopy((void *)(uintptr_t)regs[rd],
 				    dvar->dtdv_data, &v->dtdv_type, lim);
 			} else {
 				*((uint64_t *)dvar->dtdv_data) = regs[rd];
 			}
 
 			break;
 		}
 
 		case DIF_OP_SRA:
 			regs[rd] = (int64_t)regs[r1] >> regs[r2];
 			break;
 
 		case DIF_OP_CALL:
 			dtrace_dif_subr(DIF_INSTR_SUBR(instr), rd,
 			    regs, tupregs, ttop, mstate, state);
 			break;
 
 		case DIF_OP_PUSHTR:
 			if (ttop == DIF_DTR_NREGS) {
 				*flags |= CPU_DTRACE_TUPOFLOW;
 				break;
 			}
 
 			if (r1 == DIF_TYPE_STRING) {
 				/*
 				 * If this is a string type and the size is 0,
 				 * we'll use the system-wide default string
 				 * size.  Note that we are _not_ looking at
 				 * the value of the DTRACEOPT_STRSIZE option;
 				 * had this been set, we would expect to have
 				 * a non-zero size value in the "pushtr".
 				 */
 				tupregs[ttop].dttk_size =
 				    dtrace_strlen((char *)(uintptr_t)regs[rd],
 				    regs[r2] ? regs[r2] :
 				    dtrace_strsize_default) + 1;
 			} else {
 				if (regs[r2] > LONG_MAX) {
 					*flags |= CPU_DTRACE_ILLOP;
 					break;
 				}
 
 				tupregs[ttop].dttk_size = regs[r2];
 			}
 
 			tupregs[ttop++].dttk_value = regs[rd];
 			break;
 
 		case DIF_OP_PUSHTV:
 			if (ttop == DIF_DTR_NREGS) {
 				*flags |= CPU_DTRACE_TUPOFLOW;
 				break;
 			}
 
 			tupregs[ttop].dttk_value = regs[rd];
 			tupregs[ttop++].dttk_size = 0;
 			break;
 
 		case DIF_OP_POPTS:
 			if (ttop != 0)
 				ttop--;
 			break;
 
 		case DIF_OP_FLUSHTS:
 			ttop = 0;
 			break;
 
 		case DIF_OP_LDGAA:
 		case DIF_OP_LDTAA: {
 			dtrace_dynvar_t *dvar;
 			dtrace_key_t *key = tupregs;
 			uint_t nkeys = ttop;
 
 			id = DIF_INSTR_VAR(instr);
 			ASSERT(id >= DIF_VAR_OTHER_UBASE);
 			id -= DIF_VAR_OTHER_UBASE;
 
 			key[nkeys].dttk_value = (uint64_t)id;
 			key[nkeys++].dttk_size = 0;
 
 			if (DIF_INSTR_OP(instr) == DIF_OP_LDTAA) {
 				DTRACE_TLS_THRKEY(key[nkeys].dttk_value);
 				key[nkeys++].dttk_size = 0;
 				VERIFY(id < vstate->dtvs_ntlocals);
 				v = &vstate->dtvs_tlocals[id];
 			} else {
 				VERIFY(id < vstate->dtvs_nglobals);
 				v = &vstate->dtvs_globals[id]->dtsv_var;
 			}
 
 			dvar = dtrace_dynvar(dstate, nkeys, key,
 			    v->dtdv_type.dtdt_size > sizeof (uint64_t) ?
 			    v->dtdv_type.dtdt_size : sizeof (uint64_t),
 			    DTRACE_DYNVAR_NOALLOC, mstate, vstate);
 
 			if (dvar == NULL) {
 				regs[rd] = 0;
 				break;
 			}
 
 			if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
 				regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data;
 			} else {
 				regs[rd] = *((uint64_t *)dvar->dtdv_data);
 			}
 
 			break;
 		}
 
 		case DIF_OP_STGAA:
 		case DIF_OP_STTAA: {
 			dtrace_dynvar_t *dvar;
 			dtrace_key_t *key = tupregs;
 			uint_t nkeys = ttop;
 
 			id = DIF_INSTR_VAR(instr);
 			ASSERT(id >= DIF_VAR_OTHER_UBASE);
 			id -= DIF_VAR_OTHER_UBASE;
 
 			key[nkeys].dttk_value = (uint64_t)id;
 			key[nkeys++].dttk_size = 0;
 
 			if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) {
 				DTRACE_TLS_THRKEY(key[nkeys].dttk_value);
 				key[nkeys++].dttk_size = 0;
 				VERIFY(id < vstate->dtvs_ntlocals);
 				v = &vstate->dtvs_tlocals[id];
 			} else {
 				VERIFY(id < vstate->dtvs_nglobals);
 				v = &vstate->dtvs_globals[id]->dtsv_var;
 			}
 
 			dvar = dtrace_dynvar(dstate, nkeys, key,
 			    v->dtdv_type.dtdt_size > sizeof (uint64_t) ?
 			    v->dtdv_type.dtdt_size : sizeof (uint64_t),
 			    regs[rd] ? DTRACE_DYNVAR_ALLOC :
 			    DTRACE_DYNVAR_DEALLOC, mstate, vstate);
 
 			if (dvar == NULL)
 				break;
 
 			if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) {
 				size_t lim;
 
 				if (!dtrace_vcanload(
 				    (void *)(uintptr_t)regs[rd], &v->dtdv_type,
 				    &lim, mstate, vstate))
 					break;
 
 				dtrace_vcopy((void *)(uintptr_t)regs[rd],
 				    dvar->dtdv_data, &v->dtdv_type, lim);
 			} else {
 				*((uint64_t *)dvar->dtdv_data) = regs[rd];
 			}
 
 			break;
 		}
 
 		case DIF_OP_ALLOCS: {
 			uintptr_t ptr = P2ROUNDUP(mstate->dtms_scratch_ptr, 8);
 			size_t size = ptr - mstate->dtms_scratch_ptr + regs[r1];
 
 			/*
 			 * Rounding up the user allocation size could have
 			 * overflowed large, bogus allocations (like -1ULL) to
 			 * 0.
 			 */
 			if (size < regs[r1] ||
 			    !DTRACE_INSCRATCH(mstate, size)) {
 				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
 				regs[rd] = 0;
 				break;
 			}
 
 			dtrace_bzero((void *) mstate->dtms_scratch_ptr, size);
 			mstate->dtms_scratch_ptr += size;
 			regs[rd] = ptr;
 			break;
 		}
 
 		case DIF_OP_COPYS:
 			if (!dtrace_canstore(regs[rd], regs[r2],
 			    mstate, vstate)) {
 				*flags |= CPU_DTRACE_BADADDR;
 				*illval = regs[rd];
 				break;
 			}
 
 			if (!dtrace_canload(regs[r1], regs[r2], mstate, vstate))
 				break;
 
 			dtrace_bcopy((void *)(uintptr_t)regs[r1],
 			    (void *)(uintptr_t)regs[rd], (size_t)regs[r2]);
 			break;
 
 		case DIF_OP_STB:
 			if (!dtrace_canstore(regs[rd], 1, mstate, vstate)) {
 				*flags |= CPU_DTRACE_BADADDR;
 				*illval = regs[rd];
 				break;
 			}
 			*((uint8_t *)(uintptr_t)regs[rd]) = (uint8_t)regs[r1];
 			break;
 
 		case DIF_OP_STH:
 			if (!dtrace_canstore(regs[rd], 2, mstate, vstate)) {
 				*flags |= CPU_DTRACE_BADADDR;
 				*illval = regs[rd];
 				break;
 			}
 			if (regs[rd] & 1) {
 				*flags |= CPU_DTRACE_BADALIGN;
 				*illval = regs[rd];
 				break;
 			}
 			*((uint16_t *)(uintptr_t)regs[rd]) = (uint16_t)regs[r1];
 			break;
 
 		case DIF_OP_STW:
 			if (!dtrace_canstore(regs[rd], 4, mstate, vstate)) {
 				*flags |= CPU_DTRACE_BADADDR;
 				*illval = regs[rd];
 				break;
 			}
 			if (regs[rd] & 3) {
 				*flags |= CPU_DTRACE_BADALIGN;
 				*illval = regs[rd];
 				break;
 			}
 			*((uint32_t *)(uintptr_t)regs[rd]) = (uint32_t)regs[r1];
 			break;
 
 		case DIF_OP_STX:
 			if (!dtrace_canstore(regs[rd], 8, mstate, vstate)) {
 				*flags |= CPU_DTRACE_BADADDR;
 				*illval = regs[rd];
 				break;
 			}
 			if (regs[rd] & 7) {
 				*flags |= CPU_DTRACE_BADALIGN;
 				*illval = regs[rd];
 				break;
 			}
 			*((uint64_t *)(uintptr_t)regs[rd]) = regs[r1];
 			break;
 		}
 	}
 
 	if (!(*flags & CPU_DTRACE_FAULT))
 		return (rval);
 
 	mstate->dtms_fltoffs = opc * sizeof (dif_instr_t);
 	mstate->dtms_present |= DTRACE_MSTATE_FLTOFFS;
 
 	return (0);
 }
 
 static void
 dtrace_action_breakpoint(dtrace_ecb_t *ecb)
 {
 	dtrace_probe_t *probe = ecb->dte_probe;
 	dtrace_provider_t *prov = probe->dtpr_provider;
 	char c[DTRACE_FULLNAMELEN + 80], *str;
 	char *msg = "dtrace: breakpoint action at probe ";
 	char *ecbmsg = " (ecb ";
 	uintptr_t mask = (0xf << (sizeof (uintptr_t) * NBBY / 4));
 	uintptr_t val = (uintptr_t)ecb;
 	int shift = (sizeof (uintptr_t) * NBBY) - 4, i = 0;
 
 	if (dtrace_destructive_disallow)
 		return;
 
 	/*
 	 * It's impossible to be taking action on the NULL probe.
 	 */
 	ASSERT(probe != NULL);
 
 	/*
 	 * This is a poor man's (destitute man's?) sprintf():  we want to
 	 * print the provider name, module name, function name and name of
 	 * the probe, along with the hex address of the ECB with the breakpoint
 	 * action -- all of which we must place in the character buffer by
 	 * hand.
 	 */
 	while (*msg != '\0')
 		c[i++] = *msg++;
 
 	for (str = prov->dtpv_name; *str != '\0'; str++)
 		c[i++] = *str;
 	c[i++] = ':';
 
 	for (str = probe->dtpr_mod; *str != '\0'; str++)
 		c[i++] = *str;
 	c[i++] = ':';
 
 	for (str = probe->dtpr_func; *str != '\0'; str++)
 		c[i++] = *str;
 	c[i++] = ':';
 
 	for (str = probe->dtpr_name; *str != '\0'; str++)
 		c[i++] = *str;
 
 	while (*ecbmsg != '\0')
 		c[i++] = *ecbmsg++;
 
 	while (shift >= 0) {
 		mask = (uintptr_t)0xf << shift;
 
 		if (val >= ((uintptr_t)1 << shift))
 			c[i++] = "0123456789abcdef"[(val & mask) >> shift];
 		shift -= 4;
 	}
 
 	c[i++] = ')';
 	c[i] = '\0';
 
 #ifdef illumos
 	debug_enter(c);
 #else
 	kdb_enter(KDB_WHY_DTRACE, "breakpoint action");
 #endif
 }
 
 static void
 dtrace_action_panic(dtrace_ecb_t *ecb)
 {
 	dtrace_probe_t *probe = ecb->dte_probe;
 
 	/*
 	 * It's impossible to be taking action on the NULL probe.
 	 */
 	ASSERT(probe != NULL);
 
 	if (dtrace_destructive_disallow)
 		return;
 
 	if (dtrace_panicked != NULL)
 		return;
 
 	if (dtrace_casptr(&dtrace_panicked, NULL, curthread) != NULL)
 		return;
 
 	/*
 	 * We won the right to panic.  (We want to be sure that only one
 	 * thread calls panic() from dtrace_probe(), and that panic() is
 	 * called exactly once.)
 	 */
 	dtrace_panic("dtrace: panic action at probe %s:%s:%s:%s (ecb %p)",
 	    probe->dtpr_provider->dtpv_name, probe->dtpr_mod,
 	    probe->dtpr_func, probe->dtpr_name, (void *)ecb);
 }
 
 static void
 dtrace_action_raise(uint64_t sig)
 {
 	if (dtrace_destructive_disallow)
 		return;
 
 	if (sig >= NSIG) {
 		DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
 		return;
 	}
 
 #ifdef illumos
 	/*
 	 * raise() has a queue depth of 1 -- we ignore all subsequent
 	 * invocations of the raise() action.
 	 */
 	if (curthread->t_dtrace_sig == 0)
 		curthread->t_dtrace_sig = (uint8_t)sig;
 
 	curthread->t_sig_check = 1;
 	aston(curthread);
 #else
 	struct proc *p = curproc;
 	PROC_LOCK(p);
 	kern_psignal(p, sig);
 	PROC_UNLOCK(p);
 #endif
 }
 
 static void
 dtrace_action_stop(void)
 {
 	if (dtrace_destructive_disallow)
 		return;
 
 #ifdef illumos
 	if (!curthread->t_dtrace_stop) {
 		curthread->t_dtrace_stop = 1;
 		curthread->t_sig_check = 1;
 		aston(curthread);
 	}
 #else
 	struct proc *p = curproc;
 	PROC_LOCK(p);
 	kern_psignal(p, SIGSTOP);
 	PROC_UNLOCK(p);
 #endif
 }
 
 static void
 dtrace_action_chill(dtrace_mstate_t *mstate, hrtime_t val)
 {
 	hrtime_t now;
 	volatile uint16_t *flags;
 #ifdef illumos
 	cpu_t *cpu = CPU;
 #else
 	cpu_t *cpu = &solaris_cpu[curcpu];
 #endif
 
 	if (dtrace_destructive_disallow)
 		return;
 
 	flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags;
 
 	now = dtrace_gethrtime();
 
 	if (now - cpu->cpu_dtrace_chillmark > dtrace_chill_interval) {
 		/*
 		 * We need to advance the mark to the current time.
 		 */
 		cpu->cpu_dtrace_chillmark = now;
 		cpu->cpu_dtrace_chilled = 0;
 	}
 
 	/*
 	 * Now check to see if the requested chill time would take us over
 	 * the maximum amount of time allowed in the chill interval.  (Or
 	 * worse, if the calculation itself induces overflow.)
 	 */
 	if (cpu->cpu_dtrace_chilled + val > dtrace_chill_max ||
 	    cpu->cpu_dtrace_chilled + val < cpu->cpu_dtrace_chilled) {
 		*flags |= CPU_DTRACE_ILLOP;
 		return;
 	}
 
 	while (dtrace_gethrtime() - now < val)
 		continue;
 
 	/*
 	 * Normally, we assure that the value of the variable "timestamp" does
 	 * not change within an ECB.  The presence of chill() represents an
 	 * exception to this rule, however.
 	 */
 	mstate->dtms_present &= ~DTRACE_MSTATE_TIMESTAMP;
 	cpu->cpu_dtrace_chilled += val;
 }
 
 static void
 dtrace_action_ustack(dtrace_mstate_t *mstate, dtrace_state_t *state,
     uint64_t *buf, uint64_t arg)
 {
 	int nframes = DTRACE_USTACK_NFRAMES(arg);
 	int strsize = DTRACE_USTACK_STRSIZE(arg);
 	uint64_t *pcs = &buf[1], *fps;
 	char *str = (char *)&pcs[nframes];
 	int size, offs = 0, i, j;
 	size_t rem;
 	uintptr_t old = mstate->dtms_scratch_ptr, saved;
 	uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags;
 	char *sym;
 
 	/*
 	 * Should be taking a faster path if string space has not been
 	 * allocated.
 	 */
 	ASSERT(strsize != 0);
 
 	/*
 	 * We will first allocate some temporary space for the frame pointers.
 	 */
 	fps = (uint64_t *)P2ROUNDUP(mstate->dtms_scratch_ptr, 8);
 	size = (uintptr_t)fps - mstate->dtms_scratch_ptr +
 	    (nframes * sizeof (uint64_t));
 
 	if (!DTRACE_INSCRATCH(mstate, size)) {
 		/*
 		 * Not enough room for our frame pointers -- need to indicate
 		 * that we ran out of scratch space.
 		 */
 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH);
 		return;
 	}
 
 	mstate->dtms_scratch_ptr += size;
 	saved = mstate->dtms_scratch_ptr;
 
 	/*
 	 * Now get a stack with both program counters and frame pointers.
 	 */
 	DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
 	dtrace_getufpstack(buf, fps, nframes + 1);
 	DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
 
 	/*
 	 * If that faulted, we're cooked.
 	 */
 	if (*flags & CPU_DTRACE_FAULT)
 		goto out;
 
 	/*
 	 * Now we want to walk up the stack, calling the USTACK helper.  For
 	 * each iteration, we restore the scratch pointer.
 	 */
 	for (i = 0; i < nframes; i++) {
 		mstate->dtms_scratch_ptr = saved;
 
 		if (offs >= strsize)
 			break;
 
 		sym = (char *)(uintptr_t)dtrace_helper(
 		    DTRACE_HELPER_ACTION_USTACK,
 		    mstate, state, pcs[i], fps[i]);
 
 		/*
 		 * If we faulted while running the helper, we're going to
 		 * clear the fault and null out the corresponding string.
 		 */
 		if (*flags & CPU_DTRACE_FAULT) {
 			*flags &= ~CPU_DTRACE_FAULT;
 			str[offs++] = '\0';
 			continue;
 		}
 
 		if (sym == NULL) {
 			str[offs++] = '\0';
 			continue;
 		}
 
 		if (!dtrace_strcanload((uintptr_t)sym, strsize, &rem, mstate,
 		    &(state->dts_vstate))) {
 			str[offs++] = '\0';
 			continue;
 		}
 
 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
 
 		/*
 		 * Now copy in the string that the helper returned to us.
 		 */
 		for (j = 0; offs + j < strsize && j < rem; j++) {
 			if ((str[offs + j] = sym[j]) == '\0')
 				break;
 		}
 
 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
 
 		offs += j + 1;
 	}
 
 	if (offs >= strsize) {
 		/*
 		 * If we didn't have room for all of the strings, we don't
 		 * abort processing -- this needn't be a fatal error -- but we
 		 * still want to increment a counter (dts_stkstroverflows) to
 		 * allow this condition to be warned about.  (If this is from
 		 * a jstack() action, it is easily tuned via jstackstrsize.)
 		 */
 		dtrace_error(&state->dts_stkstroverflows);
 	}
 
 	while (offs < strsize)
 		str[offs++] = '\0';
 
 out:
 	mstate->dtms_scratch_ptr = old;
 }
 
 static void
 dtrace_store_by_ref(dtrace_difo_t *dp, caddr_t tomax, size_t size,
     size_t *valoffsp, uint64_t *valp, uint64_t end, int intuple, int dtkind)
 {
 	volatile uint16_t *flags;
 	uint64_t val = *valp;
 	size_t valoffs = *valoffsp;
 
 	flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags;
 	ASSERT(dtkind == DIF_TF_BYREF || dtkind == DIF_TF_BYUREF);
 
 	/*
 	 * If this is a string, we're going to only load until we find the zero
 	 * byte -- after which we'll store zero bytes.
 	 */
 	if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) {
 		char c = '\0' + 1;
 		size_t s;
 
 		for (s = 0; s < size; s++) {
 			if (c != '\0' && dtkind == DIF_TF_BYREF) {
 				c = dtrace_load8(val++);
 			} else if (c != '\0' && dtkind == DIF_TF_BYUREF) {
 				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
 				c = dtrace_fuword8((void *)(uintptr_t)val++);
 				DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
 				if (*flags & CPU_DTRACE_FAULT)
 					break;
 			}
 
 			DTRACE_STORE(uint8_t, tomax, valoffs++, c);
 
 			if (c == '\0' && intuple)
 				break;
 		}
 	} else {
 		uint8_t c;
 		while (valoffs < end) {
 			if (dtkind == DIF_TF_BYREF) {
 				c = dtrace_load8(val++);
 			} else if (dtkind == DIF_TF_BYUREF) {
 				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
 				c = dtrace_fuword8((void *)(uintptr_t)val++);
 				DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
 				if (*flags & CPU_DTRACE_FAULT)
 					break;
 			}
 
 			DTRACE_STORE(uint8_t, tomax,
 			    valoffs++, c);
 		}
 	}
 
 	*valp = val;
 	*valoffsp = valoffs;
 }
 
 /*
  * Disables interrupts and sets the per-thread inprobe flag. When DEBUG is
  * defined, we also assert that we are not recursing unless the probe ID is an
  * error probe.
  */
 static dtrace_icookie_t
 dtrace_probe_enter(dtrace_id_t id)
 {
 	dtrace_icookie_t cookie;
 
 	cookie = dtrace_interrupt_disable();
 
 	/*
 	 * Unless this is an ERROR probe, we are not allowed to recurse in
 	 * dtrace_probe(). Recursing into DTrace probe usually means that a
 	 * function is instrumented that should not have been instrumented or
 	 * that the ordering guarantee of the records will be violated,
 	 * resulting in unexpected output. If there is an exception to this
 	 * assertion, a new case should be added.
 	 */
 	ASSERT(curthread->t_dtrace_inprobe == 0 ||
 	    id == dtrace_probeid_error);
 	curthread->t_dtrace_inprobe = 1;
 
 	return (cookie);
 }
 
 /*
  * Clears the per-thread inprobe flag and enables interrupts.
  */
 static void
 dtrace_probe_exit(dtrace_icookie_t cookie)
 {
 
 	curthread->t_dtrace_inprobe = 0;
 	dtrace_interrupt_enable(cookie);
 }
 
 /*
  * If you're looking for the epicenter of DTrace, you just found it.  This
  * is the function called by the provider to fire a probe -- from which all
  * subsequent probe-context DTrace activity emanates.
  */
 void
 dtrace_probe(dtrace_id_t id, uintptr_t arg0, uintptr_t arg1,
     uintptr_t arg2, uintptr_t arg3, uintptr_t arg4)
 {
 	processorid_t cpuid;
 	dtrace_icookie_t cookie;
 	dtrace_probe_t *probe;
 	dtrace_mstate_t mstate;
 	dtrace_ecb_t *ecb;
 	dtrace_action_t *act;
 	intptr_t offs;
 	size_t size;
 	int vtime, onintr;
 	volatile uint16_t *flags;
 	hrtime_t now;
 
-	if (panicstr != NULL)
+	if (KERNEL_PANICKED())
 		return;
 
 #ifdef illumos
 	/*
 	 * Kick out immediately if this CPU is still being born (in which case
 	 * curthread will be set to -1) or the current thread can't allow
 	 * probes in its current context.
 	 */
 	if (((uintptr_t)curthread & 1) || (curthread->t_flag & T_DONTDTRACE))
 		return;
 #endif
 
 	cookie = dtrace_probe_enter(id);
 	probe = dtrace_probes[id - 1];
 	cpuid = curcpu;
 	onintr = CPU_ON_INTR(CPU);
 
 	if (!onintr && probe->dtpr_predcache != DTRACE_CACHEIDNONE &&
 	    probe->dtpr_predcache == curthread->t_predcache) {
 		/*
 		 * We have hit in the predicate cache; we know that
 		 * this predicate would evaluate to be false.
 		 */
 		dtrace_probe_exit(cookie);
 		return;
 	}
 
 #ifdef illumos
 	if (panic_quiesce) {
 #else
-	if (panicstr != NULL) {
+	if (KERNEL_PANICKED()) {
 #endif
 		/*
 		 * We don't trace anything if we're panicking.
 		 */
 		dtrace_probe_exit(cookie);
 		return;
 	}
 
 	now = mstate.dtms_timestamp = dtrace_gethrtime();
 	mstate.dtms_present = DTRACE_MSTATE_TIMESTAMP;
 	vtime = dtrace_vtime_references != 0;
 
 	if (vtime && curthread->t_dtrace_start)
 		curthread->t_dtrace_vtime += now - curthread->t_dtrace_start;
 
 	mstate.dtms_difo = NULL;
 	mstate.dtms_probe = probe;
 	mstate.dtms_strtok = 0;
 	mstate.dtms_arg[0] = arg0;
 	mstate.dtms_arg[1] = arg1;
 	mstate.dtms_arg[2] = arg2;
 	mstate.dtms_arg[3] = arg3;
 	mstate.dtms_arg[4] = arg4;
 
 	flags = (volatile uint16_t *)&cpu_core[cpuid].cpuc_dtrace_flags;
 
 	for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) {
 		dtrace_predicate_t *pred = ecb->dte_predicate;
 		dtrace_state_t *state = ecb->dte_state;
 		dtrace_buffer_t *buf = &state->dts_buffer[cpuid];
 		dtrace_buffer_t *aggbuf = &state->dts_aggbuffer[cpuid];
 		dtrace_vstate_t *vstate = &state->dts_vstate;
 		dtrace_provider_t *prov = probe->dtpr_provider;
 		uint64_t tracememsize = 0;
 		int committed = 0;
 		caddr_t tomax;
 
 		/*
 		 * A little subtlety with the following (seemingly innocuous)
 		 * declaration of the automatic 'val':  by looking at the
 		 * code, you might think that it could be declared in the
 		 * action processing loop, below.  (That is, it's only used in
 		 * the action processing loop.)  However, it must be declared
 		 * out of that scope because in the case of DIF expression
 		 * arguments to aggregating actions, one iteration of the
 		 * action loop will use the last iteration's value.
 		 */
 		uint64_t val = 0;
 
 		mstate.dtms_present = DTRACE_MSTATE_ARGS | DTRACE_MSTATE_PROBE;
 		mstate.dtms_getf = NULL;
 
 		*flags &= ~CPU_DTRACE_ERROR;
 
 		if (prov == dtrace_provider) {
 			/*
 			 * If dtrace itself is the provider of this probe,
 			 * we're only going to continue processing the ECB if
 			 * arg0 (the dtrace_state_t) is equal to the ECB's
 			 * creating state.  (This prevents disjoint consumers
 			 * from seeing one another's metaprobes.)
 			 */
 			if (arg0 != (uint64_t)(uintptr_t)state)
 				continue;
 		}
 
 		if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) {
 			/*
 			 * We're not currently active.  If our provider isn't
 			 * the dtrace pseudo provider, we're not interested.
 			 */
 			if (prov != dtrace_provider)
 				continue;
 
 			/*
 			 * Now we must further check if we are in the BEGIN
 			 * probe.  If we are, we will only continue processing
 			 * if we're still in WARMUP -- if one BEGIN enabling
 			 * has invoked the exit() action, we don't want to
 			 * evaluate subsequent BEGIN enablings.
 			 */
 			if (probe->dtpr_id == dtrace_probeid_begin &&
 			    state->dts_activity != DTRACE_ACTIVITY_WARMUP) {
 				ASSERT(state->dts_activity ==
 				    DTRACE_ACTIVITY_DRAINING);
 				continue;
 			}
 		}
 
 		if (ecb->dte_cond) {
 			/*
 			 * If the dte_cond bits indicate that this
 			 * consumer is only allowed to see user-mode firings
 			 * of this probe, call the provider's dtps_usermode()
 			 * entry point to check that the probe was fired
 			 * while in a user context. Skip this ECB if that's
 			 * not the case.
 			 */
 			if ((ecb->dte_cond & DTRACE_COND_USERMODE) &&
 			    prov->dtpv_pops.dtps_usermode(prov->dtpv_arg,
 			    probe->dtpr_id, probe->dtpr_arg) == 0)
 				continue;
 
 #ifdef illumos
 			/*
 			 * This is more subtle than it looks. We have to be
 			 * absolutely certain that CRED() isn't going to
 			 * change out from under us so it's only legit to
 			 * examine that structure if we're in constrained
 			 * situations. Currently, the only times we'll this
 			 * check is if a non-super-user has enabled the
 			 * profile or syscall providers -- providers that
 			 * allow visibility of all processes. For the
 			 * profile case, the check above will ensure that
 			 * we're examining a user context.
 			 */
 			if (ecb->dte_cond & DTRACE_COND_OWNER) {
 				cred_t *cr;
 				cred_t *s_cr =
 				    ecb->dte_state->dts_cred.dcr_cred;
 				proc_t *proc;
 
 				ASSERT(s_cr != NULL);
 
 				if ((cr = CRED()) == NULL ||
 				    s_cr->cr_uid != cr->cr_uid ||
 				    s_cr->cr_uid != cr->cr_ruid ||
 				    s_cr->cr_uid != cr->cr_suid ||
 				    s_cr->cr_gid != cr->cr_gid ||
 				    s_cr->cr_gid != cr->cr_rgid ||
 				    s_cr->cr_gid != cr->cr_sgid ||
 				    (proc = ttoproc(curthread)) == NULL ||
 				    (proc->p_flag & SNOCD))
 					continue;
 			}
 
 			if (ecb->dte_cond & DTRACE_COND_ZONEOWNER) {
 				cred_t *cr;
 				cred_t *s_cr =
 				    ecb->dte_state->dts_cred.dcr_cred;
 
 				ASSERT(s_cr != NULL);
 
 				if ((cr = CRED()) == NULL ||
 				    s_cr->cr_zone->zone_id !=
 				    cr->cr_zone->zone_id)
 					continue;
 			}
 #endif
 		}
 
 		if (now - state->dts_alive > dtrace_deadman_timeout) {
 			/*
 			 * We seem to be dead.  Unless we (a) have kernel
 			 * destructive permissions (b) have explicitly enabled
 			 * destructive actions and (c) destructive actions have
 			 * not been disabled, we're going to transition into
 			 * the KILLED state, from which no further processing
 			 * on this state will be performed.
 			 */
 			if (!dtrace_priv_kernel_destructive(state) ||
 			    !state->dts_cred.dcr_destructive ||
 			    dtrace_destructive_disallow) {
 				void *activity = &state->dts_activity;
 				dtrace_activity_t curstate;
 
 				do {
 					curstate = state->dts_activity;
 				} while (dtrace_cas32(activity, curstate,
 				    DTRACE_ACTIVITY_KILLED) != curstate);
 
 				continue;
 			}
 		}
 
 		if ((offs = dtrace_buffer_reserve(buf, ecb->dte_needed,
 		    ecb->dte_alignment, state, &mstate)) < 0)
 			continue;
 
 		tomax = buf->dtb_tomax;
 		ASSERT(tomax != NULL);
 
 		if (ecb->dte_size != 0) {
 			dtrace_rechdr_t dtrh;
 			if (!(mstate.dtms_present & DTRACE_MSTATE_TIMESTAMP)) {
 				mstate.dtms_timestamp = dtrace_gethrtime();
 				mstate.dtms_present |= DTRACE_MSTATE_TIMESTAMP;
 			}
 			ASSERT3U(ecb->dte_size, >=, sizeof (dtrace_rechdr_t));
 			dtrh.dtrh_epid = ecb->dte_epid;
 			DTRACE_RECORD_STORE_TIMESTAMP(&dtrh,
 			    mstate.dtms_timestamp);
 			*((dtrace_rechdr_t *)(tomax + offs)) = dtrh;
 		}
 
 		mstate.dtms_epid = ecb->dte_epid;
 		mstate.dtms_present |= DTRACE_MSTATE_EPID;
 
 		if (state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL)
 			mstate.dtms_access = DTRACE_ACCESS_KERNEL;
 		else
 			mstate.dtms_access = 0;
 
 		if (pred != NULL) {
 			dtrace_difo_t *dp = pred->dtp_difo;
 			uint64_t rval;
 
 			rval = dtrace_dif_emulate(dp, &mstate, vstate, state);
 
 			if (!(*flags & CPU_DTRACE_ERROR) && !rval) {
 				dtrace_cacheid_t cid = probe->dtpr_predcache;
 
 				if (cid != DTRACE_CACHEIDNONE && !onintr) {
 					/*
 					 * Update the predicate cache...
 					 */
 					ASSERT(cid == pred->dtp_cacheid);
 					curthread->t_predcache = cid;
 				}
 
 				continue;
 			}
 		}
 
 		for (act = ecb->dte_action; !(*flags & CPU_DTRACE_ERROR) &&
 		    act != NULL; act = act->dta_next) {
 			size_t valoffs;
 			dtrace_difo_t *dp;
 			dtrace_recdesc_t *rec = &act->dta_rec;
 
 			size = rec->dtrd_size;
 			valoffs = offs + rec->dtrd_offset;
 
 			if (DTRACEACT_ISAGG(act->dta_kind)) {
 				uint64_t v = 0xbad;
 				dtrace_aggregation_t *agg;
 
 				agg = (dtrace_aggregation_t *)act;
 
 				if ((dp = act->dta_difo) != NULL)
 					v = dtrace_dif_emulate(dp,
 					    &mstate, vstate, state);
 
 				if (*flags & CPU_DTRACE_ERROR)
 					continue;
 
 				/*
 				 * Note that we always pass the expression
 				 * value from the previous iteration of the
 				 * action loop.  This value will only be used
 				 * if there is an expression argument to the
 				 * aggregating action, denoted by the
 				 * dtag_hasarg field.
 				 */
 				dtrace_aggregate(agg, buf,
 				    offs, aggbuf, v, val);
 				continue;
 			}
 
 			switch (act->dta_kind) {
 			case DTRACEACT_STOP:
 				if (dtrace_priv_proc_destructive(state))
 					dtrace_action_stop();
 				continue;
 
 			case DTRACEACT_BREAKPOINT:
 				if (dtrace_priv_kernel_destructive(state))
 					dtrace_action_breakpoint(ecb);
 				continue;
 
 			case DTRACEACT_PANIC:
 				if (dtrace_priv_kernel_destructive(state))
 					dtrace_action_panic(ecb);
 				continue;
 
 			case DTRACEACT_STACK:
 				if (!dtrace_priv_kernel(state))
 					continue;
 
 				dtrace_getpcstack((pc_t *)(tomax + valoffs),
 				    size / sizeof (pc_t), probe->dtpr_aframes,
 				    DTRACE_ANCHORED(probe) ? NULL :
 				    (uint32_t *)arg0);
 				continue;
 
 			case DTRACEACT_JSTACK:
 			case DTRACEACT_USTACK:
 				if (!dtrace_priv_proc(state))
 					continue;
 
 				/*
 				 * See comment in DIF_VAR_PID.
 				 */
 				if (DTRACE_ANCHORED(mstate.dtms_probe) &&
 				    CPU_ON_INTR(CPU)) {
 					int depth = DTRACE_USTACK_NFRAMES(
 					    rec->dtrd_arg) + 1;
 
 					dtrace_bzero((void *)(tomax + valoffs),
 					    DTRACE_USTACK_STRSIZE(rec->dtrd_arg)
 					    + depth * sizeof (uint64_t));
 
 					continue;
 				}
 
 				if (DTRACE_USTACK_STRSIZE(rec->dtrd_arg) != 0 &&
 				    curproc->p_dtrace_helpers != NULL) {
 					/*
 					 * This is the slow path -- we have
 					 * allocated string space, and we're
 					 * getting the stack of a process that
 					 * has helpers.  Call into a separate
 					 * routine to perform this processing.
 					 */
 					dtrace_action_ustack(&mstate, state,
 					    (uint64_t *)(tomax + valoffs),
 					    rec->dtrd_arg);
 					continue;
 				}
 
 				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
 				dtrace_getupcstack((uint64_t *)
 				    (tomax + valoffs),
 				    DTRACE_USTACK_NFRAMES(rec->dtrd_arg) + 1);
 				DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
 				continue;
 
 			default:
 				break;
 			}
 
 			dp = act->dta_difo;
 			ASSERT(dp != NULL);
 
 			val = dtrace_dif_emulate(dp, &mstate, vstate, state);
 
 			if (*flags & CPU_DTRACE_ERROR)
 				continue;
 
 			switch (act->dta_kind) {
 			case DTRACEACT_SPECULATE: {
 				dtrace_rechdr_t *dtrh;
 
 				ASSERT(buf == &state->dts_buffer[cpuid]);
 				buf = dtrace_speculation_buffer(state,
 				    cpuid, val);
 
 				if (buf == NULL) {
 					*flags |= CPU_DTRACE_DROP;
 					continue;
 				}
 
 				offs = dtrace_buffer_reserve(buf,
 				    ecb->dte_needed, ecb->dte_alignment,
 				    state, NULL);
 
 				if (offs < 0) {
 					*flags |= CPU_DTRACE_DROP;
 					continue;
 				}
 
 				tomax = buf->dtb_tomax;
 				ASSERT(tomax != NULL);
 
 				if (ecb->dte_size == 0)
 					continue;
 
 				ASSERT3U(ecb->dte_size, >=,
 				    sizeof (dtrace_rechdr_t));
 				dtrh = ((void *)(tomax + offs));
 				dtrh->dtrh_epid = ecb->dte_epid;
 				/*
 				 * When the speculation is committed, all of
 				 * the records in the speculative buffer will
 				 * have their timestamps set to the commit
 				 * time.  Until then, it is set to a sentinel
 				 * value, for debugability.
 				 */
 				DTRACE_RECORD_STORE_TIMESTAMP(dtrh, UINT64_MAX);
 				continue;
 			}
 
 			case DTRACEACT_PRINTM: {
 				/* The DIF returns a 'memref'. */
 				uintptr_t *memref = (uintptr_t *)(uintptr_t) val;
 
 				/* Get the size from the memref. */
 				size = memref[1];
 
 				/*
 				 * Check if the size exceeds the allocated
 				 * buffer size.
 				 */
 				if (size + sizeof(uintptr_t) > dp->dtdo_rtype.dtdt_size) {
 					/* Flag a drop! */
 					*flags |= CPU_DTRACE_DROP;
 					continue;
 				}
 
 				/* Store the size in the buffer first. */
 				DTRACE_STORE(uintptr_t, tomax,
 				    valoffs, size);
 
 				/*
 				 * Offset the buffer address to the start
 				 * of the data.
 				 */
 				valoffs += sizeof(uintptr_t);
 
 				/*
 				 * Reset to the memory address rather than
 				 * the memref array, then let the BYREF
 				 * code below do the work to store the 
 				 * memory data in the buffer.
 				 */
 				val = memref[0];
 				break;
 			}
 
 			case DTRACEACT_CHILL:
 				if (dtrace_priv_kernel_destructive(state))
 					dtrace_action_chill(&mstate, val);
 				continue;
 
 			case DTRACEACT_RAISE:
 				if (dtrace_priv_proc_destructive(state))
 					dtrace_action_raise(val);
 				continue;
 
 			case DTRACEACT_COMMIT:
 				ASSERT(!committed);
 
 				/*
 				 * We need to commit our buffer state.
 				 */
 				if (ecb->dte_size)
 					buf->dtb_offset = offs + ecb->dte_size;
 				buf = &state->dts_buffer[cpuid];
 				dtrace_speculation_commit(state, cpuid, val);
 				committed = 1;
 				continue;
 
 			case DTRACEACT_DISCARD:
 				dtrace_speculation_discard(state, cpuid, val);
 				continue;
 
 			case DTRACEACT_DIFEXPR:
 			case DTRACEACT_LIBACT:
 			case DTRACEACT_PRINTF:
 			case DTRACEACT_PRINTA:
 			case DTRACEACT_SYSTEM:
 			case DTRACEACT_FREOPEN:
 			case DTRACEACT_TRACEMEM:
 				break;
 
 			case DTRACEACT_TRACEMEM_DYNSIZE:
 				tracememsize = val;
 				break;
 
 			case DTRACEACT_SYM:
 			case DTRACEACT_MOD:
 				if (!dtrace_priv_kernel(state))
 					continue;
 				break;
 
 			case DTRACEACT_USYM:
 			case DTRACEACT_UMOD:
 			case DTRACEACT_UADDR: {
 #ifdef illumos
 				struct pid *pid = curthread->t_procp->p_pidp;
 #endif
 
 				if (!dtrace_priv_proc(state))
 					continue;
 
 				DTRACE_STORE(uint64_t, tomax,
 #ifdef illumos
 				    valoffs, (uint64_t)pid->pid_id);
 #else
 				    valoffs, (uint64_t) curproc->p_pid);
 #endif
 				DTRACE_STORE(uint64_t, tomax,
 				    valoffs + sizeof (uint64_t), val);
 
 				continue;
 			}
 
 			case DTRACEACT_EXIT: {
 				/*
 				 * For the exit action, we are going to attempt
 				 * to atomically set our activity to be
 				 * draining.  If this fails (either because
 				 * another CPU has beat us to the exit action,
 				 * or because our current activity is something
 				 * other than ACTIVE or WARMUP), we will
 				 * continue.  This assures that the exit action
 				 * can be successfully recorded at most once
 				 * when we're in the ACTIVE state.  If we're
 				 * encountering the exit() action while in
 				 * COOLDOWN, however, we want to honor the new
 				 * status code.  (We know that we're the only
 				 * thread in COOLDOWN, so there is no race.)
 				 */
 				void *activity = &state->dts_activity;
 				dtrace_activity_t curstate = state->dts_activity;
 
 				if (curstate == DTRACE_ACTIVITY_COOLDOWN)
 					break;
 
 				if (curstate != DTRACE_ACTIVITY_WARMUP)
 					curstate = DTRACE_ACTIVITY_ACTIVE;
 
 				if (dtrace_cas32(activity, curstate,
 				    DTRACE_ACTIVITY_DRAINING) != curstate) {
 					*flags |= CPU_DTRACE_DROP;
 					continue;
 				}
 
 				break;
 			}
 
 			default:
 				ASSERT(0);
 			}
 
 			if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF ||
 			    dp->dtdo_rtype.dtdt_flags & DIF_TF_BYUREF) {
 				uintptr_t end = valoffs + size;
 
 				if (tracememsize != 0 &&
 				    valoffs + tracememsize < end) {
 					end = valoffs + tracememsize;
 					tracememsize = 0;
 				}
 
 				if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF &&
 				    !dtrace_vcanload((void *)(uintptr_t)val,
 				    &dp->dtdo_rtype, NULL, &mstate, vstate))
 					continue;
 
 				dtrace_store_by_ref(dp, tomax, size, &valoffs,
 				    &val, end, act->dta_intuple,
 				    dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF ?
 				    DIF_TF_BYREF: DIF_TF_BYUREF);
 				continue;
 			}
 
 			switch (size) {
 			case 0:
 				break;
 
 			case sizeof (uint8_t):
 				DTRACE_STORE(uint8_t, tomax, valoffs, val);
 				break;
 			case sizeof (uint16_t):
 				DTRACE_STORE(uint16_t, tomax, valoffs, val);
 				break;
 			case sizeof (uint32_t):
 				DTRACE_STORE(uint32_t, tomax, valoffs, val);
 				break;
 			case sizeof (uint64_t):
 				DTRACE_STORE(uint64_t, tomax, valoffs, val);
 				break;
 			default:
 				/*
 				 * Any other size should have been returned by
 				 * reference, not by value.
 				 */
 				ASSERT(0);
 				break;
 			}
 		}
 
 		if (*flags & CPU_DTRACE_DROP)
 			continue;
 
 		if (*flags & CPU_DTRACE_FAULT) {
 			int ndx;
 			dtrace_action_t *err;
 
 			buf->dtb_errors++;
 
 			if (probe->dtpr_id == dtrace_probeid_error) {
 				/*
 				 * There's nothing we can do -- we had an
 				 * error on the error probe.  We bump an
 				 * error counter to at least indicate that
 				 * this condition happened.
 				 */
 				dtrace_error(&state->dts_dblerrors);
 				continue;
 			}
 
 			if (vtime) {
 				/*
 				 * Before recursing on dtrace_probe(), we
 				 * need to explicitly clear out our start
 				 * time to prevent it from being accumulated
 				 * into t_dtrace_vtime.
 				 */
 				curthread->t_dtrace_start = 0;
 			}
 
 			/*
 			 * Iterate over the actions to figure out which action
 			 * we were processing when we experienced the error.
 			 * Note that act points _past_ the faulting action; if
 			 * act is ecb->dte_action, the fault was in the
 			 * predicate, if it's ecb->dte_action->dta_next it's
 			 * in action #1, and so on.
 			 */
 			for (err = ecb->dte_action, ndx = 0;
 			    err != act; err = err->dta_next, ndx++)
 				continue;
 
 			dtrace_probe_error(state, ecb->dte_epid, ndx,
 			    (mstate.dtms_present & DTRACE_MSTATE_FLTOFFS) ?
 			    mstate.dtms_fltoffs : -1, DTRACE_FLAGS2FLT(*flags),
 			    cpu_core[cpuid].cpuc_dtrace_illval);
 
 			continue;
 		}
 
 		if (!committed)
 			buf->dtb_offset = offs + ecb->dte_size;
 	}
 
 	if (vtime)
 		curthread->t_dtrace_start = dtrace_gethrtime();
 
 	dtrace_probe_exit(cookie);
 }
 
 /*
  * DTrace Probe Hashing Functions
  *
  * The functions in this section (and indeed, the functions in remaining
  * sections) are not _called_ from probe context.  (Any exceptions to this are
  * marked with a "Note:".)  Rather, they are called from elsewhere in the
  * DTrace framework to look-up probes in, add probes to and remove probes from
  * the DTrace probe hashes.  (Each probe is hashed by each element of the
  * probe tuple -- allowing for fast lookups, regardless of what was
  * specified.)
  */
 static uint_t
 dtrace_hash_str(const char *p)
 {
 	unsigned int g;
 	uint_t hval = 0;
 
 	while (*p) {
 		hval = (hval << 4) + *p++;
 		if ((g = (hval & 0xf0000000)) != 0)
 			hval ^= g >> 24;
 		hval &= ~g;
 	}
 	return (hval);
 }
 
 static dtrace_hash_t *
 dtrace_hash_create(uintptr_t stroffs, uintptr_t nextoffs, uintptr_t prevoffs)
 {
 	dtrace_hash_t *hash = kmem_zalloc(sizeof (dtrace_hash_t), KM_SLEEP);
 
 	hash->dth_stroffs = stroffs;
 	hash->dth_nextoffs = nextoffs;
 	hash->dth_prevoffs = prevoffs;
 
 	hash->dth_size = 1;
 	hash->dth_mask = hash->dth_size - 1;
 
 	hash->dth_tab = kmem_zalloc(hash->dth_size *
 	    sizeof (dtrace_hashbucket_t *), KM_SLEEP);
 
 	return (hash);
 }
 
 static void
 dtrace_hash_destroy(dtrace_hash_t *hash)
 {
 #ifdef DEBUG
 	int i;
 
 	for (i = 0; i < hash->dth_size; i++)
 		ASSERT(hash->dth_tab[i] == NULL);
 #endif
 
 	kmem_free(hash->dth_tab,
 	    hash->dth_size * sizeof (dtrace_hashbucket_t *));
 	kmem_free(hash, sizeof (dtrace_hash_t));
 }
 
 static void
 dtrace_hash_resize(dtrace_hash_t *hash)
 {
 	int size = hash->dth_size, i, ndx;
 	int new_size = hash->dth_size << 1;
 	int new_mask = new_size - 1;
 	dtrace_hashbucket_t **new_tab, *bucket, *next;
 
 	ASSERT((new_size & new_mask) == 0);
 
 	new_tab = kmem_zalloc(new_size * sizeof (void *), KM_SLEEP);
 
 	for (i = 0; i < size; i++) {
 		for (bucket = hash->dth_tab[i]; bucket != NULL; bucket = next) {
 			dtrace_probe_t *probe = bucket->dthb_chain;
 
 			ASSERT(probe != NULL);
 			ndx = DTRACE_HASHSTR(hash, probe) & new_mask;
 
 			next = bucket->dthb_next;
 			bucket->dthb_next = new_tab[ndx];
 			new_tab[ndx] = bucket;
 		}
 	}
 
 	kmem_free(hash->dth_tab, hash->dth_size * sizeof (void *));
 	hash->dth_tab = new_tab;
 	hash->dth_size = new_size;
 	hash->dth_mask = new_mask;
 }
 
 static void
 dtrace_hash_add(dtrace_hash_t *hash, dtrace_probe_t *new)
 {
 	int hashval = DTRACE_HASHSTR(hash, new);
 	int ndx = hashval & hash->dth_mask;
 	dtrace_hashbucket_t *bucket = hash->dth_tab[ndx];
 	dtrace_probe_t **nextp, **prevp;
 
 	for (; bucket != NULL; bucket = bucket->dthb_next) {
 		if (DTRACE_HASHEQ(hash, bucket->dthb_chain, new))
 			goto add;
 	}
 
 	if ((hash->dth_nbuckets >> 1) > hash->dth_size) {
 		dtrace_hash_resize(hash);
 		dtrace_hash_add(hash, new);
 		return;
 	}
 
 	bucket = kmem_zalloc(sizeof (dtrace_hashbucket_t), KM_SLEEP);
 	bucket->dthb_next = hash->dth_tab[ndx];
 	hash->dth_tab[ndx] = bucket;
 	hash->dth_nbuckets++;
 
 add:
 	nextp = DTRACE_HASHNEXT(hash, new);
 	ASSERT(*nextp == NULL && *(DTRACE_HASHPREV(hash, new)) == NULL);
 	*nextp = bucket->dthb_chain;
 
 	if (bucket->dthb_chain != NULL) {
 		prevp = DTRACE_HASHPREV(hash, bucket->dthb_chain);
 		ASSERT(*prevp == NULL);
 		*prevp = new;
 	}
 
 	bucket->dthb_chain = new;
 	bucket->dthb_len++;
 }
 
 static dtrace_probe_t *
 dtrace_hash_lookup(dtrace_hash_t *hash, dtrace_probe_t *template)
 {
 	int hashval = DTRACE_HASHSTR(hash, template);
 	int ndx = hashval & hash->dth_mask;
 	dtrace_hashbucket_t *bucket = hash->dth_tab[ndx];
 
 	for (; bucket != NULL; bucket = bucket->dthb_next) {
 		if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template))
 			return (bucket->dthb_chain);
 	}
 
 	return (NULL);
 }
 
 static int
 dtrace_hash_collisions(dtrace_hash_t *hash, dtrace_probe_t *template)
 {
 	int hashval = DTRACE_HASHSTR(hash, template);
 	int ndx = hashval & hash->dth_mask;
 	dtrace_hashbucket_t *bucket = hash->dth_tab[ndx];
 
 	for (; bucket != NULL; bucket = bucket->dthb_next) {
 		if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template))
 			return (bucket->dthb_len);
 	}
 
 	return (0);
 }
 
 static void
 dtrace_hash_remove(dtrace_hash_t *hash, dtrace_probe_t *probe)
 {
 	int ndx = DTRACE_HASHSTR(hash, probe) & hash->dth_mask;
 	dtrace_hashbucket_t *bucket = hash->dth_tab[ndx];
 
 	dtrace_probe_t **prevp = DTRACE_HASHPREV(hash, probe);
 	dtrace_probe_t **nextp = DTRACE_HASHNEXT(hash, probe);
 
 	/*
 	 * Find the bucket that we're removing this probe from.
 	 */
 	for (; bucket != NULL; bucket = bucket->dthb_next) {
 		if (DTRACE_HASHEQ(hash, bucket->dthb_chain, probe))
 			break;
 	}
 
 	ASSERT(bucket != NULL);
 
 	if (*prevp == NULL) {
 		if (*nextp == NULL) {
 			/*
 			 * The removed probe was the only probe on this
 			 * bucket; we need to remove the bucket.
 			 */
 			dtrace_hashbucket_t *b = hash->dth_tab[ndx];
 
 			ASSERT(bucket->dthb_chain == probe);
 			ASSERT(b != NULL);
 
 			if (b == bucket) {
 				hash->dth_tab[ndx] = bucket->dthb_next;
 			} else {
 				while (b->dthb_next != bucket)
 					b = b->dthb_next;
 				b->dthb_next = bucket->dthb_next;
 			}
 
 			ASSERT(hash->dth_nbuckets > 0);
 			hash->dth_nbuckets--;
 			kmem_free(bucket, sizeof (dtrace_hashbucket_t));
 			return;
 		}
 
 		bucket->dthb_chain = *nextp;
 	} else {
 		*(DTRACE_HASHNEXT(hash, *prevp)) = *nextp;
 	}
 
 	if (*nextp != NULL)
 		*(DTRACE_HASHPREV(hash, *nextp)) = *prevp;
 }
 
 /*
  * DTrace Utility Functions
  *
  * These are random utility functions that are _not_ called from probe context.
  */
 static int
 dtrace_badattr(const dtrace_attribute_t *a)
 {
 	return (a->dtat_name > DTRACE_STABILITY_MAX ||
 	    a->dtat_data > DTRACE_STABILITY_MAX ||
 	    a->dtat_class > DTRACE_CLASS_MAX);
 }
 
 /*
  * Return a duplicate copy of a string.  If the specified string is NULL,
  * this function returns a zero-length string.
  */
 static char *
 dtrace_strdup(const char *str)
 {
 	char *new = kmem_zalloc((str != NULL ? strlen(str) : 0) + 1, KM_SLEEP);
 
 	if (str != NULL)
 		(void) strcpy(new, str);
 
 	return (new);
 }
 
 #define	DTRACE_ISALPHA(c)	\
 	(((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
 
 static int
 dtrace_badname(const char *s)
 {
 	char c;
 
 	if (s == NULL || (c = *s++) == '\0')
 		return (0);
 
 	if (!DTRACE_ISALPHA(c) && c != '-' && c != '_' && c != '.')
 		return (1);
 
 	while ((c = *s++) != '\0') {
 		if (!DTRACE_ISALPHA(c) && (c < '0' || c > '9') &&
 		    c != '-' && c != '_' && c != '.' && c != '`')
 			return (1);
 	}
 
 	return (0);
 }
 
 static void
 dtrace_cred2priv(cred_t *cr, uint32_t *privp, uid_t *uidp, zoneid_t *zoneidp)
 {
 	uint32_t priv;
 
 #ifdef illumos
 	if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) {
 		/*
 		 * For DTRACE_PRIV_ALL, the uid and zoneid don't matter.
 		 */
 		priv = DTRACE_PRIV_ALL;
 	} else {
 		*uidp = crgetuid(cr);
 		*zoneidp = crgetzoneid(cr);
 
 		priv = 0;
 		if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE))
 			priv |= DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER;
 		else if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE))
 			priv |= DTRACE_PRIV_USER;
 		if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE))
 			priv |= DTRACE_PRIV_PROC;
 		if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE))
 			priv |= DTRACE_PRIV_OWNER;
 		if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE))
 			priv |= DTRACE_PRIV_ZONEOWNER;
 	}
 #else
 	priv = DTRACE_PRIV_ALL;
 #endif
 
 	*privp = priv;
 }
 
 #ifdef DTRACE_ERRDEBUG
 static void
 dtrace_errdebug(const char *str)
 {
 	int hval = dtrace_hash_str(str) % DTRACE_ERRHASHSZ;
 	int occupied = 0;
 
 	mutex_enter(&dtrace_errlock);
 	dtrace_errlast = str;
 	dtrace_errthread = curthread;
 
 	while (occupied++ < DTRACE_ERRHASHSZ) {
 		if (dtrace_errhash[hval].dter_msg == str) {
 			dtrace_errhash[hval].dter_count++;
 			goto out;
 		}
 
 		if (dtrace_errhash[hval].dter_msg != NULL) {
 			hval = (hval + 1) % DTRACE_ERRHASHSZ;
 			continue;
 		}
 
 		dtrace_errhash[hval].dter_msg = str;
 		dtrace_errhash[hval].dter_count = 1;
 		goto out;
 	}
 
 	panic("dtrace: undersized error hash");
 out:
 	mutex_exit(&dtrace_errlock);
 }
 #endif
 
 /*
  * DTrace Matching Functions
  *
  * These functions are used to match groups of probes, given some elements of
  * a probe tuple, or some globbed expressions for elements of a probe tuple.
  */
 static int
 dtrace_match_priv(const dtrace_probe_t *prp, uint32_t priv, uid_t uid,
     zoneid_t zoneid)
 {
 	if (priv != DTRACE_PRIV_ALL) {
 		uint32_t ppriv = prp->dtpr_provider->dtpv_priv.dtpp_flags;
 		uint32_t match = priv & ppriv;
 
 		/*
 		 * No PRIV_DTRACE_* privileges...
 		 */
 		if ((priv & (DTRACE_PRIV_PROC | DTRACE_PRIV_USER |
 		    DTRACE_PRIV_KERNEL)) == 0)
 			return (0);
 
 		/*
 		 * No matching bits, but there were bits to match...
 		 */
 		if (match == 0 && ppriv != 0)
 			return (0);
 
 		/*
 		 * Need to have permissions to the process, but don't...
 		 */
 		if (((ppriv & ~match) & DTRACE_PRIV_OWNER) != 0 &&
 		    uid != prp->dtpr_provider->dtpv_priv.dtpp_uid) {
 			return (0);
 		}
 
 		/*
 		 * Need to be in the same zone unless we possess the
 		 * privilege to examine all zones.
 		 */
 		if (((ppriv & ~match) & DTRACE_PRIV_ZONEOWNER) != 0 &&
 		    zoneid != prp->dtpr_provider->dtpv_priv.dtpp_zoneid) {
 			return (0);
 		}
 	}
 
 	return (1);
 }
 
 /*
  * dtrace_match_probe compares a dtrace_probe_t to a pre-compiled key, which
  * consists of input pattern strings and an ops-vector to evaluate them.
  * This function returns >0 for match, 0 for no match, and <0 for error.
  */
 static int
 dtrace_match_probe(const dtrace_probe_t *prp, const dtrace_probekey_t *pkp,
     uint32_t priv, uid_t uid, zoneid_t zoneid)
 {
 	dtrace_provider_t *pvp = prp->dtpr_provider;
 	int rv;
 
 	if (pvp->dtpv_defunct)
 		return (0);
 
 	if ((rv = pkp->dtpk_pmatch(pvp->dtpv_name, pkp->dtpk_prov, 0)) <= 0)
 		return (rv);
 
 	if ((rv = pkp->dtpk_mmatch(prp->dtpr_mod, pkp->dtpk_mod, 0)) <= 0)
 		return (rv);
 
 	if ((rv = pkp->dtpk_fmatch(prp->dtpr_func, pkp->dtpk_func, 0)) <= 0)
 		return (rv);
 
 	if ((rv = pkp->dtpk_nmatch(prp->dtpr_name, pkp->dtpk_name, 0)) <= 0)
 		return (rv);
 
 	if (dtrace_match_priv(prp, priv, uid, zoneid) == 0)
 		return (0);
 
 	return (rv);
 }
 
 /*
  * dtrace_match_glob() is a safe kernel implementation of the gmatch(3GEN)
  * interface for matching a glob pattern 'p' to an input string 's'.  Unlike
  * libc's version, the kernel version only applies to 8-bit ASCII strings.
  * In addition, all of the recursion cases except for '*' matching have been
  * unwound.  For '*', we still implement recursive evaluation, but a depth
  * counter is maintained and matching is aborted if we recurse too deep.
  * The function returns 0 if no match, >0 if match, and <0 if recursion error.
  */
 static int
 dtrace_match_glob(const char *s, const char *p, int depth)
 {
 	const char *olds;
 	char s1, c;
 	int gs;
 
 	if (depth > DTRACE_PROBEKEY_MAXDEPTH)
 		return (-1);
 
 	if (s == NULL)
 		s = ""; /* treat NULL as empty string */
 
 top:
 	olds = s;
 	s1 = *s++;
 
 	if (p == NULL)
 		return (0);
 
 	if ((c = *p++) == '\0')
 		return (s1 == '\0');
 
 	switch (c) {
 	case '[': {
 		int ok = 0, notflag = 0;
 		char lc = '\0';
 
 		if (s1 == '\0')
 			return (0);
 
 		if (*p == '!') {
 			notflag = 1;
 			p++;
 		}
 
 		if ((c = *p++) == '\0')
 			return (0);
 
 		do {
 			if (c == '-' && lc != '\0' && *p != ']') {
 				if ((c = *p++) == '\0')
 					return (0);
 				if (c == '\\' && (c = *p++) == '\0')
 					return (0);
 
 				if (notflag) {
 					if (s1 < lc || s1 > c)
 						ok++;
 					else
 						return (0);
 				} else if (lc <= s1 && s1 <= c)
 					ok++;
 
 			} else if (c == '\\' && (c = *p++) == '\0')
 				return (0);
 
 			lc = c; /* save left-hand 'c' for next iteration */
 
 			if (notflag) {
 				if (s1 != c)
 					ok++;
 				else
 					return (0);
 			} else if (s1 == c)
 				ok++;
 
 			if ((c = *p++) == '\0')
 				return (0);
 
 		} while (c != ']');
 
 		if (ok)
 			goto top;
 
 		return (0);
 	}
 
 	case '\\':
 		if ((c = *p++) == '\0')
 			return (0);
 		/*FALLTHRU*/
 
 	default:
 		if (c != s1)
 			return (0);
 		/*FALLTHRU*/
 
 	case '?':
 		if (s1 != '\0')
 			goto top;
 		return (0);
 
 	case '*':
 		while (*p == '*')
 			p++; /* consecutive *'s are identical to a single one */
 
 		if (*p == '\0')
 			return (1);
 
 		for (s = olds; *s != '\0'; s++) {
 			if ((gs = dtrace_match_glob(s, p, depth + 1)) != 0)
 				return (gs);
 		}
 
 		return (0);
 	}
 }
 
 /*ARGSUSED*/
 static int
 dtrace_match_string(const char *s, const char *p, int depth)
 {
 	return (s != NULL && strcmp(s, p) == 0);
 }
 
 /*ARGSUSED*/
 static int
 dtrace_match_nul(const char *s, const char *p, int depth)
 {
 	return (1); /* always match the empty pattern */
 }
 
 /*ARGSUSED*/
 static int
 dtrace_match_nonzero(const char *s, const char *p, int depth)
 {
 	return (s != NULL && s[0] != '\0');
 }
 
 static int
 dtrace_match(const dtrace_probekey_t *pkp, uint32_t priv, uid_t uid,
     zoneid_t zoneid, int (*matched)(dtrace_probe_t *, void *), void *arg)
 {
 	dtrace_probe_t template, *probe;
 	dtrace_hash_t *hash = NULL;
 	int len, best = INT_MAX, nmatched = 0;
 	dtrace_id_t i;
 
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 
 	/*
 	 * If the probe ID is specified in the key, just lookup by ID and
 	 * invoke the match callback once if a matching probe is found.
 	 */
 	if (pkp->dtpk_id != DTRACE_IDNONE) {
 		if ((probe = dtrace_probe_lookup_id(pkp->dtpk_id)) != NULL &&
 		    dtrace_match_probe(probe, pkp, priv, uid, zoneid) > 0) {
 			(void) (*matched)(probe, arg);
 			nmatched++;
 		}
 		return (nmatched);
 	}
 
 	template.dtpr_mod = (char *)pkp->dtpk_mod;
 	template.dtpr_func = (char *)pkp->dtpk_func;
 	template.dtpr_name = (char *)pkp->dtpk_name;
 
 	/*
 	 * We want to find the most distinct of the module name, function
 	 * name, and name.  So for each one that is not a glob pattern or
 	 * empty string, we perform a lookup in the corresponding hash and
 	 * use the hash table with the fewest collisions to do our search.
 	 */
 	if (pkp->dtpk_mmatch == &dtrace_match_string &&
 	    (len = dtrace_hash_collisions(dtrace_bymod, &template)) < best) {
 		best = len;
 		hash = dtrace_bymod;
 	}
 
 	if (pkp->dtpk_fmatch == &dtrace_match_string &&
 	    (len = dtrace_hash_collisions(dtrace_byfunc, &template)) < best) {
 		best = len;
 		hash = dtrace_byfunc;
 	}
 
 	if (pkp->dtpk_nmatch == &dtrace_match_string &&
 	    (len = dtrace_hash_collisions(dtrace_byname, &template)) < best) {
 		best = len;
 		hash = dtrace_byname;
 	}
 
 	/*
 	 * If we did not select a hash table, iterate over every probe and
 	 * invoke our callback for each one that matches our input probe key.
 	 */
 	if (hash == NULL) {
 		for (i = 0; i < dtrace_nprobes; i++) {
 			if ((probe = dtrace_probes[i]) == NULL ||
 			    dtrace_match_probe(probe, pkp, priv, uid,
 			    zoneid) <= 0)
 				continue;
 
 			nmatched++;
 
 			if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT)
 				break;
 		}
 
 		return (nmatched);
 	}
 
 	/*
 	 * If we selected a hash table, iterate over each probe of the same key
 	 * name and invoke the callback for every probe that matches the other
 	 * attributes of our input probe key.
 	 */
 	for (probe = dtrace_hash_lookup(hash, &template); probe != NULL;
 	    probe = *(DTRACE_HASHNEXT(hash, probe))) {
 
 		if (dtrace_match_probe(probe, pkp, priv, uid, zoneid) <= 0)
 			continue;
 
 		nmatched++;
 
 		if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT)
 			break;
 	}
 
 	return (nmatched);
 }
 
 /*
  * Return the function pointer dtrace_probecmp() should use to compare the
  * specified pattern with a string.  For NULL or empty patterns, we select
  * dtrace_match_nul().  For glob pattern strings, we use dtrace_match_glob().
  * For non-empty non-glob strings, we use dtrace_match_string().
  */
 static dtrace_probekey_f *
 dtrace_probekey_func(const char *p)
 {
 	char c;
 
 	if (p == NULL || *p == '\0')
 		return (&dtrace_match_nul);
 
 	while ((c = *p++) != '\0') {
 		if (c == '[' || c == '?' || c == '*' || c == '\\')
 			return (&dtrace_match_glob);
 	}
 
 	return (&dtrace_match_string);
 }
 
 /*
  * Build a probe comparison key for use with dtrace_match_probe() from the
  * given probe description.  By convention, a null key only matches anchored
  * probes: if each field is the empty string, reset dtpk_fmatch to
  * dtrace_match_nonzero().
  */
 static void
 dtrace_probekey(dtrace_probedesc_t *pdp, dtrace_probekey_t *pkp)
 {
 	pkp->dtpk_prov = pdp->dtpd_provider;
 	pkp->dtpk_pmatch = dtrace_probekey_func(pdp->dtpd_provider);
 
 	pkp->dtpk_mod = pdp->dtpd_mod;
 	pkp->dtpk_mmatch = dtrace_probekey_func(pdp->dtpd_mod);
 
 	pkp->dtpk_func = pdp->dtpd_func;
 	pkp->dtpk_fmatch = dtrace_probekey_func(pdp->dtpd_func);
 
 	pkp->dtpk_name = pdp->dtpd_name;
 	pkp->dtpk_nmatch = dtrace_probekey_func(pdp->dtpd_name);
 
 	pkp->dtpk_id = pdp->dtpd_id;
 
 	if (pkp->dtpk_id == DTRACE_IDNONE &&
 	    pkp->dtpk_pmatch == &dtrace_match_nul &&
 	    pkp->dtpk_mmatch == &dtrace_match_nul &&
 	    pkp->dtpk_fmatch == &dtrace_match_nul &&
 	    pkp->dtpk_nmatch == &dtrace_match_nul)
 		pkp->dtpk_fmatch = &dtrace_match_nonzero;
 }
 
 /*
  * DTrace Provider-to-Framework API Functions
  *
  * These functions implement much of the Provider-to-Framework API, as
  * described in <sys/dtrace.h>.  The parts of the API not in this section are
  * the functions in the API for probe management (found below), and
  * dtrace_probe() itself (found above).
  */
 
 /*
  * Register the calling provider with the DTrace framework.  This should
  * generally be called by DTrace providers in their attach(9E) entry point.
  */
 int
 dtrace_register(const char *name, const dtrace_pattr_t *pap, uint32_t priv,
     cred_t *cr, const dtrace_pops_t *pops, void *arg, dtrace_provider_id_t *idp)
 {
 	dtrace_provider_t *provider;
 
 	if (name == NULL || pap == NULL || pops == NULL || idp == NULL) {
 		cmn_err(CE_WARN, "failed to register provider '%s': invalid "
 		    "arguments", name ? name : "<NULL>");
 		return (EINVAL);
 	}
 
 	if (name[0] == '\0' || dtrace_badname(name)) {
 		cmn_err(CE_WARN, "failed to register provider '%s': invalid "
 		    "provider name", name);
 		return (EINVAL);
 	}
 
 	if ((pops->dtps_provide == NULL && pops->dtps_provide_module == NULL) ||
 	    pops->dtps_enable == NULL || pops->dtps_disable == NULL ||
 	    pops->dtps_destroy == NULL ||
 	    ((pops->dtps_resume == NULL) != (pops->dtps_suspend == NULL))) {
 		cmn_err(CE_WARN, "failed to register provider '%s': invalid "
 		    "provider ops", name);
 		return (EINVAL);
 	}
 
 	if (dtrace_badattr(&pap->dtpa_provider) ||
 	    dtrace_badattr(&pap->dtpa_mod) ||
 	    dtrace_badattr(&pap->dtpa_func) ||
 	    dtrace_badattr(&pap->dtpa_name) ||
 	    dtrace_badattr(&pap->dtpa_args)) {
 		cmn_err(CE_WARN, "failed to register provider '%s': invalid "
 		    "provider attributes", name);
 		return (EINVAL);
 	}
 
 	if (priv & ~DTRACE_PRIV_ALL) {
 		cmn_err(CE_WARN, "failed to register provider '%s': invalid "
 		    "privilege attributes", name);
 		return (EINVAL);
 	}
 
 	if ((priv & DTRACE_PRIV_KERNEL) &&
 	    (priv & (DTRACE_PRIV_USER | DTRACE_PRIV_OWNER)) &&
 	    pops->dtps_usermode == NULL) {
 		cmn_err(CE_WARN, "failed to register provider '%s': need "
 		    "dtps_usermode() op for given privilege attributes", name);
 		return (EINVAL);
 	}
 
 	provider = kmem_zalloc(sizeof (dtrace_provider_t), KM_SLEEP);
 	provider->dtpv_name = kmem_alloc(strlen(name) + 1, KM_SLEEP);
 	(void) strcpy(provider->dtpv_name, name);
 
 	provider->dtpv_attr = *pap;
 	provider->dtpv_priv.dtpp_flags = priv;
 	if (cr != NULL) {
 		provider->dtpv_priv.dtpp_uid = crgetuid(cr);
 		provider->dtpv_priv.dtpp_zoneid = crgetzoneid(cr);
 	}
 	provider->dtpv_pops = *pops;
 
 	if (pops->dtps_provide == NULL) {
 		ASSERT(pops->dtps_provide_module != NULL);
 		provider->dtpv_pops.dtps_provide =
 		    (void (*)(void *, dtrace_probedesc_t *))dtrace_nullop;
 	}
 
 	if (pops->dtps_provide_module == NULL) {
 		ASSERT(pops->dtps_provide != NULL);
 		provider->dtpv_pops.dtps_provide_module =
 		    (void (*)(void *, modctl_t *))dtrace_nullop;
 	}
 
 	if (pops->dtps_suspend == NULL) {
 		ASSERT(pops->dtps_resume == NULL);
 		provider->dtpv_pops.dtps_suspend =
 		    (void (*)(void *, dtrace_id_t, void *))dtrace_nullop;
 		provider->dtpv_pops.dtps_resume =
 		    (void (*)(void *, dtrace_id_t, void *))dtrace_nullop;
 	}
 
 	provider->dtpv_arg = arg;
 	*idp = (dtrace_provider_id_t)provider;
 
 	if (pops == &dtrace_provider_ops) {
 		ASSERT(MUTEX_HELD(&dtrace_provider_lock));
 		ASSERT(MUTEX_HELD(&dtrace_lock));
 		ASSERT(dtrace_anon.dta_enabling == NULL);
 
 		/*
 		 * We make sure that the DTrace provider is at the head of
 		 * the provider chain.
 		 */
 		provider->dtpv_next = dtrace_provider;
 		dtrace_provider = provider;
 		return (0);
 	}
 
 	mutex_enter(&dtrace_provider_lock);
 	mutex_enter(&dtrace_lock);
 
 	/*
 	 * If there is at least one provider registered, we'll add this
 	 * provider after the first provider.
 	 */
 	if (dtrace_provider != NULL) {
 		provider->dtpv_next = dtrace_provider->dtpv_next;
 		dtrace_provider->dtpv_next = provider;
 	} else {
 		dtrace_provider = provider;
 	}
 
 	if (dtrace_retained != NULL) {
 		dtrace_enabling_provide(provider);
 
 		/*
 		 * Now we need to call dtrace_enabling_matchall() -- which
 		 * will acquire cpu_lock and dtrace_lock.  We therefore need
 		 * to drop all of our locks before calling into it...
 		 */
 		mutex_exit(&dtrace_lock);
 		mutex_exit(&dtrace_provider_lock);
 		dtrace_enabling_matchall();
 
 		return (0);
 	}
 
 	mutex_exit(&dtrace_lock);
 	mutex_exit(&dtrace_provider_lock);
 
 	return (0);
 }
 
 /*
  * Unregister the specified provider from the DTrace framework.  This should
  * generally be called by DTrace providers in their detach(9E) entry point.
  */
 int
 dtrace_unregister(dtrace_provider_id_t id)
 {
 	dtrace_provider_t *old = (dtrace_provider_t *)id;
 	dtrace_provider_t *prev = NULL;
 	int i, self = 0, noreap = 0;
 	dtrace_probe_t *probe, *first = NULL;
 
 	if (old->dtpv_pops.dtps_enable ==
 	    (void (*)(void *, dtrace_id_t, void *))dtrace_nullop) {
 		/*
 		 * If DTrace itself is the provider, we're called with locks
 		 * already held.
 		 */
 		ASSERT(old == dtrace_provider);
 #ifdef illumos
 		ASSERT(dtrace_devi != NULL);
 #endif
 		ASSERT(MUTEX_HELD(&dtrace_provider_lock));
 		ASSERT(MUTEX_HELD(&dtrace_lock));
 		self = 1;
 
 		if (dtrace_provider->dtpv_next != NULL) {
 			/*
 			 * There's another provider here; return failure.
 			 */
 			return (EBUSY);
 		}
 	} else {
 		mutex_enter(&dtrace_provider_lock);
 #ifdef illumos
 		mutex_enter(&mod_lock);
 #endif
 		mutex_enter(&dtrace_lock);
 	}
 
 	/*
 	 * If anyone has /dev/dtrace open, or if there are anonymous enabled
 	 * probes, we refuse to let providers slither away, unless this
 	 * provider has already been explicitly invalidated.
 	 */
 	if (!old->dtpv_defunct &&
 	    (dtrace_opens || (dtrace_anon.dta_state != NULL &&
 	    dtrace_anon.dta_state->dts_necbs > 0))) {
 		if (!self) {
 			mutex_exit(&dtrace_lock);
 #ifdef illumos
 			mutex_exit(&mod_lock);
 #endif
 			mutex_exit(&dtrace_provider_lock);
 		}
 		return (EBUSY);
 	}
 
 	/*
 	 * Attempt to destroy the probes associated with this provider.
 	 */
 	for (i = 0; i < dtrace_nprobes; i++) {
 		if ((probe = dtrace_probes[i]) == NULL)
 			continue;
 
 		if (probe->dtpr_provider != old)
 			continue;
 
 		if (probe->dtpr_ecb == NULL)
 			continue;
 
 		/*
 		 * If we are trying to unregister a defunct provider, and the
 		 * provider was made defunct within the interval dictated by
 		 * dtrace_unregister_defunct_reap, we'll (asynchronously)
 		 * attempt to reap our enablings.  To denote that the provider
 		 * should reattempt to unregister itself at some point in the
 		 * future, we will return a differentiable error code (EAGAIN
 		 * instead of EBUSY) in this case.
 		 */
 		if (dtrace_gethrtime() - old->dtpv_defunct >
 		    dtrace_unregister_defunct_reap)
 			noreap = 1;
 
 		if (!self) {
 			mutex_exit(&dtrace_lock);
 #ifdef illumos
 			mutex_exit(&mod_lock);
 #endif
 			mutex_exit(&dtrace_provider_lock);
 		}
 
 		if (noreap)
 			return (EBUSY);
 
 		(void) taskq_dispatch(dtrace_taskq,
 		    (task_func_t *)dtrace_enabling_reap, NULL, TQ_SLEEP);
 
 		return (EAGAIN);
 	}
 
 	/*
 	 * All of the probes for this provider are disabled; we can safely
 	 * remove all of them from their hash chains and from the probe array.
 	 */
 	for (i = 0; i < dtrace_nprobes; i++) {
 		if ((probe = dtrace_probes[i]) == NULL)
 			continue;
 
 		if (probe->dtpr_provider != old)
 			continue;
 
 		dtrace_probes[i] = NULL;
 
 		dtrace_hash_remove(dtrace_bymod, probe);
 		dtrace_hash_remove(dtrace_byfunc, probe);
 		dtrace_hash_remove(dtrace_byname, probe);
 
 		if (first == NULL) {
 			first = probe;
 			probe->dtpr_nextmod = NULL;
 		} else {
 			probe->dtpr_nextmod = first;
 			first = probe;
 		}
 	}
 
 	/*
 	 * The provider's probes have been removed from the hash chains and
 	 * from the probe array.  Now issue a dtrace_sync() to be sure that
 	 * everyone has cleared out from any probe array processing.
 	 */
 	dtrace_sync();
 
 	for (probe = first; probe != NULL; probe = first) {
 		first = probe->dtpr_nextmod;
 
 		old->dtpv_pops.dtps_destroy(old->dtpv_arg, probe->dtpr_id,
 		    probe->dtpr_arg);
 		kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1);
 		kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1);
 		kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1);
 #ifdef illumos
 		vmem_free(dtrace_arena, (void *)(uintptr_t)(probe->dtpr_id), 1);
 #else
 		free_unr(dtrace_arena, probe->dtpr_id);
 #endif
 		kmem_free(probe, sizeof (dtrace_probe_t));
 	}
 
 	if ((prev = dtrace_provider) == old) {
 #ifdef illumos
 		ASSERT(self || dtrace_devi == NULL);
 		ASSERT(old->dtpv_next == NULL || dtrace_devi == NULL);
 #endif
 		dtrace_provider = old->dtpv_next;
 	} else {
 		while (prev != NULL && prev->dtpv_next != old)
 			prev = prev->dtpv_next;
 
 		if (prev == NULL) {
 			panic("attempt to unregister non-existent "
 			    "dtrace provider %p\n", (void *)id);
 		}
 
 		prev->dtpv_next = old->dtpv_next;
 	}
 
 	if (!self) {
 		mutex_exit(&dtrace_lock);
 #ifdef illumos
 		mutex_exit(&mod_lock);
 #endif
 		mutex_exit(&dtrace_provider_lock);
 	}
 
 	kmem_free(old->dtpv_name, strlen(old->dtpv_name) + 1);
 	kmem_free(old, sizeof (dtrace_provider_t));
 
 	return (0);
 }
 
 /*
  * Invalidate the specified provider.  All subsequent probe lookups for the
  * specified provider will fail, but its probes will not be removed.
  */
 void
 dtrace_invalidate(dtrace_provider_id_t id)
 {
 	dtrace_provider_t *pvp = (dtrace_provider_t *)id;
 
 	ASSERT(pvp->dtpv_pops.dtps_enable !=
 	    (void (*)(void *, dtrace_id_t, void *))dtrace_nullop);
 
 	mutex_enter(&dtrace_provider_lock);
 	mutex_enter(&dtrace_lock);
 
 	pvp->dtpv_defunct = dtrace_gethrtime();
 
 	mutex_exit(&dtrace_lock);
 	mutex_exit(&dtrace_provider_lock);
 }
 
 /*
  * Indicate whether or not DTrace has attached.
  */
 int
 dtrace_attached(void)
 {
 	/*
 	 * dtrace_provider will be non-NULL iff the DTrace driver has
 	 * attached.  (It's non-NULL because DTrace is always itself a
 	 * provider.)
 	 */
 	return (dtrace_provider != NULL);
 }
 
 /*
  * Remove all the unenabled probes for the given provider.  This function is
  * not unlike dtrace_unregister(), except that it doesn't remove the provider
  * -- just as many of its associated probes as it can.
  */
 int
 dtrace_condense(dtrace_provider_id_t id)
 {
 	dtrace_provider_t *prov = (dtrace_provider_t *)id;
 	int i;
 	dtrace_probe_t *probe;
 
 	/*
 	 * Make sure this isn't the dtrace provider itself.
 	 */
 	ASSERT(prov->dtpv_pops.dtps_enable !=
 	    (void (*)(void *, dtrace_id_t, void *))dtrace_nullop);
 
 	mutex_enter(&dtrace_provider_lock);
 	mutex_enter(&dtrace_lock);
 
 	/*
 	 * Attempt to destroy the probes associated with this provider.
 	 */
 	for (i = 0; i < dtrace_nprobes; i++) {
 		if ((probe = dtrace_probes[i]) == NULL)
 			continue;
 
 		if (probe->dtpr_provider != prov)
 			continue;
 
 		if (probe->dtpr_ecb != NULL)
 			continue;
 
 		dtrace_probes[i] = NULL;
 
 		dtrace_hash_remove(dtrace_bymod, probe);
 		dtrace_hash_remove(dtrace_byfunc, probe);
 		dtrace_hash_remove(dtrace_byname, probe);
 
 		prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, i + 1,
 		    probe->dtpr_arg);
 		kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1);
 		kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1);
 		kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1);
 		kmem_free(probe, sizeof (dtrace_probe_t));
 #ifdef illumos
 		vmem_free(dtrace_arena, (void *)((uintptr_t)i + 1), 1);
 #else
 		free_unr(dtrace_arena, i + 1);
 #endif
 	}
 
 	mutex_exit(&dtrace_lock);
 	mutex_exit(&dtrace_provider_lock);
 
 	return (0);
 }
 
 /*
  * DTrace Probe Management Functions
  *
  * The functions in this section perform the DTrace probe management,
  * including functions to create probes, look-up probes, and call into the
  * providers to request that probes be provided.  Some of these functions are
  * in the Provider-to-Framework API; these functions can be identified by the
  * fact that they are not declared "static".
  */
 
 /*
  * Create a probe with the specified module name, function name, and name.
  */
 dtrace_id_t
 dtrace_probe_create(dtrace_provider_id_t prov, const char *mod,
     const char *func, const char *name, int aframes, void *arg)
 {
 	dtrace_probe_t *probe, **probes;
 	dtrace_provider_t *provider = (dtrace_provider_t *)prov;
 	dtrace_id_t id;
 
 	if (provider == dtrace_provider) {
 		ASSERT(MUTEX_HELD(&dtrace_lock));
 	} else {
 		mutex_enter(&dtrace_lock);
 	}
 
 #ifdef illumos
 	id = (dtrace_id_t)(uintptr_t)vmem_alloc(dtrace_arena, 1,
 	    VM_BESTFIT | VM_SLEEP);
 #else
 	id = alloc_unr(dtrace_arena);
 #endif
 	probe = kmem_zalloc(sizeof (dtrace_probe_t), KM_SLEEP);
 
 	probe->dtpr_id = id;
 	probe->dtpr_gen = dtrace_probegen++;
 	probe->dtpr_mod = dtrace_strdup(mod);
 	probe->dtpr_func = dtrace_strdup(func);
 	probe->dtpr_name = dtrace_strdup(name);
 	probe->dtpr_arg = arg;
 	probe->dtpr_aframes = aframes;
 	probe->dtpr_provider = provider;
 
 	dtrace_hash_add(dtrace_bymod, probe);
 	dtrace_hash_add(dtrace_byfunc, probe);
 	dtrace_hash_add(dtrace_byname, probe);
 
 	if (id - 1 >= dtrace_nprobes) {
 		size_t osize = dtrace_nprobes * sizeof (dtrace_probe_t *);
 		size_t nsize = osize << 1;
 
 		if (nsize == 0) {
 			ASSERT(osize == 0);
 			ASSERT(dtrace_probes == NULL);
 			nsize = sizeof (dtrace_probe_t *);
 		}
 
 		probes = kmem_zalloc(nsize, KM_SLEEP);
 
 		if (dtrace_probes == NULL) {
 			ASSERT(osize == 0);
 			dtrace_probes = probes;
 			dtrace_nprobes = 1;
 		} else {
 			dtrace_probe_t **oprobes = dtrace_probes;
 
 			bcopy(oprobes, probes, osize);
 			dtrace_membar_producer();
 			dtrace_probes = probes;
 
 			dtrace_sync();
 
 			/*
 			 * All CPUs are now seeing the new probes array; we can
 			 * safely free the old array.
 			 */
 			kmem_free(oprobes, osize);
 			dtrace_nprobes <<= 1;
 		}
 
 		ASSERT(id - 1 < dtrace_nprobes);
 	}
 
 	ASSERT(dtrace_probes[id - 1] == NULL);
 	dtrace_probes[id - 1] = probe;
 
 	if (provider != dtrace_provider)
 		mutex_exit(&dtrace_lock);
 
 	return (id);
 }
 
 static dtrace_probe_t *
 dtrace_probe_lookup_id(dtrace_id_t id)
 {
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 
 	if (id == 0 || id > dtrace_nprobes)
 		return (NULL);
 
 	return (dtrace_probes[id - 1]);
 }
 
 static int
 dtrace_probe_lookup_match(dtrace_probe_t *probe, void *arg)
 {
 	*((dtrace_id_t *)arg) = probe->dtpr_id;
 
 	return (DTRACE_MATCH_DONE);
 }
 
 /*
  * Look up a probe based on provider and one or more of module name, function
  * name and probe name.
  */
 dtrace_id_t
 dtrace_probe_lookup(dtrace_provider_id_t prid, char *mod,
     char *func, char *name)
 {
 	dtrace_probekey_t pkey;
 	dtrace_id_t id;
 	int match;
 
 	pkey.dtpk_prov = ((dtrace_provider_t *)prid)->dtpv_name;
 	pkey.dtpk_pmatch = &dtrace_match_string;
 	pkey.dtpk_mod = mod;
 	pkey.dtpk_mmatch = mod ? &dtrace_match_string : &dtrace_match_nul;
 	pkey.dtpk_func = func;
 	pkey.dtpk_fmatch = func ? &dtrace_match_string : &dtrace_match_nul;
 	pkey.dtpk_name = name;
 	pkey.dtpk_nmatch = name ? &dtrace_match_string : &dtrace_match_nul;
 	pkey.dtpk_id = DTRACE_IDNONE;
 
 	mutex_enter(&dtrace_lock);
 	match = dtrace_match(&pkey, DTRACE_PRIV_ALL, 0, 0,
 	    dtrace_probe_lookup_match, &id);
 	mutex_exit(&dtrace_lock);
 
 	ASSERT(match == 1 || match == 0);
 	return (match ? id : 0);
 }
 
 /*
  * Returns the probe argument associated with the specified probe.
  */
 void *
 dtrace_probe_arg(dtrace_provider_id_t id, dtrace_id_t pid)
 {
 	dtrace_probe_t *probe;
 	void *rval = NULL;
 
 	mutex_enter(&dtrace_lock);
 
 	if ((probe = dtrace_probe_lookup_id(pid)) != NULL &&
 	    probe->dtpr_provider == (dtrace_provider_t *)id)
 		rval = probe->dtpr_arg;
 
 	mutex_exit(&dtrace_lock);
 
 	return (rval);
 }
 
 /*
  * Copy a probe into a probe description.
  */
 static void
 dtrace_probe_description(const dtrace_probe_t *prp, dtrace_probedesc_t *pdp)
 {
 	bzero(pdp, sizeof (dtrace_probedesc_t));
 	pdp->dtpd_id = prp->dtpr_id;
 
 	(void) strncpy(pdp->dtpd_provider,
 	    prp->dtpr_provider->dtpv_name, DTRACE_PROVNAMELEN - 1);
 
 	(void) strncpy(pdp->dtpd_mod, prp->dtpr_mod, DTRACE_MODNAMELEN - 1);
 	(void) strncpy(pdp->dtpd_func, prp->dtpr_func, DTRACE_FUNCNAMELEN - 1);
 	(void) strncpy(pdp->dtpd_name, prp->dtpr_name, DTRACE_NAMELEN - 1);
 }
 
 /*
  * Called to indicate that a probe -- or probes -- should be provided by a
  * specfied provider.  If the specified description is NULL, the provider will
  * be told to provide all of its probes.  (This is done whenever a new
  * consumer comes along, or whenever a retained enabling is to be matched.) If
  * the specified description is non-NULL, the provider is given the
  * opportunity to dynamically provide the specified probe, allowing providers
  * to support the creation of probes on-the-fly.  (So-called _autocreated_
  * probes.)  If the provider is NULL, the operations will be applied to all
  * providers; if the provider is non-NULL the operations will only be applied
  * to the specified provider.  The dtrace_provider_lock must be held, and the
  * dtrace_lock must _not_ be held -- the provider's dtps_provide() operation
  * will need to grab the dtrace_lock when it reenters the framework through
  * dtrace_probe_lookup(), dtrace_probe_create(), etc.
  */
 static void
 dtrace_probe_provide(dtrace_probedesc_t *desc, dtrace_provider_t *prv)
 {
 #ifdef illumos
 	modctl_t *ctl;
 #endif
 	int all = 0;
 
 	ASSERT(MUTEX_HELD(&dtrace_provider_lock));
 
 	if (prv == NULL) {
 		all = 1;
 		prv = dtrace_provider;
 	}
 
 	do {
 		/*
 		 * First, call the blanket provide operation.
 		 */
 		prv->dtpv_pops.dtps_provide(prv->dtpv_arg, desc);
 
 #ifdef illumos
 		/*
 		 * Now call the per-module provide operation.  We will grab
 		 * mod_lock to prevent the list from being modified.  Note
 		 * that this also prevents the mod_busy bits from changing.
 		 * (mod_busy can only be changed with mod_lock held.)
 		 */
 		mutex_enter(&mod_lock);
 
 		ctl = &modules;
 		do {
 			if (ctl->mod_busy || ctl->mod_mp == NULL)
 				continue;
 
 			prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl);
 
 		} while ((ctl = ctl->mod_next) != &modules);
 
 		mutex_exit(&mod_lock);
 #endif
 	} while (all && (prv = prv->dtpv_next) != NULL);
 }
 
 #ifdef illumos
 /*
  * Iterate over each probe, and call the Framework-to-Provider API function
  * denoted by offs.
  */
 static void
 dtrace_probe_foreach(uintptr_t offs)
 {
 	dtrace_provider_t *prov;
 	void (*func)(void *, dtrace_id_t, void *);
 	dtrace_probe_t *probe;
 	dtrace_icookie_t cookie;
 	int i;
 
 	/*
 	 * We disable interrupts to walk through the probe array.  This is
 	 * safe -- the dtrace_sync() in dtrace_unregister() assures that we
 	 * won't see stale data.
 	 */
 	cookie = dtrace_interrupt_disable();
 
 	for (i = 0; i < dtrace_nprobes; i++) {
 		if ((probe = dtrace_probes[i]) == NULL)
 			continue;
 
 		if (probe->dtpr_ecb == NULL) {
 			/*
 			 * This probe isn't enabled -- don't call the function.
 			 */
 			continue;
 		}
 
 		prov = probe->dtpr_provider;
 		func = *((void(**)(void *, dtrace_id_t, void *))
 		    ((uintptr_t)&prov->dtpv_pops + offs));
 
 		func(prov->dtpv_arg, i + 1, probe->dtpr_arg);
 	}
 
 	dtrace_interrupt_enable(cookie);
 }
 #endif
 
 static int
 dtrace_probe_enable(dtrace_probedesc_t *desc, dtrace_enabling_t *enab)
 {
 	dtrace_probekey_t pkey;
 	uint32_t priv;
 	uid_t uid;
 	zoneid_t zoneid;
 
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 	dtrace_ecb_create_cache = NULL;
 
 	if (desc == NULL) {
 		/*
 		 * If we're passed a NULL description, we're being asked to
 		 * create an ECB with a NULL probe.
 		 */
 		(void) dtrace_ecb_create_enable(NULL, enab);
 		return (0);
 	}
 
 	dtrace_probekey(desc, &pkey);
 	dtrace_cred2priv(enab->dten_vstate->dtvs_state->dts_cred.dcr_cred,
 	    &priv, &uid, &zoneid);
 
 	return (dtrace_match(&pkey, priv, uid, zoneid, dtrace_ecb_create_enable,
 	    enab));
 }
 
 /*
  * DTrace Helper Provider Functions
  */
 static void
 dtrace_dofattr2attr(dtrace_attribute_t *attr, const dof_attr_t dofattr)
 {
 	attr->dtat_name = DOF_ATTR_NAME(dofattr);
 	attr->dtat_data = DOF_ATTR_DATA(dofattr);
 	attr->dtat_class = DOF_ATTR_CLASS(dofattr);
 }
 
 static void
 dtrace_dofprov2hprov(dtrace_helper_provdesc_t *hprov,
     const dof_provider_t *dofprov, char *strtab)
 {
 	hprov->dthpv_provname = strtab + dofprov->dofpv_name;
 	dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_provider,
 	    dofprov->dofpv_provattr);
 	dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_mod,
 	    dofprov->dofpv_modattr);
 	dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_func,
 	    dofprov->dofpv_funcattr);
 	dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_name,
 	    dofprov->dofpv_nameattr);
 	dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_args,
 	    dofprov->dofpv_argsattr);
 }
 
 static void
 dtrace_helper_provide_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid)
 {
 	uintptr_t daddr = (uintptr_t)dhp->dofhp_dof;
 	dof_hdr_t *dof = (dof_hdr_t *)daddr;
 	dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec;
 	dof_provider_t *provider;
 	dof_probe_t *probe;
 	uint32_t *off, *enoff;
 	uint8_t *arg;
 	char *strtab;
 	uint_t i, nprobes;
 	dtrace_helper_provdesc_t dhpv;
 	dtrace_helper_probedesc_t dhpb;
 	dtrace_meta_t *meta = dtrace_meta_pid;
 	dtrace_mops_t *mops = &meta->dtm_mops;
 	void *parg;
 
 	provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset);
 	str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
 	    provider->dofpv_strtab * dof->dofh_secsize);
 	prb_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
 	    provider->dofpv_probes * dof->dofh_secsize);
 	arg_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
 	    provider->dofpv_prargs * dof->dofh_secsize);
 	off_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
 	    provider->dofpv_proffs * dof->dofh_secsize);
 
 	strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset);
 	off = (uint32_t *)(uintptr_t)(daddr + off_sec->dofs_offset);
 	arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset);
 	enoff = NULL;
 
 	/*
 	 * See dtrace_helper_provider_validate().
 	 */
 	if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 &&
 	    provider->dofpv_prenoffs != DOF_SECT_NONE) {
 		enoff_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
 		    provider->dofpv_prenoffs * dof->dofh_secsize);
 		enoff = (uint32_t *)(uintptr_t)(daddr + enoff_sec->dofs_offset);
 	}
 
 	nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize;
 
 	/*
 	 * Create the provider.
 	 */
 	dtrace_dofprov2hprov(&dhpv, provider, strtab);
 
 	if ((parg = mops->dtms_provide_pid(meta->dtm_arg, &dhpv, pid)) == NULL)
 		return;
 
 	meta->dtm_count++;
 
 	/*
 	 * Create the probes.
 	 */
 	for (i = 0; i < nprobes; i++) {
 		probe = (dof_probe_t *)(uintptr_t)(daddr +
 		    prb_sec->dofs_offset + i * prb_sec->dofs_entsize);
 
 		/* See the check in dtrace_helper_provider_validate(). */
 		if (strlen(strtab + probe->dofpr_func) >= DTRACE_FUNCNAMELEN)
 			continue;
 
 		dhpb.dthpb_mod = dhp->dofhp_mod;
 		dhpb.dthpb_func = strtab + probe->dofpr_func;
 		dhpb.dthpb_name = strtab + probe->dofpr_name;
 		dhpb.dthpb_base = probe->dofpr_addr;
 		dhpb.dthpb_offs = off + probe->dofpr_offidx;
 		dhpb.dthpb_noffs = probe->dofpr_noffs;
 		if (enoff != NULL) {
 			dhpb.dthpb_enoffs = enoff + probe->dofpr_enoffidx;
 			dhpb.dthpb_nenoffs = probe->dofpr_nenoffs;
 		} else {
 			dhpb.dthpb_enoffs = NULL;
 			dhpb.dthpb_nenoffs = 0;
 		}
 		dhpb.dthpb_args = arg + probe->dofpr_argidx;
 		dhpb.dthpb_nargc = probe->dofpr_nargc;
 		dhpb.dthpb_xargc = probe->dofpr_xargc;
 		dhpb.dthpb_ntypes = strtab + probe->dofpr_nargv;
 		dhpb.dthpb_xtypes = strtab + probe->dofpr_xargv;
 
 		mops->dtms_create_probe(meta->dtm_arg, parg, &dhpb);
 	}
 }
 
 static void
 dtrace_helper_provide(dof_helper_t *dhp, pid_t pid)
 {
 	uintptr_t daddr = (uintptr_t)dhp->dofhp_dof;
 	dof_hdr_t *dof = (dof_hdr_t *)daddr;
 	int i;
 
 	ASSERT(MUTEX_HELD(&dtrace_meta_lock));
 
 	for (i = 0; i < dof->dofh_secnum; i++) {
 		dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr +
 		    dof->dofh_secoff + i * dof->dofh_secsize);
 
 		if (sec->dofs_type != DOF_SECT_PROVIDER)
 			continue;
 
 		dtrace_helper_provide_one(dhp, sec, pid);
 	}
 
 	/*
 	 * We may have just created probes, so we must now rematch against
 	 * any retained enablings.  Note that this call will acquire both
 	 * cpu_lock and dtrace_lock; the fact that we are holding
 	 * dtrace_meta_lock now is what defines the ordering with respect to
 	 * these three locks.
 	 */
 	dtrace_enabling_matchall();
 }
 
 static void
 dtrace_helper_provider_remove_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid)
 {
 	uintptr_t daddr = (uintptr_t)dhp->dofhp_dof;
 	dof_hdr_t *dof = (dof_hdr_t *)daddr;
 	dof_sec_t *str_sec;
 	dof_provider_t *provider;
 	char *strtab;
 	dtrace_helper_provdesc_t dhpv;
 	dtrace_meta_t *meta = dtrace_meta_pid;
 	dtrace_mops_t *mops = &meta->dtm_mops;
 
 	provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset);
 	str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff +
 	    provider->dofpv_strtab * dof->dofh_secsize);
 
 	strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset);
 
 	/*
 	 * Create the provider.
 	 */
 	dtrace_dofprov2hprov(&dhpv, provider, strtab);
 
 	mops->dtms_remove_pid(meta->dtm_arg, &dhpv, pid);
 
 	meta->dtm_count--;
 }
 
 static void
 dtrace_helper_provider_remove(dof_helper_t *dhp, pid_t pid)
 {
 	uintptr_t daddr = (uintptr_t)dhp->dofhp_dof;
 	dof_hdr_t *dof = (dof_hdr_t *)daddr;
 	int i;
 
 	ASSERT(MUTEX_HELD(&dtrace_meta_lock));
 
 	for (i = 0; i < dof->dofh_secnum; i++) {
 		dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr +
 		    dof->dofh_secoff + i * dof->dofh_secsize);
 
 		if (sec->dofs_type != DOF_SECT_PROVIDER)
 			continue;
 
 		dtrace_helper_provider_remove_one(dhp, sec, pid);
 	}
 }
 
 /*
  * DTrace Meta Provider-to-Framework API Functions
  *
  * These functions implement the Meta Provider-to-Framework API, as described
  * in <sys/dtrace.h>.
  */
 int
 dtrace_meta_register(const char *name, const dtrace_mops_t *mops, void *arg,
     dtrace_meta_provider_id_t *idp)
 {
 	dtrace_meta_t *meta;
 	dtrace_helpers_t *help, *next;
 	int i;
 
 	*idp = DTRACE_METAPROVNONE;
 
 	/*
 	 * We strictly don't need the name, but we hold onto it for
 	 * debuggability. All hail error queues!
 	 */
 	if (name == NULL) {
 		cmn_err(CE_WARN, "failed to register meta-provider: "
 		    "invalid name");
 		return (EINVAL);
 	}
 
 	if (mops == NULL ||
 	    mops->dtms_create_probe == NULL ||
 	    mops->dtms_provide_pid == NULL ||
 	    mops->dtms_remove_pid == NULL) {
 		cmn_err(CE_WARN, "failed to register meta-register %s: "
 		    "invalid ops", name);
 		return (EINVAL);
 	}
 
 	meta = kmem_zalloc(sizeof (dtrace_meta_t), KM_SLEEP);
 	meta->dtm_mops = *mops;
 	meta->dtm_name = kmem_alloc(strlen(name) + 1, KM_SLEEP);
 	(void) strcpy(meta->dtm_name, name);
 	meta->dtm_arg = arg;
 
 	mutex_enter(&dtrace_meta_lock);
 	mutex_enter(&dtrace_lock);
 
 	if (dtrace_meta_pid != NULL) {
 		mutex_exit(&dtrace_lock);
 		mutex_exit(&dtrace_meta_lock);
 		cmn_err(CE_WARN, "failed to register meta-register %s: "
 		    "user-land meta-provider exists", name);
 		kmem_free(meta->dtm_name, strlen(meta->dtm_name) + 1);
 		kmem_free(meta, sizeof (dtrace_meta_t));
 		return (EINVAL);
 	}
 
 	dtrace_meta_pid = meta;
 	*idp = (dtrace_meta_provider_id_t)meta;
 
 	/*
 	 * If there are providers and probes ready to go, pass them
 	 * off to the new meta provider now.
 	 */
 
 	help = dtrace_deferred_pid;
 	dtrace_deferred_pid = NULL;
 
 	mutex_exit(&dtrace_lock);
 
 	while (help != NULL) {
 		for (i = 0; i < help->dthps_nprovs; i++) {
 			dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov,
 			    help->dthps_pid);
 		}
 
 		next = help->dthps_next;
 		help->dthps_next = NULL;
 		help->dthps_prev = NULL;
 		help->dthps_deferred = 0;
 		help = next;
 	}
 
 	mutex_exit(&dtrace_meta_lock);
 
 	return (0);
 }
 
 int
 dtrace_meta_unregister(dtrace_meta_provider_id_t id)
 {
 	dtrace_meta_t **pp, *old = (dtrace_meta_t *)id;
 
 	mutex_enter(&dtrace_meta_lock);
 	mutex_enter(&dtrace_lock);
 
 	if (old == dtrace_meta_pid) {
 		pp = &dtrace_meta_pid;
 	} else {
 		panic("attempt to unregister non-existent "
 		    "dtrace meta-provider %p\n", (void *)old);
 	}
 
 	if (old->dtm_count != 0) {
 		mutex_exit(&dtrace_lock);
 		mutex_exit(&dtrace_meta_lock);
 		return (EBUSY);
 	}
 
 	*pp = NULL;
 
 	mutex_exit(&dtrace_lock);
 	mutex_exit(&dtrace_meta_lock);
 
 	kmem_free(old->dtm_name, strlen(old->dtm_name) + 1);
 	kmem_free(old, sizeof (dtrace_meta_t));
 
 	return (0);
 }
 
 
 /*
  * DTrace DIF Object Functions
  */
 static int
 dtrace_difo_err(uint_t pc, const char *format, ...)
 {
 	if (dtrace_err_verbose) {
 		va_list alist;
 
 		(void) uprintf("dtrace DIF object error: [%u]: ", pc);
 		va_start(alist, format);
 		(void) vuprintf(format, alist);
 		va_end(alist);
 	}
 
 #ifdef DTRACE_ERRDEBUG
 	dtrace_errdebug(format);
 #endif
 	return (1);
 }
 
 /*
  * Validate a DTrace DIF object by checking the IR instructions.  The following
  * rules are currently enforced by dtrace_difo_validate():
  *
  * 1. Each instruction must have a valid opcode
  * 2. Each register, string, variable, or subroutine reference must be valid
  * 3. No instruction can modify register %r0 (must be zero)
  * 4. All instruction reserved bits must be set to zero
  * 5. The last instruction must be a "ret" instruction
  * 6. All branch targets must reference a valid instruction _after_ the branch
  */
 static int
 dtrace_difo_validate(dtrace_difo_t *dp, dtrace_vstate_t *vstate, uint_t nregs,
     cred_t *cr)
 {
 	int err = 0, i;
 	int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err;
 	int kcheckload;
 	uint_t pc;
 	int maxglobal = -1, maxlocal = -1, maxtlocal = -1;
 
 	kcheckload = cr == NULL ||
 	    (vstate->dtvs_state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) == 0;
 
 	dp->dtdo_destructive = 0;
 
 	for (pc = 0; pc < dp->dtdo_len && err == 0; pc++) {
 		dif_instr_t instr = dp->dtdo_buf[pc];
 
 		uint_t r1 = DIF_INSTR_R1(instr);
 		uint_t r2 = DIF_INSTR_R2(instr);
 		uint_t rd = DIF_INSTR_RD(instr);
 		uint_t rs = DIF_INSTR_RS(instr);
 		uint_t label = DIF_INSTR_LABEL(instr);
 		uint_t v = DIF_INSTR_VAR(instr);
 		uint_t subr = DIF_INSTR_SUBR(instr);
 		uint_t type = DIF_INSTR_TYPE(instr);
 		uint_t op = DIF_INSTR_OP(instr);
 
 		switch (op) {
 		case DIF_OP_OR:
 		case DIF_OP_XOR:
 		case DIF_OP_AND:
 		case DIF_OP_SLL:
 		case DIF_OP_SRL:
 		case DIF_OP_SRA:
 		case DIF_OP_SUB:
 		case DIF_OP_ADD:
 		case DIF_OP_MUL:
 		case DIF_OP_SDIV:
 		case DIF_OP_UDIV:
 		case DIF_OP_SREM:
 		case DIF_OP_UREM:
 		case DIF_OP_COPYS:
 			if (r1 >= nregs)
 				err += efunc(pc, "invalid register %u\n", r1);
 			if (r2 >= nregs)
 				err += efunc(pc, "invalid register %u\n", r2);
 			if (rd >= nregs)
 				err += efunc(pc, "invalid register %u\n", rd);
 			if (rd == 0)
 				err += efunc(pc, "cannot write to %r0\n");
 			break;
 		case DIF_OP_NOT:
 		case DIF_OP_MOV:
 		case DIF_OP_ALLOCS:
 			if (r1 >= nregs)
 				err += efunc(pc, "invalid register %u\n", r1);
 			if (r2 != 0)
 				err += efunc(pc, "non-zero reserved bits\n");
 			if (rd >= nregs)
 				err += efunc(pc, "invalid register %u\n", rd);
 			if (rd == 0)
 				err += efunc(pc, "cannot write to %r0\n");
 			break;
 		case DIF_OP_LDSB:
 		case DIF_OP_LDSH:
 		case DIF_OP_LDSW:
 		case DIF_OP_LDUB:
 		case DIF_OP_LDUH:
 		case DIF_OP_LDUW:
 		case DIF_OP_LDX:
 			if (r1 >= nregs)
 				err += efunc(pc, "invalid register %u\n", r1);
 			if (r2 != 0)
 				err += efunc(pc, "non-zero reserved bits\n");
 			if (rd >= nregs)
 				err += efunc(pc, "invalid register %u\n", rd);
 			if (rd == 0)
 				err += efunc(pc, "cannot write to %r0\n");
 			if (kcheckload)
 				dp->dtdo_buf[pc] = DIF_INSTR_LOAD(op +
 				    DIF_OP_RLDSB - DIF_OP_LDSB, r1, rd);
 			break;
 		case DIF_OP_RLDSB:
 		case DIF_OP_RLDSH:
 		case DIF_OP_RLDSW:
 		case DIF_OP_RLDUB:
 		case DIF_OP_RLDUH:
 		case DIF_OP_RLDUW:
 		case DIF_OP_RLDX:
 			if (r1 >= nregs)
 				err += efunc(pc, "invalid register %u\n", r1);
 			if (r2 != 0)
 				err += efunc(pc, "non-zero reserved bits\n");
 			if (rd >= nregs)
 				err += efunc(pc, "invalid register %u\n", rd);
 			if (rd == 0)
 				err += efunc(pc, "cannot write to %r0\n");
 			break;
 		case DIF_OP_ULDSB:
 		case DIF_OP_ULDSH:
 		case DIF_OP_ULDSW:
 		case DIF_OP_ULDUB:
 		case DIF_OP_ULDUH:
 		case DIF_OP_ULDUW:
 		case DIF_OP_ULDX:
 			if (r1 >= nregs)
 				err += efunc(pc, "invalid register %u\n", r1);
 			if (r2 != 0)
 				err += efunc(pc, "non-zero reserved bits\n");
 			if (rd >= nregs)
 				err += efunc(pc, "invalid register %u\n", rd);
 			if (rd == 0)
 				err += efunc(pc, "cannot write to %r0\n");
 			break;
 		case DIF_OP_STB:
 		case DIF_OP_STH:
 		case DIF_OP_STW:
 		case DIF_OP_STX:
 			if (r1 >= nregs)
 				err += efunc(pc, "invalid register %u\n", r1);
 			if (r2 != 0)
 				err += efunc(pc, "non-zero reserved bits\n");
 			if (rd >= nregs)
 				err += efunc(pc, "invalid register %u\n", rd);
 			if (rd == 0)
 				err += efunc(pc, "cannot write to 0 address\n");
 			break;
 		case DIF_OP_CMP:
 		case DIF_OP_SCMP:
 			if (r1 >= nregs)
 				err += efunc(pc, "invalid register %u\n", r1);
 			if (r2 >= nregs)
 				err += efunc(pc, "invalid register %u\n", r2);
 			if (rd != 0)
 				err += efunc(pc, "non-zero reserved bits\n");
 			break;
 		case DIF_OP_TST:
 			if (r1 >= nregs)
 				err += efunc(pc, "invalid register %u\n", r1);
 			if (r2 != 0 || rd != 0)
 				err += efunc(pc, "non-zero reserved bits\n");
 			break;
 		case DIF_OP_BA:
 		case DIF_OP_BE:
 		case DIF_OP_BNE:
 		case DIF_OP_BG:
 		case DIF_OP_BGU:
 		case DIF_OP_BGE:
 		case DIF_OP_BGEU:
 		case DIF_OP_BL:
 		case DIF_OP_BLU:
 		case DIF_OP_BLE:
 		case DIF_OP_BLEU:
 			if (label >= dp->dtdo_len) {
 				err += efunc(pc, "invalid branch target %u\n",
 				    label);
 			}
 			if (label <= pc) {
 				err += efunc(pc, "backward branch to %u\n",
 				    label);
 			}
 			break;
 		case DIF_OP_RET:
 			if (r1 != 0 || r2 != 0)
 				err += efunc(pc, "non-zero reserved bits\n");
 			if (rd >= nregs)
 				err += efunc(pc, "invalid register %u\n", rd);
 			break;
 		case DIF_OP_NOP:
 		case DIF_OP_POPTS:
 		case DIF_OP_FLUSHTS:
 			if (r1 != 0 || r2 != 0 || rd != 0)
 				err += efunc(pc, "non-zero reserved bits\n");
 			break;
 		case DIF_OP_SETX:
 			if (DIF_INSTR_INTEGER(instr) >= dp->dtdo_intlen) {
 				err += efunc(pc, "invalid integer ref %u\n",
 				    DIF_INSTR_INTEGER(instr));
 			}
 			if (rd >= nregs)
 				err += efunc(pc, "invalid register %u\n", rd);
 			if (rd == 0)
 				err += efunc(pc, "cannot write to %r0\n");
 			break;
 		case DIF_OP_SETS:
 			if (DIF_INSTR_STRING(instr) >= dp->dtdo_strlen) {
 				err += efunc(pc, "invalid string ref %u\n",
 				    DIF_INSTR_STRING(instr));
 			}
 			if (rd >= nregs)
 				err += efunc(pc, "invalid register %u\n", rd);
 			if (rd == 0)
 				err += efunc(pc, "cannot write to %r0\n");
 			break;
 		case DIF_OP_LDGA:
 		case DIF_OP_LDTA:
 			if (r1 > DIF_VAR_ARRAY_MAX)
 				err += efunc(pc, "invalid array %u\n", r1);
 			if (r2 >= nregs)
 				err += efunc(pc, "invalid register %u\n", r2);
 			if (rd >= nregs)
 				err += efunc(pc, "invalid register %u\n", rd);
 			if (rd == 0)
 				err += efunc(pc, "cannot write to %r0\n");
 			break;
 		case DIF_OP_LDGS:
 		case DIF_OP_LDTS:
 		case DIF_OP_LDLS:
 		case DIF_OP_LDGAA:
 		case DIF_OP_LDTAA:
 			if (v < DIF_VAR_OTHER_MIN || v > DIF_VAR_OTHER_MAX)
 				err += efunc(pc, "invalid variable %u\n", v);
 			if (rd >= nregs)
 				err += efunc(pc, "invalid register %u\n", rd);
 			if (rd == 0)
 				err += efunc(pc, "cannot write to %r0\n");
 			break;
 		case DIF_OP_STGS:
 		case DIF_OP_STTS:
 		case DIF_OP_STLS:
 		case DIF_OP_STGAA:
 		case DIF_OP_STTAA:
 			if (v < DIF_VAR_OTHER_UBASE || v > DIF_VAR_OTHER_MAX)
 				err += efunc(pc, "invalid variable %u\n", v);
 			if (rs >= nregs)
 				err += efunc(pc, "invalid register %u\n", rd);
 			break;
 		case DIF_OP_CALL:
 			if (subr > DIF_SUBR_MAX)
 				err += efunc(pc, "invalid subr %u\n", subr);
 			if (rd >= nregs)
 				err += efunc(pc, "invalid register %u\n", rd);
 			if (rd == 0)
 				err += efunc(pc, "cannot write to %r0\n");
 
 			if (subr == DIF_SUBR_COPYOUT ||
 			    subr == DIF_SUBR_COPYOUTSTR) {
 				dp->dtdo_destructive = 1;
 			}
 
 			if (subr == DIF_SUBR_GETF) {
 #ifdef __FreeBSD__
 				err += efunc(pc, "getf() not supported");
 #else
 				/*
 				 * If we have a getf() we need to record that
 				 * in our state.  Note that our state can be
 				 * NULL if this is a helper -- but in that
 				 * case, the call to getf() is itself illegal,
 				 * and will be caught (slightly later) when
 				 * the helper is validated.
 				 */
 				if (vstate->dtvs_state != NULL)
 					vstate->dtvs_state->dts_getf++;
 #endif
 			}
 
 			break;
 		case DIF_OP_PUSHTR:
 			if (type != DIF_TYPE_STRING && type != DIF_TYPE_CTF)
 				err += efunc(pc, "invalid ref type %u\n", type);
 			if (r2 >= nregs)
 				err += efunc(pc, "invalid register %u\n", r2);
 			if (rs >= nregs)
 				err += efunc(pc, "invalid register %u\n", rs);
 			break;
 		case DIF_OP_PUSHTV:
 			if (type != DIF_TYPE_CTF)
 				err += efunc(pc, "invalid val type %u\n", type);
 			if (r2 >= nregs)
 				err += efunc(pc, "invalid register %u\n", r2);
 			if (rs >= nregs)
 				err += efunc(pc, "invalid register %u\n", rs);
 			break;
 		default:
 			err += efunc(pc, "invalid opcode %u\n",
 			    DIF_INSTR_OP(instr));
 		}
 	}
 
 	if (dp->dtdo_len != 0 &&
 	    DIF_INSTR_OP(dp->dtdo_buf[dp->dtdo_len - 1]) != DIF_OP_RET) {
 		err += efunc(dp->dtdo_len - 1,
 		    "expected 'ret' as last DIF instruction\n");
 	}
 
 	if (!(dp->dtdo_rtype.dtdt_flags & (DIF_TF_BYREF | DIF_TF_BYUREF))) {
 		/*
 		 * If we're not returning by reference, the size must be either
 		 * 0 or the size of one of the base types.
 		 */
 		switch (dp->dtdo_rtype.dtdt_size) {
 		case 0:
 		case sizeof (uint8_t):
 		case sizeof (uint16_t):
 		case sizeof (uint32_t):
 		case sizeof (uint64_t):
 			break;
 
 		default:
 			err += efunc(dp->dtdo_len - 1, "bad return size\n");
 		}
 	}
 
 	for (i = 0; i < dp->dtdo_varlen && err == 0; i++) {
 		dtrace_difv_t *v = &dp->dtdo_vartab[i], *existing = NULL;
 		dtrace_diftype_t *vt, *et;
 		uint_t id, ndx;
 
 		if (v->dtdv_scope != DIFV_SCOPE_GLOBAL &&
 		    v->dtdv_scope != DIFV_SCOPE_THREAD &&
 		    v->dtdv_scope != DIFV_SCOPE_LOCAL) {
 			err += efunc(i, "unrecognized variable scope %d\n",
 			    v->dtdv_scope);
 			break;
 		}
 
 		if (v->dtdv_kind != DIFV_KIND_ARRAY &&
 		    v->dtdv_kind != DIFV_KIND_SCALAR) {
 			err += efunc(i, "unrecognized variable type %d\n",
 			    v->dtdv_kind);
 			break;
 		}
 
 		if ((id = v->dtdv_id) > DIF_VARIABLE_MAX) {
 			err += efunc(i, "%d exceeds variable id limit\n", id);
 			break;
 		}
 
 		if (id < DIF_VAR_OTHER_UBASE)
 			continue;
 
 		/*
 		 * For user-defined variables, we need to check that this
 		 * definition is identical to any previous definition that we
 		 * encountered.
 		 */
 		ndx = id - DIF_VAR_OTHER_UBASE;
 
 		switch (v->dtdv_scope) {
 		case DIFV_SCOPE_GLOBAL:
 			if (maxglobal == -1 || ndx > maxglobal)
 				maxglobal = ndx;
 
 			if (ndx < vstate->dtvs_nglobals) {
 				dtrace_statvar_t *svar;
 
 				if ((svar = vstate->dtvs_globals[ndx]) != NULL)
 					existing = &svar->dtsv_var;
 			}
 
 			break;
 
 		case DIFV_SCOPE_THREAD:
 			if (maxtlocal == -1 || ndx > maxtlocal)
 				maxtlocal = ndx;
 
 			if (ndx < vstate->dtvs_ntlocals)
 				existing = &vstate->dtvs_tlocals[ndx];
 			break;
 
 		case DIFV_SCOPE_LOCAL:
 			if (maxlocal == -1 || ndx > maxlocal)
 				maxlocal = ndx;
 
 			if (ndx < vstate->dtvs_nlocals) {
 				dtrace_statvar_t *svar;
 
 				if ((svar = vstate->dtvs_locals[ndx]) != NULL)
 					existing = &svar->dtsv_var;
 			}
 
 			break;
 		}
 
 		vt = &v->dtdv_type;
 
 		if (vt->dtdt_flags & DIF_TF_BYREF) {
 			if (vt->dtdt_size == 0) {
 				err += efunc(i, "zero-sized variable\n");
 				break;
 			}
 
 			if ((v->dtdv_scope == DIFV_SCOPE_GLOBAL ||
 			    v->dtdv_scope == DIFV_SCOPE_LOCAL) &&
 			    vt->dtdt_size > dtrace_statvar_maxsize) {
 				err += efunc(i, "oversized by-ref static\n");
 				break;
 			}
 		}
 
 		if (existing == NULL || existing->dtdv_id == 0)
 			continue;
 
 		ASSERT(existing->dtdv_id == v->dtdv_id);
 		ASSERT(existing->dtdv_scope == v->dtdv_scope);
 
 		if (existing->dtdv_kind != v->dtdv_kind)
 			err += efunc(i, "%d changed variable kind\n", id);
 
 		et = &existing->dtdv_type;
 
 		if (vt->dtdt_flags != et->dtdt_flags) {
 			err += efunc(i, "%d changed variable type flags\n", id);
 			break;
 		}
 
 		if (vt->dtdt_size != 0 && vt->dtdt_size != et->dtdt_size) {
 			err += efunc(i, "%d changed variable type size\n", id);
 			break;
 		}
 	}
 
 	for (pc = 0; pc < dp->dtdo_len && err == 0; pc++) {
 		dif_instr_t instr = dp->dtdo_buf[pc];
 
 		uint_t v = DIF_INSTR_VAR(instr);
 		uint_t op = DIF_INSTR_OP(instr);
 
 		switch (op) {
 		case DIF_OP_LDGS:
 		case DIF_OP_LDGAA:
 		case DIF_OP_STGS:
 		case DIF_OP_STGAA:
 			if (v > DIF_VAR_OTHER_UBASE + maxglobal)
 				err += efunc(pc, "invalid variable %u\n", v);
 			break;
 		case DIF_OP_LDTS:
 		case DIF_OP_LDTAA:
 		case DIF_OP_STTS:
 		case DIF_OP_STTAA:
 			if (v > DIF_VAR_OTHER_UBASE + maxtlocal)
 				err += efunc(pc, "invalid variable %u\n", v);
 			break;
 		case DIF_OP_LDLS:
 		case DIF_OP_STLS:
 			if (v > DIF_VAR_OTHER_UBASE + maxlocal)
 				err += efunc(pc, "invalid variable %u\n", v);
 			break;
 		default:
 			break;
 		}
 	}
 
 	return (err);
 }
 
 /*
  * Validate a DTrace DIF object that it is to be used as a helper.  Helpers
  * are much more constrained than normal DIFOs.  Specifically, they may
  * not:
  *
  * 1. Make calls to subroutines other than copyin(), copyinstr() or
  *    miscellaneous string routines
  * 2. Access DTrace variables other than the args[] array, and the
  *    curthread, pid, ppid, tid, execname, zonename, uid and gid variables.
  * 3. Have thread-local variables.
  * 4. Have dynamic variables.
  */
 static int
 dtrace_difo_validate_helper(dtrace_difo_t *dp)
 {
 	int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err;
 	int err = 0;
 	uint_t pc;
 
 	for (pc = 0; pc < dp->dtdo_len; pc++) {
 		dif_instr_t instr = dp->dtdo_buf[pc];
 
 		uint_t v = DIF_INSTR_VAR(instr);
 		uint_t subr = DIF_INSTR_SUBR(instr);
 		uint_t op = DIF_INSTR_OP(instr);
 
 		switch (op) {
 		case DIF_OP_OR:
 		case DIF_OP_XOR:
 		case DIF_OP_AND:
 		case DIF_OP_SLL:
 		case DIF_OP_SRL:
 		case DIF_OP_SRA:
 		case DIF_OP_SUB:
 		case DIF_OP_ADD:
 		case DIF_OP_MUL:
 		case DIF_OP_SDIV:
 		case DIF_OP_UDIV:
 		case DIF_OP_SREM:
 		case DIF_OP_UREM:
 		case DIF_OP_COPYS:
 		case DIF_OP_NOT:
 		case DIF_OP_MOV:
 		case DIF_OP_RLDSB:
 		case DIF_OP_RLDSH:
 		case DIF_OP_RLDSW:
 		case DIF_OP_RLDUB:
 		case DIF_OP_RLDUH:
 		case DIF_OP_RLDUW:
 		case DIF_OP_RLDX:
 		case DIF_OP_ULDSB:
 		case DIF_OP_ULDSH:
 		case DIF_OP_ULDSW:
 		case DIF_OP_ULDUB:
 		case DIF_OP_ULDUH:
 		case DIF_OP_ULDUW:
 		case DIF_OP_ULDX:
 		case DIF_OP_STB:
 		case DIF_OP_STH:
 		case DIF_OP_STW:
 		case DIF_OP_STX:
 		case DIF_OP_ALLOCS:
 		case DIF_OP_CMP:
 		case DIF_OP_SCMP:
 		case DIF_OP_TST:
 		case DIF_OP_BA:
 		case DIF_OP_BE:
 		case DIF_OP_BNE:
 		case DIF_OP_BG:
 		case DIF_OP_BGU:
 		case DIF_OP_BGE:
 		case DIF_OP_BGEU:
 		case DIF_OP_BL:
 		case DIF_OP_BLU:
 		case DIF_OP_BLE:
 		case DIF_OP_BLEU:
 		case DIF_OP_RET:
 		case DIF_OP_NOP:
 		case DIF_OP_POPTS:
 		case DIF_OP_FLUSHTS:
 		case DIF_OP_SETX:
 		case DIF_OP_SETS:
 		case DIF_OP_LDGA:
 		case DIF_OP_LDLS:
 		case DIF_OP_STGS:
 		case DIF_OP_STLS:
 		case DIF_OP_PUSHTR:
 		case DIF_OP_PUSHTV:
 			break;
 
 		case DIF_OP_LDGS:
 			if (v >= DIF_VAR_OTHER_UBASE)
 				break;
 
 			if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9)
 				break;
 
 			if (v == DIF_VAR_CURTHREAD || v == DIF_VAR_PID ||
 			    v == DIF_VAR_PPID || v == DIF_VAR_TID ||
 			    v == DIF_VAR_EXECARGS ||
 			    v == DIF_VAR_EXECNAME || v == DIF_VAR_ZONENAME ||
 			    v == DIF_VAR_UID || v == DIF_VAR_GID)
 				break;
 
 			err += efunc(pc, "illegal variable %u\n", v);
 			break;
 
 		case DIF_OP_LDTA:
 		case DIF_OP_LDTS:
 		case DIF_OP_LDGAA:
 		case DIF_OP_LDTAA:
 			err += efunc(pc, "illegal dynamic variable load\n");
 			break;
 
 		case DIF_OP_STTS:
 		case DIF_OP_STGAA:
 		case DIF_OP_STTAA:
 			err += efunc(pc, "illegal dynamic variable store\n");
 			break;
 
 		case DIF_OP_CALL:
 			if (subr == DIF_SUBR_ALLOCA ||
 			    subr == DIF_SUBR_BCOPY ||
 			    subr == DIF_SUBR_COPYIN ||
 			    subr == DIF_SUBR_COPYINTO ||
 			    subr == DIF_SUBR_COPYINSTR ||
 			    subr == DIF_SUBR_INDEX ||
 			    subr == DIF_SUBR_INET_NTOA ||
 			    subr == DIF_SUBR_INET_NTOA6 ||
 			    subr == DIF_SUBR_INET_NTOP ||
 			    subr == DIF_SUBR_JSON ||
 			    subr == DIF_SUBR_LLTOSTR ||
 			    subr == DIF_SUBR_STRTOLL ||
 			    subr == DIF_SUBR_RINDEX ||
 			    subr == DIF_SUBR_STRCHR ||
 			    subr == DIF_SUBR_STRJOIN ||
 			    subr == DIF_SUBR_STRRCHR ||
 			    subr == DIF_SUBR_STRSTR ||
 			    subr == DIF_SUBR_HTONS ||
 			    subr == DIF_SUBR_HTONL ||
 			    subr == DIF_SUBR_HTONLL ||
 			    subr == DIF_SUBR_NTOHS ||
 			    subr == DIF_SUBR_NTOHL ||
 			    subr == DIF_SUBR_NTOHLL ||
 			    subr == DIF_SUBR_MEMREF)
 				break;
 #ifdef __FreeBSD__
 			if (subr == DIF_SUBR_MEMSTR)
 				break;
 #endif
 
 			err += efunc(pc, "invalid subr %u\n", subr);
 			break;
 
 		default:
 			err += efunc(pc, "invalid opcode %u\n",
 			    DIF_INSTR_OP(instr));
 		}
 	}
 
 	return (err);
 }
 
 /*
  * Returns 1 if the expression in the DIF object can be cached on a per-thread
  * basis; 0 if not.
  */
 static int
 dtrace_difo_cacheable(dtrace_difo_t *dp)
 {
 	int i;
 
 	if (dp == NULL)
 		return (0);
 
 	for (i = 0; i < dp->dtdo_varlen; i++) {
 		dtrace_difv_t *v = &dp->dtdo_vartab[i];
 
 		if (v->dtdv_scope != DIFV_SCOPE_GLOBAL)
 			continue;
 
 		switch (v->dtdv_id) {
 		case DIF_VAR_CURTHREAD:
 		case DIF_VAR_PID:
 		case DIF_VAR_TID:
 		case DIF_VAR_EXECARGS:
 		case DIF_VAR_EXECNAME:
 		case DIF_VAR_ZONENAME:
 			break;
 
 		default:
 			return (0);
 		}
 	}
 
 	/*
 	 * This DIF object may be cacheable.  Now we need to look for any
 	 * array loading instructions, any memory loading instructions, or
 	 * any stores to thread-local variables.
 	 */
 	for (i = 0; i < dp->dtdo_len; i++) {
 		uint_t op = DIF_INSTR_OP(dp->dtdo_buf[i]);
 
 		if ((op >= DIF_OP_LDSB && op <= DIF_OP_LDX) ||
 		    (op >= DIF_OP_ULDSB && op <= DIF_OP_ULDX) ||
 		    (op >= DIF_OP_RLDSB && op <= DIF_OP_RLDX) ||
 		    op == DIF_OP_LDGA || op == DIF_OP_STTS)
 			return (0);
 	}
 
 	return (1);
 }
 
 static void
 dtrace_difo_hold(dtrace_difo_t *dp)
 {
 	int i;
 
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 
 	dp->dtdo_refcnt++;
 	ASSERT(dp->dtdo_refcnt != 0);
 
 	/*
 	 * We need to check this DIF object for references to the variable
 	 * DIF_VAR_VTIMESTAMP.
 	 */
 	for (i = 0; i < dp->dtdo_varlen; i++) {
 		dtrace_difv_t *v = &dp->dtdo_vartab[i];
 
 		if (v->dtdv_id != DIF_VAR_VTIMESTAMP)
 			continue;
 
 		if (dtrace_vtime_references++ == 0)
 			dtrace_vtime_enable();
 	}
 }
 
 /*
  * This routine calculates the dynamic variable chunksize for a given DIF
  * object.  The calculation is not fool-proof, and can probably be tricked by
  * malicious DIF -- but it works for all compiler-generated DIF.  Because this
  * calculation is likely imperfect, dtrace_dynvar() is able to gracefully fail
  * if a dynamic variable size exceeds the chunksize.
  */
 static void
 dtrace_difo_chunksize(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
 {
 	uint64_t sval = 0;
 	dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */
 	const dif_instr_t *text = dp->dtdo_buf;
 	uint_t pc, srd = 0;
 	uint_t ttop = 0;
 	size_t size, ksize;
 	uint_t id, i;
 
 	for (pc = 0; pc < dp->dtdo_len; pc++) {
 		dif_instr_t instr = text[pc];
 		uint_t op = DIF_INSTR_OP(instr);
 		uint_t rd = DIF_INSTR_RD(instr);
 		uint_t r1 = DIF_INSTR_R1(instr);
 		uint_t nkeys = 0;
 		uchar_t scope = 0;
 
 		dtrace_key_t *key = tupregs;
 
 		switch (op) {
 		case DIF_OP_SETX:
 			sval = dp->dtdo_inttab[DIF_INSTR_INTEGER(instr)];
 			srd = rd;
 			continue;
 
 		case DIF_OP_STTS:
 			key = &tupregs[DIF_DTR_NREGS];
 			key[0].dttk_size = 0;
 			key[1].dttk_size = 0;
 			nkeys = 2;
 			scope = DIFV_SCOPE_THREAD;
 			break;
 
 		case DIF_OP_STGAA:
 		case DIF_OP_STTAA:
 			nkeys = ttop;
 
 			if (DIF_INSTR_OP(instr) == DIF_OP_STTAA)
 				key[nkeys++].dttk_size = 0;
 
 			key[nkeys++].dttk_size = 0;
 
 			if (op == DIF_OP_STTAA) {
 				scope = DIFV_SCOPE_THREAD;
 			} else {
 				scope = DIFV_SCOPE_GLOBAL;
 			}
 
 			break;
 
 		case DIF_OP_PUSHTR:
 			if (ttop == DIF_DTR_NREGS)
 				return;
 
 			if ((srd == 0 || sval == 0) && r1 == DIF_TYPE_STRING) {
 				/*
 				 * If the register for the size of the "pushtr"
 				 * is %r0 (or the value is 0) and the type is
 				 * a string, we'll use the system-wide default
 				 * string size.
 				 */
 				tupregs[ttop++].dttk_size =
 				    dtrace_strsize_default;
 			} else {
 				if (srd == 0)
 					return;
 
 				if (sval > LONG_MAX)
 					return;
 
 				tupregs[ttop++].dttk_size = sval;
 			}
 
 			break;
 
 		case DIF_OP_PUSHTV:
 			if (ttop == DIF_DTR_NREGS)
 				return;
 
 			tupregs[ttop++].dttk_size = 0;
 			break;
 
 		case DIF_OP_FLUSHTS:
 			ttop = 0;
 			break;
 
 		case DIF_OP_POPTS:
 			if (ttop != 0)
 				ttop--;
 			break;
 		}
 
 		sval = 0;
 		srd = 0;
 
 		if (nkeys == 0)
 			continue;
 
 		/*
 		 * We have a dynamic variable allocation; calculate its size.
 		 */
 		for (ksize = 0, i = 0; i < nkeys; i++)
 			ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t));
 
 		size = sizeof (dtrace_dynvar_t);
 		size += sizeof (dtrace_key_t) * (nkeys - 1);
 		size += ksize;
 
 		/*
 		 * Now we need to determine the size of the stored data.
 		 */
 		id = DIF_INSTR_VAR(instr);
 
 		for (i = 0; i < dp->dtdo_varlen; i++) {
 			dtrace_difv_t *v = &dp->dtdo_vartab[i];
 
 			if (v->dtdv_id == id && v->dtdv_scope == scope) {
 				size += v->dtdv_type.dtdt_size;
 				break;
 			}
 		}
 
 		if (i == dp->dtdo_varlen)
 			return;
 
 		/*
 		 * We have the size.  If this is larger than the chunk size
 		 * for our dynamic variable state, reset the chunk size.
 		 */
 		size = P2ROUNDUP(size, sizeof (uint64_t));
 
 		/*
 		 * Before setting the chunk size, check that we're not going
 		 * to set it to a negative value...
 		 */
 		if (size > LONG_MAX)
 			return;
 
 		/*
 		 * ...and make certain that we didn't badly overflow.
 		 */
 		if (size < ksize || size < sizeof (dtrace_dynvar_t))
 			return;
 
 		if (size > vstate->dtvs_dynvars.dtds_chunksize)
 			vstate->dtvs_dynvars.dtds_chunksize = size;
 	}
 }
 
 static void
 dtrace_difo_init(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
 {
 	int i, oldsvars, osz, nsz, otlocals, ntlocals;
 	uint_t id;
 
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 	ASSERT(dp->dtdo_buf != NULL && dp->dtdo_len != 0);
 
 	for (i = 0; i < dp->dtdo_varlen; i++) {
 		dtrace_difv_t *v = &dp->dtdo_vartab[i];
 		dtrace_statvar_t *svar, ***svarp = NULL;
 		size_t dsize = 0;
 		uint8_t scope = v->dtdv_scope;
 		int *np = NULL;
 
 		if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE)
 			continue;
 
 		id -= DIF_VAR_OTHER_UBASE;
 
 		switch (scope) {
 		case DIFV_SCOPE_THREAD:
 			while (id >= (otlocals = vstate->dtvs_ntlocals)) {
 				dtrace_difv_t *tlocals;
 
 				if ((ntlocals = (otlocals << 1)) == 0)
 					ntlocals = 1;
 
 				osz = otlocals * sizeof (dtrace_difv_t);
 				nsz = ntlocals * sizeof (dtrace_difv_t);
 
 				tlocals = kmem_zalloc(nsz, KM_SLEEP);
 
 				if (osz != 0) {
 					bcopy(vstate->dtvs_tlocals,
 					    tlocals, osz);
 					kmem_free(vstate->dtvs_tlocals, osz);
 				}
 
 				vstate->dtvs_tlocals = tlocals;
 				vstate->dtvs_ntlocals = ntlocals;
 			}
 
 			vstate->dtvs_tlocals[id] = *v;
 			continue;
 
 		case DIFV_SCOPE_LOCAL:
 			np = &vstate->dtvs_nlocals;
 			svarp = &vstate->dtvs_locals;
 
 			if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF)
 				dsize = NCPU * (v->dtdv_type.dtdt_size +
 				    sizeof (uint64_t));
 			else
 				dsize = NCPU * sizeof (uint64_t);
 
 			break;
 
 		case DIFV_SCOPE_GLOBAL:
 			np = &vstate->dtvs_nglobals;
 			svarp = &vstate->dtvs_globals;
 
 			if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF)
 				dsize = v->dtdv_type.dtdt_size +
 				    sizeof (uint64_t);
 
 			break;
 
 		default:
 			ASSERT(0);
 		}
 
 		while (id >= (oldsvars = *np)) {
 			dtrace_statvar_t **statics;
 			int newsvars, oldsize, newsize;
 
 			if ((newsvars = (oldsvars << 1)) == 0)
 				newsvars = 1;
 
 			oldsize = oldsvars * sizeof (dtrace_statvar_t *);
 			newsize = newsvars * sizeof (dtrace_statvar_t *);
 
 			statics = kmem_zalloc(newsize, KM_SLEEP);
 
 			if (oldsize != 0) {
 				bcopy(*svarp, statics, oldsize);
 				kmem_free(*svarp, oldsize);
 			}
 
 			*svarp = statics;
 			*np = newsvars;
 		}
 
 		if ((svar = (*svarp)[id]) == NULL) {
 			svar = kmem_zalloc(sizeof (dtrace_statvar_t), KM_SLEEP);
 			svar->dtsv_var = *v;
 
 			if ((svar->dtsv_size = dsize) != 0) {
 				svar->dtsv_data = (uint64_t)(uintptr_t)
 				    kmem_zalloc(dsize, KM_SLEEP);
 			}
 
 			(*svarp)[id] = svar;
 		}
 
 		svar->dtsv_refcnt++;
 	}
 
 	dtrace_difo_chunksize(dp, vstate);
 	dtrace_difo_hold(dp);
 }
 
 static dtrace_difo_t *
 dtrace_difo_duplicate(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
 {
 	dtrace_difo_t *new;
 	size_t sz;
 
 	ASSERT(dp->dtdo_buf != NULL);
 	ASSERT(dp->dtdo_refcnt != 0);
 
 	new = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP);
 
 	ASSERT(dp->dtdo_buf != NULL);
 	sz = dp->dtdo_len * sizeof (dif_instr_t);
 	new->dtdo_buf = kmem_alloc(sz, KM_SLEEP);
 	bcopy(dp->dtdo_buf, new->dtdo_buf, sz);
 	new->dtdo_len = dp->dtdo_len;
 
 	if (dp->dtdo_strtab != NULL) {
 		ASSERT(dp->dtdo_strlen != 0);
 		new->dtdo_strtab = kmem_alloc(dp->dtdo_strlen, KM_SLEEP);
 		bcopy(dp->dtdo_strtab, new->dtdo_strtab, dp->dtdo_strlen);
 		new->dtdo_strlen = dp->dtdo_strlen;
 	}
 
 	if (dp->dtdo_inttab != NULL) {
 		ASSERT(dp->dtdo_intlen != 0);
 		sz = dp->dtdo_intlen * sizeof (uint64_t);
 		new->dtdo_inttab = kmem_alloc(sz, KM_SLEEP);
 		bcopy(dp->dtdo_inttab, new->dtdo_inttab, sz);
 		new->dtdo_intlen = dp->dtdo_intlen;
 	}
 
 	if (dp->dtdo_vartab != NULL) {
 		ASSERT(dp->dtdo_varlen != 0);
 		sz = dp->dtdo_varlen * sizeof (dtrace_difv_t);
 		new->dtdo_vartab = kmem_alloc(sz, KM_SLEEP);
 		bcopy(dp->dtdo_vartab, new->dtdo_vartab, sz);
 		new->dtdo_varlen = dp->dtdo_varlen;
 	}
 
 	dtrace_difo_init(new, vstate);
 	return (new);
 }
 
 static void
 dtrace_difo_destroy(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
 {
 	int i;
 
 	ASSERT(dp->dtdo_refcnt == 0);
 
 	for (i = 0; i < dp->dtdo_varlen; i++) {
 		dtrace_difv_t *v = &dp->dtdo_vartab[i];
 		dtrace_statvar_t *svar, **svarp = NULL;
 		uint_t id;
 		uint8_t scope = v->dtdv_scope;
 		int *np = NULL;
 
 		switch (scope) {
 		case DIFV_SCOPE_THREAD:
 			continue;
 
 		case DIFV_SCOPE_LOCAL:
 			np = &vstate->dtvs_nlocals;
 			svarp = vstate->dtvs_locals;
 			break;
 
 		case DIFV_SCOPE_GLOBAL:
 			np = &vstate->dtvs_nglobals;
 			svarp = vstate->dtvs_globals;
 			break;
 
 		default:
 			ASSERT(0);
 		}
 
 		if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE)
 			continue;
 
 		id -= DIF_VAR_OTHER_UBASE;
 		ASSERT(id < *np);
 
 		svar = svarp[id];
 		ASSERT(svar != NULL);
 		ASSERT(svar->dtsv_refcnt > 0);
 
 		if (--svar->dtsv_refcnt > 0)
 			continue;
 
 		if (svar->dtsv_size != 0) {
 			ASSERT(svar->dtsv_data != 0);
 			kmem_free((void *)(uintptr_t)svar->dtsv_data,
 			    svar->dtsv_size);
 		}
 
 		kmem_free(svar, sizeof (dtrace_statvar_t));
 		svarp[id] = NULL;
 	}
 
 	if (dp->dtdo_buf != NULL)
 		kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t));
 	if (dp->dtdo_inttab != NULL)
 		kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t));
 	if (dp->dtdo_strtab != NULL)
 		kmem_free(dp->dtdo_strtab, dp->dtdo_strlen);
 	if (dp->dtdo_vartab != NULL)
 		kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t));
 
 	kmem_free(dp, sizeof (dtrace_difo_t));
 }
 
 static void
 dtrace_difo_release(dtrace_difo_t *dp, dtrace_vstate_t *vstate)
 {
 	int i;
 
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 	ASSERT(dp->dtdo_refcnt != 0);
 
 	for (i = 0; i < dp->dtdo_varlen; i++) {
 		dtrace_difv_t *v = &dp->dtdo_vartab[i];
 
 		if (v->dtdv_id != DIF_VAR_VTIMESTAMP)
 			continue;
 
 		ASSERT(dtrace_vtime_references > 0);
 		if (--dtrace_vtime_references == 0)
 			dtrace_vtime_disable();
 	}
 
 	if (--dp->dtdo_refcnt == 0)
 		dtrace_difo_destroy(dp, vstate);
 }
 
 /*
  * DTrace Format Functions
  */
 static uint16_t
 dtrace_format_add(dtrace_state_t *state, char *str)
 {
 	char *fmt, **new;
 	uint16_t ndx, len = strlen(str) + 1;
 
 	fmt = kmem_zalloc(len, KM_SLEEP);
 	bcopy(str, fmt, len);
 
 	for (ndx = 0; ndx < state->dts_nformats; ndx++) {
 		if (state->dts_formats[ndx] == NULL) {
 			state->dts_formats[ndx] = fmt;
 			return (ndx + 1);
 		}
 	}
 
 	if (state->dts_nformats == USHRT_MAX) {
 		/*
 		 * This is only likely if a denial-of-service attack is being
 		 * attempted.  As such, it's okay to fail silently here.
 		 */
 		kmem_free(fmt, len);
 		return (0);
 	}
 
 	/*
 	 * For simplicity, we always resize the formats array to be exactly the
 	 * number of formats.
 	 */
 	ndx = state->dts_nformats++;
 	new = kmem_alloc((ndx + 1) * sizeof (char *), KM_SLEEP);
 
 	if (state->dts_formats != NULL) {
 		ASSERT(ndx != 0);
 		bcopy(state->dts_formats, new, ndx * sizeof (char *));
 		kmem_free(state->dts_formats, ndx * sizeof (char *));
 	}
 
 	state->dts_formats = new;
 	state->dts_formats[ndx] = fmt;
 
 	return (ndx + 1);
 }
 
 static void
 dtrace_format_remove(dtrace_state_t *state, uint16_t format)
 {
 	char *fmt;
 
 	ASSERT(state->dts_formats != NULL);
 	ASSERT(format <= state->dts_nformats);
 	ASSERT(state->dts_formats[format - 1] != NULL);
 
 	fmt = state->dts_formats[format - 1];
 	kmem_free(fmt, strlen(fmt) + 1);
 	state->dts_formats[format - 1] = NULL;
 }
 
 static void
 dtrace_format_destroy(dtrace_state_t *state)
 {
 	int i;
 
 	if (state->dts_nformats == 0) {
 		ASSERT(state->dts_formats == NULL);
 		return;
 	}
 
 	ASSERT(state->dts_formats != NULL);
 
 	for (i = 0; i < state->dts_nformats; i++) {
 		char *fmt = state->dts_formats[i];
 
 		if (fmt == NULL)
 			continue;
 
 		kmem_free(fmt, strlen(fmt) + 1);
 	}
 
 	kmem_free(state->dts_formats, state->dts_nformats * sizeof (char *));
 	state->dts_nformats = 0;
 	state->dts_formats = NULL;
 }
 
 /*
  * DTrace Predicate Functions
  */
 static dtrace_predicate_t *
 dtrace_predicate_create(dtrace_difo_t *dp)
 {
 	dtrace_predicate_t *pred;
 
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 	ASSERT(dp->dtdo_refcnt != 0);
 
 	pred = kmem_zalloc(sizeof (dtrace_predicate_t), KM_SLEEP);
 	pred->dtp_difo = dp;
 	pred->dtp_refcnt = 1;
 
 	if (!dtrace_difo_cacheable(dp))
 		return (pred);
 
 	if (dtrace_predcache_id == DTRACE_CACHEIDNONE) {
 		/*
 		 * This is only theoretically possible -- we have had 2^32
 		 * cacheable predicates on this machine.  We cannot allow any
 		 * more predicates to become cacheable:  as unlikely as it is,
 		 * there may be a thread caching a (now stale) predicate cache
 		 * ID. (N.B.: the temptation is being successfully resisted to
 		 * have this cmn_err() "Holy shit -- we executed this code!")
 		 */
 		return (pred);
 	}
 
 	pred->dtp_cacheid = dtrace_predcache_id++;
 
 	return (pred);
 }
 
 static void
 dtrace_predicate_hold(dtrace_predicate_t *pred)
 {
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 	ASSERT(pred->dtp_difo != NULL && pred->dtp_difo->dtdo_refcnt != 0);
 	ASSERT(pred->dtp_refcnt > 0);
 
 	pred->dtp_refcnt++;
 }
 
 static void
 dtrace_predicate_release(dtrace_predicate_t *pred, dtrace_vstate_t *vstate)
 {
 	dtrace_difo_t *dp = pred->dtp_difo;
 
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 	ASSERT(dp != NULL && dp->dtdo_refcnt != 0);
 	ASSERT(pred->dtp_refcnt > 0);
 
 	if (--pred->dtp_refcnt == 0) {
 		dtrace_difo_release(pred->dtp_difo, vstate);
 		kmem_free(pred, sizeof (dtrace_predicate_t));
 	}
 }
 
 /*
  * DTrace Action Description Functions
  */
 static dtrace_actdesc_t *
 dtrace_actdesc_create(dtrace_actkind_t kind, uint32_t ntuple,
     uint64_t uarg, uint64_t arg)
 {
 	dtrace_actdesc_t *act;
 
 #ifdef illumos
 	ASSERT(!DTRACEACT_ISPRINTFLIKE(kind) || (arg != NULL &&
 	    arg >= KERNELBASE) || (arg == NULL && kind == DTRACEACT_PRINTA));
 #endif
 
 	act = kmem_zalloc(sizeof (dtrace_actdesc_t), KM_SLEEP);
 	act->dtad_kind = kind;
 	act->dtad_ntuple = ntuple;
 	act->dtad_uarg = uarg;
 	act->dtad_arg = arg;
 	act->dtad_refcnt = 1;
 
 	return (act);
 }
 
 static void
 dtrace_actdesc_hold(dtrace_actdesc_t *act)
 {
 	ASSERT(act->dtad_refcnt >= 1);
 	act->dtad_refcnt++;
 }
 
 static void
 dtrace_actdesc_release(dtrace_actdesc_t *act, dtrace_vstate_t *vstate)
 {
 	dtrace_actkind_t kind = act->dtad_kind;
 	dtrace_difo_t *dp;
 
 	ASSERT(act->dtad_refcnt >= 1);
 
 	if (--act->dtad_refcnt != 0)
 		return;
 
 	if ((dp = act->dtad_difo) != NULL)
 		dtrace_difo_release(dp, vstate);
 
 	if (DTRACEACT_ISPRINTFLIKE(kind)) {
 		char *str = (char *)(uintptr_t)act->dtad_arg;
 
 #ifdef illumos
 		ASSERT((str != NULL && (uintptr_t)str >= KERNELBASE) ||
 		    (str == NULL && act->dtad_kind == DTRACEACT_PRINTA));
 #endif
 
 		if (str != NULL)
 			kmem_free(str, strlen(str) + 1);
 	}
 
 	kmem_free(act, sizeof (dtrace_actdesc_t));
 }
 
 /*
  * DTrace ECB Functions
  */
 static dtrace_ecb_t *
 dtrace_ecb_add(dtrace_state_t *state, dtrace_probe_t *probe)
 {
 	dtrace_ecb_t *ecb;
 	dtrace_epid_t epid;
 
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 
 	ecb = kmem_zalloc(sizeof (dtrace_ecb_t), KM_SLEEP);
 	ecb->dte_predicate = NULL;
 	ecb->dte_probe = probe;
 
 	/*
 	 * The default size is the size of the default action: recording
 	 * the header.
 	 */
 	ecb->dte_size = ecb->dte_needed = sizeof (dtrace_rechdr_t);
 	ecb->dte_alignment = sizeof (dtrace_epid_t);
 
 	epid = state->dts_epid++;
 
 	if (epid - 1 >= state->dts_necbs) {
 		dtrace_ecb_t **oecbs = state->dts_ecbs, **ecbs;
 		int necbs = state->dts_necbs << 1;
 
 		ASSERT(epid == state->dts_necbs + 1);
 
 		if (necbs == 0) {
 			ASSERT(oecbs == NULL);
 			necbs = 1;
 		}
 
 		ecbs = kmem_zalloc(necbs * sizeof (*ecbs), KM_SLEEP);
 
 		if (oecbs != NULL)
 			bcopy(oecbs, ecbs, state->dts_necbs * sizeof (*ecbs));
 
 		dtrace_membar_producer();
 		state->dts_ecbs = ecbs;
 
 		if (oecbs != NULL) {
 			/*
 			 * If this state is active, we must dtrace_sync()
 			 * before we can free the old dts_ecbs array:  we're
 			 * coming in hot, and there may be active ring
 			 * buffer processing (which indexes into the dts_ecbs
 			 * array) on another CPU.
 			 */
 			if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE)
 				dtrace_sync();
 
 			kmem_free(oecbs, state->dts_necbs * sizeof (*ecbs));
 		}
 
 		dtrace_membar_producer();
 		state->dts_necbs = necbs;
 	}
 
 	ecb->dte_state = state;
 
 	ASSERT(state->dts_ecbs[epid - 1] == NULL);
 	dtrace_membar_producer();
 	state->dts_ecbs[(ecb->dte_epid = epid) - 1] = ecb;
 
 	return (ecb);
 }
 
 static void
 dtrace_ecb_enable(dtrace_ecb_t *ecb)
 {
 	dtrace_probe_t *probe = ecb->dte_probe;
 
 	ASSERT(MUTEX_HELD(&cpu_lock));
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 	ASSERT(ecb->dte_next == NULL);
 
 	if (probe == NULL) {
 		/*
 		 * This is the NULL probe -- there's nothing to do.
 		 */
 		return;
 	}
 
 	if (probe->dtpr_ecb == NULL) {
 		dtrace_provider_t *prov = probe->dtpr_provider;
 
 		/*
 		 * We're the first ECB on this probe.
 		 */
 		probe->dtpr_ecb = probe->dtpr_ecb_last = ecb;
 
 		if (ecb->dte_predicate != NULL)
 			probe->dtpr_predcache = ecb->dte_predicate->dtp_cacheid;
 
 		prov->dtpv_pops.dtps_enable(prov->dtpv_arg,
 		    probe->dtpr_id, probe->dtpr_arg);
 	} else {
 		/*
 		 * This probe is already active.  Swing the last pointer to
 		 * point to the new ECB, and issue a dtrace_sync() to assure
 		 * that all CPUs have seen the change.
 		 */
 		ASSERT(probe->dtpr_ecb_last != NULL);
 		probe->dtpr_ecb_last->dte_next = ecb;
 		probe->dtpr_ecb_last = ecb;
 		probe->dtpr_predcache = 0;
 
 		dtrace_sync();
 	}
 }
 
 static int
 dtrace_ecb_resize(dtrace_ecb_t *ecb)
 {
 	dtrace_action_t *act;
 	uint32_t curneeded = UINT32_MAX;
 	uint32_t aggbase = UINT32_MAX;
 
 	/*
 	 * If we record anything, we always record the dtrace_rechdr_t.  (And
 	 * we always record it first.)
 	 */
 	ecb->dte_size = sizeof (dtrace_rechdr_t);
 	ecb->dte_alignment = sizeof (dtrace_epid_t);
 
 	for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
 		dtrace_recdesc_t *rec = &act->dta_rec;
 		ASSERT(rec->dtrd_size > 0 || rec->dtrd_alignment == 1);
 
 		ecb->dte_alignment = MAX(ecb->dte_alignment,
 		    rec->dtrd_alignment);
 
 		if (DTRACEACT_ISAGG(act->dta_kind)) {
 			dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act;
 
 			ASSERT(rec->dtrd_size != 0);
 			ASSERT(agg->dtag_first != NULL);
 			ASSERT(act->dta_prev->dta_intuple);
 			ASSERT(aggbase != UINT32_MAX);
 			ASSERT(curneeded != UINT32_MAX);
 
 			agg->dtag_base = aggbase;
 
 			curneeded = P2ROUNDUP(curneeded, rec->dtrd_alignment);
 			rec->dtrd_offset = curneeded;
 			if (curneeded + rec->dtrd_size < curneeded)
 				return (EINVAL);
 			curneeded += rec->dtrd_size;
 			ecb->dte_needed = MAX(ecb->dte_needed, curneeded);
 
 			aggbase = UINT32_MAX;
 			curneeded = UINT32_MAX;
 		} else if (act->dta_intuple) {
 			if (curneeded == UINT32_MAX) {
 				/*
 				 * This is the first record in a tuple.  Align
 				 * curneeded to be at offset 4 in an 8-byte
 				 * aligned block.
 				 */
 				ASSERT(act->dta_prev == NULL ||
 				    !act->dta_prev->dta_intuple);
 				ASSERT3U(aggbase, ==, UINT32_MAX);
 				curneeded = P2PHASEUP(ecb->dte_size,
 				    sizeof (uint64_t), sizeof (dtrace_aggid_t));
 
 				aggbase = curneeded - sizeof (dtrace_aggid_t);
 				ASSERT(IS_P2ALIGNED(aggbase,
 				    sizeof (uint64_t)));
 			}
 			curneeded = P2ROUNDUP(curneeded, rec->dtrd_alignment);
 			rec->dtrd_offset = curneeded;
 			if (curneeded + rec->dtrd_size < curneeded)
 				return (EINVAL);
 			curneeded += rec->dtrd_size;
 		} else {
 			/* tuples must be followed by an aggregation */
 			ASSERT(act->dta_prev == NULL ||
 			    !act->dta_prev->dta_intuple);
 
 			ecb->dte_size = P2ROUNDUP(ecb->dte_size,
 			    rec->dtrd_alignment);
 			rec->dtrd_offset = ecb->dte_size;
 			if (ecb->dte_size + rec->dtrd_size < ecb->dte_size)
 				return (EINVAL);
 			ecb->dte_size += rec->dtrd_size;
 			ecb->dte_needed = MAX(ecb->dte_needed, ecb->dte_size);
 		}
 	}
 
 	if ((act = ecb->dte_action) != NULL &&
 	    !(act->dta_kind == DTRACEACT_SPECULATE && act->dta_next == NULL) &&
 	    ecb->dte_size == sizeof (dtrace_rechdr_t)) {
 		/*
 		 * If the size is still sizeof (dtrace_rechdr_t), then all
 		 * actions store no data; set the size to 0.
 		 */
 		ecb->dte_size = 0;
 	}
 
 	ecb->dte_size = P2ROUNDUP(ecb->dte_size, sizeof (dtrace_epid_t));
 	ecb->dte_needed = P2ROUNDUP(ecb->dte_needed, (sizeof (dtrace_epid_t)));
 	ecb->dte_state->dts_needed = MAX(ecb->dte_state->dts_needed,
 	    ecb->dte_needed);
 	return (0);
 }
 
 static dtrace_action_t *
 dtrace_ecb_aggregation_create(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc)
 {
 	dtrace_aggregation_t *agg;
 	size_t size = sizeof (uint64_t);
 	int ntuple = desc->dtad_ntuple;
 	dtrace_action_t *act;
 	dtrace_recdesc_t *frec;
 	dtrace_aggid_t aggid;
 	dtrace_state_t *state = ecb->dte_state;
 
 	agg = kmem_zalloc(sizeof (dtrace_aggregation_t), KM_SLEEP);
 	agg->dtag_ecb = ecb;
 
 	ASSERT(DTRACEACT_ISAGG(desc->dtad_kind));
 
 	switch (desc->dtad_kind) {
 	case DTRACEAGG_MIN:
 		agg->dtag_initial = INT64_MAX;
 		agg->dtag_aggregate = dtrace_aggregate_min;
 		break;
 
 	case DTRACEAGG_MAX:
 		agg->dtag_initial = INT64_MIN;
 		agg->dtag_aggregate = dtrace_aggregate_max;
 		break;
 
 	case DTRACEAGG_COUNT:
 		agg->dtag_aggregate = dtrace_aggregate_count;
 		break;
 
 	case DTRACEAGG_QUANTIZE:
 		agg->dtag_aggregate = dtrace_aggregate_quantize;
 		size = (((sizeof (uint64_t) * NBBY) - 1) * 2 + 1) *
 		    sizeof (uint64_t);
 		break;
 
 	case DTRACEAGG_LQUANTIZE: {
 		uint16_t step = DTRACE_LQUANTIZE_STEP(desc->dtad_arg);
 		uint16_t levels = DTRACE_LQUANTIZE_LEVELS(desc->dtad_arg);
 
 		agg->dtag_initial = desc->dtad_arg;
 		agg->dtag_aggregate = dtrace_aggregate_lquantize;
 
 		if (step == 0 || levels == 0)
 			goto err;
 
 		size = levels * sizeof (uint64_t) + 3 * sizeof (uint64_t);
 		break;
 	}
 
 	case DTRACEAGG_LLQUANTIZE: {
 		uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(desc->dtad_arg);
 		uint16_t low = DTRACE_LLQUANTIZE_LOW(desc->dtad_arg);
 		uint16_t high = DTRACE_LLQUANTIZE_HIGH(desc->dtad_arg);
 		uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(desc->dtad_arg);
 		int64_t v;
 
 		agg->dtag_initial = desc->dtad_arg;
 		agg->dtag_aggregate = dtrace_aggregate_llquantize;
 
 		if (factor < 2 || low >= high || nsteps < factor)
 			goto err;
 
 		/*
 		 * Now check that the number of steps evenly divides a power
 		 * of the factor.  (This assures both integer bucket size and
 		 * linearity within each magnitude.)
 		 */
 		for (v = factor; v < nsteps; v *= factor)
 			continue;
 
 		if ((v % nsteps) || (nsteps % factor))
 			goto err;
 
 		size = (dtrace_aggregate_llquantize_bucket(factor,
 		    low, high, nsteps, INT64_MAX) + 2) * sizeof (uint64_t);
 		break;
 	}
 
 	case DTRACEAGG_AVG:
 		agg->dtag_aggregate = dtrace_aggregate_avg;
 		size = sizeof (uint64_t) * 2;
 		break;
 
 	case DTRACEAGG_STDDEV:
 		agg->dtag_aggregate = dtrace_aggregate_stddev;
 		size = sizeof (uint64_t) * 4;
 		break;
 
 	case DTRACEAGG_SUM:
 		agg->dtag_aggregate = dtrace_aggregate_sum;
 		break;
 
 	default:
 		goto err;
 	}
 
 	agg->dtag_action.dta_rec.dtrd_size = size;
 
 	if (ntuple == 0)
 		goto err;
 
 	/*
 	 * We must make sure that we have enough actions for the n-tuple.
 	 */
 	for (act = ecb->dte_action_last; act != NULL; act = act->dta_prev) {
 		if (DTRACEACT_ISAGG(act->dta_kind))
 			break;
 
 		if (--ntuple == 0) {
 			/*
 			 * This is the action with which our n-tuple begins.
 			 */
 			agg->dtag_first = act;
 			goto success;
 		}
 	}
 
 	/*
 	 * This n-tuple is short by ntuple elements.  Return failure.
 	 */
 	ASSERT(ntuple != 0);
 err:
 	kmem_free(agg, sizeof (dtrace_aggregation_t));
 	return (NULL);
 
 success:
 	/*
 	 * If the last action in the tuple has a size of zero, it's actually
 	 * an expression argument for the aggregating action.
 	 */
 	ASSERT(ecb->dte_action_last != NULL);
 	act = ecb->dte_action_last;
 
 	if (act->dta_kind == DTRACEACT_DIFEXPR) {
 		ASSERT(act->dta_difo != NULL);
 
 		if (act->dta_difo->dtdo_rtype.dtdt_size == 0)
 			agg->dtag_hasarg = 1;
 	}
 
 	/*
 	 * We need to allocate an id for this aggregation.
 	 */
 #ifdef illumos
 	aggid = (dtrace_aggid_t)(uintptr_t)vmem_alloc(state->dts_aggid_arena, 1,
 	    VM_BESTFIT | VM_SLEEP);
 #else
 	aggid = alloc_unr(state->dts_aggid_arena);
 #endif
 
 	if (aggid - 1 >= state->dts_naggregations) {
 		dtrace_aggregation_t **oaggs = state->dts_aggregations;
 		dtrace_aggregation_t **aggs;
 		int naggs = state->dts_naggregations << 1;
 		int onaggs = state->dts_naggregations;
 
 		ASSERT(aggid == state->dts_naggregations + 1);
 
 		if (naggs == 0) {
 			ASSERT(oaggs == NULL);
 			naggs = 1;
 		}
 
 		aggs = kmem_zalloc(naggs * sizeof (*aggs), KM_SLEEP);
 
 		if (oaggs != NULL) {
 			bcopy(oaggs, aggs, onaggs * sizeof (*aggs));
 			kmem_free(oaggs, onaggs * sizeof (*aggs));
 		}
 
 		state->dts_aggregations = aggs;
 		state->dts_naggregations = naggs;
 	}
 
 	ASSERT(state->dts_aggregations[aggid - 1] == NULL);
 	state->dts_aggregations[(agg->dtag_id = aggid) - 1] = agg;
 
 	frec = &agg->dtag_first->dta_rec;
 	if (frec->dtrd_alignment < sizeof (dtrace_aggid_t))
 		frec->dtrd_alignment = sizeof (dtrace_aggid_t);
 
 	for (act = agg->dtag_first; act != NULL; act = act->dta_next) {
 		ASSERT(!act->dta_intuple);
 		act->dta_intuple = 1;
 	}
 
 	return (&agg->dtag_action);
 }
 
 static void
 dtrace_ecb_aggregation_destroy(dtrace_ecb_t *ecb, dtrace_action_t *act)
 {
 	dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act;
 	dtrace_state_t *state = ecb->dte_state;
 	dtrace_aggid_t aggid = agg->dtag_id;
 
 	ASSERT(DTRACEACT_ISAGG(act->dta_kind));
 #ifdef illumos
 	vmem_free(state->dts_aggid_arena, (void *)(uintptr_t)aggid, 1);
 #else
 	free_unr(state->dts_aggid_arena, aggid);
 #endif
 
 	ASSERT(state->dts_aggregations[aggid - 1] == agg);
 	state->dts_aggregations[aggid - 1] = NULL;
 
 	kmem_free(agg, sizeof (dtrace_aggregation_t));
 }
 
 static int
 dtrace_ecb_action_add(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc)
 {
 	dtrace_action_t *action, *last;
 	dtrace_difo_t *dp = desc->dtad_difo;
 	uint32_t size = 0, align = sizeof (uint8_t), mask;
 	uint16_t format = 0;
 	dtrace_recdesc_t *rec;
 	dtrace_state_t *state = ecb->dte_state;
 	dtrace_optval_t *opt = state->dts_options, nframes = 0, strsize;
 	uint64_t arg = desc->dtad_arg;
 
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 	ASSERT(ecb->dte_action == NULL || ecb->dte_action->dta_refcnt == 1);
 
 	if (DTRACEACT_ISAGG(desc->dtad_kind)) {
 		/*
 		 * If this is an aggregating action, there must be neither
 		 * a speculate nor a commit on the action chain.
 		 */
 		dtrace_action_t *act;
 
 		for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
 			if (act->dta_kind == DTRACEACT_COMMIT)
 				return (EINVAL);
 
 			if (act->dta_kind == DTRACEACT_SPECULATE)
 				return (EINVAL);
 		}
 
 		action = dtrace_ecb_aggregation_create(ecb, desc);
 
 		if (action == NULL)
 			return (EINVAL);
 	} else {
 		if (DTRACEACT_ISDESTRUCTIVE(desc->dtad_kind) ||
 		    (desc->dtad_kind == DTRACEACT_DIFEXPR &&
 		    dp != NULL && dp->dtdo_destructive)) {
 			state->dts_destructive = 1;
 		}
 
 		switch (desc->dtad_kind) {
 		case DTRACEACT_PRINTF:
 		case DTRACEACT_PRINTA:
 		case DTRACEACT_SYSTEM:
 		case DTRACEACT_FREOPEN:
 		case DTRACEACT_DIFEXPR:
 			/*
 			 * We know that our arg is a string -- turn it into a
 			 * format.
 			 */
 			if (arg == 0) {
 				ASSERT(desc->dtad_kind == DTRACEACT_PRINTA ||
 				    desc->dtad_kind == DTRACEACT_DIFEXPR);
 				format = 0;
 			} else {
 				ASSERT(arg != 0);
 #ifdef illumos
 				ASSERT(arg > KERNELBASE);
 #endif
 				format = dtrace_format_add(state,
 				    (char *)(uintptr_t)arg);
 			}
 
 			/*FALLTHROUGH*/
 		case DTRACEACT_LIBACT:
 		case DTRACEACT_TRACEMEM:
 		case DTRACEACT_TRACEMEM_DYNSIZE:
 			if (dp == NULL)
 				return (EINVAL);
 
 			if ((size = dp->dtdo_rtype.dtdt_size) != 0)
 				break;
 
 			if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) {
 				if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF))
 					return (EINVAL);
 
 				size = opt[DTRACEOPT_STRSIZE];
 			}
 
 			break;
 
 		case DTRACEACT_STACK:
 			if ((nframes = arg) == 0) {
 				nframes = opt[DTRACEOPT_STACKFRAMES];
 				ASSERT(nframes > 0);
 				arg = nframes;
 			}
 
 			size = nframes * sizeof (pc_t);
 			break;
 
 		case DTRACEACT_JSTACK:
 			if ((strsize = DTRACE_USTACK_STRSIZE(arg)) == 0)
 				strsize = opt[DTRACEOPT_JSTACKSTRSIZE];
 
 			if ((nframes = DTRACE_USTACK_NFRAMES(arg)) == 0)
 				nframes = opt[DTRACEOPT_JSTACKFRAMES];
 
 			arg = DTRACE_USTACK_ARG(nframes, strsize);
 
 			/*FALLTHROUGH*/
 		case DTRACEACT_USTACK:
 			if (desc->dtad_kind != DTRACEACT_JSTACK &&
 			    (nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) {
 				strsize = DTRACE_USTACK_STRSIZE(arg);
 				nframes = opt[DTRACEOPT_USTACKFRAMES];
 				ASSERT(nframes > 0);
 				arg = DTRACE_USTACK_ARG(nframes, strsize);
 			}
 
 			/*
 			 * Save a slot for the pid.
 			 */
 			size = (nframes + 1) * sizeof (uint64_t);
 			size += DTRACE_USTACK_STRSIZE(arg);
 			size = P2ROUNDUP(size, (uint32_t)(sizeof (uintptr_t)));
 
 			break;
 
 		case DTRACEACT_SYM:
 		case DTRACEACT_MOD:
 			if (dp == NULL || ((size = dp->dtdo_rtype.dtdt_size) !=
 			    sizeof (uint64_t)) ||
 			    (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF))
 				return (EINVAL);
 			break;
 
 		case DTRACEACT_USYM:
 		case DTRACEACT_UMOD:
 		case DTRACEACT_UADDR:
 			if (dp == NULL ||
 			    (dp->dtdo_rtype.dtdt_size != sizeof (uint64_t)) ||
 			    (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF))
 				return (EINVAL);
 
 			/*
 			 * We have a slot for the pid, plus a slot for the
 			 * argument.  To keep things simple (aligned with
 			 * bitness-neutral sizing), we store each as a 64-bit
 			 * quantity.
 			 */
 			size = 2 * sizeof (uint64_t);
 			break;
 
 		case DTRACEACT_STOP:
 		case DTRACEACT_BREAKPOINT:
 		case DTRACEACT_PANIC:
 			break;
 
 		case DTRACEACT_CHILL:
 		case DTRACEACT_DISCARD:
 		case DTRACEACT_RAISE:
 			if (dp == NULL)
 				return (EINVAL);
 			break;
 
 		case DTRACEACT_EXIT:
 			if (dp == NULL ||
 			    (size = dp->dtdo_rtype.dtdt_size) != sizeof (int) ||
 			    (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF))
 				return (EINVAL);
 			break;
 
 		case DTRACEACT_SPECULATE:
 			if (ecb->dte_size > sizeof (dtrace_rechdr_t))
 				return (EINVAL);
 
 			if (dp == NULL)
 				return (EINVAL);
 
 			state->dts_speculates = 1;
 			break;
 
 		case DTRACEACT_PRINTM:
 		    	size = dp->dtdo_rtype.dtdt_size;
 			break;
 
 		case DTRACEACT_COMMIT: {
 			dtrace_action_t *act = ecb->dte_action;
 
 			for (; act != NULL; act = act->dta_next) {
 				if (act->dta_kind == DTRACEACT_COMMIT)
 					return (EINVAL);
 			}
 
 			if (dp == NULL)
 				return (EINVAL);
 			break;
 		}
 
 		default:
 			return (EINVAL);
 		}
 
 		if (size != 0 || desc->dtad_kind == DTRACEACT_SPECULATE) {
 			/*
 			 * If this is a data-storing action or a speculate,
 			 * we must be sure that there isn't a commit on the
 			 * action chain.
 			 */
 			dtrace_action_t *act = ecb->dte_action;
 
 			for (; act != NULL; act = act->dta_next) {
 				if (act->dta_kind == DTRACEACT_COMMIT)
 					return (EINVAL);
 			}
 		}
 
 		action = kmem_zalloc(sizeof (dtrace_action_t), KM_SLEEP);
 		action->dta_rec.dtrd_size = size;
 	}
 
 	action->dta_refcnt = 1;
 	rec = &action->dta_rec;
 	size = rec->dtrd_size;
 
 	for (mask = sizeof (uint64_t) - 1; size != 0 && mask > 0; mask >>= 1) {
 		if (!(size & mask)) {
 			align = mask + 1;
 			break;
 		}
 	}
 
 	action->dta_kind = desc->dtad_kind;
 
 	if ((action->dta_difo = dp) != NULL)
 		dtrace_difo_hold(dp);
 
 	rec->dtrd_action = action->dta_kind;
 	rec->dtrd_arg = arg;
 	rec->dtrd_uarg = desc->dtad_uarg;
 	rec->dtrd_alignment = (uint16_t)align;
 	rec->dtrd_format = format;
 
 	if ((last = ecb->dte_action_last) != NULL) {
 		ASSERT(ecb->dte_action != NULL);
 		action->dta_prev = last;
 		last->dta_next = action;
 	} else {
 		ASSERT(ecb->dte_action == NULL);
 		ecb->dte_action = action;
 	}
 
 	ecb->dte_action_last = action;
 
 	return (0);
 }
 
 static void
 dtrace_ecb_action_remove(dtrace_ecb_t *ecb)
 {
 	dtrace_action_t *act = ecb->dte_action, *next;
 	dtrace_vstate_t *vstate = &ecb->dte_state->dts_vstate;
 	dtrace_difo_t *dp;
 	uint16_t format;
 
 	if (act != NULL && act->dta_refcnt > 1) {
 		ASSERT(act->dta_next == NULL || act->dta_next->dta_refcnt == 1);
 		act->dta_refcnt--;
 	} else {
 		for (; act != NULL; act = next) {
 			next = act->dta_next;
 			ASSERT(next != NULL || act == ecb->dte_action_last);
 			ASSERT(act->dta_refcnt == 1);
 
 			if ((format = act->dta_rec.dtrd_format) != 0)
 				dtrace_format_remove(ecb->dte_state, format);
 
 			if ((dp = act->dta_difo) != NULL)
 				dtrace_difo_release(dp, vstate);
 
 			if (DTRACEACT_ISAGG(act->dta_kind)) {
 				dtrace_ecb_aggregation_destroy(ecb, act);
 			} else {
 				kmem_free(act, sizeof (dtrace_action_t));
 			}
 		}
 	}
 
 	ecb->dte_action = NULL;
 	ecb->dte_action_last = NULL;
 	ecb->dte_size = 0;
 }
 
 static void
 dtrace_ecb_disable(dtrace_ecb_t *ecb)
 {
 	/*
 	 * We disable the ECB by removing it from its probe.
 	 */
 	dtrace_ecb_t *pecb, *prev = NULL;
 	dtrace_probe_t *probe = ecb->dte_probe;
 
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 
 	if (probe == NULL) {
 		/*
 		 * This is the NULL probe; there is nothing to disable.
 		 */
 		return;
 	}
 
 	for (pecb = probe->dtpr_ecb; pecb != NULL; pecb = pecb->dte_next) {
 		if (pecb == ecb)
 			break;
 		prev = pecb;
 	}
 
 	ASSERT(pecb != NULL);
 
 	if (prev == NULL) {
 		probe->dtpr_ecb = ecb->dte_next;
 	} else {
 		prev->dte_next = ecb->dte_next;
 	}
 
 	if (ecb == probe->dtpr_ecb_last) {
 		ASSERT(ecb->dte_next == NULL);
 		probe->dtpr_ecb_last = prev;
 	}
 
 	/*
 	 * The ECB has been disconnected from the probe; now sync to assure
 	 * that all CPUs have seen the change before returning.
 	 */
 	dtrace_sync();
 
 	if (probe->dtpr_ecb == NULL) {
 		/*
 		 * That was the last ECB on the probe; clear the predicate
 		 * cache ID for the probe, disable it and sync one more time
 		 * to assure that we'll never hit it again.
 		 */
 		dtrace_provider_t *prov = probe->dtpr_provider;
 
 		ASSERT(ecb->dte_next == NULL);
 		ASSERT(probe->dtpr_ecb_last == NULL);
 		probe->dtpr_predcache = DTRACE_CACHEIDNONE;
 		prov->dtpv_pops.dtps_disable(prov->dtpv_arg,
 		    probe->dtpr_id, probe->dtpr_arg);
 		dtrace_sync();
 	} else {
 		/*
 		 * There is at least one ECB remaining on the probe.  If there
 		 * is _exactly_ one, set the probe's predicate cache ID to be
 		 * the predicate cache ID of the remaining ECB.
 		 */
 		ASSERT(probe->dtpr_ecb_last != NULL);
 		ASSERT(probe->dtpr_predcache == DTRACE_CACHEIDNONE);
 
 		if (probe->dtpr_ecb == probe->dtpr_ecb_last) {
 			dtrace_predicate_t *p = probe->dtpr_ecb->dte_predicate;
 
 			ASSERT(probe->dtpr_ecb->dte_next == NULL);
 
 			if (p != NULL)
 				probe->dtpr_predcache = p->dtp_cacheid;
 		}
 
 		ecb->dte_next = NULL;
 	}
 }
 
 static void
 dtrace_ecb_destroy(dtrace_ecb_t *ecb)
 {
 	dtrace_state_t *state = ecb->dte_state;
 	dtrace_vstate_t *vstate = &state->dts_vstate;
 	dtrace_predicate_t *pred;
 	dtrace_epid_t epid = ecb->dte_epid;
 
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 	ASSERT(ecb->dte_next == NULL);
 	ASSERT(ecb->dte_probe == NULL || ecb->dte_probe->dtpr_ecb != ecb);
 
 	if ((pred = ecb->dte_predicate) != NULL)
 		dtrace_predicate_release(pred, vstate);
 
 	dtrace_ecb_action_remove(ecb);
 
 	ASSERT(state->dts_ecbs[epid - 1] == ecb);
 	state->dts_ecbs[epid - 1] = NULL;
 
 	kmem_free(ecb, sizeof (dtrace_ecb_t));
 }
 
 static dtrace_ecb_t *
 dtrace_ecb_create(dtrace_state_t *state, dtrace_probe_t *probe,
     dtrace_enabling_t *enab)
 {
 	dtrace_ecb_t *ecb;
 	dtrace_predicate_t *pred;
 	dtrace_actdesc_t *act;
 	dtrace_provider_t *prov;
 	dtrace_ecbdesc_t *desc = enab->dten_current;
 
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 	ASSERT(state != NULL);
 
 	ecb = dtrace_ecb_add(state, probe);
 	ecb->dte_uarg = desc->dted_uarg;
 
 	if ((pred = desc->dted_pred.dtpdd_predicate) != NULL) {
 		dtrace_predicate_hold(pred);
 		ecb->dte_predicate = pred;
 	}
 
 	if (probe != NULL) {
 		/*
 		 * If the provider shows more leg than the consumer is old
 		 * enough to see, we need to enable the appropriate implicit
 		 * predicate bits to prevent the ecb from activating at
 		 * revealing times.
 		 *
 		 * Providers specifying DTRACE_PRIV_USER at register time
 		 * are stating that they need the /proc-style privilege
 		 * model to be enforced, and this is what DTRACE_COND_OWNER
 		 * and DTRACE_COND_ZONEOWNER will then do at probe time.
 		 */
 		prov = probe->dtpr_provider;
 		if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLPROC) &&
 		    (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER))
 			ecb->dte_cond |= DTRACE_COND_OWNER;
 
 		if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLZONE) &&
 		    (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER))
 			ecb->dte_cond |= DTRACE_COND_ZONEOWNER;
 
 		/*
 		 * If the provider shows us kernel innards and the user
 		 * is lacking sufficient privilege, enable the
 		 * DTRACE_COND_USERMODE implicit predicate.
 		 */
 		if (!(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) &&
 		    (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_KERNEL))
 			ecb->dte_cond |= DTRACE_COND_USERMODE;
 	}
 
 	if (dtrace_ecb_create_cache != NULL) {
 		/*
 		 * If we have a cached ecb, we'll use its action list instead
 		 * of creating our own (saving both time and space).
 		 */
 		dtrace_ecb_t *cached = dtrace_ecb_create_cache;
 		dtrace_action_t *act = cached->dte_action;
 
 		if (act != NULL) {
 			ASSERT(act->dta_refcnt > 0);
 			act->dta_refcnt++;
 			ecb->dte_action = act;
 			ecb->dte_action_last = cached->dte_action_last;
 			ecb->dte_needed = cached->dte_needed;
 			ecb->dte_size = cached->dte_size;
 			ecb->dte_alignment = cached->dte_alignment;
 		}
 
 		return (ecb);
 	}
 
 	for (act = desc->dted_action; act != NULL; act = act->dtad_next) {
 		if ((enab->dten_error = dtrace_ecb_action_add(ecb, act)) != 0) {
 			dtrace_ecb_destroy(ecb);
 			return (NULL);
 		}
 	}
 
 	if ((enab->dten_error = dtrace_ecb_resize(ecb)) != 0) {
 		dtrace_ecb_destroy(ecb);
 		return (NULL);
 	}
 
 	return (dtrace_ecb_create_cache = ecb);
 }
 
 static int
 dtrace_ecb_create_enable(dtrace_probe_t *probe, void *arg)
 {
 	dtrace_ecb_t *ecb;
 	dtrace_enabling_t *enab = arg;
 	dtrace_state_t *state = enab->dten_vstate->dtvs_state;
 
 	ASSERT(state != NULL);
 
 	if (probe != NULL && probe->dtpr_gen < enab->dten_probegen) {
 		/*
 		 * This probe was created in a generation for which this
 		 * enabling has previously created ECBs; we don't want to
 		 * enable it again, so just kick out.
 		 */
 		return (DTRACE_MATCH_NEXT);
 	}
 
 	if ((ecb = dtrace_ecb_create(state, probe, enab)) == NULL)
 		return (DTRACE_MATCH_DONE);
 
 	dtrace_ecb_enable(ecb);
 	return (DTRACE_MATCH_NEXT);
 }
 
 static dtrace_ecb_t *
 dtrace_epid2ecb(dtrace_state_t *state, dtrace_epid_t id)
 {
 	dtrace_ecb_t *ecb;
 
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 
 	if (id == 0 || id > state->dts_necbs)
 		return (NULL);
 
 	ASSERT(state->dts_necbs > 0 && state->dts_ecbs != NULL);
 	ASSERT((ecb = state->dts_ecbs[id - 1]) == NULL || ecb->dte_epid == id);
 
 	return (state->dts_ecbs[id - 1]);
 }
 
 static dtrace_aggregation_t *
 dtrace_aggid2agg(dtrace_state_t *state, dtrace_aggid_t id)
 {
 	dtrace_aggregation_t *agg;
 
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 
 	if (id == 0 || id > state->dts_naggregations)
 		return (NULL);
 
 	ASSERT(state->dts_naggregations > 0 && state->dts_aggregations != NULL);
 	ASSERT((agg = state->dts_aggregations[id - 1]) == NULL ||
 	    agg->dtag_id == id);
 
 	return (state->dts_aggregations[id - 1]);
 }
 
 /*
  * DTrace Buffer Functions
  *
  * The following functions manipulate DTrace buffers.  Most of these functions
  * are called in the context of establishing or processing consumer state;
  * exceptions are explicitly noted.
  */
 
 /*
  * Note:  called from cross call context.  This function switches the two
  * buffers on a given CPU.  The atomicity of this operation is assured by
  * disabling interrupts while the actual switch takes place; the disabling of
  * interrupts serializes the execution with any execution of dtrace_probe() on
  * the same CPU.
  */
 static void
 dtrace_buffer_switch(dtrace_buffer_t *buf)
 {
 	caddr_t tomax = buf->dtb_tomax;
 	caddr_t xamot = buf->dtb_xamot;
 	dtrace_icookie_t cookie;
 	hrtime_t now;
 
 	ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH));
 	ASSERT(!(buf->dtb_flags & DTRACEBUF_RING));
 
 	cookie = dtrace_interrupt_disable();
 	now = dtrace_gethrtime();
 	buf->dtb_tomax = xamot;
 	buf->dtb_xamot = tomax;
 	buf->dtb_xamot_drops = buf->dtb_drops;
 	buf->dtb_xamot_offset = buf->dtb_offset;
 	buf->dtb_xamot_errors = buf->dtb_errors;
 	buf->dtb_xamot_flags = buf->dtb_flags;
 	buf->dtb_offset = 0;
 	buf->dtb_drops = 0;
 	buf->dtb_errors = 0;
 	buf->dtb_flags &= ~(DTRACEBUF_ERROR | DTRACEBUF_DROPPED);
 	buf->dtb_interval = now - buf->dtb_switched;
 	buf->dtb_switched = now;
 	dtrace_interrupt_enable(cookie);
 }
 
 /*
  * Note:  called from cross call context.  This function activates a buffer
  * on a CPU.  As with dtrace_buffer_switch(), the atomicity of the operation
  * is guaranteed by the disabling of interrupts.
  */
 static void
 dtrace_buffer_activate(dtrace_state_t *state)
 {
 	dtrace_buffer_t *buf;
 	dtrace_icookie_t cookie = dtrace_interrupt_disable();
 
 	buf = &state->dts_buffer[curcpu];
 
 	if (buf->dtb_tomax != NULL) {
 		/*
 		 * We might like to assert that the buffer is marked inactive,
 		 * but this isn't necessarily true:  the buffer for the CPU
 		 * that processes the BEGIN probe has its buffer activated
 		 * manually.  In this case, we take the (harmless) action
 		 * re-clearing the bit INACTIVE bit.
 		 */
 		buf->dtb_flags &= ~DTRACEBUF_INACTIVE;
 	}
 
 	dtrace_interrupt_enable(cookie);
 }
 
 #ifdef __FreeBSD__
 /*
  * Activate the specified per-CPU buffer.  This is used instead of
  * dtrace_buffer_activate() when APs have not yet started, i.e. when
  * activating anonymous state.
  */
 static void
 dtrace_buffer_activate_cpu(dtrace_state_t *state, int cpu)
 {
 
 	if (state->dts_buffer[cpu].dtb_tomax != NULL)
 		state->dts_buffer[cpu].dtb_flags &= ~DTRACEBUF_INACTIVE;
 }
 #endif
 
 static int
 dtrace_buffer_alloc(dtrace_buffer_t *bufs, size_t size, int flags,
     processorid_t cpu, int *factor)
 {
 #ifdef illumos
 	cpu_t *cp;
 #endif
 	dtrace_buffer_t *buf;
 	int allocated = 0, desired = 0;
 
 #ifdef illumos
 	ASSERT(MUTEX_HELD(&cpu_lock));
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 
 	*factor = 1;
 
 	if (size > dtrace_nonroot_maxsize &&
 	    !PRIV_POLICY_CHOICE(CRED(), PRIV_ALL, B_FALSE))
 		return (EFBIG);
 
 	cp = cpu_list;
 
 	do {
 		if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id)
 			continue;
 
 		buf = &bufs[cp->cpu_id];
 
 		/*
 		 * If there is already a buffer allocated for this CPU, it
 		 * is only possible that this is a DR event.  In this case,
 		 */
 		if (buf->dtb_tomax != NULL) {
 			ASSERT(buf->dtb_size == size);
 			continue;
 		}
 
 		ASSERT(buf->dtb_xamot == NULL);
 
 		if ((buf->dtb_tomax = kmem_zalloc(size,
 		    KM_NOSLEEP | KM_NORMALPRI)) == NULL)
 			goto err;
 
 		buf->dtb_size = size;
 		buf->dtb_flags = flags;
 		buf->dtb_offset = 0;
 		buf->dtb_drops = 0;
 
 		if (flags & DTRACEBUF_NOSWITCH)
 			continue;
 
 		if ((buf->dtb_xamot = kmem_zalloc(size,
 		    KM_NOSLEEP | KM_NORMALPRI)) == NULL)
 			goto err;
 	} while ((cp = cp->cpu_next) != cpu_list);
 
 	return (0);
 
 err:
 	cp = cpu_list;
 
 	do {
 		if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id)
 			continue;
 
 		buf = &bufs[cp->cpu_id];
 		desired += 2;
 
 		if (buf->dtb_xamot != NULL) {
 			ASSERT(buf->dtb_tomax != NULL);
 			ASSERT(buf->dtb_size == size);
 			kmem_free(buf->dtb_xamot, size);
 			allocated++;
 		}
 
 		if (buf->dtb_tomax != NULL) {
 			ASSERT(buf->dtb_size == size);
 			kmem_free(buf->dtb_tomax, size);
 			allocated++;
 		}
 
 		buf->dtb_tomax = NULL;
 		buf->dtb_xamot = NULL;
 		buf->dtb_size = 0;
 	} while ((cp = cp->cpu_next) != cpu_list);
 #else
 	int i;
 
 	*factor = 1;
 #if defined(__aarch64__) || defined(__amd64__) || defined(__arm__) || \
     defined(__mips__) || defined(__powerpc__) || defined(__riscv)
 	/*
 	 * FreeBSD isn't good at limiting the amount of memory we
 	 * ask to malloc, so let's place a limit here before trying
 	 * to do something that might well end in tears at bedtime.
 	 */
 	int bufsize_percpu_frac = dtrace_bufsize_max_frac * mp_ncpus;
 	if (size > physmem * PAGE_SIZE / bufsize_percpu_frac)
 		return (ENOMEM);
 #endif
 
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 	CPU_FOREACH(i) {
 		if (cpu != DTRACE_CPUALL && cpu != i)
 			continue;
 
 		buf = &bufs[i];
 
 		/*
 		 * If there is already a buffer allocated for this CPU, it
 		 * is only possible that this is a DR event.  In this case,
 		 * the buffer size must match our specified size.
 		 */
 		if (buf->dtb_tomax != NULL) {
 			ASSERT(buf->dtb_size == size);
 			continue;
 		}
 
 		ASSERT(buf->dtb_xamot == NULL);
 
 		if ((buf->dtb_tomax = kmem_zalloc(size,
 		    KM_NOSLEEP | KM_NORMALPRI)) == NULL)
 			goto err;
 
 		buf->dtb_size = size;
 		buf->dtb_flags = flags;
 		buf->dtb_offset = 0;
 		buf->dtb_drops = 0;
 
 		if (flags & DTRACEBUF_NOSWITCH)
 			continue;
 
 		if ((buf->dtb_xamot = kmem_zalloc(size,
 		    KM_NOSLEEP | KM_NORMALPRI)) == NULL)
 			goto err;
 	}
 
 	return (0);
 
 err:
 	/*
 	 * Error allocating memory, so free the buffers that were
 	 * allocated before the failed allocation.
 	 */
 	CPU_FOREACH(i) {
 		if (cpu != DTRACE_CPUALL && cpu != i)
 			continue;
 
 		buf = &bufs[i];
 		desired += 2;
 
 		if (buf->dtb_xamot != NULL) {
 			ASSERT(buf->dtb_tomax != NULL);
 			ASSERT(buf->dtb_size == size);
 			kmem_free(buf->dtb_xamot, size);
 			allocated++;
 		}
 
 		if (buf->dtb_tomax != NULL) {
 			ASSERT(buf->dtb_size == size);
 			kmem_free(buf->dtb_tomax, size);
 			allocated++;
 		}
 
 		buf->dtb_tomax = NULL;
 		buf->dtb_xamot = NULL;
 		buf->dtb_size = 0;
 
 	}
 #endif
 	*factor = desired / (allocated > 0 ? allocated : 1);
 
 	return (ENOMEM);
 }
 
 /*
  * Note:  called from probe context.  This function just increments the drop
  * count on a buffer.  It has been made a function to allow for the
  * possibility of understanding the source of mysterious drop counts.  (A
  * problem for which one may be particularly disappointed that DTrace cannot
  * be used to understand DTrace.)
  */
 static void
 dtrace_buffer_drop(dtrace_buffer_t *buf)
 {
 	buf->dtb_drops++;
 }
 
 /*
  * Note:  called from probe context.  This function is called to reserve space
  * in a buffer.  If mstate is non-NULL, sets the scratch base and size in the
  * mstate.  Returns the new offset in the buffer, or a negative value if an
  * error has occurred.
  */
 static intptr_t
 dtrace_buffer_reserve(dtrace_buffer_t *buf, size_t needed, size_t align,
     dtrace_state_t *state, dtrace_mstate_t *mstate)
 {
 	intptr_t offs = buf->dtb_offset, soffs;
 	intptr_t woffs;
 	caddr_t tomax;
 	size_t total;
 
 	if (buf->dtb_flags & DTRACEBUF_INACTIVE)
 		return (-1);
 
 	if ((tomax = buf->dtb_tomax) == NULL) {
 		dtrace_buffer_drop(buf);
 		return (-1);
 	}
 
 	if (!(buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL))) {
 		while (offs & (align - 1)) {
 			/*
 			 * Assert that our alignment is off by a number which
 			 * is itself sizeof (uint32_t) aligned.
 			 */
 			ASSERT(!((align - (offs & (align - 1))) &
 			    (sizeof (uint32_t) - 1)));
 			DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE);
 			offs += sizeof (uint32_t);
 		}
 
 		if ((soffs = offs + needed) > buf->dtb_size) {
 			dtrace_buffer_drop(buf);
 			return (-1);
 		}
 
 		if (mstate == NULL)
 			return (offs);
 
 		mstate->dtms_scratch_base = (uintptr_t)tomax + soffs;
 		mstate->dtms_scratch_size = buf->dtb_size - soffs;
 		mstate->dtms_scratch_ptr = mstate->dtms_scratch_base;
 
 		return (offs);
 	}
 
 	if (buf->dtb_flags & DTRACEBUF_FILL) {
 		if (state->dts_activity != DTRACE_ACTIVITY_COOLDOWN &&
 		    (buf->dtb_flags & DTRACEBUF_FULL))
 			return (-1);
 		goto out;
 	}
 
 	total = needed + (offs & (align - 1));
 
 	/*
 	 * For a ring buffer, life is quite a bit more complicated.  Before
 	 * we can store any padding, we need to adjust our wrapping offset.
 	 * (If we've never before wrapped or we're not about to, no adjustment
 	 * is required.)
 	 */
 	if ((buf->dtb_flags & DTRACEBUF_WRAPPED) ||
 	    offs + total > buf->dtb_size) {
 		woffs = buf->dtb_xamot_offset;
 
 		if (offs + total > buf->dtb_size) {
 			/*
 			 * We can't fit in the end of the buffer.  First, a
 			 * sanity check that we can fit in the buffer at all.
 			 */
 			if (total > buf->dtb_size) {
 				dtrace_buffer_drop(buf);
 				return (-1);
 			}
 
 			/*
 			 * We're going to be storing at the top of the buffer,
 			 * so now we need to deal with the wrapped offset.  We
 			 * only reset our wrapped offset to 0 if it is
 			 * currently greater than the current offset.  If it
 			 * is less than the current offset, it is because a
 			 * previous allocation induced a wrap -- but the
 			 * allocation didn't subsequently take the space due
 			 * to an error or false predicate evaluation.  In this
 			 * case, we'll just leave the wrapped offset alone: if
 			 * the wrapped offset hasn't been advanced far enough
 			 * for this allocation, it will be adjusted in the
 			 * lower loop.
 			 */
 			if (buf->dtb_flags & DTRACEBUF_WRAPPED) {
 				if (woffs >= offs)
 					woffs = 0;
 			} else {
 				woffs = 0;
 			}
 
 			/*
 			 * Now we know that we're going to be storing to the
 			 * top of the buffer and that there is room for us
 			 * there.  We need to clear the buffer from the current
 			 * offset to the end (there may be old gunk there).
 			 */
 			while (offs < buf->dtb_size)
 				tomax[offs++] = 0;
 
 			/*
 			 * We need to set our offset to zero.  And because we
 			 * are wrapping, we need to set the bit indicating as
 			 * much.  We can also adjust our needed space back
 			 * down to the space required by the ECB -- we know
 			 * that the top of the buffer is aligned.
 			 */
 			offs = 0;
 			total = needed;
 			buf->dtb_flags |= DTRACEBUF_WRAPPED;
 		} else {
 			/*
 			 * There is room for us in the buffer, so we simply
 			 * need to check the wrapped offset.
 			 */
 			if (woffs < offs) {
 				/*
 				 * The wrapped offset is less than the offset.
 				 * This can happen if we allocated buffer space
 				 * that induced a wrap, but then we didn't
 				 * subsequently take the space due to an error
 				 * or false predicate evaluation.  This is
 				 * okay; we know that _this_ allocation isn't
 				 * going to induce a wrap.  We still can't
 				 * reset the wrapped offset to be zero,
 				 * however: the space may have been trashed in
 				 * the previous failed probe attempt.  But at
 				 * least the wrapped offset doesn't need to
 				 * be adjusted at all...
 				 */
 				goto out;
 			}
 		}
 
 		while (offs + total > woffs) {
 			dtrace_epid_t epid = *(uint32_t *)(tomax + woffs);
 			size_t size;
 
 			if (epid == DTRACE_EPIDNONE) {
 				size = sizeof (uint32_t);
 			} else {
 				ASSERT3U(epid, <=, state->dts_necbs);
 				ASSERT(state->dts_ecbs[epid - 1] != NULL);
 
 				size = state->dts_ecbs[epid - 1]->dte_size;
 			}
 
 			ASSERT(woffs + size <= buf->dtb_size);
 			ASSERT(size != 0);
 
 			if (woffs + size == buf->dtb_size) {
 				/*
 				 * We've reached the end of the buffer; we want
 				 * to set the wrapped offset to 0 and break
 				 * out.  However, if the offs is 0, then we're
 				 * in a strange edge-condition:  the amount of
 				 * space that we want to reserve plus the size
 				 * of the record that we're overwriting is
 				 * greater than the size of the buffer.  This
 				 * is problematic because if we reserve the
 				 * space but subsequently don't consume it (due
 				 * to a failed predicate or error) the wrapped
 				 * offset will be 0 -- yet the EPID at offset 0
 				 * will not be committed.  This situation is
 				 * relatively easy to deal with:  if we're in
 				 * this case, the buffer is indistinguishable
 				 * from one that hasn't wrapped; we need only
 				 * finish the job by clearing the wrapped bit,
 				 * explicitly setting the offset to be 0, and
 				 * zero'ing out the old data in the buffer.
 				 */
 				if (offs == 0) {
 					buf->dtb_flags &= ~DTRACEBUF_WRAPPED;
 					buf->dtb_offset = 0;
 					woffs = total;
 
 					while (woffs < buf->dtb_size)
 						tomax[woffs++] = 0;
 				}
 
 				woffs = 0;
 				break;
 			}
 
 			woffs += size;
 		}
 
 		/*
 		 * We have a wrapped offset.  It may be that the wrapped offset
 		 * has become zero -- that's okay.
 		 */
 		buf->dtb_xamot_offset = woffs;
 	}
 
 out:
 	/*
 	 * Now we can plow the buffer with any necessary padding.
 	 */
 	while (offs & (align - 1)) {
 		/*
 		 * Assert that our alignment is off by a number which
 		 * is itself sizeof (uint32_t) aligned.
 		 */
 		ASSERT(!((align - (offs & (align - 1))) &
 		    (sizeof (uint32_t) - 1)));
 		DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE);
 		offs += sizeof (uint32_t);
 	}
 
 	if (buf->dtb_flags & DTRACEBUF_FILL) {
 		if (offs + needed > buf->dtb_size - state->dts_reserve) {
 			buf->dtb_flags |= DTRACEBUF_FULL;
 			return (-1);
 		}
 	}
 
 	if (mstate == NULL)
 		return (offs);
 
 	/*
 	 * For ring buffers and fill buffers, the scratch space is always
 	 * the inactive buffer.
 	 */
 	mstate->dtms_scratch_base = (uintptr_t)buf->dtb_xamot;
 	mstate->dtms_scratch_size = buf->dtb_size;
 	mstate->dtms_scratch_ptr = mstate->dtms_scratch_base;
 
 	return (offs);
 }
 
 static void
 dtrace_buffer_polish(dtrace_buffer_t *buf)
 {
 	ASSERT(buf->dtb_flags & DTRACEBUF_RING);
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 
 	if (!(buf->dtb_flags & DTRACEBUF_WRAPPED))
 		return;
 
 	/*
 	 * We need to polish the ring buffer.  There are three cases:
 	 *
 	 * - The first (and presumably most common) is that there is no gap
 	 *   between the buffer offset and the wrapped offset.  In this case,
 	 *   there is nothing in the buffer that isn't valid data; we can
 	 *   mark the buffer as polished and return.
 	 *
 	 * - The second (less common than the first but still more common
 	 *   than the third) is that there is a gap between the buffer offset
 	 *   and the wrapped offset, and the wrapped offset is larger than the
 	 *   buffer offset.  This can happen because of an alignment issue, or
 	 *   can happen because of a call to dtrace_buffer_reserve() that
 	 *   didn't subsequently consume the buffer space.  In this case,
 	 *   we need to zero the data from the buffer offset to the wrapped
 	 *   offset.
 	 *
 	 * - The third (and least common) is that there is a gap between the
 	 *   buffer offset and the wrapped offset, but the wrapped offset is
 	 *   _less_ than the buffer offset.  This can only happen because a
 	 *   call to dtrace_buffer_reserve() induced a wrap, but the space
 	 *   was not subsequently consumed.  In this case, we need to zero the
 	 *   space from the offset to the end of the buffer _and_ from the
 	 *   top of the buffer to the wrapped offset.
 	 */
 	if (buf->dtb_offset < buf->dtb_xamot_offset) {
 		bzero(buf->dtb_tomax + buf->dtb_offset,
 		    buf->dtb_xamot_offset - buf->dtb_offset);
 	}
 
 	if (buf->dtb_offset > buf->dtb_xamot_offset) {
 		bzero(buf->dtb_tomax + buf->dtb_offset,
 		    buf->dtb_size - buf->dtb_offset);
 		bzero(buf->dtb_tomax, buf->dtb_xamot_offset);
 	}
 }
 
 /*
  * This routine determines if data generated at the specified time has likely
  * been entirely consumed at user-level.  This routine is called to determine
  * if an ECB on a defunct probe (but for an active enabling) can be safely
  * disabled and destroyed.
  */
 static int
 dtrace_buffer_consumed(dtrace_buffer_t *bufs, hrtime_t when)
 {
 	int i;
 
 	for (i = 0; i < NCPU; i++) {
 		dtrace_buffer_t *buf = &bufs[i];
 
 		if (buf->dtb_size == 0)
 			continue;
 
 		if (buf->dtb_flags & DTRACEBUF_RING)
 			return (0);
 
 		if (!buf->dtb_switched && buf->dtb_offset != 0)
 			return (0);
 
 		if (buf->dtb_switched - buf->dtb_interval < when)
 			return (0);
 	}
 
 	return (1);
 }
 
 static void
 dtrace_buffer_free(dtrace_buffer_t *bufs)
 {
 	int i;
 
 	for (i = 0; i < NCPU; i++) {
 		dtrace_buffer_t *buf = &bufs[i];
 
 		if (buf->dtb_tomax == NULL) {
 			ASSERT(buf->dtb_xamot == NULL);
 			ASSERT(buf->dtb_size == 0);
 			continue;
 		}
 
 		if (buf->dtb_xamot != NULL) {
 			ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH));
 			kmem_free(buf->dtb_xamot, buf->dtb_size);
 		}
 
 		kmem_free(buf->dtb_tomax, buf->dtb_size);
 		buf->dtb_size = 0;
 		buf->dtb_tomax = NULL;
 		buf->dtb_xamot = NULL;
 	}
 }
 
 /*
  * DTrace Enabling Functions
  */
 static dtrace_enabling_t *
 dtrace_enabling_create(dtrace_vstate_t *vstate)
 {
 	dtrace_enabling_t *enab;
 
 	enab = kmem_zalloc(sizeof (dtrace_enabling_t), KM_SLEEP);
 	enab->dten_vstate = vstate;
 
 	return (enab);
 }
 
 static void
 dtrace_enabling_add(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb)
 {
 	dtrace_ecbdesc_t **ndesc;
 	size_t osize, nsize;
 
 	/*
 	 * We can't add to enablings after we've enabled them, or after we've
 	 * retained them.
 	 */
 	ASSERT(enab->dten_probegen == 0);
 	ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL);
 
 	if (enab->dten_ndesc < enab->dten_maxdesc) {
 		enab->dten_desc[enab->dten_ndesc++] = ecb;
 		return;
 	}
 
 	osize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *);
 
 	if (enab->dten_maxdesc == 0) {
 		enab->dten_maxdesc = 1;
 	} else {
 		enab->dten_maxdesc <<= 1;
 	}
 
 	ASSERT(enab->dten_ndesc < enab->dten_maxdesc);
 
 	nsize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *);
 	ndesc = kmem_zalloc(nsize, KM_SLEEP);
 	bcopy(enab->dten_desc, ndesc, osize);
 	if (enab->dten_desc != NULL)
 		kmem_free(enab->dten_desc, osize);
 
 	enab->dten_desc = ndesc;
 	enab->dten_desc[enab->dten_ndesc++] = ecb;
 }
 
 static void
 dtrace_enabling_addlike(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb,
     dtrace_probedesc_t *pd)
 {
 	dtrace_ecbdesc_t *new;
 	dtrace_predicate_t *pred;
 	dtrace_actdesc_t *act;
 
 	/*
 	 * We're going to create a new ECB description that matches the
 	 * specified ECB in every way, but has the specified probe description.
 	 */
 	new = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP);
 
 	if ((pred = ecb->dted_pred.dtpdd_predicate) != NULL)
 		dtrace_predicate_hold(pred);
 
 	for (act = ecb->dted_action; act != NULL; act = act->dtad_next)
 		dtrace_actdesc_hold(act);
 
 	new->dted_action = ecb->dted_action;
 	new->dted_pred = ecb->dted_pred;
 	new->dted_probe = *pd;
 	new->dted_uarg = ecb->dted_uarg;
 
 	dtrace_enabling_add(enab, new);
 }
 
 static void
 dtrace_enabling_dump(dtrace_enabling_t *enab)
 {
 	int i;
 
 	for (i = 0; i < enab->dten_ndesc; i++) {
 		dtrace_probedesc_t *desc = &enab->dten_desc[i]->dted_probe;
 
 #ifdef __FreeBSD__
 		printf("dtrace: enabling probe %d (%s:%s:%s:%s)\n", i,
 		    desc->dtpd_provider, desc->dtpd_mod,
 		    desc->dtpd_func, desc->dtpd_name);
 #else
 		cmn_err(CE_NOTE, "enabling probe %d (%s:%s:%s:%s)", i,
 		    desc->dtpd_provider, desc->dtpd_mod,
 		    desc->dtpd_func, desc->dtpd_name);
 #endif
 	}
 }
 
 static void
 dtrace_enabling_destroy(dtrace_enabling_t *enab)
 {
 	int i;
 	dtrace_ecbdesc_t *ep;
 	dtrace_vstate_t *vstate = enab->dten_vstate;
 
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 
 	for (i = 0; i < enab->dten_ndesc; i++) {
 		dtrace_actdesc_t *act, *next;
 		dtrace_predicate_t *pred;
 
 		ep = enab->dten_desc[i];
 
 		if ((pred = ep->dted_pred.dtpdd_predicate) != NULL)
 			dtrace_predicate_release(pred, vstate);
 
 		for (act = ep->dted_action; act != NULL; act = next) {
 			next = act->dtad_next;
 			dtrace_actdesc_release(act, vstate);
 		}
 
 		kmem_free(ep, sizeof (dtrace_ecbdesc_t));
 	}
 
 	if (enab->dten_desc != NULL)
 		kmem_free(enab->dten_desc,
 		    enab->dten_maxdesc * sizeof (dtrace_enabling_t *));
 
 	/*
 	 * If this was a retained enabling, decrement the dts_nretained count
 	 * and take it off of the dtrace_retained list.
 	 */
 	if (enab->dten_prev != NULL || enab->dten_next != NULL ||
 	    dtrace_retained == enab) {
 		ASSERT(enab->dten_vstate->dtvs_state != NULL);
 		ASSERT(enab->dten_vstate->dtvs_state->dts_nretained > 0);
 		enab->dten_vstate->dtvs_state->dts_nretained--;
 		dtrace_retained_gen++;
 	}
 
 	if (enab->dten_prev == NULL) {
 		if (dtrace_retained == enab) {
 			dtrace_retained = enab->dten_next;
 
 			if (dtrace_retained != NULL)
 				dtrace_retained->dten_prev = NULL;
 		}
 	} else {
 		ASSERT(enab != dtrace_retained);
 		ASSERT(dtrace_retained != NULL);
 		enab->dten_prev->dten_next = enab->dten_next;
 	}
 
 	if (enab->dten_next != NULL) {
 		ASSERT(dtrace_retained != NULL);
 		enab->dten_next->dten_prev = enab->dten_prev;
 	}
 
 	kmem_free(enab, sizeof (dtrace_enabling_t));
 }
 
 static int
 dtrace_enabling_retain(dtrace_enabling_t *enab)
 {
 	dtrace_state_t *state;
 
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 	ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL);
 	ASSERT(enab->dten_vstate != NULL);
 
 	state = enab->dten_vstate->dtvs_state;
 	ASSERT(state != NULL);
 
 	/*
 	 * We only allow each state to retain dtrace_retain_max enablings.
 	 */
 	if (state->dts_nretained >= dtrace_retain_max)
 		return (ENOSPC);
 
 	state->dts_nretained++;
 	dtrace_retained_gen++;
 
 	if (dtrace_retained == NULL) {
 		dtrace_retained = enab;
 		return (0);
 	}
 
 	enab->dten_next = dtrace_retained;
 	dtrace_retained->dten_prev = enab;
 	dtrace_retained = enab;
 
 	return (0);
 }
 
 static int
 dtrace_enabling_replicate(dtrace_state_t *state, dtrace_probedesc_t *match,
     dtrace_probedesc_t *create)
 {
 	dtrace_enabling_t *new, *enab;
 	int found = 0, err = ENOENT;
 
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 	ASSERT(strlen(match->dtpd_provider) < DTRACE_PROVNAMELEN);
 	ASSERT(strlen(match->dtpd_mod) < DTRACE_MODNAMELEN);
 	ASSERT(strlen(match->dtpd_func) < DTRACE_FUNCNAMELEN);
 	ASSERT(strlen(match->dtpd_name) < DTRACE_NAMELEN);
 
 	new = dtrace_enabling_create(&state->dts_vstate);
 
 	/*
 	 * Iterate over all retained enablings, looking for enablings that
 	 * match the specified state.
 	 */
 	for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) {
 		int i;
 
 		/*
 		 * dtvs_state can only be NULL for helper enablings -- and
 		 * helper enablings can't be retained.
 		 */
 		ASSERT(enab->dten_vstate->dtvs_state != NULL);
 
 		if (enab->dten_vstate->dtvs_state != state)
 			continue;
 
 		/*
 		 * Now iterate over each probe description; we're looking for
 		 * an exact match to the specified probe description.
 		 */
 		for (i = 0; i < enab->dten_ndesc; i++) {
 			dtrace_ecbdesc_t *ep = enab->dten_desc[i];
 			dtrace_probedesc_t *pd = &ep->dted_probe;
 
 			if (strcmp(pd->dtpd_provider, match->dtpd_provider))
 				continue;
 
 			if (strcmp(pd->dtpd_mod, match->dtpd_mod))
 				continue;
 
 			if (strcmp(pd->dtpd_func, match->dtpd_func))
 				continue;
 
 			if (strcmp(pd->dtpd_name, match->dtpd_name))
 				continue;
 
 			/*
 			 * We have a winning probe!  Add it to our growing
 			 * enabling.
 			 */
 			found = 1;
 			dtrace_enabling_addlike(new, ep, create);
 		}
 	}
 
 	if (!found || (err = dtrace_enabling_retain(new)) != 0) {
 		dtrace_enabling_destroy(new);
 		return (err);
 	}
 
 	return (0);
 }
 
 static void
 dtrace_enabling_retract(dtrace_state_t *state)
 {
 	dtrace_enabling_t *enab, *next;
 
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 
 	/*
 	 * Iterate over all retained enablings, destroy the enablings retained
 	 * for the specified state.
 	 */
 	for (enab = dtrace_retained; enab != NULL; enab = next) {
 		next = enab->dten_next;
 
 		/*
 		 * dtvs_state can only be NULL for helper enablings -- and
 		 * helper enablings can't be retained.
 		 */
 		ASSERT(enab->dten_vstate->dtvs_state != NULL);
 
 		if (enab->dten_vstate->dtvs_state == state) {
 			ASSERT(state->dts_nretained > 0);
 			dtrace_enabling_destroy(enab);
 		}
 	}
 
 	ASSERT(state->dts_nretained == 0);
 }
 
 static int
 dtrace_enabling_match(dtrace_enabling_t *enab, int *nmatched)
 {
 	int i = 0;
 	int matched = 0;
 
 	ASSERT(MUTEX_HELD(&cpu_lock));
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 
 	for (i = 0; i < enab->dten_ndesc; i++) {
 		dtrace_ecbdesc_t *ep = enab->dten_desc[i];
 
 		enab->dten_current = ep;
 		enab->dten_error = 0;
 
 		matched += dtrace_probe_enable(&ep->dted_probe, enab);
 
 		if (enab->dten_error != 0) {
 			/*
 			 * If we get an error half-way through enabling the
 			 * probes, we kick out -- perhaps with some number of
 			 * them enabled.  Leaving enabled probes enabled may
 			 * be slightly confusing for user-level, but we expect
 			 * that no one will attempt to actually drive on in
 			 * the face of such errors.  If this is an anonymous
 			 * enabling (indicated with a NULL nmatched pointer),
 			 * we cmn_err() a message.  We aren't expecting to
 			 * get such an error -- such as it can exist at all,
 			 * it would be a result of corrupted DOF in the driver
 			 * properties.
 			 */
 			if (nmatched == NULL) {
 				cmn_err(CE_WARN, "dtrace_enabling_match() "
 				    "error on %p: %d", (void *)ep,
 				    enab->dten_error);
 			}
 
 			return (enab->dten_error);
 		}
 	}
 
 	enab->dten_probegen = dtrace_probegen;
 	if (nmatched != NULL)
 		*nmatched = matched;
 
 	return (0);
 }
 
 static void
 dtrace_enabling_matchall(void)
 {
 	dtrace_enabling_t *enab;
 
 	mutex_enter(&cpu_lock);
 	mutex_enter(&dtrace_lock);
 
 	/*
 	 * Iterate over all retained enablings to see if any probes match
 	 * against them.  We only perform this operation on enablings for which
 	 * we have sufficient permissions by virtue of being in the global zone
 	 * or in the same zone as the DTrace client.  Because we can be called
 	 * after dtrace_detach() has been called, we cannot assert that there
 	 * are retained enablings.  We can safely load from dtrace_retained,
 	 * however:  the taskq_destroy() at the end of dtrace_detach() will
 	 * block pending our completion.
 	 */
 	for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) {
 #ifdef illumos
 		cred_t *cr = enab->dten_vstate->dtvs_state->dts_cred.dcr_cred;
 
 		if (INGLOBALZONE(curproc) ||
 		    cr != NULL && getzoneid() == crgetzoneid(cr))
 #endif
 			(void) dtrace_enabling_match(enab, NULL);
 	}
 
 	mutex_exit(&dtrace_lock);
 	mutex_exit(&cpu_lock);
 }
 
 /*
  * If an enabling is to be enabled without having matched probes (that is, if
  * dtrace_state_go() is to be called on the underlying dtrace_state_t), the
  * enabling must be _primed_ by creating an ECB for every ECB description.
  * This must be done to assure that we know the number of speculations, the
  * number of aggregations, the minimum buffer size needed, etc. before we
  * transition out of DTRACE_ACTIVITY_INACTIVE.  To do this without actually
  * enabling any probes, we create ECBs for every ECB decription, but with a
  * NULL probe -- which is exactly what this function does.
  */
 static void
 dtrace_enabling_prime(dtrace_state_t *state)
 {
 	dtrace_enabling_t *enab;
 	int i;
 
 	for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) {
 		ASSERT(enab->dten_vstate->dtvs_state != NULL);
 
 		if (enab->dten_vstate->dtvs_state != state)
 			continue;
 
 		/*
 		 * We don't want to prime an enabling more than once, lest
 		 * we allow a malicious user to induce resource exhaustion.
 		 * (The ECBs that result from priming an enabling aren't
 		 * leaked -- but they also aren't deallocated until the
 		 * consumer state is destroyed.)
 		 */
 		if (enab->dten_primed)
 			continue;
 
 		for (i = 0; i < enab->dten_ndesc; i++) {
 			enab->dten_current = enab->dten_desc[i];
 			(void) dtrace_probe_enable(NULL, enab);
 		}
 
 		enab->dten_primed = 1;
 	}
 }
 
 /*
  * Called to indicate that probes should be provided due to retained
  * enablings.  This is implemented in terms of dtrace_probe_provide(), but it
  * must take an initial lap through the enabling calling the dtps_provide()
  * entry point explicitly to allow for autocreated probes.
  */
 static void
 dtrace_enabling_provide(dtrace_provider_t *prv)
 {
 	int i, all = 0;
 	dtrace_probedesc_t desc;
 	dtrace_genid_t gen;
 
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 	ASSERT(MUTEX_HELD(&dtrace_provider_lock));
 
 	if (prv == NULL) {
 		all = 1;
 		prv = dtrace_provider;
 	}
 
 	do {
 		dtrace_enabling_t *enab;
 		void *parg = prv->dtpv_arg;
 
 retry:
 		gen = dtrace_retained_gen;
 		for (enab = dtrace_retained; enab != NULL;
 		    enab = enab->dten_next) {
 			for (i = 0; i < enab->dten_ndesc; i++) {
 				desc = enab->dten_desc[i]->dted_probe;
 				mutex_exit(&dtrace_lock);
 				prv->dtpv_pops.dtps_provide(parg, &desc);
 				mutex_enter(&dtrace_lock);
 				/*
 				 * Process the retained enablings again if
 				 * they have changed while we weren't holding
 				 * dtrace_lock.
 				 */
 				if (gen != dtrace_retained_gen)
 					goto retry;
 			}
 		}
 	} while (all && (prv = prv->dtpv_next) != NULL);
 
 	mutex_exit(&dtrace_lock);
 	dtrace_probe_provide(NULL, all ? NULL : prv);
 	mutex_enter(&dtrace_lock);
 }
 
 /*
  * Called to reap ECBs that are attached to probes from defunct providers.
  */
 static void
 dtrace_enabling_reap(void)
 {
 	dtrace_provider_t *prov;
 	dtrace_probe_t *probe;
 	dtrace_ecb_t *ecb;
 	hrtime_t when;
 	int i;
 
 	mutex_enter(&cpu_lock);
 	mutex_enter(&dtrace_lock);
 
 	for (i = 0; i < dtrace_nprobes; i++) {
 		if ((probe = dtrace_probes[i]) == NULL)
 			continue;
 
 		if (probe->dtpr_ecb == NULL)
 			continue;
 
 		prov = probe->dtpr_provider;
 
 		if ((when = prov->dtpv_defunct) == 0)
 			continue;
 
 		/*
 		 * We have ECBs on a defunct provider:  we want to reap these
 		 * ECBs to allow the provider to unregister.  The destruction
 		 * of these ECBs must be done carefully:  if we destroy the ECB
 		 * and the consumer later wishes to consume an EPID that
 		 * corresponds to the destroyed ECB (and if the EPID metadata
 		 * has not been previously consumed), the consumer will abort
 		 * processing on the unknown EPID.  To reduce (but not, sadly,
 		 * eliminate) the possibility of this, we will only destroy an
 		 * ECB for a defunct provider if, for the state that
 		 * corresponds to the ECB:
 		 *
 		 *  (a)	There is no speculative tracing (which can effectively
 		 *	cache an EPID for an arbitrary amount of time).
 		 *
 		 *  (b)	The principal buffers have been switched twice since the
 		 *	provider became defunct.
 		 *
 		 *  (c)	The aggregation buffers are of zero size or have been
 		 *	switched twice since the provider became defunct.
 		 *
 		 * We use dts_speculates to determine (a) and call a function
 		 * (dtrace_buffer_consumed()) to determine (b) and (c).  Note
 		 * that as soon as we've been unable to destroy one of the ECBs
 		 * associated with the probe, we quit trying -- reaping is only
 		 * fruitful in as much as we can destroy all ECBs associated
 		 * with the defunct provider's probes.
 		 */
 		while ((ecb = probe->dtpr_ecb) != NULL) {
 			dtrace_state_t *state = ecb->dte_state;
 			dtrace_buffer_t *buf = state->dts_buffer;
 			dtrace_buffer_t *aggbuf = state->dts_aggbuffer;
 
 			if (state->dts_speculates)
 				break;
 
 			if (!dtrace_buffer_consumed(buf, when))
 				break;
 
 			if (!dtrace_buffer_consumed(aggbuf, when))
 				break;
 
 			dtrace_ecb_disable(ecb);
 			ASSERT(probe->dtpr_ecb != ecb);
 			dtrace_ecb_destroy(ecb);
 		}
 	}
 
 	mutex_exit(&dtrace_lock);
 	mutex_exit(&cpu_lock);
 }
 
 /*
  * DTrace DOF Functions
  */
 /*ARGSUSED*/
 static void
 dtrace_dof_error(dof_hdr_t *dof, const char *str)
 {
 	if (dtrace_err_verbose)
 		cmn_err(CE_WARN, "failed to process DOF: %s", str);
 
 #ifdef DTRACE_ERRDEBUG
 	dtrace_errdebug(str);
 #endif
 }
 
 /*
  * Create DOF out of a currently enabled state.  Right now, we only create
  * DOF containing the run-time options -- but this could be expanded to create
  * complete DOF representing the enabled state.
  */
 static dof_hdr_t *
 dtrace_dof_create(dtrace_state_t *state)
 {
 	dof_hdr_t *dof;
 	dof_sec_t *sec;
 	dof_optdesc_t *opt;
 	int i, len = sizeof (dof_hdr_t) +
 	    roundup(sizeof (dof_sec_t), sizeof (uint64_t)) +
 	    sizeof (dof_optdesc_t) * DTRACEOPT_MAX;
 
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 
 	dof = kmem_zalloc(len, KM_SLEEP);
 	dof->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0;
 	dof->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1;
 	dof->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2;
 	dof->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3;
 
 	dof->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_NATIVE;
 	dof->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE;
 	dof->dofh_ident[DOF_ID_VERSION] = DOF_VERSION;
 	dof->dofh_ident[DOF_ID_DIFVERS] = DIF_VERSION;
 	dof->dofh_ident[DOF_ID_DIFIREG] = DIF_DIR_NREGS;
 	dof->dofh_ident[DOF_ID_DIFTREG] = DIF_DTR_NREGS;
 
 	dof->dofh_flags = 0;
 	dof->dofh_hdrsize = sizeof (dof_hdr_t);
 	dof->dofh_secsize = sizeof (dof_sec_t);
 	dof->dofh_secnum = 1;	/* only DOF_SECT_OPTDESC */
 	dof->dofh_secoff = sizeof (dof_hdr_t);
 	dof->dofh_loadsz = len;
 	dof->dofh_filesz = len;
 	dof->dofh_pad = 0;
 
 	/*
 	 * Fill in the option section header...
 	 */
 	sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t));
 	sec->dofs_type = DOF_SECT_OPTDESC;
 	sec->dofs_align = sizeof (uint64_t);
 	sec->dofs_flags = DOF_SECF_LOAD;
 	sec->dofs_entsize = sizeof (dof_optdesc_t);
 
 	opt = (dof_optdesc_t *)((uintptr_t)sec +
 	    roundup(sizeof (dof_sec_t), sizeof (uint64_t)));
 
 	sec->dofs_offset = (uintptr_t)opt - (uintptr_t)dof;
 	sec->dofs_size = sizeof (dof_optdesc_t) * DTRACEOPT_MAX;
 
 	for (i = 0; i < DTRACEOPT_MAX; i++) {
 		opt[i].dofo_option = i;
 		opt[i].dofo_strtab = DOF_SECIDX_NONE;
 		opt[i].dofo_value = state->dts_options[i];
 	}
 
 	return (dof);
 }
 
 static dof_hdr_t *
 dtrace_dof_copyin(uintptr_t uarg, int *errp)
 {
 	dof_hdr_t hdr, *dof;
 
 	ASSERT(!MUTEX_HELD(&dtrace_lock));
 
 	/*
 	 * First, we're going to copyin() the sizeof (dof_hdr_t).
 	 */
 	if (copyin((void *)uarg, &hdr, sizeof (hdr)) != 0) {
 		dtrace_dof_error(NULL, "failed to copyin DOF header");
 		*errp = EFAULT;
 		return (NULL);
 	}
 
 	/*
 	 * Now we'll allocate the entire DOF and copy it in -- provided
 	 * that the length isn't outrageous.
 	 */
 	if (hdr.dofh_loadsz >= dtrace_dof_maxsize) {
 		dtrace_dof_error(&hdr, "load size exceeds maximum");
 		*errp = E2BIG;
 		return (NULL);
 	}
 
 	if (hdr.dofh_loadsz < sizeof (hdr)) {
 		dtrace_dof_error(&hdr, "invalid load size");
 		*errp = EINVAL;
 		return (NULL);
 	}
 
 	dof = kmem_alloc(hdr.dofh_loadsz, KM_SLEEP);
 
 	if (copyin((void *)uarg, dof, hdr.dofh_loadsz) != 0 ||
 	    dof->dofh_loadsz != hdr.dofh_loadsz) {
 		kmem_free(dof, hdr.dofh_loadsz);
 		*errp = EFAULT;
 		return (NULL);
 	}
 
 	return (dof);
 }
 
 #ifdef __FreeBSD__
 static dof_hdr_t *
 dtrace_dof_copyin_proc(struct proc *p, uintptr_t uarg, int *errp)
 {
 	dof_hdr_t hdr, *dof;
 	struct thread *td;
 	size_t loadsz;
 
 	ASSERT(!MUTEX_HELD(&dtrace_lock));
 
 	td = curthread;
 
 	/*
 	 * First, we're going to copyin() the sizeof (dof_hdr_t).
 	 */
 	if (proc_readmem(td, p, uarg, &hdr, sizeof(hdr)) != sizeof(hdr)) {
 		dtrace_dof_error(NULL, "failed to copyin DOF header");
 		*errp = EFAULT;
 		return (NULL);
 	}
 
 	/*
 	 * Now we'll allocate the entire DOF and copy it in -- provided
 	 * that the length isn't outrageous.
 	 */
 	if (hdr.dofh_loadsz >= dtrace_dof_maxsize) {
 		dtrace_dof_error(&hdr, "load size exceeds maximum");
 		*errp = E2BIG;
 		return (NULL);
 	}
 	loadsz = (size_t)hdr.dofh_loadsz;
 
 	if (loadsz < sizeof (hdr)) {
 		dtrace_dof_error(&hdr, "invalid load size");
 		*errp = EINVAL;
 		return (NULL);
 	}
 
 	dof = kmem_alloc(loadsz, KM_SLEEP);
 
 	if (proc_readmem(td, p, uarg, dof, loadsz) != loadsz ||
 	    dof->dofh_loadsz != loadsz) {
 		kmem_free(dof, hdr.dofh_loadsz);
 		*errp = EFAULT;
 		return (NULL);
 	}
 
 	return (dof);
 }
 
 static __inline uchar_t
 dtrace_dof_char(char c)
 {
 
 	switch (c) {
 	case '0':
 	case '1':
 	case '2':
 	case '3':
 	case '4':
 	case '5':
 	case '6':
 	case '7':
 	case '8':
 	case '9':
 		return (c - '0');
 	case 'A':
 	case 'B':
 	case 'C':
 	case 'D':
 	case 'E':
 	case 'F':
 		return (c - 'A' + 10);
 	case 'a':
 	case 'b':
 	case 'c':
 	case 'd':
 	case 'e':
 	case 'f':
 		return (c - 'a' + 10);
 	}
 	/* Should not reach here. */
 	return (UCHAR_MAX);
 }
 #endif /* __FreeBSD__ */
 
 static dof_hdr_t *
 dtrace_dof_property(const char *name)
 {
 #ifdef __FreeBSD__
 	uint8_t *dofbuf;
 	u_char *data, *eol;
 	caddr_t doffile;
 	size_t bytes, len, i;
 	dof_hdr_t *dof;
 	u_char c1, c2;
 
 	dof = NULL;
 
 	doffile = preload_search_by_type("dtrace_dof");
 	if (doffile == NULL)
 		return (NULL);
 
 	data = preload_fetch_addr(doffile);
 	len = preload_fetch_size(doffile);
 	for (;;) {
 		/* Look for the end of the line. All lines end in a newline. */
 		eol = memchr(data, '\n', len);
 		if (eol == NULL)
 			return (NULL);
 
 		if (strncmp(name, data, strlen(name)) == 0)
 			break;
 
 		eol++; /* skip past the newline */
 		len -= eol - data;
 		data = eol;
 	}
 
 	/* We've found the data corresponding to the specified key. */
 
 	data += strlen(name) + 1; /* skip past the '=' */
 	len = eol - data;
 	if (len % 2 != 0) {
 		dtrace_dof_error(NULL, "invalid DOF encoding length");
 		goto doferr;
 	}
 	bytes = len / 2;
 	if (bytes < sizeof(dof_hdr_t)) {
 		dtrace_dof_error(NULL, "truncated header");
 		goto doferr;
 	}
 
 	/*
 	 * Each byte is represented by the two ASCII characters in its hex
 	 * representation.
 	 */
 	dofbuf = malloc(bytes, M_SOLARIS, M_WAITOK);
 	for (i = 0; i < bytes; i++) {
 		c1 = dtrace_dof_char(data[i * 2]);
 		c2 = dtrace_dof_char(data[i * 2 + 1]);
 		if (c1 == UCHAR_MAX || c2 == UCHAR_MAX) {
 			dtrace_dof_error(NULL, "invalid hex char in DOF");
 			goto doferr;
 		}
 		dofbuf[i] = c1 * 16 + c2;
 	}
 
 	dof = (dof_hdr_t *)dofbuf;
 	if (bytes < dof->dofh_loadsz) {
 		dtrace_dof_error(NULL, "truncated DOF");
 		goto doferr;
 	}
 
 	if (dof->dofh_loadsz >= dtrace_dof_maxsize) {
 		dtrace_dof_error(NULL, "oversized DOF");
 		goto doferr;
 	}
 
 	return (dof);
 
 doferr:
 	free(dof, M_SOLARIS);
 	return (NULL);
 #else /* __FreeBSD__ */
 	uchar_t *buf;
 	uint64_t loadsz;
 	unsigned int len, i;
 	dof_hdr_t *dof;
 
 	/*
 	 * Unfortunately, array of values in .conf files are always (and
 	 * only) interpreted to be integer arrays.  We must read our DOF
 	 * as an integer array, and then squeeze it into a byte array.
 	 */
 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dtrace_devi, 0,
 	    (char *)name, (int **)&buf, &len) != DDI_PROP_SUCCESS)
 		return (NULL);
 
 	for (i = 0; i < len; i++)
 		buf[i] = (uchar_t)(((int *)buf)[i]);
 
 	if (len < sizeof (dof_hdr_t)) {
 		ddi_prop_free(buf);
 		dtrace_dof_error(NULL, "truncated header");
 		return (NULL);
 	}
 
 	if (len < (loadsz = ((dof_hdr_t *)buf)->dofh_loadsz)) {
 		ddi_prop_free(buf);
 		dtrace_dof_error(NULL, "truncated DOF");
 		return (NULL);
 	}
 
 	if (loadsz >= dtrace_dof_maxsize) {
 		ddi_prop_free(buf);
 		dtrace_dof_error(NULL, "oversized DOF");
 		return (NULL);
 	}
 
 	dof = kmem_alloc(loadsz, KM_SLEEP);
 	bcopy(buf, dof, loadsz);
 	ddi_prop_free(buf);
 
 	return (dof);
 #endif /* !__FreeBSD__ */
 }
 
 static void
 dtrace_dof_destroy(dof_hdr_t *dof)
 {
 	kmem_free(dof, dof->dofh_loadsz);
 }
 
 /*
  * Return the dof_sec_t pointer corresponding to a given section index.  If the
  * index is not valid, dtrace_dof_error() is called and NULL is returned.  If
  * a type other than DOF_SECT_NONE is specified, the header is checked against
  * this type and NULL is returned if the types do not match.
  */
 static dof_sec_t *
 dtrace_dof_sect(dof_hdr_t *dof, uint32_t type, dof_secidx_t i)
 {
 	dof_sec_t *sec = (dof_sec_t *)(uintptr_t)
 	    ((uintptr_t)dof + dof->dofh_secoff + i * dof->dofh_secsize);
 
 	if (i >= dof->dofh_secnum) {
 		dtrace_dof_error(dof, "referenced section index is invalid");
 		return (NULL);
 	}
 
 	if (!(sec->dofs_flags & DOF_SECF_LOAD)) {
 		dtrace_dof_error(dof, "referenced section is not loadable");
 		return (NULL);
 	}
 
 	if (type != DOF_SECT_NONE && type != sec->dofs_type) {
 		dtrace_dof_error(dof, "referenced section is the wrong type");
 		return (NULL);
 	}
 
 	return (sec);
 }
 
 static dtrace_probedesc_t *
 dtrace_dof_probedesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_probedesc_t *desc)
 {
 	dof_probedesc_t *probe;
 	dof_sec_t *strtab;
 	uintptr_t daddr = (uintptr_t)dof;
 	uintptr_t str;
 	size_t size;
 
 	if (sec->dofs_type != DOF_SECT_PROBEDESC) {
 		dtrace_dof_error(dof, "invalid probe section");
 		return (NULL);
 	}
 
 	if (sec->dofs_align != sizeof (dof_secidx_t)) {
 		dtrace_dof_error(dof, "bad alignment in probe description");
 		return (NULL);
 	}
 
 	if (sec->dofs_offset + sizeof (dof_probedesc_t) > dof->dofh_loadsz) {
 		dtrace_dof_error(dof, "truncated probe description");
 		return (NULL);
 	}
 
 	probe = (dof_probedesc_t *)(uintptr_t)(daddr + sec->dofs_offset);
 	strtab = dtrace_dof_sect(dof, DOF_SECT_STRTAB, probe->dofp_strtab);
 
 	if (strtab == NULL)
 		return (NULL);
 
 	str = daddr + strtab->dofs_offset;
 	size = strtab->dofs_size;
 
 	if (probe->dofp_provider >= strtab->dofs_size) {
 		dtrace_dof_error(dof, "corrupt probe provider");
 		return (NULL);
 	}
 
 	(void) strncpy(desc->dtpd_provider,
 	    (char *)(str + probe->dofp_provider),
 	    MIN(DTRACE_PROVNAMELEN - 1, size - probe->dofp_provider));
 
 	if (probe->dofp_mod >= strtab->dofs_size) {
 		dtrace_dof_error(dof, "corrupt probe module");
 		return (NULL);
 	}
 
 	(void) strncpy(desc->dtpd_mod, (char *)(str + probe->dofp_mod),
 	    MIN(DTRACE_MODNAMELEN - 1, size - probe->dofp_mod));
 
 	if (probe->dofp_func >= strtab->dofs_size) {
 		dtrace_dof_error(dof, "corrupt probe function");
 		return (NULL);
 	}
 
 	(void) strncpy(desc->dtpd_func, (char *)(str + probe->dofp_func),
 	    MIN(DTRACE_FUNCNAMELEN - 1, size - probe->dofp_func));
 
 	if (probe->dofp_name >= strtab->dofs_size) {
 		dtrace_dof_error(dof, "corrupt probe name");
 		return (NULL);
 	}
 
 	(void) strncpy(desc->dtpd_name, (char *)(str + probe->dofp_name),
 	    MIN(DTRACE_NAMELEN - 1, size - probe->dofp_name));
 
 	return (desc);
 }
 
 static dtrace_difo_t *
 dtrace_dof_difo(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate,
     cred_t *cr)
 {
 	dtrace_difo_t *dp;
 	size_t ttl = 0;
 	dof_difohdr_t *dofd;
 	uintptr_t daddr = (uintptr_t)dof;
 	size_t max = dtrace_difo_maxsize;
 	int i, l, n;
 
 	static const struct {
 		int section;
 		int bufoffs;
 		int lenoffs;
 		int entsize;
 		int align;
 		const char *msg;
 	} difo[] = {
 		{ DOF_SECT_DIF, offsetof(dtrace_difo_t, dtdo_buf),
 		offsetof(dtrace_difo_t, dtdo_len), sizeof (dif_instr_t),
 		sizeof (dif_instr_t), "multiple DIF sections" },
 
 		{ DOF_SECT_INTTAB, offsetof(dtrace_difo_t, dtdo_inttab),
 		offsetof(dtrace_difo_t, dtdo_intlen), sizeof (uint64_t),
 		sizeof (uint64_t), "multiple integer tables" },
 
 		{ DOF_SECT_STRTAB, offsetof(dtrace_difo_t, dtdo_strtab),
 		offsetof(dtrace_difo_t, dtdo_strlen), 0,
 		sizeof (char), "multiple string tables" },
 
 		{ DOF_SECT_VARTAB, offsetof(dtrace_difo_t, dtdo_vartab),
 		offsetof(dtrace_difo_t, dtdo_varlen), sizeof (dtrace_difv_t),
 		sizeof (uint_t), "multiple variable tables" },
 
 		{ DOF_SECT_NONE, 0, 0, 0, 0, NULL }
 	};
 
 	if (sec->dofs_type != DOF_SECT_DIFOHDR) {
 		dtrace_dof_error(dof, "invalid DIFO header section");
 		return (NULL);
 	}
 
 	if (sec->dofs_align != sizeof (dof_secidx_t)) {
 		dtrace_dof_error(dof, "bad alignment in DIFO header");
 		return (NULL);
 	}
 
 	if (sec->dofs_size < sizeof (dof_difohdr_t) ||
 	    sec->dofs_size % sizeof (dof_secidx_t)) {
 		dtrace_dof_error(dof, "bad size in DIFO header");
 		return (NULL);
 	}
 
 	dofd = (dof_difohdr_t *)(uintptr_t)(daddr + sec->dofs_offset);
 	n = (sec->dofs_size - sizeof (*dofd)) / sizeof (dof_secidx_t) + 1;
 
 	dp = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP);
 	dp->dtdo_rtype = dofd->dofd_rtype;
 
 	for (l = 0; l < n; l++) {
 		dof_sec_t *subsec;
 		void **bufp;
 		uint32_t *lenp;
 
 		if ((subsec = dtrace_dof_sect(dof, DOF_SECT_NONE,
 		    dofd->dofd_links[l])) == NULL)
 			goto err; /* invalid section link */
 
 		if (ttl + subsec->dofs_size > max) {
 			dtrace_dof_error(dof, "exceeds maximum size");
 			goto err;
 		}
 
 		ttl += subsec->dofs_size;
 
 		for (i = 0; difo[i].section != DOF_SECT_NONE; i++) {
 			if (subsec->dofs_type != difo[i].section)
 				continue;
 
 			if (!(subsec->dofs_flags & DOF_SECF_LOAD)) {
 				dtrace_dof_error(dof, "section not loaded");
 				goto err;
 			}
 
 			if (subsec->dofs_align != difo[i].align) {
 				dtrace_dof_error(dof, "bad alignment");
 				goto err;
 			}
 
 			bufp = (void **)((uintptr_t)dp + difo[i].bufoffs);
 			lenp = (uint32_t *)((uintptr_t)dp + difo[i].lenoffs);
 
 			if (*bufp != NULL) {
 				dtrace_dof_error(dof, difo[i].msg);
 				goto err;
 			}
 
 			if (difo[i].entsize != subsec->dofs_entsize) {
 				dtrace_dof_error(dof, "entry size mismatch");
 				goto err;
 			}
 
 			if (subsec->dofs_entsize != 0 &&
 			    (subsec->dofs_size % subsec->dofs_entsize) != 0) {
 				dtrace_dof_error(dof, "corrupt entry size");
 				goto err;
 			}
 
 			*lenp = subsec->dofs_size;
 			*bufp = kmem_alloc(subsec->dofs_size, KM_SLEEP);
 			bcopy((char *)(uintptr_t)(daddr + subsec->dofs_offset),
 			    *bufp, subsec->dofs_size);
 
 			if (subsec->dofs_entsize != 0)
 				*lenp /= subsec->dofs_entsize;
 
 			break;
 		}
 
 		/*
 		 * If we encounter a loadable DIFO sub-section that is not
 		 * known to us, assume this is a broken program and fail.
 		 */
 		if (difo[i].section == DOF_SECT_NONE &&
 		    (subsec->dofs_flags & DOF_SECF_LOAD)) {
 			dtrace_dof_error(dof, "unrecognized DIFO subsection");
 			goto err;
 		}
 	}
 
 	if (dp->dtdo_buf == NULL) {
 		/*
 		 * We can't have a DIF object without DIF text.
 		 */
 		dtrace_dof_error(dof, "missing DIF text");
 		goto err;
 	}
 
 	/*
 	 * Before we validate the DIF object, run through the variable table
 	 * looking for the strings -- if any of their size are under, we'll set
 	 * their size to be the system-wide default string size.  Note that
 	 * this should _not_ happen if the "strsize" option has been set --
 	 * in this case, the compiler should have set the size to reflect the
 	 * setting of the option.
 	 */
 	for (i = 0; i < dp->dtdo_varlen; i++) {
 		dtrace_difv_t *v = &dp->dtdo_vartab[i];
 		dtrace_diftype_t *t = &v->dtdv_type;
 
 		if (v->dtdv_id < DIF_VAR_OTHER_UBASE)
 			continue;
 
 		if (t->dtdt_kind == DIF_TYPE_STRING && t->dtdt_size == 0)
 			t->dtdt_size = dtrace_strsize_default;
 	}
 
 	if (dtrace_difo_validate(dp, vstate, DIF_DIR_NREGS, cr) != 0)
 		goto err;
 
 	dtrace_difo_init(dp, vstate);
 	return (dp);
 
 err:
 	kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t));
 	kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t));
 	kmem_free(dp->dtdo_strtab, dp->dtdo_strlen);
 	kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t));
 
 	kmem_free(dp, sizeof (dtrace_difo_t));
 	return (NULL);
 }
 
 static dtrace_predicate_t *
 dtrace_dof_predicate(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate,
     cred_t *cr)
 {
 	dtrace_difo_t *dp;
 
 	if ((dp = dtrace_dof_difo(dof, sec, vstate, cr)) == NULL)
 		return (NULL);
 
 	return (dtrace_predicate_create(dp));
 }
 
 static dtrace_actdesc_t *
 dtrace_dof_actdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate,
     cred_t *cr)
 {
 	dtrace_actdesc_t *act, *first = NULL, *last = NULL, *next;
 	dof_actdesc_t *desc;
 	dof_sec_t *difosec;
 	size_t offs;
 	uintptr_t daddr = (uintptr_t)dof;
 	uint64_t arg;
 	dtrace_actkind_t kind;
 
 	if (sec->dofs_type != DOF_SECT_ACTDESC) {
 		dtrace_dof_error(dof, "invalid action section");
 		return (NULL);
 	}
 
 	if (sec->dofs_offset + sizeof (dof_actdesc_t) > dof->dofh_loadsz) {
 		dtrace_dof_error(dof, "truncated action description");
 		return (NULL);
 	}
 
 	if (sec->dofs_align != sizeof (uint64_t)) {
 		dtrace_dof_error(dof, "bad alignment in action description");
 		return (NULL);
 	}
 
 	if (sec->dofs_size < sec->dofs_entsize) {
 		dtrace_dof_error(dof, "section entry size exceeds total size");
 		return (NULL);
 	}
 
 	if (sec->dofs_entsize != sizeof (dof_actdesc_t)) {
 		dtrace_dof_error(dof, "bad entry size in action description");
 		return (NULL);
 	}
 
 	if (sec->dofs_size / sec->dofs_entsize > dtrace_actions_max) {
 		dtrace_dof_error(dof, "actions exceed dtrace_actions_max");
 		return (NULL);
 	}
 
 	for (offs = 0; offs < sec->dofs_size; offs += sec->dofs_entsize) {
 		desc = (dof_actdesc_t *)(daddr +
 		    (uintptr_t)sec->dofs_offset + offs);
 		kind = (dtrace_actkind_t)desc->dofa_kind;
 
 		if ((DTRACEACT_ISPRINTFLIKE(kind) &&
 		    (kind != DTRACEACT_PRINTA ||
 		    desc->dofa_strtab != DOF_SECIDX_NONE)) ||
 		    (kind == DTRACEACT_DIFEXPR &&
 		    desc->dofa_strtab != DOF_SECIDX_NONE)) {
 			dof_sec_t *strtab;
 			char *str, *fmt;
 			uint64_t i;
 
 			/*
 			 * The argument to these actions is an index into the
 			 * DOF string table.  For printf()-like actions, this
 			 * is the format string.  For print(), this is the
 			 * CTF type of the expression result.
 			 */
 			if ((strtab = dtrace_dof_sect(dof,
 			    DOF_SECT_STRTAB, desc->dofa_strtab)) == NULL)
 				goto err;
 
 			str = (char *)((uintptr_t)dof +
 			    (uintptr_t)strtab->dofs_offset);
 
 			for (i = desc->dofa_arg; i < strtab->dofs_size; i++) {
 				if (str[i] == '\0')
 					break;
 			}
 
 			if (i >= strtab->dofs_size) {
 				dtrace_dof_error(dof, "bogus format string");
 				goto err;
 			}
 
 			if (i == desc->dofa_arg) {
 				dtrace_dof_error(dof, "empty format string");
 				goto err;
 			}
 
 			i -= desc->dofa_arg;
 			fmt = kmem_alloc(i + 1, KM_SLEEP);
 			bcopy(&str[desc->dofa_arg], fmt, i + 1);
 			arg = (uint64_t)(uintptr_t)fmt;
 		} else {
 			if (kind == DTRACEACT_PRINTA) {
 				ASSERT(desc->dofa_strtab == DOF_SECIDX_NONE);
 				arg = 0;
 			} else {
 				arg = desc->dofa_arg;
 			}
 		}
 
 		act = dtrace_actdesc_create(kind, desc->dofa_ntuple,
 		    desc->dofa_uarg, arg);
 
 		if (last != NULL) {
 			last->dtad_next = act;
 		} else {
 			first = act;
 		}
 
 		last = act;
 
 		if (desc->dofa_difo == DOF_SECIDX_NONE)
 			continue;
 
 		if ((difosec = dtrace_dof_sect(dof,
 		    DOF_SECT_DIFOHDR, desc->dofa_difo)) == NULL)
 			goto err;
 
 		act->dtad_difo = dtrace_dof_difo(dof, difosec, vstate, cr);
 
 		if (act->dtad_difo == NULL)
 			goto err;
 	}
 
 	ASSERT(first != NULL);
 	return (first);
 
 err:
 	for (act = first; act != NULL; act = next) {
 		next = act->dtad_next;
 		dtrace_actdesc_release(act, vstate);
 	}
 
 	return (NULL);
 }
 
 static dtrace_ecbdesc_t *
 dtrace_dof_ecbdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate,
     cred_t *cr)
 {
 	dtrace_ecbdesc_t *ep;
 	dof_ecbdesc_t *ecb;
 	dtrace_probedesc_t *desc;
 	dtrace_predicate_t *pred = NULL;
 
 	if (sec->dofs_size < sizeof (dof_ecbdesc_t)) {
 		dtrace_dof_error(dof, "truncated ECB description");
 		return (NULL);
 	}
 
 	if (sec->dofs_align != sizeof (uint64_t)) {
 		dtrace_dof_error(dof, "bad alignment in ECB description");
 		return (NULL);
 	}
 
 	ecb = (dof_ecbdesc_t *)((uintptr_t)dof + (uintptr_t)sec->dofs_offset);
 	sec = dtrace_dof_sect(dof, DOF_SECT_PROBEDESC, ecb->dofe_probes);
 
 	if (sec == NULL)
 		return (NULL);
 
 	ep = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP);
 	ep->dted_uarg = ecb->dofe_uarg;
 	desc = &ep->dted_probe;
 
 	if (dtrace_dof_probedesc(dof, sec, desc) == NULL)
 		goto err;
 
 	if (ecb->dofe_pred != DOF_SECIDX_NONE) {
 		if ((sec = dtrace_dof_sect(dof,
 		    DOF_SECT_DIFOHDR, ecb->dofe_pred)) == NULL)
 			goto err;
 
 		if ((pred = dtrace_dof_predicate(dof, sec, vstate, cr)) == NULL)
 			goto err;
 
 		ep->dted_pred.dtpdd_predicate = pred;
 	}
 
 	if (ecb->dofe_actions != DOF_SECIDX_NONE) {
 		if ((sec = dtrace_dof_sect(dof,
 		    DOF_SECT_ACTDESC, ecb->dofe_actions)) == NULL)
 			goto err;
 
 		ep->dted_action = dtrace_dof_actdesc(dof, sec, vstate, cr);
 
 		if (ep->dted_action == NULL)
 			goto err;
 	}
 
 	return (ep);
 
 err:
 	if (pred != NULL)
 		dtrace_predicate_release(pred, vstate);
 	kmem_free(ep, sizeof (dtrace_ecbdesc_t));
 	return (NULL);
 }
 
 /*
  * Apply the relocations from the specified 'sec' (a DOF_SECT_URELHDR) to the
  * specified DOF.  SETX relocations are computed using 'ubase', the base load
  * address of the object containing the DOF, and DOFREL relocations are relative
  * to the relocation offset within the DOF.
  */
 static int
 dtrace_dof_relocate(dof_hdr_t *dof, dof_sec_t *sec, uint64_t ubase,
     uint64_t udaddr)
 {
 	uintptr_t daddr = (uintptr_t)dof;
 	uintptr_t ts_end;
 	dof_relohdr_t *dofr =
 	    (dof_relohdr_t *)(uintptr_t)(daddr + sec->dofs_offset);
 	dof_sec_t *ss, *rs, *ts;
 	dof_relodesc_t *r;
 	uint_t i, n;
 
 	if (sec->dofs_size < sizeof (dof_relohdr_t) ||
 	    sec->dofs_align != sizeof (dof_secidx_t)) {
 		dtrace_dof_error(dof, "invalid relocation header");
 		return (-1);
 	}
 
 	ss = dtrace_dof_sect(dof, DOF_SECT_STRTAB, dofr->dofr_strtab);
 	rs = dtrace_dof_sect(dof, DOF_SECT_RELTAB, dofr->dofr_relsec);
 	ts = dtrace_dof_sect(dof, DOF_SECT_NONE, dofr->dofr_tgtsec);
 	ts_end = (uintptr_t)ts + sizeof (dof_sec_t);
 
 	if (ss == NULL || rs == NULL || ts == NULL)
 		return (-1); /* dtrace_dof_error() has been called already */
 
 	if (rs->dofs_entsize < sizeof (dof_relodesc_t) ||
 	    rs->dofs_align != sizeof (uint64_t)) {
 		dtrace_dof_error(dof, "invalid relocation section");
 		return (-1);
 	}
 
 	r = (dof_relodesc_t *)(uintptr_t)(daddr + rs->dofs_offset);
 	n = rs->dofs_size / rs->dofs_entsize;
 
 	for (i = 0; i < n; i++) {
 		uintptr_t taddr = daddr + ts->dofs_offset + r->dofr_offset;
 
 		switch (r->dofr_type) {
 		case DOF_RELO_NONE:
 			break;
 		case DOF_RELO_SETX:
 		case DOF_RELO_DOFREL:
 			if (r->dofr_offset >= ts->dofs_size || r->dofr_offset +
 			    sizeof (uint64_t) > ts->dofs_size) {
 				dtrace_dof_error(dof, "bad relocation offset");
 				return (-1);
 			}
 
 			if (taddr >= (uintptr_t)ts && taddr < ts_end) {
 				dtrace_dof_error(dof, "bad relocation offset");
 				return (-1);
 			}
 
 			if (!IS_P2ALIGNED(taddr, sizeof (uint64_t))) {
 				dtrace_dof_error(dof, "misaligned setx relo");
 				return (-1);
 			}
 
 			if (r->dofr_type == DOF_RELO_SETX)
 				*(uint64_t *)taddr += ubase;
 			else
 				*(uint64_t *)taddr +=
 				    udaddr + ts->dofs_offset + r->dofr_offset;
 			break;
 		default:
 			dtrace_dof_error(dof, "invalid relocation type");
 			return (-1);
 		}
 
 		r = (dof_relodesc_t *)((uintptr_t)r + rs->dofs_entsize);
 	}
 
 	return (0);
 }
 
 /*
  * The dof_hdr_t passed to dtrace_dof_slurp() should be a partially validated
  * header:  it should be at the front of a memory region that is at least
  * sizeof (dof_hdr_t) in size -- and then at least dof_hdr.dofh_loadsz in
  * size.  It need not be validated in any other way.
  */
 static int
 dtrace_dof_slurp(dof_hdr_t *dof, dtrace_vstate_t *vstate, cred_t *cr,
     dtrace_enabling_t **enabp, uint64_t ubase, uint64_t udaddr, int noprobes)
 {
 	uint64_t len = dof->dofh_loadsz, seclen;
 	uintptr_t daddr = (uintptr_t)dof;
 	dtrace_ecbdesc_t *ep;
 	dtrace_enabling_t *enab;
 	uint_t i;
 
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 	ASSERT(dof->dofh_loadsz >= sizeof (dof_hdr_t));
 
 	/*
 	 * Check the DOF header identification bytes.  In addition to checking
 	 * valid settings, we also verify that unused bits/bytes are zeroed so
 	 * we can use them later without fear of regressing existing binaries.
 	 */
 	if (bcmp(&dof->dofh_ident[DOF_ID_MAG0],
 	    DOF_MAG_STRING, DOF_MAG_STRLEN) != 0) {
 		dtrace_dof_error(dof, "DOF magic string mismatch");
 		return (-1);
 	}
 
 	if (dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_ILP32 &&
 	    dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_LP64) {
 		dtrace_dof_error(dof, "DOF has invalid data model");
 		return (-1);
 	}
 
 	if (dof->dofh_ident[DOF_ID_ENCODING] != DOF_ENCODE_NATIVE) {
 		dtrace_dof_error(dof, "DOF encoding mismatch");
 		return (-1);
 	}
 
 	if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 &&
 	    dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_2) {
 		dtrace_dof_error(dof, "DOF version mismatch");
 		return (-1);
 	}
 
 	if (dof->dofh_ident[DOF_ID_DIFVERS] != DIF_VERSION_2) {
 		dtrace_dof_error(dof, "DOF uses unsupported instruction set");
 		return (-1);
 	}
 
 	if (dof->dofh_ident[DOF_ID_DIFIREG] > DIF_DIR_NREGS) {
 		dtrace_dof_error(dof, "DOF uses too many integer registers");
 		return (-1);
 	}
 
 	if (dof->dofh_ident[DOF_ID_DIFTREG] > DIF_DTR_NREGS) {
 		dtrace_dof_error(dof, "DOF uses too many tuple registers");
 		return (-1);
 	}
 
 	for (i = DOF_ID_PAD; i < DOF_ID_SIZE; i++) {
 		if (dof->dofh_ident[i] != 0) {
 			dtrace_dof_error(dof, "DOF has invalid ident byte set");
 			return (-1);
 		}
 	}
 
 	if (dof->dofh_flags & ~DOF_FL_VALID) {
 		dtrace_dof_error(dof, "DOF has invalid flag bits set");
 		return (-1);
 	}
 
 	if (dof->dofh_secsize == 0) {
 		dtrace_dof_error(dof, "zero section header size");
 		return (-1);
 	}
 
 	/*
 	 * Check that the section headers don't exceed the amount of DOF
 	 * data.  Note that we cast the section size and number of sections
 	 * to uint64_t's to prevent possible overflow in the multiplication.
 	 */
 	seclen = (uint64_t)dof->dofh_secnum * (uint64_t)dof->dofh_secsize;
 
 	if (dof->dofh_secoff > len || seclen > len ||
 	    dof->dofh_secoff + seclen > len) {
 		dtrace_dof_error(dof, "truncated section headers");
 		return (-1);
 	}
 
 	if (!IS_P2ALIGNED(dof->dofh_secoff, sizeof (uint64_t))) {
 		dtrace_dof_error(dof, "misaligned section headers");
 		return (-1);
 	}
 
 	if (!IS_P2ALIGNED(dof->dofh_secsize, sizeof (uint64_t))) {
 		dtrace_dof_error(dof, "misaligned section size");
 		return (-1);
 	}
 
 	/*
 	 * Take an initial pass through the section headers to be sure that
 	 * the headers don't have stray offsets.  If the 'noprobes' flag is
 	 * set, do not permit sections relating to providers, probes, or args.
 	 */
 	for (i = 0; i < dof->dofh_secnum; i++) {
 		dof_sec_t *sec = (dof_sec_t *)(daddr +
 		    (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize);
 
 		if (noprobes) {
 			switch (sec->dofs_type) {
 			case DOF_SECT_PROVIDER:
 			case DOF_SECT_PROBES:
 			case DOF_SECT_PRARGS:
 			case DOF_SECT_PROFFS:
 				dtrace_dof_error(dof, "illegal sections "
 				    "for enabling");
 				return (-1);
 			}
 		}
 
 		if (DOF_SEC_ISLOADABLE(sec->dofs_type) &&
 		    !(sec->dofs_flags & DOF_SECF_LOAD)) {
 			dtrace_dof_error(dof, "loadable section with load "
 			    "flag unset");
 			return (-1);
 		}
 
 		if (!(sec->dofs_flags & DOF_SECF_LOAD))
 			continue; /* just ignore non-loadable sections */
 
 		if (!ISP2(sec->dofs_align)) {
 			dtrace_dof_error(dof, "bad section alignment");
 			return (-1);
 		}
 
 		if (sec->dofs_offset & (sec->dofs_align - 1)) {
 			dtrace_dof_error(dof, "misaligned section");
 			return (-1);
 		}
 
 		if (sec->dofs_offset > len || sec->dofs_size > len ||
 		    sec->dofs_offset + sec->dofs_size > len) {
 			dtrace_dof_error(dof, "corrupt section header");
 			return (-1);
 		}
 
 		if (sec->dofs_type == DOF_SECT_STRTAB && *((char *)daddr +
 		    sec->dofs_offset + sec->dofs_size - 1) != '\0') {
 			dtrace_dof_error(dof, "non-terminating string table");
 			return (-1);
 		}
 	}
 
 	/*
 	 * Take a second pass through the sections and locate and perform any
 	 * relocations that are present.  We do this after the first pass to
 	 * be sure that all sections have had their headers validated.
 	 */
 	for (i = 0; i < dof->dofh_secnum; i++) {
 		dof_sec_t *sec = (dof_sec_t *)(daddr +
 		    (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize);
 
 		if (!(sec->dofs_flags & DOF_SECF_LOAD))
 			continue; /* skip sections that are not loadable */
 
 		switch (sec->dofs_type) {
 		case DOF_SECT_URELHDR:
 			if (dtrace_dof_relocate(dof, sec, ubase, udaddr) != 0)
 				return (-1);
 			break;
 		}
 	}
 
 	if ((enab = *enabp) == NULL)
 		enab = *enabp = dtrace_enabling_create(vstate);
 
 	for (i = 0; i < dof->dofh_secnum; i++) {
 		dof_sec_t *sec = (dof_sec_t *)(daddr +
 		    (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize);
 
 		if (sec->dofs_type != DOF_SECT_ECBDESC)
 			continue;
 
 		if ((ep = dtrace_dof_ecbdesc(dof, sec, vstate, cr)) == NULL) {
 			dtrace_enabling_destroy(enab);
 			*enabp = NULL;
 			return (-1);
 		}
 
 		dtrace_enabling_add(enab, ep);
 	}
 
 	return (0);
 }
 
 /*
  * Process DOF for any options.  This routine assumes that the DOF has been
  * at least processed by dtrace_dof_slurp().
  */
 static int
 dtrace_dof_options(dof_hdr_t *dof, dtrace_state_t *state)
 {
 	int i, rval;
 	uint32_t entsize;
 	size_t offs;
 	dof_optdesc_t *desc;
 
 	for (i = 0; i < dof->dofh_secnum; i++) {
 		dof_sec_t *sec = (dof_sec_t *)((uintptr_t)dof +
 		    (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize);
 
 		if (sec->dofs_type != DOF_SECT_OPTDESC)
 			continue;
 
 		if (sec->dofs_align != sizeof (uint64_t)) {
 			dtrace_dof_error(dof, "bad alignment in "
 			    "option description");
 			return (EINVAL);
 		}
 
 		if ((entsize = sec->dofs_entsize) == 0) {
 			dtrace_dof_error(dof, "zeroed option entry size");
 			return (EINVAL);
 		}
 
 		if (entsize < sizeof (dof_optdesc_t)) {
 			dtrace_dof_error(dof, "bad option entry size");
 			return (EINVAL);
 		}
 
 		for (offs = 0; offs < sec->dofs_size; offs += entsize) {
 			desc = (dof_optdesc_t *)((uintptr_t)dof +
 			    (uintptr_t)sec->dofs_offset + offs);
 
 			if (desc->dofo_strtab != DOF_SECIDX_NONE) {
 				dtrace_dof_error(dof, "non-zero option string");
 				return (EINVAL);
 			}
 
 			if (desc->dofo_value == DTRACEOPT_UNSET) {
 				dtrace_dof_error(dof, "unset option");
 				return (EINVAL);
 			}
 
 			if ((rval = dtrace_state_option(state,
 			    desc->dofo_option, desc->dofo_value)) != 0) {
 				dtrace_dof_error(dof, "rejected option");
 				return (rval);
 			}
 		}
 	}
 
 	return (0);
 }
 
 /*
  * DTrace Consumer State Functions
  */
 static int
 dtrace_dstate_init(dtrace_dstate_t *dstate, size_t size)
 {
 	size_t hashsize, maxper, min, chunksize = dstate->dtds_chunksize;
 	void *base;
 	uintptr_t limit;
 	dtrace_dynvar_t *dvar, *next, *start;
 	int i;
 
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 	ASSERT(dstate->dtds_base == NULL && dstate->dtds_percpu == NULL);
 
 	bzero(dstate, sizeof (dtrace_dstate_t));
 
 	if ((dstate->dtds_chunksize = chunksize) == 0)
 		dstate->dtds_chunksize = DTRACE_DYNVAR_CHUNKSIZE;
 
 	VERIFY(dstate->dtds_chunksize < LONG_MAX);
 
 	if (size < (min = dstate->dtds_chunksize + sizeof (dtrace_dynhash_t)))
 		size = min;
 
 	if ((base = kmem_zalloc(size, KM_NOSLEEP | KM_NORMALPRI)) == NULL)
 		return (ENOMEM);
 
 	dstate->dtds_size = size;
 	dstate->dtds_base = base;
 	dstate->dtds_percpu = kmem_cache_alloc(dtrace_state_cache, KM_SLEEP);
 	bzero(dstate->dtds_percpu, NCPU * sizeof (dtrace_dstate_percpu_t));
 
 	hashsize = size / (dstate->dtds_chunksize + sizeof (dtrace_dynhash_t));
 
 	if (hashsize != 1 && (hashsize & 1))
 		hashsize--;
 
 	dstate->dtds_hashsize = hashsize;
 	dstate->dtds_hash = dstate->dtds_base;
 
 	/*
 	 * Set all of our hash buckets to point to the single sink, and (if
 	 * it hasn't already been set), set the sink's hash value to be the
 	 * sink sentinel value.  The sink is needed for dynamic variable
 	 * lookups to know that they have iterated over an entire, valid hash
 	 * chain.
 	 */
 	for (i = 0; i < hashsize; i++)
 		dstate->dtds_hash[i].dtdh_chain = &dtrace_dynhash_sink;
 
 	if (dtrace_dynhash_sink.dtdv_hashval != DTRACE_DYNHASH_SINK)
 		dtrace_dynhash_sink.dtdv_hashval = DTRACE_DYNHASH_SINK;
 
 	/*
 	 * Determine number of active CPUs.  Divide free list evenly among
 	 * active CPUs.
 	 */
 	start = (dtrace_dynvar_t *)
 	    ((uintptr_t)base + hashsize * sizeof (dtrace_dynhash_t));
 	limit = (uintptr_t)base + size;
 
 	VERIFY((uintptr_t)start < limit);
 	VERIFY((uintptr_t)start >= (uintptr_t)base);
 
 	maxper = (limit - (uintptr_t)start) / NCPU;
 	maxper = (maxper / dstate->dtds_chunksize) * dstate->dtds_chunksize;
 
 #ifndef illumos
 	CPU_FOREACH(i) {
 #else
 	for (i = 0; i < NCPU; i++) {
 #endif
 		dstate->dtds_percpu[i].dtdsc_free = dvar = start;
 
 		/*
 		 * If we don't even have enough chunks to make it once through
 		 * NCPUs, we're just going to allocate everything to the first
 		 * CPU.  And if we're on the last CPU, we're going to allocate
 		 * whatever is left over.  In either case, we set the limit to
 		 * be the limit of the dynamic variable space.
 		 */
 		if (maxper == 0 || i == NCPU - 1) {
 			limit = (uintptr_t)base + size;
 			start = NULL;
 		} else {
 			limit = (uintptr_t)start + maxper;
 			start = (dtrace_dynvar_t *)limit;
 		}
 
 		VERIFY(limit <= (uintptr_t)base + size);
 
 		for (;;) {
 			next = (dtrace_dynvar_t *)((uintptr_t)dvar +
 			    dstate->dtds_chunksize);
 
 			if ((uintptr_t)next + dstate->dtds_chunksize >= limit)
 				break;
 
 			VERIFY((uintptr_t)dvar >= (uintptr_t)base &&
 			    (uintptr_t)dvar <= (uintptr_t)base + size);
 			dvar->dtdv_next = next;
 			dvar = next;
 		}
 
 		if (maxper == 0)
 			break;
 	}
 
 	return (0);
 }
 
 static void
 dtrace_dstate_fini(dtrace_dstate_t *dstate)
 {
 	ASSERT(MUTEX_HELD(&cpu_lock));
 
 	if (dstate->dtds_base == NULL)
 		return;
 
 	kmem_free(dstate->dtds_base, dstate->dtds_size);
 	kmem_cache_free(dtrace_state_cache, dstate->dtds_percpu);
 }
 
 static void
 dtrace_vstate_fini(dtrace_vstate_t *vstate)
 {
 	/*
 	 * Logical XOR, where are you?
 	 */
 	ASSERT((vstate->dtvs_nglobals == 0) ^ (vstate->dtvs_globals != NULL));
 
 	if (vstate->dtvs_nglobals > 0) {
 		kmem_free(vstate->dtvs_globals, vstate->dtvs_nglobals *
 		    sizeof (dtrace_statvar_t *));
 	}
 
 	if (vstate->dtvs_ntlocals > 0) {
 		kmem_free(vstate->dtvs_tlocals, vstate->dtvs_ntlocals *
 		    sizeof (dtrace_difv_t));
 	}
 
 	ASSERT((vstate->dtvs_nlocals == 0) ^ (vstate->dtvs_locals != NULL));
 
 	if (vstate->dtvs_nlocals > 0) {
 		kmem_free(vstate->dtvs_locals, vstate->dtvs_nlocals *
 		    sizeof (dtrace_statvar_t *));
 	}
 }
 
 #ifdef illumos
 static void
 dtrace_state_clean(dtrace_state_t *state)
 {
 	if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE)
 		return;
 
 	dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars);
 	dtrace_speculation_clean(state);
 }
 
 static void
 dtrace_state_deadman(dtrace_state_t *state)
 {
 	hrtime_t now;
 
 	dtrace_sync();
 
 	now = dtrace_gethrtime();
 
 	if (state != dtrace_anon.dta_state &&
 	    now - state->dts_laststatus >= dtrace_deadman_user)
 		return;
 
 	/*
 	 * We must be sure that dts_alive never appears to be less than the
 	 * value upon entry to dtrace_state_deadman(), and because we lack a
 	 * dtrace_cas64(), we cannot store to it atomically.  We thus instead
 	 * store INT64_MAX to it, followed by a memory barrier, followed by
 	 * the new value.  This assures that dts_alive never appears to be
 	 * less than its true value, regardless of the order in which the
 	 * stores to the underlying storage are issued.
 	 */
 	state->dts_alive = INT64_MAX;
 	dtrace_membar_producer();
 	state->dts_alive = now;
 }
 #else	/* !illumos */
 static void
 dtrace_state_clean(void *arg)
 {
 	dtrace_state_t *state = arg;
 	dtrace_optval_t *opt = state->dts_options;
 
 	if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE)
 		return;
 
 	dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars);
 	dtrace_speculation_clean(state);
 
 	callout_reset(&state->dts_cleaner, hz * opt[DTRACEOPT_CLEANRATE] / NANOSEC,
 	    dtrace_state_clean, state);
 }
 
 static void
 dtrace_state_deadman(void *arg)
 {
 	dtrace_state_t *state = arg;
 	hrtime_t now;
 
 	dtrace_sync();
 
 	dtrace_debug_output();
 
 	now = dtrace_gethrtime();
 
 	if (state != dtrace_anon.dta_state &&
 	    now - state->dts_laststatus >= dtrace_deadman_user)
 		return;
 
 	/*
 	 * We must be sure that dts_alive never appears to be less than the
 	 * value upon entry to dtrace_state_deadman(), and because we lack a
 	 * dtrace_cas64(), we cannot store to it atomically.  We thus instead
 	 * store INT64_MAX to it, followed by a memory barrier, followed by
 	 * the new value.  This assures that dts_alive never appears to be
 	 * less than its true value, regardless of the order in which the
 	 * stores to the underlying storage are issued.
 	 */
 	state->dts_alive = INT64_MAX;
 	dtrace_membar_producer();
 	state->dts_alive = now;
 
 	callout_reset(&state->dts_deadman, hz * dtrace_deadman_interval / NANOSEC,
 	    dtrace_state_deadman, state);
 }
 #endif	/* illumos */
 
 static dtrace_state_t *
 #ifdef illumos
 dtrace_state_create(dev_t *devp, cred_t *cr)
 #else
 dtrace_state_create(struct cdev *dev, struct ucred *cred __unused)
 #endif
 {
 #ifdef illumos
 	minor_t minor;
 	major_t major;
 #else
 	cred_t *cr = NULL;
 	int m = 0;
 #endif
 	char c[30];
 	dtrace_state_t *state;
 	dtrace_optval_t *opt;
 	int bufsize = NCPU * sizeof (dtrace_buffer_t), i;
 	int cpu_it;
 
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 	ASSERT(MUTEX_HELD(&cpu_lock));
 
 #ifdef illumos
 	minor = (minor_t)(uintptr_t)vmem_alloc(dtrace_minor, 1,
 	    VM_BESTFIT | VM_SLEEP);
 
 	if (ddi_soft_state_zalloc(dtrace_softstate, minor) != DDI_SUCCESS) {
 		vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1);
 		return (NULL);
 	}
 
 	state = ddi_get_soft_state(dtrace_softstate, minor);
 #else
 	if (dev != NULL) {
 		cr = dev->si_cred;
 		m = dev2unit(dev);
 	}
 
 	/* Allocate memory for the state. */
 	state = kmem_zalloc(sizeof(dtrace_state_t), KM_SLEEP);
 #endif
 
 	state->dts_epid = DTRACE_EPIDNONE + 1;
 
 	(void) snprintf(c, sizeof (c), "dtrace_aggid_%d", m);
 #ifdef illumos
 	state->dts_aggid_arena = vmem_create(c, (void *)1, UINT32_MAX, 1,
 	    NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
 
 	if (devp != NULL) {
 		major = getemajor(*devp);
 	} else {
 		major = ddi_driver_major(dtrace_devi);
 	}
 
 	state->dts_dev = makedevice(major, minor);
 
 	if (devp != NULL)
 		*devp = state->dts_dev;
 #else
 	state->dts_aggid_arena = new_unrhdr(1, INT_MAX, &dtrace_unr_mtx);
 	state->dts_dev = dev;
 #endif
 
 	/*
 	 * We allocate NCPU buffers.  On the one hand, this can be quite
 	 * a bit of memory per instance (nearly 36K on a Starcat).  On the
 	 * other hand, it saves an additional memory reference in the probe
 	 * path.
 	 */
 	state->dts_buffer = kmem_zalloc(bufsize, KM_SLEEP);
 	state->dts_aggbuffer = kmem_zalloc(bufsize, KM_SLEEP);
 
 	/*
          * Allocate and initialise the per-process per-CPU random state.
 	 * SI_SUB_RANDOM < SI_SUB_DTRACE_ANON therefore entropy device is
          * assumed to be seeded at this point (if from Fortuna seed file).
 	 */
 	arc4random_buf(&state->dts_rstate[0], 2 * sizeof(uint64_t));
 	for (cpu_it = 1; cpu_it < NCPU; cpu_it++) {
 		/*
 		 * Each CPU is assigned a 2^64 period, non-overlapping
 		 * subsequence.
 		 */
 		dtrace_xoroshiro128_plus_jump(state->dts_rstate[cpu_it-1],
 		    state->dts_rstate[cpu_it]); 
 	}
 
 #ifdef illumos
 	state->dts_cleaner = CYCLIC_NONE;
 	state->dts_deadman = CYCLIC_NONE;
 #else
 	callout_init(&state->dts_cleaner, 1);
 	callout_init(&state->dts_deadman, 1);
 #endif
 	state->dts_vstate.dtvs_state = state;
 
 	for (i = 0; i < DTRACEOPT_MAX; i++)
 		state->dts_options[i] = DTRACEOPT_UNSET;
 
 	/*
 	 * Set the default options.
 	 */
 	opt = state->dts_options;
 	opt[DTRACEOPT_BUFPOLICY] = DTRACEOPT_BUFPOLICY_SWITCH;
 	opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_AUTO;
 	opt[DTRACEOPT_NSPEC] = dtrace_nspec_default;
 	opt[DTRACEOPT_SPECSIZE] = dtrace_specsize_default;
 	opt[DTRACEOPT_CPU] = (dtrace_optval_t)DTRACE_CPUALL;
 	opt[DTRACEOPT_STRSIZE] = dtrace_strsize_default;
 	opt[DTRACEOPT_STACKFRAMES] = dtrace_stackframes_default;
 	opt[DTRACEOPT_USTACKFRAMES] = dtrace_ustackframes_default;
 	opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_default;
 	opt[DTRACEOPT_AGGRATE] = dtrace_aggrate_default;
 	opt[DTRACEOPT_SWITCHRATE] = dtrace_switchrate_default;
 	opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_default;
 	opt[DTRACEOPT_JSTACKFRAMES] = dtrace_jstackframes_default;
 	opt[DTRACEOPT_JSTACKSTRSIZE] = dtrace_jstackstrsize_default;
 
 	state->dts_activity = DTRACE_ACTIVITY_INACTIVE;
 
 	/*
 	 * Depending on the user credentials, we set flag bits which alter probe
 	 * visibility or the amount of destructiveness allowed.  In the case of
 	 * actual anonymous tracing, or the possession of all privileges, all of
 	 * the normal checks are bypassed.
 	 */
 	if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) {
 		state->dts_cred.dcr_visible = DTRACE_CRV_ALL;
 		state->dts_cred.dcr_action = DTRACE_CRA_ALL;
 	} else {
 		/*
 		 * Set up the credentials for this instantiation.  We take a
 		 * hold on the credential to prevent it from disappearing on
 		 * us; this in turn prevents the zone_t referenced by this
 		 * credential from disappearing.  This means that we can
 		 * examine the credential and the zone from probe context.
 		 */
 		crhold(cr);
 		state->dts_cred.dcr_cred = cr;
 
 		/*
 		 * CRA_PROC means "we have *some* privilege for dtrace" and
 		 * unlocks the use of variables like pid, zonename, etc.
 		 */
 		if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE) ||
 		    PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) {
 			state->dts_cred.dcr_action |= DTRACE_CRA_PROC;
 		}
 
 		/*
 		 * dtrace_user allows use of syscall and profile providers.
 		 * If the user also has proc_owner and/or proc_zone, we
 		 * extend the scope to include additional visibility and
 		 * destructive power.
 		 */
 		if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) {
 			if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) {
 				state->dts_cred.dcr_visible |=
 				    DTRACE_CRV_ALLPROC;
 
 				state->dts_cred.dcr_action |=
 				    DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER;
 			}
 
 			if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) {
 				state->dts_cred.dcr_visible |=
 				    DTRACE_CRV_ALLZONE;
 
 				state->dts_cred.dcr_action |=
 				    DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE;
 			}
 
 			/*
 			 * If we have all privs in whatever zone this is,
 			 * we can do destructive things to processes which
 			 * have altered credentials.
 			 */
 #ifdef illumos
 			if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE),
 			    cr->cr_zone->zone_privset)) {
 				state->dts_cred.dcr_action |=
 				    DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG;
 			}
 #endif
 		}
 
 		/*
 		 * Holding the dtrace_kernel privilege also implies that
 		 * the user has the dtrace_user privilege from a visibility
 		 * perspective.  But without further privileges, some
 		 * destructive actions are not available.
 		 */
 		if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) {
 			/*
 			 * Make all probes in all zones visible.  However,
 			 * this doesn't mean that all actions become available
 			 * to all zones.
 			 */
 			state->dts_cred.dcr_visible |= DTRACE_CRV_KERNEL |
 			    DTRACE_CRV_ALLPROC | DTRACE_CRV_ALLZONE;
 
 			state->dts_cred.dcr_action |= DTRACE_CRA_KERNEL |
 			    DTRACE_CRA_PROC;
 			/*
 			 * Holding proc_owner means that destructive actions
 			 * for *this* zone are allowed.
 			 */
 			if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE))
 				state->dts_cred.dcr_action |=
 				    DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER;
 
 			/*
 			 * Holding proc_zone means that destructive actions
 			 * for this user/group ID in all zones is allowed.
 			 */
 			if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE))
 				state->dts_cred.dcr_action |=
 				    DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE;
 
 #ifdef illumos
 			/*
 			 * If we have all privs in whatever zone this is,
 			 * we can do destructive things to processes which
 			 * have altered credentials.
 			 */
 			if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE),
 			    cr->cr_zone->zone_privset)) {
 				state->dts_cred.dcr_action |=
 				    DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG;
 			}
 #endif
 		}
 
 		/*
 		 * Holding the dtrace_proc privilege gives control over fasttrap
 		 * and pid providers.  We need to grant wider destructive
 		 * privileges in the event that the user has proc_owner and/or
 		 * proc_zone.
 		 */
 		if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) {
 			if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE))
 				state->dts_cred.dcr_action |=
 				    DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER;
 
 			if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE))
 				state->dts_cred.dcr_action |=
 				    DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE;
 		}
 	}
 
 	return (state);
 }
 
 static int
 dtrace_state_buffer(dtrace_state_t *state, dtrace_buffer_t *buf, int which)
 {
 	dtrace_optval_t *opt = state->dts_options, size;
 	processorid_t cpu = 0;
 	int flags = 0, rval, factor, divisor = 1;
 
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 	ASSERT(MUTEX_HELD(&cpu_lock));
 	ASSERT(which < DTRACEOPT_MAX);
 	ASSERT(state->dts_activity == DTRACE_ACTIVITY_INACTIVE ||
 	    (state == dtrace_anon.dta_state &&
 	    state->dts_activity == DTRACE_ACTIVITY_ACTIVE));
 
 	if (opt[which] == DTRACEOPT_UNSET || opt[which] == 0)
 		return (0);
 
 	if (opt[DTRACEOPT_CPU] != DTRACEOPT_UNSET)
 		cpu = opt[DTRACEOPT_CPU];
 
 	if (which == DTRACEOPT_SPECSIZE)
 		flags |= DTRACEBUF_NOSWITCH;
 
 	if (which == DTRACEOPT_BUFSIZE) {
 		if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_RING)
 			flags |= DTRACEBUF_RING;
 
 		if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_FILL)
 			flags |= DTRACEBUF_FILL;
 
 		if (state != dtrace_anon.dta_state ||
 		    state->dts_activity != DTRACE_ACTIVITY_ACTIVE)
 			flags |= DTRACEBUF_INACTIVE;
 	}
 
 	for (size = opt[which]; size >= sizeof (uint64_t); size /= divisor) {
 		/*
 		 * The size must be 8-byte aligned.  If the size is not 8-byte
 		 * aligned, drop it down by the difference.
 		 */
 		if (size & (sizeof (uint64_t) - 1))
 			size -= size & (sizeof (uint64_t) - 1);
 
 		if (size < state->dts_reserve) {
 			/*
 			 * Buffers always must be large enough to accommodate
 			 * their prereserved space.  We return E2BIG instead
 			 * of ENOMEM in this case to allow for user-level
 			 * software to differentiate the cases.
 			 */
 			return (E2BIG);
 		}
 
 		rval = dtrace_buffer_alloc(buf, size, flags, cpu, &factor);
 
 		if (rval != ENOMEM) {
 			opt[which] = size;
 			return (rval);
 		}
 
 		if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL)
 			return (rval);
 
 		for (divisor = 2; divisor < factor; divisor <<= 1)
 			continue;
 	}
 
 	return (ENOMEM);
 }
 
 static int
 dtrace_state_buffers(dtrace_state_t *state)
 {
 	dtrace_speculation_t *spec = state->dts_speculations;
 	int rval, i;
 
 	if ((rval = dtrace_state_buffer(state, state->dts_buffer,
 	    DTRACEOPT_BUFSIZE)) != 0)
 		return (rval);
 
 	if ((rval = dtrace_state_buffer(state, state->dts_aggbuffer,
 	    DTRACEOPT_AGGSIZE)) != 0)
 		return (rval);
 
 	for (i = 0; i < state->dts_nspeculations; i++) {
 		if ((rval = dtrace_state_buffer(state,
 		    spec[i].dtsp_buffer, DTRACEOPT_SPECSIZE)) != 0)
 			return (rval);
 	}
 
 	return (0);
 }
 
 static void
 dtrace_state_prereserve(dtrace_state_t *state)
 {
 	dtrace_ecb_t *ecb;
 	dtrace_probe_t *probe;
 
 	state->dts_reserve = 0;
 
 	if (state->dts_options[DTRACEOPT_BUFPOLICY] != DTRACEOPT_BUFPOLICY_FILL)
 		return;
 
 	/*
 	 * If our buffer policy is a "fill" buffer policy, we need to set the
 	 * prereserved space to be the space required by the END probes.
 	 */
 	probe = dtrace_probes[dtrace_probeid_end - 1];
 	ASSERT(probe != NULL);
 
 	for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) {
 		if (ecb->dte_state != state)
 			continue;
 
 		state->dts_reserve += ecb->dte_needed + ecb->dte_alignment;
 	}
 }
 
 static int
 dtrace_state_go(dtrace_state_t *state, processorid_t *cpu)
 {
 	dtrace_optval_t *opt = state->dts_options, sz, nspec;
 	dtrace_speculation_t *spec;
 	dtrace_buffer_t *buf;
 #ifdef illumos
 	cyc_handler_t hdlr;
 	cyc_time_t when;
 #endif
 	int rval = 0, i, bufsize = NCPU * sizeof (dtrace_buffer_t);
 	dtrace_icookie_t cookie;
 
 	mutex_enter(&cpu_lock);
 	mutex_enter(&dtrace_lock);
 
 	if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) {
 		rval = EBUSY;
 		goto out;
 	}
 
 	/*
 	 * Before we can perform any checks, we must prime all of the
 	 * retained enablings that correspond to this state.
 	 */
 	dtrace_enabling_prime(state);
 
 	if (state->dts_destructive && !state->dts_cred.dcr_destructive) {
 		rval = EACCES;
 		goto out;
 	}
 
 	dtrace_state_prereserve(state);
 
 	/*
 	 * Now we want to do is try to allocate our speculations.
 	 * We do not automatically resize the number of speculations; if
 	 * this fails, we will fail the operation.
 	 */
 	nspec = opt[DTRACEOPT_NSPEC];
 	ASSERT(nspec != DTRACEOPT_UNSET);
 
 	if (nspec > INT_MAX) {
 		rval = ENOMEM;
 		goto out;
 	}
 
 	spec = kmem_zalloc(nspec * sizeof (dtrace_speculation_t),
 	    KM_NOSLEEP | KM_NORMALPRI);
 
 	if (spec == NULL) {
 		rval = ENOMEM;
 		goto out;
 	}
 
 	state->dts_speculations = spec;
 	state->dts_nspeculations = (int)nspec;
 
 	for (i = 0; i < nspec; i++) {
 		if ((buf = kmem_zalloc(bufsize,
 		    KM_NOSLEEP | KM_NORMALPRI)) == NULL) {
 			rval = ENOMEM;
 			goto err;
 		}
 
 		spec[i].dtsp_buffer = buf;
 	}
 
 	if (opt[DTRACEOPT_GRABANON] != DTRACEOPT_UNSET) {
 		if (dtrace_anon.dta_state == NULL) {
 			rval = ENOENT;
 			goto out;
 		}
 
 		if (state->dts_necbs != 0) {
 			rval = EALREADY;
 			goto out;
 		}
 
 		state->dts_anon = dtrace_anon_grab();
 		ASSERT(state->dts_anon != NULL);
 		state = state->dts_anon;
 
 		/*
 		 * We want "grabanon" to be set in the grabbed state, so we'll
 		 * copy that option value from the grabbing state into the
 		 * grabbed state.
 		 */
 		state->dts_options[DTRACEOPT_GRABANON] =
 		    opt[DTRACEOPT_GRABANON];
 
 		*cpu = dtrace_anon.dta_beganon;
 
 		/*
 		 * If the anonymous state is active (as it almost certainly
 		 * is if the anonymous enabling ultimately matched anything),
 		 * we don't allow any further option processing -- but we
 		 * don't return failure.
 		 */
 		if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE)
 			goto out;
 	}
 
 	if (opt[DTRACEOPT_AGGSIZE] != DTRACEOPT_UNSET &&
 	    opt[DTRACEOPT_AGGSIZE] != 0) {
 		if (state->dts_aggregations == NULL) {
 			/*
 			 * We're not going to create an aggregation buffer
 			 * because we don't have any ECBs that contain
 			 * aggregations -- set this option to 0.
 			 */
 			opt[DTRACEOPT_AGGSIZE] = 0;
 		} else {
 			/*
 			 * If we have an aggregation buffer, we must also have
 			 * a buffer to use as scratch.
 			 */
 			if (opt[DTRACEOPT_BUFSIZE] == DTRACEOPT_UNSET ||
 			    opt[DTRACEOPT_BUFSIZE] < state->dts_needed) {
 				opt[DTRACEOPT_BUFSIZE] = state->dts_needed;
 			}
 		}
 	}
 
 	if (opt[DTRACEOPT_SPECSIZE] != DTRACEOPT_UNSET &&
 	    opt[DTRACEOPT_SPECSIZE] != 0) {
 		if (!state->dts_speculates) {
 			/*
 			 * We're not going to create speculation buffers
 			 * because we don't have any ECBs that actually
 			 * speculate -- set the speculation size to 0.
 			 */
 			opt[DTRACEOPT_SPECSIZE] = 0;
 		}
 	}
 
 	/*
 	 * The bare minimum size for any buffer that we're actually going to
 	 * do anything to is sizeof (uint64_t).
 	 */
 	sz = sizeof (uint64_t);
 
 	if ((state->dts_needed != 0 && opt[DTRACEOPT_BUFSIZE] < sz) ||
 	    (state->dts_speculates && opt[DTRACEOPT_SPECSIZE] < sz) ||
 	    (state->dts_aggregations != NULL && opt[DTRACEOPT_AGGSIZE] < sz)) {
 		/*
 		 * A buffer size has been explicitly set to 0 (or to a size
 		 * that will be adjusted to 0) and we need the space -- we
 		 * need to return failure.  We return ENOSPC to differentiate
 		 * it from failing to allocate a buffer due to failure to meet
 		 * the reserve (for which we return E2BIG).
 		 */
 		rval = ENOSPC;
 		goto out;
 	}
 
 	if ((rval = dtrace_state_buffers(state)) != 0)
 		goto err;
 
 	if ((sz = opt[DTRACEOPT_DYNVARSIZE]) == DTRACEOPT_UNSET)
 		sz = dtrace_dstate_defsize;
 
 	do {
 		rval = dtrace_dstate_init(&state->dts_vstate.dtvs_dynvars, sz);
 
 		if (rval == 0)
 			break;
 
 		if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL)
 			goto err;
 	} while (sz >>= 1);
 
 	opt[DTRACEOPT_DYNVARSIZE] = sz;
 
 	if (rval != 0)
 		goto err;
 
 	if (opt[DTRACEOPT_STATUSRATE] > dtrace_statusrate_max)
 		opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_max;
 
 	if (opt[DTRACEOPT_CLEANRATE] == 0)
 		opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max;
 
 	if (opt[DTRACEOPT_CLEANRATE] < dtrace_cleanrate_min)
 		opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_min;
 
 	if (opt[DTRACEOPT_CLEANRATE] > dtrace_cleanrate_max)
 		opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max;
 
 	state->dts_alive = state->dts_laststatus = dtrace_gethrtime();
 #ifdef illumos
 	hdlr.cyh_func = (cyc_func_t)dtrace_state_clean;
 	hdlr.cyh_arg = state;
 	hdlr.cyh_level = CY_LOW_LEVEL;
 
 	when.cyt_when = 0;
 	when.cyt_interval = opt[DTRACEOPT_CLEANRATE];
 
 	state->dts_cleaner = cyclic_add(&hdlr, &when);
 
 	hdlr.cyh_func = (cyc_func_t)dtrace_state_deadman;
 	hdlr.cyh_arg = state;
 	hdlr.cyh_level = CY_LOW_LEVEL;
 
 	when.cyt_when = 0;
 	when.cyt_interval = dtrace_deadman_interval;
 
 	state->dts_deadman = cyclic_add(&hdlr, &when);
 #else
 	callout_reset(&state->dts_cleaner, hz * opt[DTRACEOPT_CLEANRATE] / NANOSEC,
 	    dtrace_state_clean, state);
 	callout_reset(&state->dts_deadman, hz * dtrace_deadman_interval / NANOSEC,
 	    dtrace_state_deadman, state);
 #endif
 
 	state->dts_activity = DTRACE_ACTIVITY_WARMUP;
 
 #ifdef illumos
 	if (state->dts_getf != 0 &&
 	    !(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL)) {
 		/*
 		 * We don't have kernel privs but we have at least one call
 		 * to getf(); we need to bump our zone's count, and (if
 		 * this is the first enabling to have an unprivileged call
 		 * to getf()) we need to hook into closef().
 		 */
 		state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf++;
 
 		if (dtrace_getf++ == 0) {
 			ASSERT(dtrace_closef == NULL);
 			dtrace_closef = dtrace_getf_barrier;
 		}
 	}
 #endif
 
 	/*
 	 * Now it's time to actually fire the BEGIN probe.  We need to disable
 	 * interrupts here both to record the CPU on which we fired the BEGIN
 	 * probe (the data from this CPU will be processed first at user
 	 * level) and to manually activate the buffer for this CPU.
 	 */
 	cookie = dtrace_interrupt_disable();
 	*cpu = curcpu;
 	ASSERT(state->dts_buffer[*cpu].dtb_flags & DTRACEBUF_INACTIVE);
 	state->dts_buffer[*cpu].dtb_flags &= ~DTRACEBUF_INACTIVE;
 
 	dtrace_probe(dtrace_probeid_begin,
 	    (uint64_t)(uintptr_t)state, 0, 0, 0, 0);
 	dtrace_interrupt_enable(cookie);
 	/*
 	 * We may have had an exit action from a BEGIN probe; only change our
 	 * state to ACTIVE if we're still in WARMUP.
 	 */
 	ASSERT(state->dts_activity == DTRACE_ACTIVITY_WARMUP ||
 	    state->dts_activity == DTRACE_ACTIVITY_DRAINING);
 
 	if (state->dts_activity == DTRACE_ACTIVITY_WARMUP)
 		state->dts_activity = DTRACE_ACTIVITY_ACTIVE;
 
 #ifdef __FreeBSD__
 	/*
 	 * We enable anonymous tracing before APs are started, so we must
 	 * activate buffers using the current CPU.
 	 */
 	if (state == dtrace_anon.dta_state)
 		for (int i = 0; i < NCPU; i++)
 			dtrace_buffer_activate_cpu(state, i);
 	else
 		dtrace_xcall(DTRACE_CPUALL,
 		    (dtrace_xcall_t)dtrace_buffer_activate, state);
 #else
 	/*
 	 * Regardless of whether or not now we're in ACTIVE or DRAINING, we
 	 * want each CPU to transition its principal buffer out of the
 	 * INACTIVE state.  Doing this assures that no CPU will suddenly begin
 	 * processing an ECB halfway down a probe's ECB chain; all CPUs will
 	 * atomically transition from processing none of a state's ECBs to
 	 * processing all of them.
 	 */
 	dtrace_xcall(DTRACE_CPUALL,
 	    (dtrace_xcall_t)dtrace_buffer_activate, state);
 #endif
 	goto out;
 
 err:
 	dtrace_buffer_free(state->dts_buffer);
 	dtrace_buffer_free(state->dts_aggbuffer);
 
 	if ((nspec = state->dts_nspeculations) == 0) {
 		ASSERT(state->dts_speculations == NULL);
 		goto out;
 	}
 
 	spec = state->dts_speculations;
 	ASSERT(spec != NULL);
 
 	for (i = 0; i < state->dts_nspeculations; i++) {
 		if ((buf = spec[i].dtsp_buffer) == NULL)
 			break;
 
 		dtrace_buffer_free(buf);
 		kmem_free(buf, bufsize);
 	}
 
 	kmem_free(spec, nspec * sizeof (dtrace_speculation_t));
 	state->dts_nspeculations = 0;
 	state->dts_speculations = NULL;
 
 out:
 	mutex_exit(&dtrace_lock);
 	mutex_exit(&cpu_lock);
 
 	return (rval);
 }
 
 static int
 dtrace_state_stop(dtrace_state_t *state, processorid_t *cpu)
 {
 	dtrace_icookie_t cookie;
 
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 
 	if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE &&
 	    state->dts_activity != DTRACE_ACTIVITY_DRAINING)
 		return (EINVAL);
 
 	/*
 	 * We'll set the activity to DTRACE_ACTIVITY_DRAINING, and issue a sync
 	 * to be sure that every CPU has seen it.  See below for the details
 	 * on why this is done.
 	 */
 	state->dts_activity = DTRACE_ACTIVITY_DRAINING;
 	dtrace_sync();
 
 	/*
 	 * By this point, it is impossible for any CPU to be still processing
 	 * with DTRACE_ACTIVITY_ACTIVE.  We can thus set our activity to
 	 * DTRACE_ACTIVITY_COOLDOWN and know that we're not racing with any
 	 * other CPU in dtrace_buffer_reserve().  This allows dtrace_probe()
 	 * and callees to know that the activity is DTRACE_ACTIVITY_COOLDOWN
 	 * iff we're in the END probe.
 	 */
 	state->dts_activity = DTRACE_ACTIVITY_COOLDOWN;
 	dtrace_sync();
 	ASSERT(state->dts_activity == DTRACE_ACTIVITY_COOLDOWN);
 
 	/*
 	 * Finally, we can release the reserve and call the END probe.  We
 	 * disable interrupts across calling the END probe to allow us to
 	 * return the CPU on which we actually called the END probe.  This
 	 * allows user-land to be sure that this CPU's principal buffer is
 	 * processed last.
 	 */
 	state->dts_reserve = 0;
 
 	cookie = dtrace_interrupt_disable();
 	*cpu = curcpu;
 	dtrace_probe(dtrace_probeid_end,
 	    (uint64_t)(uintptr_t)state, 0, 0, 0, 0);
 	dtrace_interrupt_enable(cookie);
 
 	state->dts_activity = DTRACE_ACTIVITY_STOPPED;
 	dtrace_sync();
 
 #ifdef illumos
 	if (state->dts_getf != 0 &&
 	    !(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL)) {
 		/*
 		 * We don't have kernel privs but we have at least one call
 		 * to getf(); we need to lower our zone's count, and (if
 		 * this is the last enabling to have an unprivileged call
 		 * to getf()) we need to clear the closef() hook.
 		 */
 		ASSERT(state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf > 0);
 		ASSERT(dtrace_closef == dtrace_getf_barrier);
 		ASSERT(dtrace_getf > 0);
 
 		state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf--;
 
 		if (--dtrace_getf == 0)
 			dtrace_closef = NULL;
 	}
 #endif
 
 	return (0);
 }
 
 static int
 dtrace_state_option(dtrace_state_t *state, dtrace_optid_t option,
     dtrace_optval_t val)
 {
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 
 	if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE)
 		return (EBUSY);
 
 	if (option >= DTRACEOPT_MAX)
 		return (EINVAL);
 
 	if (option != DTRACEOPT_CPU && val < 0)
 		return (EINVAL);
 
 	switch (option) {
 	case DTRACEOPT_DESTRUCTIVE:
 		if (dtrace_destructive_disallow)
 			return (EACCES);
 
 		state->dts_cred.dcr_destructive = 1;
 		break;
 
 	case DTRACEOPT_BUFSIZE:
 	case DTRACEOPT_DYNVARSIZE:
 	case DTRACEOPT_AGGSIZE:
 	case DTRACEOPT_SPECSIZE:
 	case DTRACEOPT_STRSIZE:
 		if (val < 0)
 			return (EINVAL);
 
 		if (val >= LONG_MAX) {
 			/*
 			 * If this is an otherwise negative value, set it to
 			 * the highest multiple of 128m less than LONG_MAX.
 			 * Technically, we're adjusting the size without
 			 * regard to the buffer resizing policy, but in fact,
 			 * this has no effect -- if we set the buffer size to
 			 * ~LONG_MAX and the buffer policy is ultimately set to
 			 * be "manual", the buffer allocation is guaranteed to
 			 * fail, if only because the allocation requires two
 			 * buffers.  (We set the the size to the highest
 			 * multiple of 128m because it ensures that the size
 			 * will remain a multiple of a megabyte when
 			 * repeatedly halved -- all the way down to 15m.)
 			 */
 			val = LONG_MAX - (1 << 27) + 1;
 		}
 	}
 
 	state->dts_options[option] = val;
 
 	return (0);
 }
 
 static void
 dtrace_state_destroy(dtrace_state_t *state)
 {
 	dtrace_ecb_t *ecb;
 	dtrace_vstate_t *vstate = &state->dts_vstate;
 #ifdef illumos
 	minor_t minor = getminor(state->dts_dev);
 #endif
 	int i, bufsize = NCPU * sizeof (dtrace_buffer_t);
 	dtrace_speculation_t *spec = state->dts_speculations;
 	int nspec = state->dts_nspeculations;
 	uint32_t match;
 
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 	ASSERT(MUTEX_HELD(&cpu_lock));
 
 	/*
 	 * First, retract any retained enablings for this state.
 	 */
 	dtrace_enabling_retract(state);
 	ASSERT(state->dts_nretained == 0);
 
 	if (state->dts_activity == DTRACE_ACTIVITY_ACTIVE ||
 	    state->dts_activity == DTRACE_ACTIVITY_DRAINING) {
 		/*
 		 * We have managed to come into dtrace_state_destroy() on a
 		 * hot enabling -- almost certainly because of a disorderly
 		 * shutdown of a consumer.  (That is, a consumer that is
 		 * exiting without having called dtrace_stop().) In this case,
 		 * we're going to set our activity to be KILLED, and then
 		 * issue a sync to be sure that everyone is out of probe
 		 * context before we start blowing away ECBs.
 		 */
 		state->dts_activity = DTRACE_ACTIVITY_KILLED;
 		dtrace_sync();
 	}
 
 	/*
 	 * Release the credential hold we took in dtrace_state_create().
 	 */
 	if (state->dts_cred.dcr_cred != NULL)
 		crfree(state->dts_cred.dcr_cred);
 
 	/*
 	 * Now we can safely disable and destroy any enabled probes.  Because
 	 * any DTRACE_PRIV_KERNEL probes may actually be slowing our progress
 	 * (especially if they're all enabled), we take two passes through the
 	 * ECBs:  in the first, we disable just DTRACE_PRIV_KERNEL probes, and
 	 * in the second we disable whatever is left over.
 	 */
 	for (match = DTRACE_PRIV_KERNEL; ; match = 0) {
 		for (i = 0; i < state->dts_necbs; i++) {
 			if ((ecb = state->dts_ecbs[i]) == NULL)
 				continue;
 
 			if (match && ecb->dte_probe != NULL) {
 				dtrace_probe_t *probe = ecb->dte_probe;
 				dtrace_provider_t *prov = probe->dtpr_provider;
 
 				if (!(prov->dtpv_priv.dtpp_flags & match))
 					continue;
 			}
 
 			dtrace_ecb_disable(ecb);
 			dtrace_ecb_destroy(ecb);
 		}
 
 		if (!match)
 			break;
 	}
 
 	/*
 	 * Before we free the buffers, perform one more sync to assure that
 	 * every CPU is out of probe context.
 	 */
 	dtrace_sync();
 
 	dtrace_buffer_free(state->dts_buffer);
 	dtrace_buffer_free(state->dts_aggbuffer);
 
 	for (i = 0; i < nspec; i++)
 		dtrace_buffer_free(spec[i].dtsp_buffer);
 
 #ifdef illumos
 	if (state->dts_cleaner != CYCLIC_NONE)
 		cyclic_remove(state->dts_cleaner);
 
 	if (state->dts_deadman != CYCLIC_NONE)
 		cyclic_remove(state->dts_deadman);
 #else
 	callout_stop(&state->dts_cleaner);
 	callout_drain(&state->dts_cleaner);
 	callout_stop(&state->dts_deadman);
 	callout_drain(&state->dts_deadman);
 #endif
 
 	dtrace_dstate_fini(&vstate->dtvs_dynvars);
 	dtrace_vstate_fini(vstate);
 	if (state->dts_ecbs != NULL)
 		kmem_free(state->dts_ecbs, state->dts_necbs * sizeof (dtrace_ecb_t *));
 
 	if (state->dts_aggregations != NULL) {
 #ifdef DEBUG
 		for (i = 0; i < state->dts_naggregations; i++)
 			ASSERT(state->dts_aggregations[i] == NULL);
 #endif
 		ASSERT(state->dts_naggregations > 0);
 		kmem_free(state->dts_aggregations,
 		    state->dts_naggregations * sizeof (dtrace_aggregation_t *));
 	}
 
 	kmem_free(state->dts_buffer, bufsize);
 	kmem_free(state->dts_aggbuffer, bufsize);
 
 	for (i = 0; i < nspec; i++)
 		kmem_free(spec[i].dtsp_buffer, bufsize);
 
 	if (spec != NULL)
 		kmem_free(spec, nspec * sizeof (dtrace_speculation_t));
 
 	dtrace_format_destroy(state);
 
 	if (state->dts_aggid_arena != NULL) {
 #ifdef illumos
 		vmem_destroy(state->dts_aggid_arena);
 #else
 		delete_unrhdr(state->dts_aggid_arena);
 #endif
 		state->dts_aggid_arena = NULL;
 	}
 #ifdef illumos
 	ddi_soft_state_free(dtrace_softstate, minor);
 	vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1);
 #endif
 }
 
 /*
  * DTrace Anonymous Enabling Functions
  */
 static dtrace_state_t *
 dtrace_anon_grab(void)
 {
 	dtrace_state_t *state;
 
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 
 	if ((state = dtrace_anon.dta_state) == NULL) {
 		ASSERT(dtrace_anon.dta_enabling == NULL);
 		return (NULL);
 	}
 
 	ASSERT(dtrace_anon.dta_enabling != NULL);
 	ASSERT(dtrace_retained != NULL);
 
 	dtrace_enabling_destroy(dtrace_anon.dta_enabling);
 	dtrace_anon.dta_enabling = NULL;
 	dtrace_anon.dta_state = NULL;
 
 	return (state);
 }
 
 static void
 dtrace_anon_property(void)
 {
 	int i, rv;
 	dtrace_state_t *state;
 	dof_hdr_t *dof;
 	char c[32];		/* enough for "dof-data-" + digits */
 
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 	ASSERT(MUTEX_HELD(&cpu_lock));
 
 	for (i = 0; ; i++) {
 		(void) snprintf(c, sizeof (c), "dof-data-%d", i);
 
 		dtrace_err_verbose = 1;
 
 		if ((dof = dtrace_dof_property(c)) == NULL) {
 			dtrace_err_verbose = 0;
 			break;
 		}
 
 #ifdef illumos
 		/*
 		 * We want to create anonymous state, so we need to transition
 		 * the kernel debugger to indicate that DTrace is active.  If
 		 * this fails (e.g. because the debugger has modified text in
 		 * some way), we won't continue with the processing.
 		 */
 		if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) {
 			cmn_err(CE_NOTE, "kernel debugger active; anonymous "
 			    "enabling ignored.");
 			dtrace_dof_destroy(dof);
 			break;
 		}
 #endif
 
 		/*
 		 * If we haven't allocated an anonymous state, we'll do so now.
 		 */
 		if ((state = dtrace_anon.dta_state) == NULL) {
 			state = dtrace_state_create(NULL, NULL);
 			dtrace_anon.dta_state = state;
 
 			if (state == NULL) {
 				/*
 				 * This basically shouldn't happen:  the only
 				 * failure mode from dtrace_state_create() is a
 				 * failure of ddi_soft_state_zalloc() that
 				 * itself should never happen.  Still, the
 				 * interface allows for a failure mode, and
 				 * we want to fail as gracefully as possible:
 				 * we'll emit an error message and cease
 				 * processing anonymous state in this case.
 				 */
 				cmn_err(CE_WARN, "failed to create "
 				    "anonymous state");
 				dtrace_dof_destroy(dof);
 				break;
 			}
 		}
 
 		rv = dtrace_dof_slurp(dof, &state->dts_vstate, CRED(),
 		    &dtrace_anon.dta_enabling, 0, 0, B_TRUE);
 
 		if (rv == 0)
 			rv = dtrace_dof_options(dof, state);
 
 		dtrace_err_verbose = 0;
 		dtrace_dof_destroy(dof);
 
 		if (rv != 0) {
 			/*
 			 * This is malformed DOF; chuck any anonymous state
 			 * that we created.
 			 */
 			ASSERT(dtrace_anon.dta_enabling == NULL);
 			dtrace_state_destroy(state);
 			dtrace_anon.dta_state = NULL;
 			break;
 		}
 
 		ASSERT(dtrace_anon.dta_enabling != NULL);
 	}
 
 	if (dtrace_anon.dta_enabling != NULL) {
 		int rval;
 
 		/*
 		 * dtrace_enabling_retain() can only fail because we are
 		 * trying to retain more enablings than are allowed -- but
 		 * we only have one anonymous enabling, and we are guaranteed
 		 * to be allowed at least one retained enabling; we assert
 		 * that dtrace_enabling_retain() returns success.
 		 */
 		rval = dtrace_enabling_retain(dtrace_anon.dta_enabling);
 		ASSERT(rval == 0);
 
 		dtrace_enabling_dump(dtrace_anon.dta_enabling);
 	}
 }
 
 /*
  * DTrace Helper Functions
  */
 static void
 dtrace_helper_trace(dtrace_helper_action_t *helper,
     dtrace_mstate_t *mstate, dtrace_vstate_t *vstate, int where)
 {
 	uint32_t size, next, nnext, i;
 	dtrace_helptrace_t *ent, *buffer;
 	uint16_t flags = cpu_core[curcpu].cpuc_dtrace_flags;
 
 	if ((buffer = dtrace_helptrace_buffer) == NULL)
 		return;
 
 	ASSERT(vstate->dtvs_nlocals <= dtrace_helptrace_nlocals);
 
 	/*
 	 * What would a tracing framework be without its own tracing
 	 * framework?  (Well, a hell of a lot simpler, for starters...)
 	 */
 	size = sizeof (dtrace_helptrace_t) + dtrace_helptrace_nlocals *
 	    sizeof (uint64_t) - sizeof (uint64_t);
 
 	/*
 	 * Iterate until we can allocate a slot in the trace buffer.
 	 */
 	do {
 		next = dtrace_helptrace_next;
 
 		if (next + size < dtrace_helptrace_bufsize) {
 			nnext = next + size;
 		} else {
 			nnext = size;
 		}
 	} while (dtrace_cas32(&dtrace_helptrace_next, next, nnext) != next);
 
 	/*
 	 * We have our slot; fill it in.
 	 */
 	if (nnext == size) {
 		dtrace_helptrace_wrapped++;
 		next = 0;
 	}
 
 	ent = (dtrace_helptrace_t *)((uintptr_t)buffer + next);
 	ent->dtht_helper = helper;
 	ent->dtht_where = where;
 	ent->dtht_nlocals = vstate->dtvs_nlocals;
 
 	ent->dtht_fltoffs = (mstate->dtms_present & DTRACE_MSTATE_FLTOFFS) ?
 	    mstate->dtms_fltoffs : -1;
 	ent->dtht_fault = DTRACE_FLAGS2FLT(flags);
 	ent->dtht_illval = cpu_core[curcpu].cpuc_dtrace_illval;
 
 	for (i = 0; i < vstate->dtvs_nlocals; i++) {
 		dtrace_statvar_t *svar;
 
 		if ((svar = vstate->dtvs_locals[i]) == NULL)
 			continue;
 
 		ASSERT(svar->dtsv_size >= NCPU * sizeof (uint64_t));
 		ent->dtht_locals[i] =
 		    ((uint64_t *)(uintptr_t)svar->dtsv_data)[curcpu];
 	}
 }
 
 static uint64_t
 dtrace_helper(int which, dtrace_mstate_t *mstate,
     dtrace_state_t *state, uint64_t arg0, uint64_t arg1)
 {
 	uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags;
 	uint64_t sarg0 = mstate->dtms_arg[0];
 	uint64_t sarg1 = mstate->dtms_arg[1];
 	uint64_t rval = 0;
 	dtrace_helpers_t *helpers = curproc->p_dtrace_helpers;
 	dtrace_helper_action_t *helper;
 	dtrace_vstate_t *vstate;
 	dtrace_difo_t *pred;
 	int i, trace = dtrace_helptrace_buffer != NULL;
 
 	ASSERT(which >= 0 && which < DTRACE_NHELPER_ACTIONS);
 
 	if (helpers == NULL)
 		return (0);
 
 	if ((helper = helpers->dthps_actions[which]) == NULL)
 		return (0);
 
 	vstate = &helpers->dthps_vstate;
 	mstate->dtms_arg[0] = arg0;
 	mstate->dtms_arg[1] = arg1;
 
 	/*
 	 * Now iterate over each helper.  If its predicate evaluates to 'true',
 	 * we'll call the corresponding actions.  Note that the below calls
 	 * to dtrace_dif_emulate() may set faults in machine state.  This is
 	 * okay:  our caller (the outer dtrace_dif_emulate()) will simply plow
 	 * the stored DIF offset with its own (which is the desired behavior).
 	 * Also, note the calls to dtrace_dif_emulate() may allocate scratch
 	 * from machine state; this is okay, too.
 	 */
 	for (; helper != NULL; helper = helper->dtha_next) {
 		if ((pred = helper->dtha_predicate) != NULL) {
 			if (trace)
 				dtrace_helper_trace(helper, mstate, vstate, 0);
 
 			if (!dtrace_dif_emulate(pred, mstate, vstate, state))
 				goto next;
 
 			if (*flags & CPU_DTRACE_FAULT)
 				goto err;
 		}
 
 		for (i = 0; i < helper->dtha_nactions; i++) {
 			if (trace)
 				dtrace_helper_trace(helper,
 				    mstate, vstate, i + 1);
 
 			rval = dtrace_dif_emulate(helper->dtha_actions[i],
 			    mstate, vstate, state);
 
 			if (*flags & CPU_DTRACE_FAULT)
 				goto err;
 		}
 
 next:
 		if (trace)
 			dtrace_helper_trace(helper, mstate, vstate,
 			    DTRACE_HELPTRACE_NEXT);
 	}
 
 	if (trace)
 		dtrace_helper_trace(helper, mstate, vstate,
 		    DTRACE_HELPTRACE_DONE);
 
 	/*
 	 * Restore the arg0 that we saved upon entry.
 	 */
 	mstate->dtms_arg[0] = sarg0;
 	mstate->dtms_arg[1] = sarg1;
 
 	return (rval);
 
 err:
 	if (trace)
 		dtrace_helper_trace(helper, mstate, vstate,
 		    DTRACE_HELPTRACE_ERR);
 
 	/*
 	 * Restore the arg0 that we saved upon entry.
 	 */
 	mstate->dtms_arg[0] = sarg0;
 	mstate->dtms_arg[1] = sarg1;
 
 	return (0);
 }
 
 static void
 dtrace_helper_action_destroy(dtrace_helper_action_t *helper,
     dtrace_vstate_t *vstate)
 {
 	int i;
 
 	if (helper->dtha_predicate != NULL)
 		dtrace_difo_release(helper->dtha_predicate, vstate);
 
 	for (i = 0; i < helper->dtha_nactions; i++) {
 		ASSERT(helper->dtha_actions[i] != NULL);
 		dtrace_difo_release(helper->dtha_actions[i], vstate);
 	}
 
 	kmem_free(helper->dtha_actions,
 	    helper->dtha_nactions * sizeof (dtrace_difo_t *));
 	kmem_free(helper, sizeof (dtrace_helper_action_t));
 }
 
 static int
 dtrace_helper_destroygen(dtrace_helpers_t *help, int gen)
 {
 	proc_t *p = curproc;
 	dtrace_vstate_t *vstate;
 	int i;
 
 	if (help == NULL)
 		help = p->p_dtrace_helpers;
 
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 
 	if (help == NULL || gen > help->dthps_generation)
 		return (EINVAL);
 
 	vstate = &help->dthps_vstate;
 
 	for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) {
 		dtrace_helper_action_t *last = NULL, *h, *next;
 
 		for (h = help->dthps_actions[i]; h != NULL; h = next) {
 			next = h->dtha_next;
 
 			if (h->dtha_generation == gen) {
 				if (last != NULL) {
 					last->dtha_next = next;
 				} else {
 					help->dthps_actions[i] = next;
 				}
 
 				dtrace_helper_action_destroy(h, vstate);
 			} else {
 				last = h;
 			}
 		}
 	}
 
 	/*
 	 * Interate until we've cleared out all helper providers with the
 	 * given generation number.
 	 */
 	for (;;) {
 		dtrace_helper_provider_t *prov;
 
 		/*
 		 * Look for a helper provider with the right generation. We
 		 * have to start back at the beginning of the list each time
 		 * because we drop dtrace_lock. It's unlikely that we'll make
 		 * more than two passes.
 		 */
 		for (i = 0; i < help->dthps_nprovs; i++) {
 			prov = help->dthps_provs[i];
 
 			if (prov->dthp_generation == gen)
 				break;
 		}
 
 		/*
 		 * If there were no matches, we're done.
 		 */
 		if (i == help->dthps_nprovs)
 			break;
 
 		/*
 		 * Move the last helper provider into this slot.
 		 */
 		help->dthps_nprovs--;
 		help->dthps_provs[i] = help->dthps_provs[help->dthps_nprovs];
 		help->dthps_provs[help->dthps_nprovs] = NULL;
 
 		mutex_exit(&dtrace_lock);
 
 		/*
 		 * If we have a meta provider, remove this helper provider.
 		 */
 		mutex_enter(&dtrace_meta_lock);
 		if (dtrace_meta_pid != NULL) {
 			ASSERT(dtrace_deferred_pid == NULL);
 			dtrace_helper_provider_remove(&prov->dthp_prov,
 			    p->p_pid);
 		}
 		mutex_exit(&dtrace_meta_lock);
 
 		dtrace_helper_provider_destroy(prov);
 
 		mutex_enter(&dtrace_lock);
 	}
 
 	return (0);
 }
 
 static int
 dtrace_helper_validate(dtrace_helper_action_t *helper)
 {
 	int err = 0, i;
 	dtrace_difo_t *dp;
 
 	if ((dp = helper->dtha_predicate) != NULL)
 		err += dtrace_difo_validate_helper(dp);
 
 	for (i = 0; i < helper->dtha_nactions; i++)
 		err += dtrace_difo_validate_helper(helper->dtha_actions[i]);
 
 	return (err == 0);
 }
 
 static int
 dtrace_helper_action_add(int which, dtrace_ecbdesc_t *ep,
     dtrace_helpers_t *help)
 {
 	dtrace_helper_action_t *helper, *last;
 	dtrace_actdesc_t *act;
 	dtrace_vstate_t *vstate;
 	dtrace_predicate_t *pred;
 	int count = 0, nactions = 0, i;
 
 	if (which < 0 || which >= DTRACE_NHELPER_ACTIONS)
 		return (EINVAL);
 
 	last = help->dthps_actions[which];
 	vstate = &help->dthps_vstate;
 
 	for (count = 0; last != NULL; last = last->dtha_next) {
 		count++;
 		if (last->dtha_next == NULL)
 			break;
 	}
 
 	/*
 	 * If we already have dtrace_helper_actions_max helper actions for this
 	 * helper action type, we'll refuse to add a new one.
 	 */
 	if (count >= dtrace_helper_actions_max)
 		return (ENOSPC);
 
 	helper = kmem_zalloc(sizeof (dtrace_helper_action_t), KM_SLEEP);
 	helper->dtha_generation = help->dthps_generation;
 
 	if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) {
 		ASSERT(pred->dtp_difo != NULL);
 		dtrace_difo_hold(pred->dtp_difo);
 		helper->dtha_predicate = pred->dtp_difo;
 	}
 
 	for (act = ep->dted_action; act != NULL; act = act->dtad_next) {
 		if (act->dtad_kind != DTRACEACT_DIFEXPR)
 			goto err;
 
 		if (act->dtad_difo == NULL)
 			goto err;
 
 		nactions++;
 	}
 
 	helper->dtha_actions = kmem_zalloc(sizeof (dtrace_difo_t *) *
 	    (helper->dtha_nactions = nactions), KM_SLEEP);
 
 	for (act = ep->dted_action, i = 0; act != NULL; act = act->dtad_next) {
 		dtrace_difo_hold(act->dtad_difo);
 		helper->dtha_actions[i++] = act->dtad_difo;
 	}
 
 	if (!dtrace_helper_validate(helper))
 		goto err;
 
 	if (last == NULL) {
 		help->dthps_actions[which] = helper;
 	} else {
 		last->dtha_next = helper;
 	}
 
 	if (vstate->dtvs_nlocals > dtrace_helptrace_nlocals) {
 		dtrace_helptrace_nlocals = vstate->dtvs_nlocals;
 		dtrace_helptrace_next = 0;
 	}
 
 	return (0);
 err:
 	dtrace_helper_action_destroy(helper, vstate);
 	return (EINVAL);
 }
 
 static void
 dtrace_helper_provider_register(proc_t *p, dtrace_helpers_t *help,
     dof_helper_t *dofhp)
 {
 	ASSERT(MUTEX_NOT_HELD(&dtrace_lock));
 
 	mutex_enter(&dtrace_meta_lock);
 	mutex_enter(&dtrace_lock);
 
 	if (!dtrace_attached() || dtrace_meta_pid == NULL) {
 		/*
 		 * If the dtrace module is loaded but not attached, or if
 		 * there aren't isn't a meta provider registered to deal with
 		 * these provider descriptions, we need to postpone creating
 		 * the actual providers until later.
 		 */
 
 		if (help->dthps_next == NULL && help->dthps_prev == NULL &&
 		    dtrace_deferred_pid != help) {
 			help->dthps_deferred = 1;
 			help->dthps_pid = p->p_pid;
 			help->dthps_next = dtrace_deferred_pid;
 			help->dthps_prev = NULL;
 			if (dtrace_deferred_pid != NULL)
 				dtrace_deferred_pid->dthps_prev = help;
 			dtrace_deferred_pid = help;
 		}
 
 		mutex_exit(&dtrace_lock);
 
 	} else if (dofhp != NULL) {
 		/*
 		 * If the dtrace module is loaded and we have a particular
 		 * helper provider description, pass that off to the
 		 * meta provider.
 		 */
 
 		mutex_exit(&dtrace_lock);
 
 		dtrace_helper_provide(dofhp, p->p_pid);
 
 	} else {
 		/*
 		 * Otherwise, just pass all the helper provider descriptions
 		 * off to the meta provider.
 		 */
 
 		int i;
 		mutex_exit(&dtrace_lock);
 
 		for (i = 0; i < help->dthps_nprovs; i++) {
 			dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov,
 			    p->p_pid);
 		}
 	}
 
 	mutex_exit(&dtrace_meta_lock);
 }
 
 static int
 dtrace_helper_provider_add(dof_helper_t *dofhp, dtrace_helpers_t *help, int gen)
 {
 	dtrace_helper_provider_t *hprov, **tmp_provs;
 	uint_t tmp_maxprovs, i;
 
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 	ASSERT(help != NULL);
 
 	/*
 	 * If we already have dtrace_helper_providers_max helper providers,
 	 * we're refuse to add a new one.
 	 */
 	if (help->dthps_nprovs >= dtrace_helper_providers_max)
 		return (ENOSPC);
 
 	/*
 	 * Check to make sure this isn't a duplicate.
 	 */
 	for (i = 0; i < help->dthps_nprovs; i++) {
 		if (dofhp->dofhp_addr ==
 		    help->dthps_provs[i]->dthp_prov.dofhp_addr)
 			return (EALREADY);
 	}
 
 	hprov = kmem_zalloc(sizeof (dtrace_helper_provider_t), KM_SLEEP);
 	hprov->dthp_prov = *dofhp;
 	hprov->dthp_ref = 1;
 	hprov->dthp_generation = gen;
 
 	/*
 	 * Allocate a bigger table for helper providers if it's already full.
 	 */
 	if (help->dthps_maxprovs == help->dthps_nprovs) {
 		tmp_maxprovs = help->dthps_maxprovs;
 		tmp_provs = help->dthps_provs;
 
 		if (help->dthps_maxprovs == 0)
 			help->dthps_maxprovs = 2;
 		else
 			help->dthps_maxprovs *= 2;
 		if (help->dthps_maxprovs > dtrace_helper_providers_max)
 			help->dthps_maxprovs = dtrace_helper_providers_max;
 
 		ASSERT(tmp_maxprovs < help->dthps_maxprovs);
 
 		help->dthps_provs = kmem_zalloc(help->dthps_maxprovs *
 		    sizeof (dtrace_helper_provider_t *), KM_SLEEP);
 
 		if (tmp_provs != NULL) {
 			bcopy(tmp_provs, help->dthps_provs, tmp_maxprovs *
 			    sizeof (dtrace_helper_provider_t *));
 			kmem_free(tmp_provs, tmp_maxprovs *
 			    sizeof (dtrace_helper_provider_t *));
 		}
 	}
 
 	help->dthps_provs[help->dthps_nprovs] = hprov;
 	help->dthps_nprovs++;
 
 	return (0);
 }
 
 static void
 dtrace_helper_provider_destroy(dtrace_helper_provider_t *hprov)
 {
 	mutex_enter(&dtrace_lock);
 
 	if (--hprov->dthp_ref == 0) {
 		dof_hdr_t *dof;
 		mutex_exit(&dtrace_lock);
 		dof = (dof_hdr_t *)(uintptr_t)hprov->dthp_prov.dofhp_dof;
 		dtrace_dof_destroy(dof);
 		kmem_free(hprov, sizeof (dtrace_helper_provider_t));
 	} else {
 		mutex_exit(&dtrace_lock);
 	}
 }
 
 static int
 dtrace_helper_provider_validate(dof_hdr_t *dof, dof_sec_t *sec)
 {
 	uintptr_t daddr = (uintptr_t)dof;
 	dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec;
 	dof_provider_t *provider;
 	dof_probe_t *probe;
 	uint8_t *arg;
 	char *strtab, *typestr;
 	dof_stridx_t typeidx;
 	size_t typesz;
 	uint_t nprobes, j, k;
 
 	ASSERT(sec->dofs_type == DOF_SECT_PROVIDER);
 
 	if (sec->dofs_offset & (sizeof (uint_t) - 1)) {
 		dtrace_dof_error(dof, "misaligned section offset");
 		return (-1);
 	}
 
 	/*
 	 * The section needs to be large enough to contain the DOF provider
 	 * structure appropriate for the given version.
 	 */
 	if (sec->dofs_size <
 	    ((dof->dofh_ident[DOF_ID_VERSION] == DOF_VERSION_1) ?
 	    offsetof(dof_provider_t, dofpv_prenoffs) :
 	    sizeof (dof_provider_t))) {
 		dtrace_dof_error(dof, "provider section too small");
 		return (-1);
 	}
 
 	provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset);
 	str_sec = dtrace_dof_sect(dof, DOF_SECT_STRTAB, provider->dofpv_strtab);
 	prb_sec = dtrace_dof_sect(dof, DOF_SECT_PROBES, provider->dofpv_probes);
 	arg_sec = dtrace_dof_sect(dof, DOF_SECT_PRARGS, provider->dofpv_prargs);
 	off_sec = dtrace_dof_sect(dof, DOF_SECT_PROFFS, provider->dofpv_proffs);
 
 	if (str_sec == NULL || prb_sec == NULL ||
 	    arg_sec == NULL || off_sec == NULL)
 		return (-1);
 
 	enoff_sec = NULL;
 
 	if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 &&
 	    provider->dofpv_prenoffs != DOF_SECT_NONE &&
 	    (enoff_sec = dtrace_dof_sect(dof, DOF_SECT_PRENOFFS,
 	    provider->dofpv_prenoffs)) == NULL)
 		return (-1);
 
 	strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset);
 
 	if (provider->dofpv_name >= str_sec->dofs_size ||
 	    strlen(strtab + provider->dofpv_name) >= DTRACE_PROVNAMELEN) {
 		dtrace_dof_error(dof, "invalid provider name");
 		return (-1);
 	}
 
 	if (prb_sec->dofs_entsize == 0 ||
 	    prb_sec->dofs_entsize > prb_sec->dofs_size) {
 		dtrace_dof_error(dof, "invalid entry size");
 		return (-1);
 	}
 
 	if (prb_sec->dofs_entsize & (sizeof (uintptr_t) - 1)) {
 		dtrace_dof_error(dof, "misaligned entry size");
 		return (-1);
 	}
 
 	if (off_sec->dofs_entsize != sizeof (uint32_t)) {
 		dtrace_dof_error(dof, "invalid entry size");
 		return (-1);
 	}
 
 	if (off_sec->dofs_offset & (sizeof (uint32_t) - 1)) {
 		dtrace_dof_error(dof, "misaligned section offset");
 		return (-1);
 	}
 
 	if (arg_sec->dofs_entsize != sizeof (uint8_t)) {
 		dtrace_dof_error(dof, "invalid entry size");
 		return (-1);
 	}
 
 	arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset);
 
 	nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize;
 
 	/*
 	 * Take a pass through the probes to check for errors.
 	 */
 	for (j = 0; j < nprobes; j++) {
 		probe = (dof_probe_t *)(uintptr_t)(daddr +
 		    prb_sec->dofs_offset + j * prb_sec->dofs_entsize);
 
 		if (probe->dofpr_func >= str_sec->dofs_size) {
 			dtrace_dof_error(dof, "invalid function name");
 			return (-1);
 		}
 
 		if (strlen(strtab + probe->dofpr_func) >= DTRACE_FUNCNAMELEN) {
 			dtrace_dof_error(dof, "function name too long");
 			/*
 			 * Keep going if the function name is too long.
 			 * Unlike provider and probe names, we cannot reasonably
 			 * impose restrictions on function names, since they're
 			 * a property of the code being instrumented. We will
 			 * skip this probe in dtrace_helper_provide_one().
 			 */
 		}
 
 		if (probe->dofpr_name >= str_sec->dofs_size ||
 		    strlen(strtab + probe->dofpr_name) >= DTRACE_NAMELEN) {
 			dtrace_dof_error(dof, "invalid probe name");
 			return (-1);
 		}
 
 		/*
 		 * The offset count must not wrap the index, and the offsets
 		 * must also not overflow the section's data.
 		 */
 		if (probe->dofpr_offidx + probe->dofpr_noffs <
 		    probe->dofpr_offidx ||
 		    (probe->dofpr_offidx + probe->dofpr_noffs) *
 		    off_sec->dofs_entsize > off_sec->dofs_size) {
 			dtrace_dof_error(dof, "invalid probe offset");
 			return (-1);
 		}
 
 		if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1) {
 			/*
 			 * If there's no is-enabled offset section, make sure
 			 * there aren't any is-enabled offsets. Otherwise
 			 * perform the same checks as for probe offsets
 			 * (immediately above).
 			 */
 			if (enoff_sec == NULL) {
 				if (probe->dofpr_enoffidx != 0 ||
 				    probe->dofpr_nenoffs != 0) {
 					dtrace_dof_error(dof, "is-enabled "
 					    "offsets with null section");
 					return (-1);
 				}
 			} else if (probe->dofpr_enoffidx +
 			    probe->dofpr_nenoffs < probe->dofpr_enoffidx ||
 			    (probe->dofpr_enoffidx + probe->dofpr_nenoffs) *
 			    enoff_sec->dofs_entsize > enoff_sec->dofs_size) {
 				dtrace_dof_error(dof, "invalid is-enabled "
 				    "offset");
 				return (-1);
 			}
 
 			if (probe->dofpr_noffs + probe->dofpr_nenoffs == 0) {
 				dtrace_dof_error(dof, "zero probe and "
 				    "is-enabled offsets");
 				return (-1);
 			}
 		} else if (probe->dofpr_noffs == 0) {
 			dtrace_dof_error(dof, "zero probe offsets");
 			return (-1);
 		}
 
 		if (probe->dofpr_argidx + probe->dofpr_xargc <
 		    probe->dofpr_argidx ||
 		    (probe->dofpr_argidx + probe->dofpr_xargc) *
 		    arg_sec->dofs_entsize > arg_sec->dofs_size) {
 			dtrace_dof_error(dof, "invalid args");
 			return (-1);
 		}
 
 		typeidx = probe->dofpr_nargv;
 		typestr = strtab + probe->dofpr_nargv;
 		for (k = 0; k < probe->dofpr_nargc; k++) {
 			if (typeidx >= str_sec->dofs_size) {
 				dtrace_dof_error(dof, "bad "
 				    "native argument type");
 				return (-1);
 			}
 
 			typesz = strlen(typestr) + 1;
 			if (typesz > DTRACE_ARGTYPELEN) {
 				dtrace_dof_error(dof, "native "
 				    "argument type too long");
 				return (-1);
 			}
 			typeidx += typesz;
 			typestr += typesz;
 		}
 
 		typeidx = probe->dofpr_xargv;
 		typestr = strtab + probe->dofpr_xargv;
 		for (k = 0; k < probe->dofpr_xargc; k++) {
 			if (arg[probe->dofpr_argidx + k] > probe->dofpr_nargc) {
 				dtrace_dof_error(dof, "bad "
 				    "native argument index");
 				return (-1);
 			}
 
 			if (typeidx >= str_sec->dofs_size) {
 				dtrace_dof_error(dof, "bad "
 				    "translated argument type");
 				return (-1);
 			}
 
 			typesz = strlen(typestr) + 1;
 			if (typesz > DTRACE_ARGTYPELEN) {
 				dtrace_dof_error(dof, "translated argument "
 				    "type too long");
 				return (-1);
 			}
 
 			typeidx += typesz;
 			typestr += typesz;
 		}
 	}
 
 	return (0);
 }
 
 static int
 dtrace_helper_slurp(dof_hdr_t *dof, dof_helper_t *dhp, struct proc *p)
 {
 	dtrace_helpers_t *help;
 	dtrace_vstate_t *vstate;
 	dtrace_enabling_t *enab = NULL;
 	int i, gen, rv, nhelpers = 0, nprovs = 0, destroy = 1;
 	uintptr_t daddr = (uintptr_t)dof;
 
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 
 	if ((help = p->p_dtrace_helpers) == NULL)
 		help = dtrace_helpers_create(p);
 
 	vstate = &help->dthps_vstate;
 
 	if ((rv = dtrace_dof_slurp(dof, vstate, NULL, &enab, dhp->dofhp_addr,
 	    dhp->dofhp_dof, B_FALSE)) != 0) {
 		dtrace_dof_destroy(dof);
 		return (rv);
 	}
 
 	/*
 	 * Look for helper providers and validate their descriptions.
 	 */
 	for (i = 0; i < dof->dofh_secnum; i++) {
 		dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr +
 		    dof->dofh_secoff + i * dof->dofh_secsize);
 
 		if (sec->dofs_type != DOF_SECT_PROVIDER)
 			continue;
 
 		if (dtrace_helper_provider_validate(dof, sec) != 0) {
 			dtrace_enabling_destroy(enab);
 			dtrace_dof_destroy(dof);
 			return (-1);
 		}
 
 		nprovs++;
 	}
 
 	/*
 	 * Now we need to walk through the ECB descriptions in the enabling.
 	 */
 	for (i = 0; i < enab->dten_ndesc; i++) {
 		dtrace_ecbdesc_t *ep = enab->dten_desc[i];
 		dtrace_probedesc_t *desc = &ep->dted_probe;
 
 		if (strcmp(desc->dtpd_provider, "dtrace") != 0)
 			continue;
 
 		if (strcmp(desc->dtpd_mod, "helper") != 0)
 			continue;
 
 		if (strcmp(desc->dtpd_func, "ustack") != 0)
 			continue;
 
 		if ((rv = dtrace_helper_action_add(DTRACE_HELPER_ACTION_USTACK,
 		    ep, help)) != 0) {
 			/*
 			 * Adding this helper action failed -- we are now going
 			 * to rip out the entire generation and return failure.
 			 */
 			(void) dtrace_helper_destroygen(help,
 			    help->dthps_generation);
 			dtrace_enabling_destroy(enab);
 			dtrace_dof_destroy(dof);
 			return (-1);
 		}
 
 		nhelpers++;
 	}
 
 	if (nhelpers < enab->dten_ndesc)
 		dtrace_dof_error(dof, "unmatched helpers");
 
 	gen = help->dthps_generation++;
 	dtrace_enabling_destroy(enab);
 
 	if (nprovs > 0) {
 		/*
 		 * Now that this is in-kernel, we change the sense of the
 		 * members:  dofhp_dof denotes the in-kernel copy of the DOF
 		 * and dofhp_addr denotes the address at user-level.
 		 */
 		dhp->dofhp_addr = dhp->dofhp_dof;
 		dhp->dofhp_dof = (uint64_t)(uintptr_t)dof;
 
 		if (dtrace_helper_provider_add(dhp, help, gen) == 0) {
 			mutex_exit(&dtrace_lock);
 			dtrace_helper_provider_register(p, help, dhp);
 			mutex_enter(&dtrace_lock);
 
 			destroy = 0;
 		}
 	}
 
 	if (destroy)
 		dtrace_dof_destroy(dof);
 
 	return (gen);
 }
 
 static dtrace_helpers_t *
 dtrace_helpers_create(proc_t *p)
 {
 	dtrace_helpers_t *help;
 
 	ASSERT(MUTEX_HELD(&dtrace_lock));
 	ASSERT(p->p_dtrace_helpers == NULL);
 
 	help = kmem_zalloc(sizeof (dtrace_helpers_t), KM_SLEEP);
 	help->dthps_actions = kmem_zalloc(sizeof (dtrace_helper_action_t *) *
 	    DTRACE_NHELPER_ACTIONS, KM_SLEEP);
 
 	p->p_dtrace_helpers = help;
 	dtrace_helpers++;
 
 	return (help);
 }
 
 #ifdef illumos
 static
 #endif
 void
 dtrace_helpers_destroy(proc_t *p)
 {
 	dtrace_helpers_t *help;
 	dtrace_vstate_t *vstate;
 #ifdef illumos
 	proc_t *p = curproc;
 #endif
 	int i;
 
 	mutex_enter(&dtrace_lock);
 
 	ASSERT(p->p_dtrace_helpers != NULL);
 	ASSERT(dtrace_helpers > 0);
 
 	help = p->p_dtrace_helpers;
 	vstate = &help->dthps_vstate;
 
 	/*
 	 * We're now going to lose the help from this process.
 	 */
 	p->p_dtrace_helpers = NULL;
 	dtrace_sync();
 
 	/*
 	 * Destory the helper actions.
 	 */
 	for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) {
 		dtrace_helper_action_t *h, *next;
 
 		for (h = help->dthps_actions[i]; h != NULL; h = next) {
 			next = h->dtha_next;
 			dtrace_helper_action_destroy(h, vstate);
 			h = next;
 		}
 	}
 
 	mutex_exit(&dtrace_lock);
 
 	/*
 	 * Destroy the helper providers.
 	 */
 	if (help->dthps_maxprovs > 0) {
 		mutex_enter(&dtrace_meta_lock);
 		if (dtrace_meta_pid != NULL) {
 			ASSERT(dtrace_deferred_pid == NULL);
 
 			for (i = 0; i < help->dthps_nprovs; i++) {
 				dtrace_helper_provider_remove(
 				    &help->dthps_provs[i]->dthp_prov, p->p_pid);
 			}
 		} else {
 			mutex_enter(&dtrace_lock);
 			ASSERT(help->dthps_deferred == 0 ||
 			    help->dthps_next != NULL ||
 			    help->dthps_prev != NULL ||
 			    help == dtrace_deferred_pid);
 
 			/*
 			 * Remove the helper from the deferred list.
 			 */
 			if (help->dthps_next != NULL)
 				help->dthps_next->dthps_prev = help->dthps_prev;
 			if (help->dthps_prev != NULL)
 				help->dthps_prev->dthps_next = help->dthps_next;
 			if (dtrace_deferred_pid == help) {
 				dtrace_deferred_pid = help->dthps_next;
 				ASSERT(help->dthps_prev == NULL);
 			}
 
 			mutex_exit(&dtrace_lock);
 		}
 
 		mutex_exit(&dtrace_meta_lock);
 
 		for (i = 0; i < help->dthps_nprovs; i++) {
 			dtrace_helper_provider_destroy(help->dthps_provs[i]);
 		}
 
 		kmem_free(help->dthps_provs, help->dthps_maxprovs *
 		    sizeof (dtrace_helper_provider_t *));
 	}
 
 	mutex_enter(&dtrace_lock);
 
 	dtrace_vstate_fini(&help->dthps_vstate);
 	kmem_free(help->dthps_actions,
 	    sizeof (dtrace_helper_action_t *) * DTRACE_NHELPER_ACTIONS);
 	kmem_free(help, sizeof (dtrace_helpers_t));
 
 	--dtrace_helpers;
 	mutex_exit(&dtrace_lock);
 }
 
 #ifdef illumos
 static
 #endif
 void
 dtrace_helpers_duplicate(proc_t *from, proc_t *to)
 {
 	dtrace_helpers_t *help, *newhelp;
 	dtrace_helper_action_t *helper, *new, *last;
 	dtrace_difo_t *dp;
 	dtrace_vstate_t *vstate;
 	int i, j, sz, hasprovs = 0;
 
 	mutex_enter(&dtrace_lock);
 	ASSERT(from->p_dtrace_helpers != NULL);
 	ASSERT(dtrace_helpers > 0);
 
 	help = from->p_dtrace_helpers;
 	newhelp = dtrace_helpers_create(to);
 	ASSERT(to->p_dtrace_helpers != NULL);
 
 	newhelp->dthps_generation = help->dthps_generation;
 	vstate = &newhelp->dthps_vstate;
 
 	/*
 	 * Duplicate the helper actions.
 	 */
 	for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) {
 		if ((helper = help->dthps_actions[i]) == NULL)
 			continue;
 
 		for (last = NULL; helper != NULL; helper = helper->dtha_next) {
 			new = kmem_zalloc(sizeof (dtrace_helper_action_t),
 			    KM_SLEEP);
 			new->dtha_generation = helper->dtha_generation;
 
 			if ((dp = helper->dtha_predicate) != NULL) {
 				dp = dtrace_difo_duplicate(dp, vstate);
 				new->dtha_predicate = dp;
 			}
 
 			new->dtha_nactions = helper->dtha_nactions;
 			sz = sizeof (dtrace_difo_t *) * new->dtha_nactions;
 			new->dtha_actions = kmem_alloc(sz, KM_SLEEP);
 
 			for (j = 0; j < new->dtha_nactions; j++) {
 				dtrace_difo_t *dp = helper->dtha_actions[j];
 
 				ASSERT(dp != NULL);
 				dp = dtrace_difo_duplicate(dp, vstate);
 				new->dtha_actions[j] = dp;
 			}
 
 			if (last != NULL) {
 				last->dtha_next = new;
 			} else {
 				newhelp->dthps_actions[i] = new;
 			}
 
 			last = new;
 		}
 	}
 
 	/*
 	 * Duplicate the helper providers and register them with the
 	 * DTrace framework.
 	 */
 	if (help->dthps_nprovs > 0) {
 		newhelp->dthps_nprovs = help->dthps_nprovs;
 		newhelp->dthps_maxprovs = help->dthps_nprovs;
 		newhelp->dthps_provs = kmem_alloc(newhelp->dthps_nprovs *
 		    sizeof (dtrace_helper_provider_t *), KM_SLEEP);
 		for (i = 0; i < newhelp->dthps_nprovs; i++) {
 			newhelp->dthps_provs[i] = help->dthps_provs[i];
 			newhelp->dthps_provs[i]->dthp_ref++;
 		}
 
 		hasprovs = 1;
 	}
 
 	mutex_exit(&dtrace_lock);
 
 	if (hasprovs)
 		dtrace_helper_provider_register(to, newhelp, NULL);
 }
 
 /*
  * DTrace Hook Functions
  */
 static void
 dtrace_module_loaded(modctl_t *ctl)
 {
 	dtrace_provider_t *prv;
 
 	mutex_enter(&dtrace_provider_lock);
 #ifdef illumos
 	mutex_enter(&mod_lock);
 #endif
 
 #ifdef illumos
 	ASSERT(ctl->mod_busy);
 #endif
 
 	/*
 	 * We're going to call each providers per-module provide operation
 	 * specifying only this module.
 	 */
 	for (prv = dtrace_provider; prv != NULL; prv = prv->dtpv_next)
 		prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl);
 
 #ifdef illumos
 	mutex_exit(&mod_lock);
 #endif
 	mutex_exit(&dtrace_provider_lock);
 
 	/*
 	 * If we have any retained enablings, we need to match against them.
 	 * Enabling probes requires that cpu_lock be held, and we cannot hold
 	 * cpu_lock here -- it is legal for cpu_lock to be held when loading a
 	 * module.  (In particular, this happens when loading scheduling
 	 * classes.)  So if we have any retained enablings, we need to dispatch
 	 * our task queue to do the match for us.
 	 */
 	mutex_enter(&dtrace_lock);
 
 	if (dtrace_retained == NULL) {
 		mutex_exit(&dtrace_lock);
 		return;
 	}
 
 	(void) taskq_dispatch(dtrace_taskq,
 	    (task_func_t *)dtrace_enabling_matchall, NULL, TQ_SLEEP);
 
 	mutex_exit(&dtrace_lock);
 
 	/*
 	 * And now, for a little heuristic sleaze:  in general, we want to
 	 * match modules as soon as they load.  However, we cannot guarantee
 	 * this, because it would lead us to the lock ordering violation
 	 * outlined above.  The common case, of course, is that cpu_lock is
 	 * _not_ held -- so we delay here for a clock tick, hoping that that's
 	 * long enough for the task queue to do its work.  If it's not, it's
 	 * not a serious problem -- it just means that the module that we
 	 * just loaded may not be immediately instrumentable.
 	 */
 	delay(1);
 }
 
 static void
 #ifdef illumos
 dtrace_module_unloaded(modctl_t *ctl)
 #else
 dtrace_module_unloaded(modctl_t *ctl, int *error)
 #endif
 {
 	dtrace_probe_t template, *probe, *first, *next;
 	dtrace_provider_t *prov;
 #ifndef illumos
 	char modname[DTRACE_MODNAMELEN];
 	size_t len;
 #endif
 
 #ifdef illumos
 	template.dtpr_mod = ctl->mod_modname;
 #else
 	/* Handle the fact that ctl->filename may end in ".ko". */
 	strlcpy(modname, ctl->filename, sizeof(modname));
 	len = strlen(ctl->filename);
 	if (len > 3 && strcmp(modname + len - 3, ".ko") == 0)
 		modname[len - 3] = '\0';
 	template.dtpr_mod = modname;
 #endif
 
 	mutex_enter(&dtrace_provider_lock);
 #ifdef illumos
 	mutex_enter(&mod_lock);
 #endif
 	mutex_enter(&dtrace_lock);
 
 #ifndef illumos
 	if (ctl->nenabled > 0) {
 		/* Don't allow unloads if a probe is enabled. */
 		mutex_exit(&dtrace_provider_lock);
 		mutex_exit(&dtrace_lock);
 		*error = -1;
 		printf(
 	"kldunload: attempt to unload module that has DTrace probes enabled\n");
 		return;
 	}
 #endif
 
 	if (dtrace_bymod == NULL) {
 		/*
 		 * The DTrace module is loaded (obviously) but not attached;
 		 * we don't have any work to do.
 		 */
 		mutex_exit(&dtrace_provider_lock);
 #ifdef illumos
 		mutex_exit(&mod_lock);
 #endif
 		mutex_exit(&dtrace_lock);
 		return;
 	}
 
 	for (probe = first = dtrace_hash_lookup(dtrace_bymod, &template);
 	    probe != NULL; probe = probe->dtpr_nextmod) {
 		if (probe->dtpr_ecb != NULL) {
 			mutex_exit(&dtrace_provider_lock);
 #ifdef illumos
 			mutex_exit(&mod_lock);
 #endif
 			mutex_exit(&dtrace_lock);
 
 			/*
 			 * This shouldn't _actually_ be possible -- we're
 			 * unloading a module that has an enabled probe in it.
 			 * (It's normally up to the provider to make sure that
 			 * this can't happen.)  However, because dtps_enable()
 			 * doesn't have a failure mode, there can be an
 			 * enable/unload race.  Upshot:  we don't want to
 			 * assert, but we're not going to disable the
 			 * probe, either.
 			 */
 			if (dtrace_err_verbose) {
 #ifdef illumos
 				cmn_err(CE_WARN, "unloaded module '%s' had "
 				    "enabled probes", ctl->mod_modname);
 #else
 				cmn_err(CE_WARN, "unloaded module '%s' had "
 				    "enabled probes", modname);
 #endif
 			}
 
 			return;
 		}
 	}
 
 	probe = first;
 
 	for (first = NULL; probe != NULL; probe = next) {
 		ASSERT(dtrace_probes[probe->dtpr_id - 1] == probe);
 
 		dtrace_probes[probe->dtpr_id - 1] = NULL;
 
 		next = probe->dtpr_nextmod;
 		dtrace_hash_remove(dtrace_bymod, probe);
 		dtrace_hash_remove(dtrace_byfunc, probe);
 		dtrace_hash_remove(dtrace_byname, probe);
 
 		if (first == NULL) {
 			first = probe;
 			probe->dtpr_nextmod = NULL;
 		} else {
 			probe->dtpr_nextmod = first;
 			first = probe;
 		}
 	}
 
 	/*
 	 * We've removed all of the module's probes from the hash chains and
 	 * from the probe array.  Now issue a dtrace_sync() to be sure that
 	 * everyone has cleared out from any probe array processing.
 	 */
 	dtrace_sync();
 
 	for (probe = first; probe != NULL; probe = first) {
 		first = probe->dtpr_nextmod;
 		prov = probe->dtpr_provider;
 		prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, probe->dtpr_id,
 		    probe->dtpr_arg);
 		kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1);
 		kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1);
 		kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1);
 #ifdef illumos
 		vmem_free(dtrace_arena, (void *)(uintptr_t)probe->dtpr_id, 1);
 #else
 		free_unr(dtrace_arena, probe->dtpr_id);
 #endif
 		kmem_free(probe, sizeof (dtrace_probe_t));
 	}
 
 	mutex_exit(&dtrace_lock);
 #ifdef illumos
 	mutex_exit(&mod_lock);
 #endif
 	mutex_exit(&dtrace_provider_lock);
 }
 
 #ifndef illumos
 static void
 dtrace_kld_load(void *arg __unused, linker_file_t lf)
 {
 
 	dtrace_module_loaded(lf);
 }
 
 static void
 dtrace_kld_unload_try(void *arg __unused, linker_file_t lf, int *error)
 {
 
 	if (*error != 0)
 		/* We already have an error, so don't do anything. */
 		return;
 	dtrace_module_unloaded(lf, error);
 }
 #endif
 
 #ifdef illumos
 static void
 dtrace_suspend(void)
 {
 	dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_suspend));
 }
 
 static void
 dtrace_resume(void)
 {
 	dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_resume));
 }
 #endif
 
 static int
 dtrace_cpu_setup(cpu_setup_t what, processorid_t cpu)
 {
 	ASSERT(MUTEX_HELD(&cpu_lock));
 	mutex_enter(&dtrace_lock);
 
 	switch (what) {
 	case CPU_CONFIG: {
 		dtrace_state_t *state;
 		dtrace_optval_t *opt, rs, c;
 
 		/*
 		 * For now, we only allocate a new buffer for anonymous state.
 		 */
 		if ((state = dtrace_anon.dta_state) == NULL)
 			break;
 
 		if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE)
 			break;
 
 		opt = state->dts_options;
 		c = opt[DTRACEOPT_CPU];
 
 		if (c != DTRACE_CPUALL && c != DTRACEOPT_UNSET && c != cpu)
 			break;
 
 		/*
 		 * Regardless of what the actual policy is, we're going to
 		 * temporarily set our resize policy to be manual.  We're
 		 * also going to temporarily set our CPU option to denote
 		 * the newly configured CPU.
 		 */
 		rs = opt[DTRACEOPT_BUFRESIZE];
 		opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_MANUAL;
 		opt[DTRACEOPT_CPU] = (dtrace_optval_t)cpu;
 
 		(void) dtrace_state_buffers(state);
 
 		opt[DTRACEOPT_BUFRESIZE] = rs;
 		opt[DTRACEOPT_CPU] = c;
 
 		break;
 	}
 
 	case CPU_UNCONFIG:
 		/*
 		 * We don't free the buffer in the CPU_UNCONFIG case.  (The
 		 * buffer will be freed when the consumer exits.)
 		 */
 		break;
 
 	default:
 		break;
 	}
 
 	mutex_exit(&dtrace_lock);
 	return (0);
 }
 
 #ifdef illumos
 static void
 dtrace_cpu_setup_initial(processorid_t cpu)
 {
 	(void) dtrace_cpu_setup(CPU_CONFIG, cpu);
 }
 #endif
 
 static void
 dtrace_toxrange_add(uintptr_t base, uintptr_t limit)
 {
 	if (dtrace_toxranges >= dtrace_toxranges_max) {
 		int osize, nsize;
 		dtrace_toxrange_t *range;
 
 		osize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t);
 
 		if (osize == 0) {
 			ASSERT(dtrace_toxrange == NULL);
 			ASSERT(dtrace_toxranges_max == 0);
 			dtrace_toxranges_max = 1;
 		} else {
 			dtrace_toxranges_max <<= 1;
 		}
 
 		nsize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t);
 		range = kmem_zalloc(nsize, KM_SLEEP);
 
 		if (dtrace_toxrange != NULL) {
 			ASSERT(osize != 0);
 			bcopy(dtrace_toxrange, range, osize);
 			kmem_free(dtrace_toxrange, osize);
 		}
 
 		dtrace_toxrange = range;
 	}
 
 	ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_base == 0);
 	ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_limit == 0);
 
 	dtrace_toxrange[dtrace_toxranges].dtt_base = base;
 	dtrace_toxrange[dtrace_toxranges].dtt_limit = limit;
 	dtrace_toxranges++;
 }
 
 static void
 dtrace_getf_barrier()
 {
 #ifdef illumos
 	/*
 	 * When we have unprivileged (that is, non-DTRACE_CRV_KERNEL) enablings
 	 * that contain calls to getf(), this routine will be called on every
 	 * closef() before either the underlying vnode is released or the
 	 * file_t itself is freed.  By the time we are here, it is essential
 	 * that the file_t can no longer be accessed from a call to getf()
 	 * in probe context -- that assures that a dtrace_sync() can be used
 	 * to clear out any enablings referring to the old structures.
 	 */
 	if (curthread->t_procp->p_zone->zone_dtrace_getf != 0 ||
 	    kcred->cr_zone->zone_dtrace_getf != 0)
 		dtrace_sync();
 #endif
 }
 
 /*
  * DTrace Driver Cookbook Functions
  */
 #ifdef illumos
 /*ARGSUSED*/
 static int
 dtrace_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
 {
 	dtrace_provider_id_t id;
 	dtrace_state_t *state = NULL;
 	dtrace_enabling_t *enab;
 
 	mutex_enter(&cpu_lock);
 	mutex_enter(&dtrace_provider_lock);
 	mutex_enter(&dtrace_lock);
 
 	if (ddi_soft_state_init(&dtrace_softstate,
 	    sizeof (dtrace_state_t), 0) != 0) {
 		cmn_err(CE_NOTE, "/dev/dtrace failed to initialize soft state");
 		mutex_exit(&cpu_lock);
 		mutex_exit(&dtrace_provider_lock);
 		mutex_exit(&dtrace_lock);
 		return (DDI_FAILURE);
 	}
 
 	if (ddi_create_minor_node(devi, DTRACEMNR_DTRACE, S_IFCHR,
 	    DTRACEMNRN_DTRACE, DDI_PSEUDO, NULL) == DDI_FAILURE ||
 	    ddi_create_minor_node(devi, DTRACEMNR_HELPER, S_IFCHR,
 	    DTRACEMNRN_HELPER, DDI_PSEUDO, NULL) == DDI_FAILURE) {
 		cmn_err(CE_NOTE, "/dev/dtrace couldn't create minor nodes");
 		ddi_remove_minor_node(devi, NULL);
 		ddi_soft_state_fini(&dtrace_softstate);
 		mutex_exit(&cpu_lock);
 		mutex_exit(&dtrace_provider_lock);
 		mutex_exit(&dtrace_lock);
 		return (DDI_FAILURE);
 	}
 
 	ddi_report_dev(devi);
 	dtrace_devi = devi;
 
 	dtrace_modload = dtrace_module_loaded;
 	dtrace_modunload = dtrace_module_unloaded;
 	dtrace_cpu_init = dtrace_cpu_setup_initial;
 	dtrace_helpers_cleanup = dtrace_helpers_destroy;
 	dtrace_helpers_fork = dtrace_helpers_duplicate;
 	dtrace_cpustart_init = dtrace_suspend;
 	dtrace_cpustart_fini = dtrace_resume;
 	dtrace_debugger_init = dtrace_suspend;
 	dtrace_debugger_fini = dtrace_resume;
 
 	register_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL);
 
 	ASSERT(MUTEX_HELD(&cpu_lock));
 
 	dtrace_arena = vmem_create("dtrace", (void *)1, UINT32_MAX, 1,
 	    NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
 	dtrace_minor = vmem_create("dtrace_minor", (void *)DTRACEMNRN_CLONE,
 	    UINT32_MAX - DTRACEMNRN_CLONE, 1, NULL, NULL, NULL, 0,
 	    VM_SLEEP | VMC_IDENTIFIER);
 	dtrace_taskq = taskq_create("dtrace_taskq", 1, maxclsyspri,
 	    1, INT_MAX, 0);
 
 	dtrace_state_cache = kmem_cache_create("dtrace_state_cache",
 	    sizeof (dtrace_dstate_percpu_t) * NCPU, DTRACE_STATE_ALIGN,
 	    NULL, NULL, NULL, NULL, NULL, 0);
 
 	ASSERT(MUTEX_HELD(&cpu_lock));
 	dtrace_bymod = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_mod),
 	    offsetof(dtrace_probe_t, dtpr_nextmod),
 	    offsetof(dtrace_probe_t, dtpr_prevmod));
 
 	dtrace_byfunc = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_func),
 	    offsetof(dtrace_probe_t, dtpr_nextfunc),
 	    offsetof(dtrace_probe_t, dtpr_prevfunc));
 
 	dtrace_byname = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_name),
 	    offsetof(dtrace_probe_t, dtpr_nextname),
 	    offsetof(dtrace_probe_t, dtpr_prevname));
 
 	if (dtrace_retain_max < 1) {
 		cmn_err(CE_WARN, "illegal value (%zu) for dtrace_retain_max; "
 		    "setting to 1", dtrace_retain_max);
 		dtrace_retain_max = 1;
 	}
 
 	/*
 	 * Now discover our toxic ranges.
 	 */
 	dtrace_toxic_ranges(dtrace_toxrange_add);
 
 	/*
 	 * Before we register ourselves as a provider to our own framework,
 	 * we would like to assert that dtrace_provider is NULL -- but that's
 	 * not true if we were loaded as a dependency of a DTrace provider.
 	 * Once we've registered, we can assert that dtrace_provider is our
 	 * pseudo provider.
 	 */
 	(void) dtrace_register("dtrace", &dtrace_provider_attr,
 	    DTRACE_PRIV_NONE, 0, &dtrace_provider_ops, NULL, &id);
 
 	ASSERT(dtrace_provider != NULL);
 	ASSERT((dtrace_provider_id_t)dtrace_provider == id);
 
 	dtrace_probeid_begin = dtrace_probe_create((dtrace_provider_id_t)
 	    dtrace_provider, NULL, NULL, "BEGIN", 0, NULL);
 	dtrace_probeid_end = dtrace_probe_create((dtrace_provider_id_t)
 	    dtrace_provider, NULL, NULL, "END", 0, NULL);
 	dtrace_probeid_error = dtrace_probe_create((dtrace_provider_id_t)
 	    dtrace_provider, NULL, NULL, "ERROR", 1, NULL);
 
 	dtrace_anon_property();
 	mutex_exit(&cpu_lock);
 
 	/*
 	 * If there are already providers, we must ask them to provide their
 	 * probes, and then match any anonymous enabling against them.  Note
 	 * that there should be no other retained enablings at this time:
 	 * the only retained enablings at this time should be the anonymous
 	 * enabling.
 	 */
 	if (dtrace_anon.dta_enabling != NULL) {
 		ASSERT(dtrace_retained == dtrace_anon.dta_enabling);
 
 		dtrace_enabling_provide(NULL);
 		state = dtrace_anon.dta_state;
 
 		/*
 		 * We couldn't hold cpu_lock across the above call to
 		 * dtrace_enabling_provide(), but we must hold it to actually
 		 * enable the probes.  We have to drop all of our locks, pick
 		 * up cpu_lock, and regain our locks before matching the
 		 * retained anonymous enabling.
 		 */
 		mutex_exit(&dtrace_lock);
 		mutex_exit(&dtrace_provider_lock);
 
 		mutex_enter(&cpu_lock);
 		mutex_enter(&dtrace_provider_lock);
 		mutex_enter(&dtrace_lock);
 
 		if ((enab = dtrace_anon.dta_enabling) != NULL)
 			(void) dtrace_enabling_match(enab, NULL);
 
 		mutex_exit(&cpu_lock);
 	}
 
 	mutex_exit(&dtrace_lock);
 	mutex_exit(&dtrace_provider_lock);
 
 	if (state != NULL) {
 		/*
 		 * If we created any anonymous state, set it going now.
 		 */
 		(void) dtrace_state_go(state, &dtrace_anon.dta_beganon);
 	}
 
 	return (DDI_SUCCESS);
 }
 #endif	/* illumos */
 
 #ifndef illumos
 static void dtrace_dtr(void *);
 #endif
 
 /*ARGSUSED*/
 static int
 #ifdef illumos
 dtrace_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
 #else
 dtrace_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
 #endif
 {
 	dtrace_state_t *state;
 	uint32_t priv;
 	uid_t uid;
 	zoneid_t zoneid;
 
 #ifdef illumos
 	if (getminor(*devp) == DTRACEMNRN_HELPER)
 		return (0);
 
 	/*
 	 * If this wasn't an open with the "helper" minor, then it must be
 	 * the "dtrace" minor.
 	 */
 	if (getminor(*devp) == DTRACEMNRN_DTRACE)
 		return (ENXIO);
 #else
 	cred_t *cred_p = NULL;
 	cred_p = dev->si_cred;
 
 	/*
 	 * If no DTRACE_PRIV_* bits are set in the credential, then the
 	 * caller lacks sufficient permission to do anything with DTrace.
 	 */
 	dtrace_cred2priv(cred_p, &priv, &uid, &zoneid);
 	if (priv == DTRACE_PRIV_NONE) {
 #endif
 
 		return (EACCES);
 	}
 
 	/*
 	 * Ask all providers to provide all their probes.
 	 */
 	mutex_enter(&dtrace_provider_lock);
 	dtrace_probe_provide(NULL, NULL);
 	mutex_exit(&dtrace_provider_lock);
 
 	mutex_enter(&cpu_lock);
 	mutex_enter(&dtrace_lock);
 	dtrace_opens++;
 	dtrace_membar_producer();
 
 #ifdef illumos
 	/*
 	 * If the kernel debugger is active (that is, if the kernel debugger
 	 * modified text in some way), we won't allow the open.
 	 */
 	if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) {
 		dtrace_opens--;
 		mutex_exit(&cpu_lock);
 		mutex_exit(&dtrace_lock);
 		return (EBUSY);
 	}
 
 	if (dtrace_helptrace_enable && dtrace_helptrace_buffer == NULL) {
 		/*
 		 * If DTrace helper tracing is enabled, we need to allocate the
 		 * trace buffer and initialize the values.
 		 */
 		dtrace_helptrace_buffer =
 		    kmem_zalloc(dtrace_helptrace_bufsize, KM_SLEEP);
 		dtrace_helptrace_next = 0;
 		dtrace_helptrace_wrapped = 0;
 		dtrace_helptrace_enable = 0;
 	}
 
 	state = dtrace_state_create(devp, cred_p);
 #else
 	state = dtrace_state_create(dev, NULL);
 	devfs_set_cdevpriv(state, dtrace_dtr);
 #endif
 
 	mutex_exit(&cpu_lock);
 
 	if (state == NULL) {
 #ifdef illumos
 		if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL)
 			(void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE);
 #else
 		--dtrace_opens;
 #endif
 		mutex_exit(&dtrace_lock);
 		return (EAGAIN);
 	}
 
 	mutex_exit(&dtrace_lock);
 
 	return (0);
 }
 
 /*ARGSUSED*/
 #ifdef illumos
 static int
 dtrace_close(dev_t dev, int flag, int otyp, cred_t *cred_p)
 #else
 static void
 dtrace_dtr(void *data)
 #endif
 {
 #ifdef illumos
 	minor_t minor = getminor(dev);
 	dtrace_state_t *state;
 #endif
 	dtrace_helptrace_t *buf = NULL;
 
 #ifdef illumos
 	if (minor == DTRACEMNRN_HELPER)
 		return (0);
 
 	state = ddi_get_soft_state(dtrace_softstate, minor);
 #else
 	dtrace_state_t *state = data;
 #endif
 
 	mutex_enter(&cpu_lock);
 	mutex_enter(&dtrace_lock);
 
 #ifdef illumos
 	if (state->dts_anon)
 #else
 	if (state != NULL && state->dts_anon)
 #endif
 	{
 		/*
 		 * There is anonymous state. Destroy that first.
 		 */
 		ASSERT(dtrace_anon.dta_state == NULL);
 		dtrace_state_destroy(state->dts_anon);
 	}
 
 	if (dtrace_helptrace_disable) {
 		/*
 		 * If we have been told to disable helper tracing, set the
 		 * buffer to NULL before calling into dtrace_state_destroy();
 		 * we take advantage of its dtrace_sync() to know that no
 		 * CPU is in probe context with enabled helper tracing
 		 * after it returns.
 		 */
 		buf = dtrace_helptrace_buffer;
 		dtrace_helptrace_buffer = NULL;
 	}
 
 #ifdef illumos
 	dtrace_state_destroy(state);
 #else
 	if (state != NULL) {
 		dtrace_state_destroy(state);
 		kmem_free(state, 0);
 	}
 #endif
 	ASSERT(dtrace_opens > 0);
 
 #ifdef illumos
 	/*
 	 * Only relinquish control of the kernel debugger interface when there
 	 * are no consumers and no anonymous enablings.
 	 */
 	if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL)
 		(void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE);
 #else
 	--dtrace_opens;
 #endif
 
 	if (buf != NULL) {
 		kmem_free(buf, dtrace_helptrace_bufsize);
 		dtrace_helptrace_disable = 0;
 	}
 
 	mutex_exit(&dtrace_lock);
 	mutex_exit(&cpu_lock);
 
 #ifdef illumos
 	return (0);
 #endif
 }
 
 #ifdef illumos
 /*ARGSUSED*/
 static int
 dtrace_ioctl_helper(int cmd, intptr_t arg, int *rv)
 {
 	int rval;
 	dof_helper_t help, *dhp = NULL;
 
 	switch (cmd) {
 	case DTRACEHIOC_ADDDOF:
 		if (copyin((void *)arg, &help, sizeof (help)) != 0) {
 			dtrace_dof_error(NULL, "failed to copyin DOF helper");
 			return (EFAULT);
 		}
 
 		dhp = &help;
 		arg = (intptr_t)help.dofhp_dof;
 		/*FALLTHROUGH*/
 
 	case DTRACEHIOC_ADD: {
 		dof_hdr_t *dof = dtrace_dof_copyin(arg, &rval);
 
 		if (dof == NULL)
 			return (rval);
 
 		mutex_enter(&dtrace_lock);
 
 		/*
 		 * dtrace_helper_slurp() takes responsibility for the dof --
 		 * it may free it now or it may save it and free it later.
 		 */
 		if ((rval = dtrace_helper_slurp(dof, dhp)) != -1) {
 			*rv = rval;
 			rval = 0;
 		} else {
 			rval = EINVAL;
 		}
 
 		mutex_exit(&dtrace_lock);
 		return (rval);
 	}
 
 	case DTRACEHIOC_REMOVE: {
 		mutex_enter(&dtrace_lock);
 		rval = dtrace_helper_destroygen(NULL, arg);
 		mutex_exit(&dtrace_lock);
 
 		return (rval);
 	}
 
 	default:
 		break;
 	}
 
 	return (ENOTTY);
 }
 
 /*ARGSUSED*/
 static int
 dtrace_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *cr, int *rv)
 {
 	minor_t minor = getminor(dev);
 	dtrace_state_t *state;
 	int rval;
 
 	if (minor == DTRACEMNRN_HELPER)
 		return (dtrace_ioctl_helper(cmd, arg, rv));
 
 	state = ddi_get_soft_state(dtrace_softstate, minor);
 
 	if (state->dts_anon) {
 		ASSERT(dtrace_anon.dta_state == NULL);
 		state = state->dts_anon;
 	}
 
 	switch (cmd) {
 	case DTRACEIOC_PROVIDER: {
 		dtrace_providerdesc_t pvd;
 		dtrace_provider_t *pvp;
 
 		if (copyin((void *)arg, &pvd, sizeof (pvd)) != 0)
 			return (EFAULT);
 
 		pvd.dtvd_name[DTRACE_PROVNAMELEN - 1] = '\0';
 		mutex_enter(&dtrace_provider_lock);
 
 		for (pvp = dtrace_provider; pvp != NULL; pvp = pvp->dtpv_next) {
 			if (strcmp(pvp->dtpv_name, pvd.dtvd_name) == 0)
 				break;
 		}
 
 		mutex_exit(&dtrace_provider_lock);
 
 		if (pvp == NULL)
 			return (ESRCH);
 
 		bcopy(&pvp->dtpv_priv, &pvd.dtvd_priv, sizeof (dtrace_ppriv_t));
 		bcopy(&pvp->dtpv_attr, &pvd.dtvd_attr, sizeof (dtrace_pattr_t));
 
 		if (copyout(&pvd, (void *)arg, sizeof (pvd)) != 0)
 			return (EFAULT);
 
 		return (0);
 	}
 
 	case DTRACEIOC_EPROBE: {
 		dtrace_eprobedesc_t epdesc;
 		dtrace_ecb_t *ecb;
 		dtrace_action_t *act;
 		void *buf;
 		size_t size;
 		uintptr_t dest;
 		int nrecs;
 
 		if (copyin((void *)arg, &epdesc, sizeof (epdesc)) != 0)
 			return (EFAULT);
 
 		mutex_enter(&dtrace_lock);
 
 		if ((ecb = dtrace_epid2ecb(state, epdesc.dtepd_epid)) == NULL) {
 			mutex_exit(&dtrace_lock);
 			return (EINVAL);
 		}
 
 		if (ecb->dte_probe == NULL) {
 			mutex_exit(&dtrace_lock);
 			return (EINVAL);
 		}
 
 		epdesc.dtepd_probeid = ecb->dte_probe->dtpr_id;
 		epdesc.dtepd_uarg = ecb->dte_uarg;
 		epdesc.dtepd_size = ecb->dte_size;
 
 		nrecs = epdesc.dtepd_nrecs;
 		epdesc.dtepd_nrecs = 0;
 		for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
 			if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple)
 				continue;
 
 			epdesc.dtepd_nrecs++;
 		}
 
 		/*
 		 * Now that we have the size, we need to allocate a temporary
 		 * buffer in which to store the complete description.  We need
 		 * the temporary buffer to be able to drop dtrace_lock()
 		 * across the copyout(), below.
 		 */
 		size = sizeof (dtrace_eprobedesc_t) +
 		    (epdesc.dtepd_nrecs * sizeof (dtrace_recdesc_t));
 
 		buf = kmem_alloc(size, KM_SLEEP);
 		dest = (uintptr_t)buf;
 
 		bcopy(&epdesc, (void *)dest, sizeof (epdesc));
 		dest += offsetof(dtrace_eprobedesc_t, dtepd_rec[0]);
 
 		for (act = ecb->dte_action; act != NULL; act = act->dta_next) {
 			if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple)
 				continue;
 
 			if (nrecs-- == 0)
 				break;
 
 			bcopy(&act->dta_rec, (void *)dest,
 			    sizeof (dtrace_recdesc_t));
 			dest += sizeof (dtrace_recdesc_t);
 		}
 
 		mutex_exit(&dtrace_lock);
 
 		if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) {
 			kmem_free(buf, size);
 			return (EFAULT);
 		}
 
 		kmem_free(buf, size);
 		return (0);
 	}
 
 	case DTRACEIOC_AGGDESC: {
 		dtrace_aggdesc_t aggdesc;
 		dtrace_action_t *act;
 		dtrace_aggregation_t *agg;
 		int nrecs;
 		uint32_t offs;
 		dtrace_recdesc_t *lrec;
 		void *buf;
 		size_t size;
 		uintptr_t dest;
 
 		if (copyin((void *)arg, &aggdesc, sizeof (aggdesc)) != 0)
 			return (EFAULT);
 
 		mutex_enter(&dtrace_lock);
 
 		if ((agg = dtrace_aggid2agg(state, aggdesc.dtagd_id)) == NULL) {
 			mutex_exit(&dtrace_lock);
 			return (EINVAL);
 		}
 
 		aggdesc.dtagd_epid = agg->dtag_ecb->dte_epid;
 
 		nrecs = aggdesc.dtagd_nrecs;
 		aggdesc.dtagd_nrecs = 0;
 
 		offs = agg->dtag_base;
 		lrec = &agg->dtag_action.dta_rec;
 		aggdesc.dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - offs;
 
 		for (act = agg->dtag_first; ; act = act->dta_next) {
 			ASSERT(act->dta_intuple ||
 			    DTRACEACT_ISAGG(act->dta_kind));
 
 			/*
 			 * If this action has a record size of zero, it
 			 * denotes an argument to the aggregating action.
 			 * Because the presence of this record doesn't (or
 			 * shouldn't) affect the way the data is interpreted,
 			 * we don't copy it out to save user-level the
 			 * confusion of dealing with a zero-length record.
 			 */
 			if (act->dta_rec.dtrd_size == 0) {
 				ASSERT(agg->dtag_hasarg);
 				continue;
 			}
 
 			aggdesc.dtagd_nrecs++;
 
 			if (act == &agg->dtag_action)
 				break;
 		}
 
 		/*
 		 * Now that we have the size, we need to allocate a temporary
 		 * buffer in which to store the complete description.  We need
 		 * the temporary buffer to be able to drop dtrace_lock()
 		 * across the copyout(), below.
 		 */
 		size = sizeof (dtrace_aggdesc_t) +
 		    (aggdesc.dtagd_nrecs * sizeof (dtrace_recdesc_t));
 
 		buf = kmem_alloc(size, KM_SLEEP);
 		dest = (uintptr_t)buf;
 
 		bcopy(&aggdesc, (void *)dest, sizeof (aggdesc));
 		dest += offsetof(dtrace_aggdesc_t, dtagd_rec[0]);
 
 		for (act = agg->dtag_first; ; act = act->dta_next) {
 			dtrace_recdesc_t rec = act->dta_rec;
 
 			/*
 			 * See the comment in the above loop for why we pass
 			 * over zero-length records.
 			 */
 			if (rec.dtrd_size == 0) {
 				ASSERT(agg->dtag_hasarg);
 				continue;
 			}
 
 			if (nrecs-- == 0)
 				break;
 
 			rec.dtrd_offset -= offs;
 			bcopy(&rec, (void *)dest, sizeof (rec));
 			dest += sizeof (dtrace_recdesc_t);
 
 			if (act == &agg->dtag_action)
 				break;
 		}
 
 		mutex_exit(&dtrace_lock);
 
 		if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) {
 			kmem_free(buf, size);
 			return (EFAULT);
 		}
 
 		kmem_free(buf, size);
 		return (0);
 	}
 
 	case DTRACEIOC_ENABLE: {
 		dof_hdr_t *dof;
 		dtrace_enabling_t *enab = NULL;
 		dtrace_vstate_t *vstate;
 		int err = 0;
 
 		*rv = 0;
 
 		/*
 		 * If a NULL argument has been passed, we take this as our
 		 * cue to reevaluate our enablings.
 		 */
 		if (arg == NULL) {
 			dtrace_enabling_matchall();
 
 			return (0);
 		}
 
 		if ((dof = dtrace_dof_copyin(arg, &rval)) == NULL)
 			return (rval);
 
 		mutex_enter(&cpu_lock);
 		mutex_enter(&dtrace_lock);
 		vstate = &state->dts_vstate;
 
 		if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) {
 			mutex_exit(&dtrace_lock);
 			mutex_exit(&cpu_lock);
 			dtrace_dof_destroy(dof);
 			return (EBUSY);
 		}
 
 		if (dtrace_dof_slurp(dof, vstate, cr, &enab, 0, B_TRUE) != 0) {
 			mutex_exit(&dtrace_lock);
 			mutex_exit(&cpu_lock);
 			dtrace_dof_destroy(dof);
 			return (EINVAL);
 		}
 
 		if ((rval = dtrace_dof_options(dof, state)) != 0) {
 			dtrace_enabling_destroy(enab);
 			mutex_exit(&dtrace_lock);
 			mutex_exit(&cpu_lock);
 			dtrace_dof_destroy(dof);
 			return (rval);
 		}
 
 		if ((err = dtrace_enabling_match(enab, rv)) == 0) {
 			err = dtrace_enabling_retain(enab);
 		} else {
 			dtrace_enabling_destroy(enab);
 		}
 
 		mutex_exit(&cpu_lock);
 		mutex_exit(&dtrace_lock);
 		dtrace_dof_destroy(dof);
 
 		return (err);
 	}
 
 	case DTRACEIOC_REPLICATE: {
 		dtrace_repldesc_t desc;
 		dtrace_probedesc_t *match = &desc.dtrpd_match;
 		dtrace_probedesc_t *create = &desc.dtrpd_create;
 		int err;
 
 		if (copyin((void *)arg, &desc, sizeof (desc)) != 0)
 			return (EFAULT);
 
 		match->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
 		match->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
 		match->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
 		match->dtpd_name[DTRACE_NAMELEN - 1] = '\0';
 
 		create->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
 		create->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
 		create->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
 		create->dtpd_name[DTRACE_NAMELEN - 1] = '\0';
 
 		mutex_enter(&dtrace_lock);
 		err = dtrace_enabling_replicate(state, match, create);
 		mutex_exit(&dtrace_lock);
 
 		return (err);
 	}
 
 	case DTRACEIOC_PROBEMATCH:
 	case DTRACEIOC_PROBES: {
 		dtrace_probe_t *probe = NULL;
 		dtrace_probedesc_t desc;
 		dtrace_probekey_t pkey;
 		dtrace_id_t i;
 		int m = 0;
 		uint32_t priv;
 		uid_t uid;
 		zoneid_t zoneid;
 
 		if (copyin((void *)arg, &desc, sizeof (desc)) != 0)
 			return (EFAULT);
 
 		desc.dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0';
 		desc.dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0';
 		desc.dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0';
 		desc.dtpd_name[DTRACE_NAMELEN - 1] = '\0';
 
 		/*
 		 * Before we attempt to match this probe, we want to give
 		 * all providers the opportunity to provide it.
 		 */
 		if (desc.dtpd_id == DTRACE_IDNONE) {
 			mutex_enter(&dtrace_provider_lock);
 			dtrace_probe_provide(&desc, NULL);
 			mutex_exit(&dtrace_provider_lock);
 			desc.dtpd_id++;
 		}
 
 		if (cmd == DTRACEIOC_PROBEMATCH)  {
 			dtrace_probekey(&desc, &pkey);
 			pkey.dtpk_id = DTRACE_IDNONE;
 		}
 
 		dtrace_cred2priv(cr, &priv, &uid, &zoneid);
 
 		mutex_enter(&dtrace_lock);
 
 		if (cmd == DTRACEIOC_PROBEMATCH) {
 			for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) {
 				if ((probe = dtrace_probes[i - 1]) != NULL &&
 				    (m = dtrace_match_probe(probe, &pkey,
 				    priv, uid, zoneid)) != 0)
 					break;
 			}
 
 			if (m < 0) {
 				mutex_exit(&dtrace_lock);
 				return (EINVAL);
 			}
 
 		} else {
 			for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) {
 				if ((probe = dtrace_probes[i - 1]) != NULL &&
 				    dtrace_match_priv(probe, priv, uid, zoneid))
 					break;
 			}
 		}
 
 		if (probe == NULL) {
 			mutex_exit(&dtrace_lock);
 			return (ESRCH);
 		}
 
 		dtrace_probe_description(probe, &desc);
 		mutex_exit(&dtrace_lock);
 
 		if (copyout(&desc, (void *)arg, sizeof (desc)) != 0)
 			return (EFAULT);
 
 		return (0);
 	}
 
 	case DTRACEIOC_PROBEARG: {
 		dtrace_argdesc_t desc;
 		dtrace_probe_t *probe;
 		dtrace_provider_t *prov;
 
 		if (copyin((void *)arg, &desc, sizeof (desc)) != 0)
 			return (EFAULT);
 
 		if (desc.dtargd_id == DTRACE_IDNONE)
 			return (EINVAL);
 
 		if (desc.dtargd_ndx == DTRACE_ARGNONE)
 			return (EINVAL);
 
 		mutex_enter(&dtrace_provider_lock);
 		mutex_enter(&mod_lock);
 		mutex_enter(&dtrace_lock);
 
 		if (desc.dtargd_id > dtrace_nprobes) {
 			mutex_exit(&dtrace_lock);
 			mutex_exit(&mod_lock);
 			mutex_exit(&dtrace_provider_lock);
 			return (EINVAL);
 		}
 
 		if ((probe = dtrace_probes[desc.dtargd_id - 1]) == NULL) {
 			mutex_exit(&dtrace_lock);
 			mutex_exit(&mod_lock);
 			mutex_exit(&dtrace_provider_lock);
 			return (EINVAL);
 		}
 
 		mutex_exit(&dtrace_lock);
 
 		prov = probe->dtpr_provider;
 
 		if (prov->dtpv_pops.dtps_getargdesc == NULL) {
 			/*
 			 * There isn't any typed information for this probe.
 			 * Set the argument number to DTRACE_ARGNONE.
 			 */
 			desc.dtargd_ndx = DTRACE_ARGNONE;
 		} else {
 			desc.dtargd_native[0] = '\0';
 			desc.dtargd_xlate[0] = '\0';
 			desc.dtargd_mapping = desc.dtargd_ndx;
 
 			prov->dtpv_pops.dtps_getargdesc(prov->dtpv_arg,
 			    probe->dtpr_id, probe->dtpr_arg, &desc);
 		}
 
 		mutex_exit(&mod_lock);
 		mutex_exit(&dtrace_provider_lock);
 
 		if (copyout(&desc, (void *)arg, sizeof (desc)) != 0)
 			return (EFAULT);
 
 		return (0);
 	}
 
 	case DTRACEIOC_GO: {
 		processorid_t cpuid;
 		rval = dtrace_state_go(state, &cpuid);
 
 		if (rval != 0)
 			return (rval);
 
 		if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0)
 			return (EFAULT);
 
 		return (0);
 	}
 
 	case DTRACEIOC_STOP: {
 		processorid_t cpuid;
 
 		mutex_enter(&dtrace_lock);
 		rval = dtrace_state_stop(state, &cpuid);
 		mutex_exit(&dtrace_lock);
 
 		if (rval != 0)
 			return (rval);
 
 		if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0)
 			return (EFAULT);
 
 		return (0);
 	}
 
 	case DTRACEIOC_DOFGET: {
 		dof_hdr_t hdr, *dof;
 		uint64_t len;
 
 		if (copyin((void *)arg, &hdr, sizeof (hdr)) != 0)
 			return (EFAULT);
 
 		mutex_enter(&dtrace_lock);
 		dof = dtrace_dof_create(state);
 		mutex_exit(&dtrace_lock);
 
 		len = MIN(hdr.dofh_loadsz, dof->dofh_loadsz);
 		rval = copyout(dof, (void *)arg, len);
 		dtrace_dof_destroy(dof);
 
 		return (rval == 0 ? 0 : EFAULT);
 	}
 
 	case DTRACEIOC_AGGSNAP:
 	case DTRACEIOC_BUFSNAP: {
 		dtrace_bufdesc_t desc;
 		caddr_t cached;
 		dtrace_buffer_t *buf;
 
 		if (copyin((void *)arg, &desc, sizeof (desc)) != 0)
 			return (EFAULT);
 
 		if (desc.dtbd_cpu < 0 || desc.dtbd_cpu >= NCPU)
 			return (EINVAL);
 
 		mutex_enter(&dtrace_lock);
 
 		if (cmd == DTRACEIOC_BUFSNAP) {
 			buf = &state->dts_buffer[desc.dtbd_cpu];
 		} else {
 			buf = &state->dts_aggbuffer[desc.dtbd_cpu];
 		}
 
 		if (buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL)) {
 			size_t sz = buf->dtb_offset;
 
 			if (state->dts_activity != DTRACE_ACTIVITY_STOPPED) {
 				mutex_exit(&dtrace_lock);
 				return (EBUSY);
 			}
 
 			/*
 			 * If this buffer has already been consumed, we're
 			 * going to indicate that there's nothing left here
 			 * to consume.
 			 */
 			if (buf->dtb_flags & DTRACEBUF_CONSUMED) {
 				mutex_exit(&dtrace_lock);
 
 				desc.dtbd_size = 0;
 				desc.dtbd_drops = 0;
 				desc.dtbd_errors = 0;
 				desc.dtbd_oldest = 0;
 				sz = sizeof (desc);
 
 				if (copyout(&desc, (void *)arg, sz) != 0)
 					return (EFAULT);
 
 				return (0);
 			}
 
 			/*
 			 * If this is a ring buffer that has wrapped, we want
 			 * to copy the whole thing out.
 			 */
 			if (buf->dtb_flags & DTRACEBUF_WRAPPED) {
 				dtrace_buffer_polish(buf);
 				sz = buf->dtb_size;
 			}
 
 			if (copyout(buf->dtb_tomax, desc.dtbd_data, sz) != 0) {
 				mutex_exit(&dtrace_lock);
 				return (EFAULT);
 			}
 
 			desc.dtbd_size = sz;
 			desc.dtbd_drops = buf->dtb_drops;
 			desc.dtbd_errors = buf->dtb_errors;
 			desc.dtbd_oldest = buf->dtb_xamot_offset;
 			desc.dtbd_timestamp = dtrace_gethrtime();
 
 			mutex_exit(&dtrace_lock);
 
 			if (copyout(&desc, (void *)arg, sizeof (desc)) != 0)
 				return (EFAULT);
 
 			buf->dtb_flags |= DTRACEBUF_CONSUMED;
 
 			return (0);
 		}
 
 		if (buf->dtb_tomax == NULL) {
 			ASSERT(buf->dtb_xamot == NULL);
 			mutex_exit(&dtrace_lock);
 			return (ENOENT);
 		}
 
 		cached = buf->dtb_tomax;
 		ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH));
 
 		dtrace_xcall(desc.dtbd_cpu,
 		    (dtrace_xcall_t)dtrace_buffer_switch, buf);
 
 		state->dts_errors += buf->dtb_xamot_errors;
 
 		/*
 		 * If the buffers did not actually switch, then the cross call
 		 * did not take place -- presumably because the given CPU is
 		 * not in the ready set.  If this is the case, we'll return
 		 * ENOENT.
 		 */
 		if (buf->dtb_tomax == cached) {
 			ASSERT(buf->dtb_xamot != cached);
 			mutex_exit(&dtrace_lock);
 			return (ENOENT);
 		}
 
 		ASSERT(cached == buf->dtb_xamot);
 
 		/*
 		 * We have our snapshot; now copy it out.
 		 */
 		if (copyout(buf->dtb_xamot, desc.dtbd_data,
 		    buf->dtb_xamot_offset) != 0) {
 			mutex_exit(&dtrace_lock);
 			return (EFAULT);
 		}
 
 		desc.dtbd_size = buf->dtb_xamot_offset;
 		desc.dtbd_drops = buf->dtb_xamot_drops;
 		desc.dtbd_errors = buf->dtb_xamot_errors;
 		desc.dtbd_oldest = 0;
 		desc.dtbd_timestamp = buf->dtb_switched;
 
 		mutex_exit(&dtrace_lock);
 
 		/*
 		 * Finally, copy out the buffer description.
 		 */
 		if (copyout(&desc, (void *)arg, sizeof (desc)) != 0)
 			return (EFAULT);
 
 		return (0);
 	}
 
 	case DTRACEIOC_CONF: {
 		dtrace_conf_t conf;
 
 		bzero(&conf, sizeof (conf));
 		conf.dtc_difversion = DIF_VERSION;
 		conf.dtc_difintregs = DIF_DIR_NREGS;
 		conf.dtc_diftupregs = DIF_DTR_NREGS;
 		conf.dtc_ctfmodel = CTF_MODEL_NATIVE;
 
 		if (copyout(&conf, (void *)arg, sizeof (conf)) != 0)
 			return (EFAULT);
 
 		return (0);
 	}
 
 	case DTRACEIOC_STATUS: {
 		dtrace_status_t stat;
 		dtrace_dstate_t *dstate;
 		int i, j;
 		uint64_t nerrs;
 
 		/*
 		 * See the comment in dtrace_state_deadman() for the reason
 		 * for setting dts_laststatus to INT64_MAX before setting
 		 * it to the correct value.
 		 */
 		state->dts_laststatus = INT64_MAX;
 		dtrace_membar_producer();
 		state->dts_laststatus = dtrace_gethrtime();
 
 		bzero(&stat, sizeof (stat));
 
 		mutex_enter(&dtrace_lock);
 
 		if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) {
 			mutex_exit(&dtrace_lock);
 			return (ENOENT);
 		}
 
 		if (state->dts_activity == DTRACE_ACTIVITY_DRAINING)
 			stat.dtst_exiting = 1;
 
 		nerrs = state->dts_errors;
 		dstate = &state->dts_vstate.dtvs_dynvars;
 
 		for (i = 0; i < NCPU; i++) {
 			dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[i];
 
 			stat.dtst_dyndrops += dcpu->dtdsc_drops;
 			stat.dtst_dyndrops_dirty += dcpu->dtdsc_dirty_drops;
 			stat.dtst_dyndrops_rinsing += dcpu->dtdsc_rinsing_drops;
 
 			if (state->dts_buffer[i].dtb_flags & DTRACEBUF_FULL)
 				stat.dtst_filled++;
 
 			nerrs += state->dts_buffer[i].dtb_errors;
 
 			for (j = 0; j < state->dts_nspeculations; j++) {
 				dtrace_speculation_t *spec;
 				dtrace_buffer_t *buf;
 
 				spec = &state->dts_speculations[j];
 				buf = &spec->dtsp_buffer[i];
 				stat.dtst_specdrops += buf->dtb_xamot_drops;
 			}
 		}
 
 		stat.dtst_specdrops_busy = state->dts_speculations_busy;
 		stat.dtst_specdrops_unavail = state->dts_speculations_unavail;
 		stat.dtst_stkstroverflows = state->dts_stkstroverflows;
 		stat.dtst_dblerrors = state->dts_dblerrors;
 		stat.dtst_killed =
 		    (state->dts_activity == DTRACE_ACTIVITY_KILLED);
 		stat.dtst_errors = nerrs;
 
 		mutex_exit(&dtrace_lock);
 
 		if (copyout(&stat, (void *)arg, sizeof (stat)) != 0)
 			return (EFAULT);
 
 		return (0);
 	}
 
 	case DTRACEIOC_FORMAT: {
 		dtrace_fmtdesc_t fmt;
 		char *str;
 		int len;
 
 		if (copyin((void *)arg, &fmt, sizeof (fmt)) != 0)
 			return (EFAULT);
 
 		mutex_enter(&dtrace_lock);
 
 		if (fmt.dtfd_format == 0 ||
 		    fmt.dtfd_format > state->dts_nformats) {
 			mutex_exit(&dtrace_lock);
 			return (EINVAL);
 		}
 
 		/*
 		 * Format strings are allocated contiguously and they are
 		 * never freed; if a format index is less than the number
 		 * of formats, we can assert that the format map is non-NULL
 		 * and that the format for the specified index is non-NULL.
 		 */
 		ASSERT(state->dts_formats != NULL);
 		str = state->dts_formats[fmt.dtfd_format - 1];
 		ASSERT(str != NULL);
 
 		len = strlen(str) + 1;
 
 		if (len > fmt.dtfd_length) {
 			fmt.dtfd_length = len;
 
 			if (copyout(&fmt, (void *)arg, sizeof (fmt)) != 0) {
 				mutex_exit(&dtrace_lock);
 				return (EINVAL);
 			}
 		} else {
 			if (copyout(str, fmt.dtfd_string, len) != 0) {
 				mutex_exit(&dtrace_lock);
 				return (EINVAL);
 			}
 		}
 
 		mutex_exit(&dtrace_lock);
 		return (0);
 	}
 
 	default:
 		break;
 	}
 
 	return (ENOTTY);
 }
 
 /*ARGSUSED*/
 static int
 dtrace_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
 {
 	dtrace_state_t *state;
 
 	switch (cmd) {
 	case DDI_DETACH:
 		break;
 
 	case DDI_SUSPEND:
 		return (DDI_SUCCESS);
 
 	default:
 		return (DDI_FAILURE);
 	}
 
 	mutex_enter(&cpu_lock);
 	mutex_enter(&dtrace_provider_lock);
 	mutex_enter(&dtrace_lock);
 
 	ASSERT(dtrace_opens == 0);
 
 	if (dtrace_helpers > 0) {
 		mutex_exit(&dtrace_provider_lock);
 		mutex_exit(&dtrace_lock);
 		mutex_exit(&cpu_lock);
 		return (DDI_FAILURE);
 	}
 
 	if (dtrace_unregister((dtrace_provider_id_t)dtrace_provider) != 0) {
 		mutex_exit(&dtrace_provider_lock);
 		mutex_exit(&dtrace_lock);
 		mutex_exit(&cpu_lock);
 		return (DDI_FAILURE);
 	}
 
 	dtrace_provider = NULL;
 
 	if ((state = dtrace_anon_grab()) != NULL) {
 		/*
 		 * If there were ECBs on this state, the provider should
 		 * have not been allowed to detach; assert that there is
 		 * none.
 		 */
 		ASSERT(state->dts_necbs == 0);
 		dtrace_state_destroy(state);
 
 		/*
 		 * If we're being detached with anonymous state, we need to
 		 * indicate to the kernel debugger that DTrace is now inactive.
 		 */
 		(void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE);
 	}
 
 	bzero(&dtrace_anon, sizeof (dtrace_anon_t));
 	unregister_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL);
 	dtrace_cpu_init = NULL;
 	dtrace_helpers_cleanup = NULL;
 	dtrace_helpers_fork = NULL;
 	dtrace_cpustart_init = NULL;
 	dtrace_cpustart_fini = NULL;
 	dtrace_debugger_init = NULL;
 	dtrace_debugger_fini = NULL;
 	dtrace_modload = NULL;
 	dtrace_modunload = NULL;
 
 	ASSERT(dtrace_getf == 0);
 	ASSERT(dtrace_closef == NULL);
 
 	mutex_exit(&cpu_lock);
 
 	kmem_free(dtrace_probes, dtrace_nprobes * sizeof (dtrace_probe_t *));
 	dtrace_probes = NULL;
 	dtrace_nprobes = 0;
 
 	dtrace_hash_destroy(dtrace_bymod);
 	dtrace_hash_destroy(dtrace_byfunc);
 	dtrace_hash_destroy(dtrace_byname);
 	dtrace_bymod = NULL;
 	dtrace_byfunc = NULL;
 	dtrace_byname = NULL;
 
 	kmem_cache_destroy(dtrace_state_cache);
 	vmem_destroy(dtrace_minor);
 	vmem_destroy(dtrace_arena);
 
 	if (dtrace_toxrange != NULL) {
 		kmem_free(dtrace_toxrange,
 		    dtrace_toxranges_max * sizeof (dtrace_toxrange_t));
 		dtrace_toxrange = NULL;
 		dtrace_toxranges = 0;
 		dtrace_toxranges_max = 0;
 	}
 
 	ddi_remove_minor_node(dtrace_devi, NULL);
 	dtrace_devi = NULL;
 
 	ddi_soft_state_fini(&dtrace_softstate);
 
 	ASSERT(dtrace_vtime_references == 0);
 	ASSERT(dtrace_opens == 0);
 	ASSERT(dtrace_retained == NULL);
 
 	mutex_exit(&dtrace_lock);
 	mutex_exit(&dtrace_provider_lock);
 
 	/*
 	 * We don't destroy the task queue until after we have dropped our
 	 * locks (taskq_destroy() may block on running tasks).  To prevent
 	 * attempting to do work after we have effectively detached but before
 	 * the task queue has been destroyed, all tasks dispatched via the
 	 * task queue must check that DTrace is still attached before
 	 * performing any operation.
 	 */
 	taskq_destroy(dtrace_taskq);
 	dtrace_taskq = NULL;
 
 	return (DDI_SUCCESS);
 }
 #endif
 
 #ifdef illumos
 /*ARGSUSED*/
 static int
 dtrace_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
 {
 	int error;
 
 	switch (infocmd) {
 	case DDI_INFO_DEVT2DEVINFO:
 		*result = (void *)dtrace_devi;
 		error = DDI_SUCCESS;
 		break;
 	case DDI_INFO_DEVT2INSTANCE:
 		*result = (void *)0;
 		error = DDI_SUCCESS;
 		break;
 	default:
 		error = DDI_FAILURE;
 	}
 	return (error);
 }
 #endif
 
 #ifdef illumos
 static struct cb_ops dtrace_cb_ops = {
 	dtrace_open,		/* open */
 	dtrace_close,		/* close */
 	nulldev,		/* strategy */
 	nulldev,		/* print */
 	nodev,			/* dump */
 	nodev,			/* read */
 	nodev,			/* write */
 	dtrace_ioctl,		/* ioctl */
 	nodev,			/* devmap */
 	nodev,			/* mmap */
 	nodev,			/* segmap */
 	nochpoll,		/* poll */
 	ddi_prop_op,		/* cb_prop_op */
 	0,			/* streamtab  */
 	D_NEW | D_MP		/* Driver compatibility flag */
 };
 
 static struct dev_ops dtrace_ops = {
 	DEVO_REV,		/* devo_rev */
 	0,			/* refcnt */
 	dtrace_info,		/* get_dev_info */
 	nulldev,		/* identify */
 	nulldev,		/* probe */
 	dtrace_attach,		/* attach */
 	dtrace_detach,		/* detach */
 	nodev,			/* reset */
 	&dtrace_cb_ops,		/* driver operations */
 	NULL,			/* bus operations */
 	nodev			/* dev power */
 };
 
 static struct modldrv modldrv = {
 	&mod_driverops,		/* module type (this is a pseudo driver) */
 	"Dynamic Tracing",	/* name of module */
 	&dtrace_ops,		/* driver ops */
 };
 
 static struct modlinkage modlinkage = {
 	MODREV_1,
 	(void *)&modldrv,
 	NULL
 };
 
 int
 _init(void)
 {
 	return (mod_install(&modlinkage));
 }
 
 int
 _info(struct modinfo *modinfop)
 {
 	return (mod_info(&modlinkage, modinfop));
 }
 
 int
 _fini(void)
 {
 	return (mod_remove(&modlinkage));
 }
 #else
 
 static d_ioctl_t	dtrace_ioctl;
 static d_ioctl_t	dtrace_ioctl_helper;
 static void		dtrace_load(void *);
 static int		dtrace_unload(void);
 static struct cdev	*dtrace_dev;
 static struct cdev	*helper_dev;
 
 void dtrace_invop_init(void);
 void dtrace_invop_uninit(void);
 
 static struct cdevsw dtrace_cdevsw = {
 	.d_version	= D_VERSION,
 	.d_ioctl	= dtrace_ioctl,
 	.d_open		= dtrace_open,
 	.d_name		= "dtrace",
 };
 
 static struct cdevsw helper_cdevsw = {
 	.d_version	= D_VERSION,
 	.d_ioctl	= dtrace_ioctl_helper,
 	.d_name		= "helper",
 };
 
 #include <dtrace_anon.c>
 #include <dtrace_ioctl.c>
 #include <dtrace_load.c>
 #include <dtrace_modevent.c>
 #include <dtrace_sysctl.c>
 #include <dtrace_unload.c>
 #include <dtrace_vtime.c>
 #include <dtrace_hacks.c>
 #include <dtrace_isa.c>
 
 SYSINIT(dtrace_load, SI_SUB_DTRACE, SI_ORDER_FIRST, dtrace_load, NULL);
 SYSUNINIT(dtrace_unload, SI_SUB_DTRACE, SI_ORDER_FIRST, dtrace_unload, NULL);
 SYSINIT(dtrace_anon_init, SI_SUB_DTRACE_ANON, SI_ORDER_FIRST, dtrace_anon_init, NULL);
 
 DEV_MODULE(dtrace, dtrace_modevent, NULL);
 MODULE_VERSION(dtrace, 1);
 MODULE_DEPEND(dtrace, opensolaris, 1, 1, 1);
 #endif
diff --git a/sys/dev/hyperv/storvsc/hv_storvsc_drv_freebsd.c b/sys/dev/hyperv/storvsc/hv_storvsc_drv_freebsd.c
index 5a8f992ce367..2e2ecf3dd228 100644
--- a/sys/dev/hyperv/storvsc/hv_storvsc_drv_freebsd.c
+++ b/sys/dev/hyperv/storvsc/hv_storvsc_drv_freebsd.c
@@ -1,2514 +1,2514 @@
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
  *
  * Copyright (c) 2009-2012,2016-2017 Microsoft Corp.
  * Copyright (c) 2012 NetApp Inc.
  * Copyright (c) 2012 Citrix Inc.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  * 1. Redistributions of source code must retain the above copyright
  *    notice unmodified, this list of conditions, and the following
  *    disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
  *
  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
 /**
  * StorVSC driver for Hyper-V.  This driver presents a SCSI HBA interface
  * to the Comman Access Method (CAM) layer.  CAM control blocks (CCBs) are
  * converted into VSCSI protocol messages which are delivered to the parent
  * partition StorVSP driver over the Hyper-V VMBUS.
  */
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 
 #include <sys/param.h>
 #include <sys/proc.h>
 #include <sys/condvar.h>
 #include <sys/time.h>
 #include <sys/systm.h>
 #include <sys/sysctl.h>
 #include <sys/sockio.h>
 #include <sys/mbuf.h>
 #include <sys/malloc.h>
 #include <sys/module.h>
 #include <sys/kernel.h>
 #include <sys/queue.h>
 #include <sys/lock.h>
 #include <sys/sx.h>
 #include <sys/taskqueue.h>
 #include <sys/bus.h>
 #include <sys/mutex.h>
 #include <sys/callout.h>
 #include <sys/smp.h>
 #include <vm/vm.h>
 #include <vm/pmap.h>
 #include <vm/uma.h>
 #include <sys/lock.h>
 #include <sys/sema.h>
 #include <sys/eventhandler.h>
 #include <machine/bus.h>
 
 #include <cam/cam.h>
 #include <cam/cam_ccb.h>
 #include <cam/cam_periph.h>
 #include <cam/cam_sim.h>
 #include <cam/cam_xpt_sim.h>
 #include <cam/cam_xpt_internal.h>
 #include <cam/cam_debug.h>
 #include <cam/scsi/scsi_all.h>
 #include <cam/scsi/scsi_message.h>
 
 #include <dev/hyperv/include/hyperv.h>
 #include <dev/hyperv/include/vmbus.h>
 #include "hv_vstorage.h"
 #include "vmbus_if.h"
 
 #define STORVSC_MAX_LUNS_PER_TARGET	(64)
 #define STORVSC_MAX_IO_REQUESTS		(STORVSC_MAX_LUNS_PER_TARGET * 2)
 #define BLKVSC_MAX_IDE_DISKS_PER_TARGET	(1)
 #define BLKVSC_MAX_IO_REQUESTS		STORVSC_MAX_IO_REQUESTS
 #define STORVSC_MAX_TARGETS		(2)
 
 #define VSTOR_PKT_SIZE	(sizeof(struct vstor_packet) - vmscsi_size_delta)
 
 /*
  * 33 segments are needed to allow 128KB maxio, in case the data
  * in the first page is _not_ PAGE_SIZE aligned, e.g.
  *
  *     |<----------- 128KB ----------->|
  *     |                               |
  *  0  2K 4K    8K   16K   124K  128K  130K
  *  |  |  |     |     |       |     |  |
  *  +--+--+-----+-----+.......+-----+--+--+
  *  |  |  |     |     |       |     |  |  | DATA
  *  |  |  |     |     |       |     |  |  |
  *  +--+--+-----+-----+.......------+--+--+
  *     |  |                         |  |
  *     | 1|            31           | 1| ...... # of segments
  */
 #define STORVSC_DATA_SEGCNT_MAX		33
 #define STORVSC_DATA_SEGSZ_MAX		PAGE_SIZE
 #define STORVSC_DATA_SIZE_MAX		\
 	((STORVSC_DATA_SEGCNT_MAX - 1) * STORVSC_DATA_SEGSZ_MAX)
 
 struct storvsc_softc;
 
 struct hv_sglist {
 	struct iovec sg_iov[STORVSC_DATA_SEGCNT_MAX];
 	u_short	sg_nseg;
 	u_short	sg_maxseg;
 };
 
 struct hv_sgl_node {
 	LIST_ENTRY(hv_sgl_node) link;
 	struct hv_sglist *sgl_data;
 };
 
 struct hv_sgl_page_pool{
 	LIST_HEAD(, hv_sgl_node) in_use_sgl_list;
 	LIST_HEAD(, hv_sgl_node) free_sgl_list;
 	boolean_t                is_init;
 } g_hv_sgl_page_pool;
 
 enum storvsc_request_type {
 	WRITE_TYPE,
 	READ_TYPE,
 	UNKNOWN_TYPE
 };
 
 SYSCTL_NODE(_hw, OID_AUTO, storvsc, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
 	"Hyper-V storage interface");
 
 static u_int hv_storvsc_use_win8ext_flags = 1;
 SYSCTL_UINT(_hw_storvsc, OID_AUTO, use_win8ext_flags, CTLFLAG_RW,
 	&hv_storvsc_use_win8ext_flags, 0,
 	"Use win8 extension flags or not");
 
 static u_int hv_storvsc_use_pim_unmapped = 1;
 SYSCTL_UINT(_hw_storvsc, OID_AUTO, use_pim_unmapped, CTLFLAG_RDTUN,
 	&hv_storvsc_use_pim_unmapped, 0,
 	"Optimize storvsc by using unmapped I/O");
 
 static u_int hv_storvsc_ringbuffer_size = (64 * PAGE_SIZE);
 SYSCTL_UINT(_hw_storvsc, OID_AUTO, ringbuffer_size, CTLFLAG_RDTUN,
 	&hv_storvsc_ringbuffer_size, 0, "Hyper-V storage ringbuffer size");
 
 static u_int hv_storvsc_max_io = 512;
 SYSCTL_UINT(_hw_storvsc, OID_AUTO, max_io, CTLFLAG_RDTUN,
 	&hv_storvsc_max_io, 0, "Hyper-V storage max io limit");
 
 static int hv_storvsc_chan_cnt = 0;
 SYSCTL_INT(_hw_storvsc, OID_AUTO, chan_cnt, CTLFLAG_RDTUN,
 	&hv_storvsc_chan_cnt, 0, "# of channels to use");
 #ifdef DIAGNOSTIC
 static int hv_storvsc_srb_status = -1;
 SYSCTL_INT(_hw_storvsc, OID_AUTO, srb_status,  CTLFLAG_RW,
 	&hv_storvsc_srb_status, 0, "srb_status to inject");
 TUNABLE_INT("hw_storvsc.srb_status", &hv_storvsc_srb_status);
 #endif /* DIAGNOSTIC */
 
 #define STORVSC_MAX_IO						\
 	vmbus_chan_prplist_nelem(hv_storvsc_ringbuffer_size,	\
 	   STORVSC_DATA_SEGCNT_MAX, VSTOR_PKT_SIZE)
 
 struct hv_storvsc_sysctl {
 	u_long		data_bio_cnt;
 	u_long		data_vaddr_cnt;
 	u_long		data_sg_cnt;
 	u_long		chan_send_cnt[MAXCPU];
 };
 
 struct storvsc_gpa_range {
 	struct vmbus_gpa_range	gpa_range;
 	uint64_t		gpa_page[STORVSC_DATA_SEGCNT_MAX];
 } __packed;
 
 struct hv_storvsc_request {
 	LIST_ENTRY(hv_storvsc_request)	link;
 	struct vstor_packet		vstor_packet;
 	int				prp_cnt;
 	struct storvsc_gpa_range	prp_list;
 	void				*sense_data;
 	uint8_t				sense_info_len;
 	uint8_t				retries;
 	union ccb			*ccb;
 	struct storvsc_softc		*softc;
 	struct callout			callout;
 	struct sema			synch_sema; /*Synchronize the request/response if needed */
 	struct hv_sglist		*bounce_sgl;
 	unsigned int			bounce_sgl_count;
 	uint64_t			not_aligned_seg_bits;
 	bus_dmamap_t			data_dmap;
 };
 
 struct storvsc_softc {
 	struct vmbus_channel		*hs_chan;
 	LIST_HEAD(, hv_storvsc_request)	hs_free_list;
 	struct mtx			hs_lock;
 	struct storvsc_driver_props	*hs_drv_props;
 	int 				hs_unit;
 	uint32_t			hs_frozen;
 	struct cam_sim			*hs_sim;
 	struct cam_path 		*hs_path;
 	uint32_t			hs_num_out_reqs;
 	boolean_t			hs_destroy;
 	boolean_t			hs_drain_notify;
 	struct sema 			hs_drain_sema;	
 	struct hv_storvsc_request	hs_init_req;
 	struct hv_storvsc_request	hs_reset_req;
 	device_t			hs_dev;
 	bus_dma_tag_t			storvsc_req_dtag;
 	struct hv_storvsc_sysctl	sysctl_data;
 	uint32_t			hs_nchan;
 	struct vmbus_channel		*hs_sel_chan[MAXCPU];
 };
 
 static eventhandler_tag storvsc_handler_tag;
 /*
  * The size of the vmscsi_request has changed in win8. The
  * additional size is for the newly added elements in the
  * structure. These elements are valid only when we are talking
  * to a win8 host.
  * Track the correct size we need to apply.
  */
 static int vmscsi_size_delta = sizeof(struct vmscsi_win8_extension);
 
 /**
  * HyperV storvsc timeout testing cases:
  * a. IO returned after first timeout;
  * b. IO returned after second timeout and queue freeze;
  * c. IO returned while timer handler is running
  * The first can be tested by "sg_senddiag -vv /dev/daX",
  * and the second and third can be done by
  * "sg_wr_mode -v -p 08 -c 0,1a -m 0,ff /dev/daX".
  */
 #define HVS_TIMEOUT_TEST 0
 
 /*
  * Bus/adapter reset functionality on the Hyper-V host is
  * buggy and it will be disabled until
  * it can be further tested.
  */
 #define HVS_HOST_RESET 0
 
 struct storvsc_driver_props {
 	char		*drv_name;
 	char		*drv_desc;
 	uint8_t		drv_max_luns_per_target;
 	uint32_t	drv_max_ios_per_target;
 	uint32_t	drv_ringbuffer_size;
 };
 
 enum hv_storage_type {
 	DRIVER_BLKVSC,
 	DRIVER_STORVSC,
 	DRIVER_UNKNOWN
 };
 
 #define HS_MAX_ADAPTERS 10
 
 #define HV_STORAGE_SUPPORTS_MULTI_CHANNEL 0x1
 
 /* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
 static const struct hyperv_guid gStorVscDeviceType={
 	.hv_guid = {0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
 		 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f}
 };
 
 /* {32412632-86cb-44a2-9b5c-50d1417354f5} */
 static const struct hyperv_guid gBlkVscDeviceType={
 	.hv_guid = {0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,
 		 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5}
 };
 
 static struct storvsc_driver_props g_drv_props_table[] = {
 	{"blkvsc", "Hyper-V IDE",
 	 BLKVSC_MAX_IDE_DISKS_PER_TARGET, BLKVSC_MAX_IO_REQUESTS,
 	 20*PAGE_SIZE},
 	{"storvsc", "Hyper-V SCSI",
 	 STORVSC_MAX_LUNS_PER_TARGET, STORVSC_MAX_IO_REQUESTS,
 	 20*PAGE_SIZE}
 };
 
 /*
  * Sense buffer size changed in win8; have a run-time
  * variable to track the size we should use.
  */
 static int sense_buffer_size = PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE;
 
 /*
  * The storage protocol version is determined during the
  * initial exchange with the host.  It will indicate which
  * storage functionality is available in the host.
 */
 static int vmstor_proto_version;
 
 struct vmstor_proto {
         int proto_version;
         int sense_buffer_size;
         int vmscsi_size_delta;
 };
 
 static const struct vmstor_proto vmstor_proto_list[] = {
         {
                 VMSTOR_PROTOCOL_VERSION_WIN10,
                 POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
                 0
         },
         {
                 VMSTOR_PROTOCOL_VERSION_WIN8_1,
                 POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
                 0
         },
         {
                 VMSTOR_PROTOCOL_VERSION_WIN8,
                 POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
                 0
         },
         {
                 VMSTOR_PROTOCOL_VERSION_WIN7,
                 PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE,
                 sizeof(struct vmscsi_win8_extension),
         },
         {
                 VMSTOR_PROTOCOL_VERSION_WIN6,
                 PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE,
                 sizeof(struct vmscsi_win8_extension),
         }
 };
 
 /* static functions */
 static int storvsc_probe(device_t dev);
 static int storvsc_attach(device_t dev);
 static int storvsc_detach(device_t dev);
 static void storvsc_poll(struct cam_sim * sim);
 static void storvsc_action(struct cam_sim * sim, union ccb * ccb);
 static int create_storvsc_request(union ccb *ccb, struct hv_storvsc_request *reqp);
 static void storvsc_free_request(struct storvsc_softc *sc, struct hv_storvsc_request *reqp);
 static enum hv_storage_type storvsc_get_storage_type(device_t dev);
 static void hv_storvsc_rescan_target(struct storvsc_softc *sc);
 static void hv_storvsc_on_channel_callback(struct vmbus_channel *chan, void *xsc);
 static void hv_storvsc_on_iocompletion( struct storvsc_softc *sc,
 					struct vstor_packet *vstor_packet,
 					struct hv_storvsc_request *request);
 static int hv_storvsc_connect_vsp(struct storvsc_softc *);
 static void storvsc_io_done(struct hv_storvsc_request *reqp);
 static void storvsc_copy_sgl_to_bounce_buf(struct hv_sglist *bounce_sgl,
 				bus_dma_segment_t *orig_sgl,
 				unsigned int orig_sgl_count,
 				uint64_t seg_bits);
 void storvsc_copy_from_bounce_buf_to_sgl(bus_dma_segment_t *dest_sgl,
 				unsigned int dest_sgl_count,
 				struct hv_sglist *src_sgl,
 				uint64_t seg_bits);
 
 static device_method_t storvsc_methods[] = {
 	/* Device interface */
 	DEVMETHOD(device_probe,		storvsc_probe),
 	DEVMETHOD(device_attach,	storvsc_attach),
 	DEVMETHOD(device_detach,	storvsc_detach),
 	DEVMETHOD(device_shutdown,      bus_generic_shutdown),
 	DEVMETHOD_END
 };
 
 static driver_t storvsc_driver = {
 	"storvsc", storvsc_methods, sizeof(struct storvsc_softc),
 };
 
 DRIVER_MODULE(storvsc, vmbus, storvsc_driver, 0, 0);
 MODULE_VERSION(storvsc, 1);
 MODULE_DEPEND(storvsc, vmbus, 1, 1, 1);
 
 static void
 storvsc_subchan_attach(struct storvsc_softc *sc,
     struct vmbus_channel *new_channel)
 {
 	struct vmstor_chan_props props;
 
 	memset(&props, 0, sizeof(props));
 
 	vmbus_chan_cpu_rr(new_channel);
 	vmbus_chan_open(new_channel,
 	    sc->hs_drv_props->drv_ringbuffer_size,
   	    sc->hs_drv_props->drv_ringbuffer_size,
 	    (void *)&props,
 	    sizeof(struct vmstor_chan_props),
 	    hv_storvsc_on_channel_callback, sc);
 }
 
 /**
  * @brief Send multi-channel creation request to host
  *
  * @param device  a Hyper-V device pointer
  * @param max_chans  the max channels supported by vmbus
  */
 static void
 storvsc_send_multichannel_request(struct storvsc_softc *sc, int max_subch)
 {
 	struct vmbus_channel **subchan;
 	struct hv_storvsc_request *request;
 	struct vstor_packet *vstor_packet;	
 	int request_subch;
 	int i;
 
 	/* get sub-channel count that need to create */
 	request_subch = MIN(max_subch, mp_ncpus - 1);
 
 	request = &sc->hs_init_req;
 
 	/* request the host to create multi-channel */
 	memset(request, 0, sizeof(struct hv_storvsc_request));
 	
 	sema_init(&request->synch_sema, 0, ("stor_synch_sema"));
 
 	vstor_packet = &request->vstor_packet;
 	
 	vstor_packet->operation = VSTOR_OPERATION_CREATE_MULTI_CHANNELS;
 	vstor_packet->flags = REQUEST_COMPLETION_FLAG;
 	vstor_packet->u.multi_channels_cnt = request_subch;
 
 	vmbus_chan_send(sc->hs_chan,
 	    VMBUS_CHANPKT_TYPE_INBAND, VMBUS_CHANPKT_FLAG_RC,
 	    vstor_packet, VSTOR_PKT_SIZE, (uint64_t)(uintptr_t)request);
 
 	sema_wait(&request->synch_sema);
 
 	if (vstor_packet->operation != VSTOR_OPERATION_COMPLETEIO ||
 	    vstor_packet->status != 0) {		
 		printf("Storvsc_error: create multi-channel invalid operation "
 		    "(%d) or statue (%u)\n",
 		    vstor_packet->operation, vstor_packet->status);
 		return;
 	}
 
 	/* Update channel count */
 	sc->hs_nchan = request_subch + 1;
 
 	/* Wait for sub-channels setup to complete. */
 	subchan = vmbus_subchan_get(sc->hs_chan, request_subch);
 
 	/* Attach the sub-channels. */
 	for (i = 0; i < request_subch; ++i)
 		storvsc_subchan_attach(sc, subchan[i]);
 
 	/* Release the sub-channels. */
 	vmbus_subchan_rel(subchan, request_subch);
 
 	if (bootverbose)
 		printf("Storvsc create multi-channel success!\n");
 }
 
 /**
  * @brief initialize channel connection to parent partition
  *
  * @param dev  a Hyper-V device pointer
  * @returns  0 on success, non-zero error on failure
  */
 static int
 hv_storvsc_channel_init(struct storvsc_softc *sc)
 {
 	int ret = 0, i;
 	struct hv_storvsc_request *request;
 	struct vstor_packet *vstor_packet;
 	uint16_t max_subch;
 	boolean_t support_multichannel;
 	uint32_t version;
 
 	max_subch = 0;
 	support_multichannel = FALSE;
 
 	request = &sc->hs_init_req;
 	memset(request, 0, sizeof(struct hv_storvsc_request));
 	vstor_packet = &request->vstor_packet;
 	request->softc = sc;
 
 	/**
 	 * Initiate the vsc/vsp initialization protocol on the open channel
 	 */
 	sema_init(&request->synch_sema, 0, ("stor_synch_sema"));
 
 	vstor_packet->operation = VSTOR_OPERATION_BEGININITIALIZATION;
 	vstor_packet->flags = REQUEST_COMPLETION_FLAG;
 
 
 	ret = vmbus_chan_send(sc->hs_chan,
 	    VMBUS_CHANPKT_TYPE_INBAND, VMBUS_CHANPKT_FLAG_RC,
 	    vstor_packet, VSTOR_PKT_SIZE, (uint64_t)(uintptr_t)request);
 
 	if (ret != 0)
 		goto cleanup;
 
 	sema_wait(&request->synch_sema);
 
 	if (vstor_packet->operation != VSTOR_OPERATION_COMPLETEIO ||
 		vstor_packet->status != 0) {
 		goto cleanup;
 	}
 
 	for (i = 0; i < nitems(vmstor_proto_list); i++) {
 		/* reuse the packet for version range supported */
 
 		memset(vstor_packet, 0, sizeof(struct vstor_packet));
 		vstor_packet->operation = VSTOR_OPERATION_QUERYPROTOCOLVERSION;
 		vstor_packet->flags = REQUEST_COMPLETION_FLAG;
 
 		vstor_packet->u.version.major_minor =
 			vmstor_proto_list[i].proto_version;
 
 		/* revision is only significant for Windows guests */
 		vstor_packet->u.version.revision = 0;
 
 		ret = vmbus_chan_send(sc->hs_chan,
 		    VMBUS_CHANPKT_TYPE_INBAND, VMBUS_CHANPKT_FLAG_RC,
 		    vstor_packet, VSTOR_PKT_SIZE, (uint64_t)(uintptr_t)request);
 
 		if (ret != 0)
 			goto cleanup;
 
 		sema_wait(&request->synch_sema);
 
 		if (vstor_packet->operation != VSTOR_OPERATION_COMPLETEIO) {
 			ret = EINVAL;
 			goto cleanup;	
 		}
 		if (vstor_packet->status == 0) {
 			vmstor_proto_version =
 				vmstor_proto_list[i].proto_version;
 			sense_buffer_size =
 				vmstor_proto_list[i].sense_buffer_size;
 			vmscsi_size_delta =
 				vmstor_proto_list[i].vmscsi_size_delta;
 			break;
 		}
 	}
 
 	if (vstor_packet->status != 0) {
 		ret = EINVAL;
 		goto cleanup;
 	}
 	/**
 	 * Query channel properties
 	 */
 	memset(vstor_packet, 0, sizeof(struct vstor_packet));
 	vstor_packet->operation = VSTOR_OPERATION_QUERYPROPERTIES;
 	vstor_packet->flags = REQUEST_COMPLETION_FLAG;
 
 	ret = vmbus_chan_send(sc->hs_chan,
 	    VMBUS_CHANPKT_TYPE_INBAND, VMBUS_CHANPKT_FLAG_RC,
 	    vstor_packet, VSTOR_PKT_SIZE, (uint64_t)(uintptr_t)request);
 
 	if ( ret != 0)
 		goto cleanup;
 
 	sema_wait(&request->synch_sema);
 
 	/* TODO: Check returned version */
 	if (vstor_packet->operation != VSTOR_OPERATION_COMPLETEIO ||
 	    vstor_packet->status != 0) {
 		goto cleanup;
 	}
 
 	max_subch = vstor_packet->u.chan_props.max_channel_cnt;
 	if (hv_storvsc_chan_cnt > 0 && hv_storvsc_chan_cnt < (max_subch + 1))
 		max_subch = hv_storvsc_chan_cnt - 1;
 
 	/* multi-channels feature is supported by WIN8 and above version */
 	version = VMBUS_GET_VERSION(device_get_parent(sc->hs_dev), sc->hs_dev);
 	if (version != VMBUS_VERSION_WIN7 && version != VMBUS_VERSION_WS2008 &&
 	    (vstor_packet->u.chan_props.flags &
 	     HV_STORAGE_SUPPORTS_MULTI_CHANNEL)) {
 		support_multichannel = TRUE;
 	}
 	if (bootverbose) {
 		device_printf(sc->hs_dev, "max chans %d%s\n", max_subch + 1,
 		    support_multichannel ? ", multi-chan capable" : "");
 	}
 
 	memset(vstor_packet, 0, sizeof(struct vstor_packet));
 	vstor_packet->operation = VSTOR_OPERATION_ENDINITIALIZATION;
 	vstor_packet->flags = REQUEST_COMPLETION_FLAG;
 
 	ret = vmbus_chan_send(sc->hs_chan,
 	    VMBUS_CHANPKT_TYPE_INBAND, VMBUS_CHANPKT_FLAG_RC,
 	    vstor_packet, VSTOR_PKT_SIZE, (uint64_t)(uintptr_t)request);
 
 	if (ret != 0) {
 		goto cleanup;
 	}
 
 	sema_wait(&request->synch_sema);
 
 	if (vstor_packet->operation != VSTOR_OPERATION_COMPLETEIO ||
 	    vstor_packet->status != 0)
 		goto cleanup;
 
 	/*
 	 * If multi-channel is supported, send multichannel create
 	 * request to host.
 	 */
 	if (support_multichannel && max_subch > 0)
 		storvsc_send_multichannel_request(sc, max_subch);
 cleanup:
 	sema_destroy(&request->synch_sema);
 	return (ret);
 }
 
 /**
  * @brief Open channel connection to paraent partition StorVSP driver
  *
  * Open and initialize channel connection to parent partition StorVSP driver.
  *
  * @param pointer to a Hyper-V device
  * @returns 0 on success, non-zero error on failure
  */
 static int
 hv_storvsc_connect_vsp(struct storvsc_softc *sc)
 {	
 	int ret = 0;
 	struct vmstor_chan_props props;
 
 	memset(&props, 0, sizeof(struct vmstor_chan_props));
 
 	/*
 	 * Open the channel
 	 */
 	vmbus_chan_cpu_rr(sc->hs_chan);
 	ret = vmbus_chan_open(
 		sc->hs_chan,
 		sc->hs_drv_props->drv_ringbuffer_size,
 		sc->hs_drv_props->drv_ringbuffer_size,
 		(void *)&props,
 		sizeof(struct vmstor_chan_props),
 		hv_storvsc_on_channel_callback, sc);
 
 	if (ret != 0) {
 		return ret;
 	}
 
 	ret = hv_storvsc_channel_init(sc);
 	return (ret);
 }
 
 #if HVS_HOST_RESET
 static int
 hv_storvsc_host_reset(struct storvsc_softc *sc)
 {
 	int ret = 0;
 
 	struct hv_storvsc_request *request;
 	struct vstor_packet *vstor_packet;
 
 	request = &sc->hs_reset_req;
 	request->softc = sc;
 	vstor_packet = &request->vstor_packet;
 
 	sema_init(&request->synch_sema, 0, "stor synch sema");
 
 	vstor_packet->operation = VSTOR_OPERATION_RESETBUS;
 	vstor_packet->flags = REQUEST_COMPLETION_FLAG;
 
 	ret = vmbus_chan_send(dev->channel,
 	    VMBUS_CHANPKT_TYPE_INBAND, VMBUS_CHANPKT_FLAG_RC,
 	    vstor_packet, VSTOR_PKT_SIZE,
 	    (uint64_t)(uintptr_t)&sc->hs_reset_req);
 
 	if (ret != 0) {
 		goto cleanup;
 	}
 
 	sema_wait(&request->synch_sema);
 
 	/*
 	 * At this point, all outstanding requests in the adapter
 	 * should have been flushed out and return to us
 	 */
 
 cleanup:
 	sema_destroy(&request->synch_sema);
 	return (ret);
 }
 #endif /* HVS_HOST_RESET */
 
 /**
  * @brief Function to initiate an I/O request
  *
  * @param device Hyper-V device pointer
  * @param request pointer to a request structure
  * @returns 0 on success, non-zero error on failure
  */
 static int
 hv_storvsc_io_request(struct storvsc_softc *sc,
 					  struct hv_storvsc_request *request)
 {
 	struct vstor_packet *vstor_packet = &request->vstor_packet;
 	struct vmbus_channel* outgoing_channel = NULL;
 	int ret = 0, ch_sel;
 
 	vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
 
 	vstor_packet->u.vm_srb.length =
 	    sizeof(struct vmscsi_req) - vmscsi_size_delta;
 	
 	vstor_packet->u.vm_srb.sense_info_len = sense_buffer_size;
 
 	vstor_packet->u.vm_srb.transfer_len =
 	    request->prp_list.gpa_range.gpa_len;
 
 	vstor_packet->operation = VSTOR_OPERATION_EXECUTESRB;
 
 	ch_sel = (vstor_packet->u.vm_srb.lun + curcpu) % sc->hs_nchan;
 	/*
 	 * If we are panic'ing, then we are dumping core. Since storvsc_polls
 	 * always uses sc->hs_chan, then we must send to that channel or a poll
 	 * timeout will occur.
 	 */
-	if (panicstr) {
+	if (KERNEL_PANICKED()) {
 		outgoing_channel = sc->hs_chan;
 	} else {
 		outgoing_channel = sc->hs_sel_chan[ch_sel];
 	}
 
 	mtx_unlock(&request->softc->hs_lock);
 	if (request->prp_list.gpa_range.gpa_len) {
 		ret = vmbus_chan_send_prplist(outgoing_channel,
 		    &request->prp_list.gpa_range, request->prp_cnt,
 		    vstor_packet, VSTOR_PKT_SIZE, (uint64_t)(uintptr_t)request);
 	} else {
 		ret = vmbus_chan_send(outgoing_channel,
 		    VMBUS_CHANPKT_TYPE_INBAND, VMBUS_CHANPKT_FLAG_RC,
 		    vstor_packet, VSTOR_PKT_SIZE, (uint64_t)(uintptr_t)request);
 	}
 	/* statistic for successful request sending on each channel */
 	if (!ret) {
 		sc->sysctl_data.chan_send_cnt[ch_sel]++;
 	}
 	mtx_lock(&request->softc->hs_lock);
 
 	if (ret != 0) {
 		printf("Unable to send packet %p ret %d", vstor_packet, ret);
 	} else {
 		atomic_add_int(&sc->hs_num_out_reqs, 1);
 	}
 
 	return (ret);
 }
 
 
 /**
  * Process IO_COMPLETION_OPERATION and ready
  * the result to be completed for upper layer
  * processing by the CAM layer.
  */
 static void
 hv_storvsc_on_iocompletion(struct storvsc_softc *sc,
 			   struct vstor_packet *vstor_packet,
 			   struct hv_storvsc_request *request)
 {
 	struct vmscsi_req *vm_srb;
 
 	vm_srb = &vstor_packet->u.vm_srb;
 
 	/*
 	 * Copy some fields of the host's response into the request structure,
 	 * because the fields will be used later in storvsc_io_done().
 	 */
 	request->vstor_packet.u.vm_srb.scsi_status = vm_srb->scsi_status;
 	request->vstor_packet.u.vm_srb.srb_status = vm_srb->srb_status;
 	request->vstor_packet.u.vm_srb.transfer_len = vm_srb->transfer_len;
 
 	if (((vm_srb->scsi_status & 0xFF) == SCSI_STATUS_CHECK_COND) &&
 			(vm_srb->srb_status & SRB_STATUS_AUTOSENSE_VALID)) {
 		/* Autosense data available */
 
 		KASSERT(vm_srb->sense_info_len <= request->sense_info_len,
 				("vm_srb->sense_info_len <= "
 				 "request->sense_info_len"));
 
 		memcpy(request->sense_data, vm_srb->u.sense_data,
 			vm_srb->sense_info_len);
 
 		request->sense_info_len = vm_srb->sense_info_len;
 	}
 
 	/* Complete request by passing to the CAM layer */
 	storvsc_io_done(request);
 	atomic_subtract_int(&sc->hs_num_out_reqs, 1);
 	if (sc->hs_drain_notify && (sc->hs_num_out_reqs == 0)) {
 		sema_post(&sc->hs_drain_sema);
 	}
 }
 
 static void
 hv_storvsc_rescan_target(struct storvsc_softc *sc)
 {
 	path_id_t pathid;
 	target_id_t targetid;
 	union ccb *ccb;
 
 	pathid = cam_sim_path(sc->hs_sim);
 	targetid = CAM_TARGET_WILDCARD;
 
 	/*
 	 * Allocate a CCB and schedule a rescan.
 	 */
 	ccb = xpt_alloc_ccb_nowait();
 	if (ccb == NULL) {
 		printf("unable to alloc CCB for rescan\n");
 		return;
 	}
 
 	if (xpt_create_path(&ccb->ccb_h.path, NULL, pathid, targetid,
 	    CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
 		printf("unable to create path for rescan, pathid: %u,"
 		    "targetid: %u\n", pathid, targetid);
 		xpt_free_ccb(ccb);
 		return;
 	}
 
 	if (targetid == CAM_TARGET_WILDCARD)
 		ccb->ccb_h.func_code = XPT_SCAN_BUS;
 	else
 		ccb->ccb_h.func_code = XPT_SCAN_TGT;
 
 	xpt_rescan(ccb);
 }
 
 static void
 hv_storvsc_on_channel_callback(struct vmbus_channel *channel, void *xsc)
 {
 	int ret = 0;
 	struct storvsc_softc *sc = xsc;
 	uint32_t bytes_recvd;
 	uint64_t request_id;
 	uint8_t packet[roundup2(sizeof(struct vstor_packet), 8)];
 	struct hv_storvsc_request *request;
 	struct vstor_packet *vstor_packet;
 
 	bytes_recvd = roundup2(VSTOR_PKT_SIZE, 8);
 	ret = vmbus_chan_recv(channel, packet, &bytes_recvd, &request_id);
 	KASSERT(ret != ENOBUFS, ("storvsc recvbuf is not large enough"));
 	/* XXX check bytes_recvd to make sure that it contains enough data */
 
 	while ((ret == 0) && (bytes_recvd > 0)) {
 		request = (struct hv_storvsc_request *)(uintptr_t)request_id;
 
 		if ((request == &sc->hs_init_req) ||
 			(request == &sc->hs_reset_req)) {
 			memcpy(&request->vstor_packet, packet,
 				   sizeof(struct vstor_packet));
 			sema_post(&request->synch_sema);
 		} else {
 			vstor_packet = (struct vstor_packet *)packet;
 			switch(vstor_packet->operation) {
 			case VSTOR_OPERATION_COMPLETEIO:
 				if (request == NULL)
 					panic("VMBUS: storvsc received a "
 					    "packet with NULL request id in "
 					    "COMPLETEIO operation.");
 
 				hv_storvsc_on_iocompletion(sc,
 							vstor_packet, request);
 				break;
 			case VSTOR_OPERATION_REMOVEDEVICE:
 				printf("VMBUS: storvsc operation %d not "
 				    "implemented.\n", vstor_packet->operation);
 				/* TODO: implement */
 				break;
 			case VSTOR_OPERATION_ENUMERATE_BUS:
 				hv_storvsc_rescan_target(sc);
 				break;
 			default:
 				break;
 			}			
 		}
 
 		bytes_recvd = roundup2(VSTOR_PKT_SIZE, 8),
 		ret = vmbus_chan_recv(channel, packet, &bytes_recvd,
 		    &request_id);
 		KASSERT(ret != ENOBUFS,
 		    ("storvsc recvbuf is not large enough"));
 		/*
 		 * XXX check bytes_recvd to make sure that it contains
 		 * enough data
 		 */
 	}
 }
 
 /**
  * @brief StorVSC probe function
  *
  * Device probe function.  Returns 0 if the input device is a StorVSC
  * device.  Otherwise, a ENXIO is returned.  If the input device is
  * for BlkVSC (paravirtual IDE) device and this support is disabled in
  * favor of the emulated ATA/IDE device, return ENXIO.
  *
  * @param a device
  * @returns 0 on success, ENXIO if not a matcing StorVSC device
  */
 static int
 storvsc_probe(device_t dev)
 {
 	int ret	= ENXIO;
 	
 	switch (storvsc_get_storage_type(dev)) {
 	case DRIVER_BLKVSC:
 		if(bootverbose)
 			device_printf(dev,
 			    "Enlightened ATA/IDE detected\n");
 		device_set_desc(dev, g_drv_props_table[DRIVER_BLKVSC].drv_desc);
 		ret = BUS_PROBE_DEFAULT;
 		break;
 	case DRIVER_STORVSC:
 		if(bootverbose)
 			device_printf(dev, "Enlightened SCSI device detected\n");
 		device_set_desc(dev, g_drv_props_table[DRIVER_STORVSC].drv_desc);
 		ret = BUS_PROBE_DEFAULT;
 		break;
 	default:
 		ret = ENXIO;
 	}
 	return (ret);
 }
 
 static void
 storvsc_create_chan_sel(struct storvsc_softc *sc)
 {
 	struct vmbus_channel **subch;
 	int i, nsubch;
 
 	sc->hs_sel_chan[0] = sc->hs_chan;
 	nsubch = sc->hs_nchan - 1;
 	if (nsubch == 0)
 		return;
 
 	subch = vmbus_subchan_get(sc->hs_chan, nsubch);
 	for (i = 0; i < nsubch; i++)
 		sc->hs_sel_chan[i + 1] = subch[i];
 	vmbus_subchan_rel(subch, nsubch);
 }
 
 static int
 storvsc_init_requests(device_t dev)
 {
 	struct storvsc_softc *sc = device_get_softc(dev);
 	struct hv_storvsc_request *reqp;
 	int error, i;
 
 	LIST_INIT(&sc->hs_free_list);
 
 	error = bus_dma_tag_create(
 		bus_get_dma_tag(dev),		/* parent */
 		1,				/* alignment */
 		PAGE_SIZE,			/* boundary */
 		BUS_SPACE_MAXADDR,		/* lowaddr */
 		BUS_SPACE_MAXADDR,		/* highaddr */
 		NULL, NULL,			/* filter, filterarg */
 		STORVSC_DATA_SIZE_MAX,		/* maxsize */
 		STORVSC_DATA_SEGCNT_MAX,	/* nsegments */
 		STORVSC_DATA_SEGSZ_MAX,		/* maxsegsize */
 		0,				/* flags */
 		NULL,				/* lockfunc */
 		NULL,				/* lockfuncarg */
 		&sc->storvsc_req_dtag);
 	if (error) {
 		device_printf(dev, "failed to create storvsc dma tag\n");
 		return (error);
 	}
 
 	for (i = 0; i < sc->hs_drv_props->drv_max_ios_per_target; ++i) {
 		reqp = malloc(sizeof(struct hv_storvsc_request),
 				 M_DEVBUF, M_WAITOK|M_ZERO);
 		reqp->softc = sc;
 		error = bus_dmamap_create(sc->storvsc_req_dtag, 0,
 				&reqp->data_dmap);
 		if (error) {
 			device_printf(dev, "failed to allocate storvsc "
 			    "data dmamap\n");
 			goto cleanup;
 		}
 		LIST_INSERT_HEAD(&sc->hs_free_list, reqp, link);
 	}
 	return (0);
 
 cleanup:
 	while ((reqp = LIST_FIRST(&sc->hs_free_list)) != NULL) {
 		LIST_REMOVE(reqp, link);
 		bus_dmamap_destroy(sc->storvsc_req_dtag, reqp->data_dmap);
 		free(reqp, M_DEVBUF);
 	}
 	return (error);
 }
 
 static void
 storvsc_sysctl(device_t dev)
 {
 	struct sysctl_oid_list *child;
 	struct sysctl_ctx_list *ctx;
 	struct sysctl_oid *ch_tree, *chid_tree;
 	struct storvsc_softc *sc;
 	char name[16];
 	int i;
 
 	sc = device_get_softc(dev);
 	ctx = device_get_sysctl_ctx(dev);
 	child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
 
 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "data_bio_cnt",
 		CTLFLAG_RW | CTLFLAG_STATS, &sc->sysctl_data.data_bio_cnt,
 		"# of bio data block");
 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "data_vaddr_cnt",
 		CTLFLAG_RW | CTLFLAG_STATS, &sc->sysctl_data.data_vaddr_cnt,
 		"# of vaddr data block");
 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "data_sg_cnt",
 		CTLFLAG_RW | CTLFLAG_STATS, &sc->sysctl_data.data_sg_cnt,
 		"# of sg data block");
 
 	/* dev.storvsc.UNIT.channel */
 	ch_tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "channel",
 		CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
 	if (ch_tree == NULL)
 		return;
 
 	for (i = 0; i < sc->hs_nchan; i++) {
 		uint32_t ch_id;
 
 		ch_id = vmbus_chan_id(sc->hs_sel_chan[i]);
 		snprintf(name, sizeof(name), "%d", ch_id);
 		/* dev.storvsc.UNIT.channel.CHID */
 		chid_tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(ch_tree),
 			OID_AUTO, name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
 		if (chid_tree == NULL)
 			return;
 		/* dev.storvsc.UNIT.channel.CHID.send_req */
 		SYSCTL_ADD_ULONG(ctx, SYSCTL_CHILDREN(chid_tree), OID_AUTO,
 			"send_req", CTLFLAG_RD, &sc->sysctl_data.chan_send_cnt[i],
 			"# of request sending from this channel");
 	}
 }
 
 /**
  * @brief StorVSC attach function
  *
  * Function responsible for allocating per-device structures,
  * setting up CAM interfaces and scanning for available LUNs to
  * be used for SCSI device peripherals.
  *
  * @param a device
  * @returns 0 on success or an error on failure
  */
 static int
 storvsc_attach(device_t dev)
 {
 	enum hv_storage_type stor_type;
 	struct storvsc_softc *sc;
 	struct cam_devq *devq;
 	int ret, i, j;
 	struct hv_storvsc_request *reqp;
 	struct root_hold_token *root_mount_token = NULL;
 	struct hv_sgl_node *sgl_node = NULL;
 	void *tmp_buff = NULL;
 
 	/*
 	 * We need to serialize storvsc attach calls.
 	 */
 	root_mount_token = root_mount_hold("storvsc");
 
 	sc = device_get_softc(dev);
 	sc->hs_nchan = 1;
 	sc->hs_chan = vmbus_get_channel(dev);
 
 	stor_type = storvsc_get_storage_type(dev);
 
 	if (stor_type == DRIVER_UNKNOWN) {
 		ret = ENODEV;
 		goto cleanup;
 	}
 
 	/* fill in driver specific properties */
 	sc->hs_drv_props = &g_drv_props_table[stor_type];
 	sc->hs_drv_props->drv_ringbuffer_size = hv_storvsc_ringbuffer_size;
 	sc->hs_drv_props->drv_max_ios_per_target =
 		MIN(STORVSC_MAX_IO, hv_storvsc_max_io);
 	if (bootverbose) {
 		printf("storvsc ringbuffer size: %d, max_io: %d\n",
 			sc->hs_drv_props->drv_ringbuffer_size,
 			sc->hs_drv_props->drv_max_ios_per_target);
 	}
 	/* fill in device specific properties */
 	sc->hs_unit	= device_get_unit(dev);
 	sc->hs_dev	= dev;
 
 	mtx_init(&sc->hs_lock, "hvslck", NULL, MTX_DEF);
 
 	ret = storvsc_init_requests(dev);
 	if (ret != 0)
 		goto cleanup;
 
 	/* create sg-list page pool */
 	if (FALSE == g_hv_sgl_page_pool.is_init) {
 		g_hv_sgl_page_pool.is_init = TRUE;
 		LIST_INIT(&g_hv_sgl_page_pool.in_use_sgl_list);
 		LIST_INIT(&g_hv_sgl_page_pool.free_sgl_list);
 
 		/*
 		 * Pre-create SG list, each SG list with
 		 * STORVSC_DATA_SEGCNT_MAX segments, each
 		 * segment has one page buffer
 		 */
 		for (i = 0; i < sc->hs_drv_props->drv_max_ios_per_target; i++) {
 	        	sgl_node = malloc(sizeof(struct hv_sgl_node),
 			    M_DEVBUF, M_WAITOK|M_ZERO);
 
 			sgl_node->sgl_data = malloc(sizeof(struct hv_sglist),
 			    M_DEVBUF, M_WAITOK|M_ZERO);
 
 			for (j = 0; j < STORVSC_DATA_SEGCNT_MAX; j++) {
 				tmp_buff = malloc(PAGE_SIZE,
 				    M_DEVBUF, M_WAITOK|M_ZERO);
 
 				sgl_node->sgl_data->sg_iov[j].iov_base =
 				    tmp_buff;
 			}
 
 			LIST_INSERT_HEAD(&g_hv_sgl_page_pool.free_sgl_list,
 			    sgl_node, link);
 		}
 	}
 
 	sc->hs_destroy = FALSE;
 	sc->hs_drain_notify = FALSE;
 	sema_init(&sc->hs_drain_sema, 0, "Store Drain Sema");
 
 	ret = hv_storvsc_connect_vsp(sc);
 	if (ret != 0) {
 		goto cleanup;
 	}
 
 	/* Construct cpu to channel mapping */
 	storvsc_create_chan_sel(sc);
 
 	/*
 	 * Create the device queue.
 	 * Hyper-V maps each target to one SCSI HBA
 	 */
 	devq = cam_simq_alloc(sc->hs_drv_props->drv_max_ios_per_target);
 	if (devq == NULL) {
 		device_printf(dev, "Failed to alloc device queue\n");
 		ret = ENOMEM;
 		goto cleanup;
 	}
 
 	sc->hs_sim = cam_sim_alloc(storvsc_action,
 				storvsc_poll,
 				sc->hs_drv_props->drv_name,
 				sc,
 				sc->hs_unit,
 				&sc->hs_lock, 1,
 				sc->hs_drv_props->drv_max_ios_per_target,
 				devq);
 
 	if (sc->hs_sim == NULL) {
 		device_printf(dev, "Failed to alloc sim\n");
 		cam_simq_free(devq);
 		ret = ENOMEM;
 		goto cleanup;
 	}
 
 	mtx_lock(&sc->hs_lock);
 	/* bus_id is set to 0, need to get it from VMBUS channel query? */
 	if (xpt_bus_register(sc->hs_sim, dev, 0) != CAM_SUCCESS) {
 		cam_sim_free(sc->hs_sim, /*free_devq*/TRUE);
 		mtx_unlock(&sc->hs_lock);
 		device_printf(dev, "Unable to register SCSI bus\n");
 		ret = ENXIO;
 		goto cleanup;
 	}
 
 	if (xpt_create_path(&sc->hs_path, /*periph*/NULL,
 		 cam_sim_path(sc->hs_sim),
 		CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
 		xpt_bus_deregister(cam_sim_path(sc->hs_sim));
 		cam_sim_free(sc->hs_sim, /*free_devq*/TRUE);
 		mtx_unlock(&sc->hs_lock);
 		device_printf(dev, "Unable to create path\n");
 		ret = ENXIO;
 		goto cleanup;
 	}
 
 	mtx_unlock(&sc->hs_lock);
 
 	storvsc_sysctl(dev);
 
 	root_mount_rel(root_mount_token);
 	return (0);
 
 
 cleanup:
 	root_mount_rel(root_mount_token);
 	while (!LIST_EMPTY(&sc->hs_free_list)) {
 		reqp = LIST_FIRST(&sc->hs_free_list);
 		LIST_REMOVE(reqp, link);
 		bus_dmamap_destroy(sc->storvsc_req_dtag, reqp->data_dmap);
 		free(reqp, M_DEVBUF);
 	}
 
 	while (!LIST_EMPTY(&g_hv_sgl_page_pool.free_sgl_list)) {
 		sgl_node = LIST_FIRST(&g_hv_sgl_page_pool.free_sgl_list);
 		LIST_REMOVE(sgl_node, link);
 		for (j = 0; j < STORVSC_DATA_SEGCNT_MAX; j++) {
 			free(sgl_node->sgl_data->sg_iov[j].iov_base, M_DEVBUF);
 		}
 		free(sgl_node->sgl_data, M_DEVBUF);
 		free(sgl_node, M_DEVBUF);
 	}
 
 	return (ret);
 }
 
 /**
  * @brief StorVSC device detach function
  *
  * This function is responsible for safely detaching a
  * StorVSC device.  This includes waiting for inbound responses
  * to complete and freeing associated per-device structures.
  *
  * @param dev a device
  * returns 0 on success
  */
 static int
 storvsc_detach(device_t dev)
 {
 	struct storvsc_softc *sc = device_get_softc(dev);
 	struct hv_storvsc_request *reqp = NULL;
 	struct hv_sgl_node *sgl_node = NULL;
 	int j = 0;
 
 	sc->hs_destroy = TRUE;
 
 	/*
 	 * At this point, all outbound traffic should be disabled. We
 	 * only allow inbound traffic (responses) to proceed so that
 	 * outstanding requests can be completed.
 	 */
 
 	sc->hs_drain_notify = TRUE;
 	sema_wait(&sc->hs_drain_sema);
 	sc->hs_drain_notify = FALSE;
 
 	/*
 	 * Since we have already drained, we don't need to busy wait.
 	 * The call to close the channel will reset the callback
 	 * under the protection of the incoming channel lock.
 	 */
 
 	vmbus_chan_close(sc->hs_chan);
 
 	mtx_lock(&sc->hs_lock);
 	while (!LIST_EMPTY(&sc->hs_free_list)) {
 		reqp = LIST_FIRST(&sc->hs_free_list);
 		LIST_REMOVE(reqp, link);
 		bus_dmamap_destroy(sc->storvsc_req_dtag, reqp->data_dmap);
 		free(reqp, M_DEVBUF);
 	}
 	mtx_unlock(&sc->hs_lock);
 
 	while (!LIST_EMPTY(&g_hv_sgl_page_pool.free_sgl_list)) {
 		sgl_node = LIST_FIRST(&g_hv_sgl_page_pool.free_sgl_list);
 		LIST_REMOVE(sgl_node, link);
 		for (j = 0; j < STORVSC_DATA_SEGCNT_MAX; j++){
 			free(sgl_node->sgl_data->sg_iov[j].iov_base, M_DEVBUF);
 		}
 		free(sgl_node->sgl_data, M_DEVBUF);
 		free(sgl_node, M_DEVBUF);
 	}
 	
 	return (0);
 }
 
 #if HVS_TIMEOUT_TEST
 /**
  * @brief unit test for timed out operations
  *
  * This function provides unit testing capability to simulate
  * timed out operations.  Recompilation with HV_TIMEOUT_TEST=1
  * is required.
  *
  * @param reqp pointer to a request structure
  * @param opcode SCSI operation being performed
  * @param wait if 1, wait for I/O to complete
  */
 static void
 storvsc_timeout_test(struct hv_storvsc_request *reqp,
 		uint8_t opcode, int wait)
 {
 	int ret;
 	union ccb *ccb = reqp->ccb;
 	struct storvsc_softc *sc = reqp->softc;
 
 	if (reqp->vstor_packet.vm_srb.cdb[0] != opcode) {
 		return;
 	}
 
 	if (wait) {
 		mtx_lock(&reqp->event.mtx);
 	}
 	ret = hv_storvsc_io_request(sc, reqp);
 	if (ret != 0) {
 		if (wait) {
 			mtx_unlock(&reqp->event.mtx);
 		}
 		printf("%s: io_request failed with %d.\n",
 				__func__, ret);
 		ccb->ccb_h.status = CAM_PROVIDE_FAIL;
 		mtx_lock(&sc->hs_lock);
 		storvsc_free_request(sc, reqp);
 		xpt_done(ccb);
 		mtx_unlock(&sc->hs_lock);
 		return;
 	}
 
 	if (wait) {
 		xpt_print(ccb->ccb_h.path,
 				"%u: %s: waiting for IO return.\n",
 				ticks, __func__);
 		ret = cv_timedwait(&reqp->event.cv, &reqp->event.mtx, 60*hz);
 		mtx_unlock(&reqp->event.mtx);
 		xpt_print(ccb->ccb_h.path, "%u: %s: %s.\n",
 				ticks, __func__, (ret == 0)?
 				"IO return detected" :
 				"IO return not detected");
 		/*
 		 * Now both the timer handler and io done are running
 		 * simultaneously. We want to confirm the io done always
 		 * finishes after the timer handler exits. So reqp used by
 		 * timer handler is not freed or stale. Do busy loop for
 		 * another 1/10 second to make sure io done does
 		 * wait for the timer handler to complete.
 		 */
 		DELAY(100*1000);
 		mtx_lock(&sc->hs_lock);
 		xpt_print(ccb->ccb_h.path,
 				"%u: %s: finishing, queue frozen %d, "
 				"ccb status 0x%x scsi_status 0x%x.\n",
 				ticks, __func__, sc->hs_frozen,
 				ccb->ccb_h.status,
 				ccb->csio.scsi_status);
 		mtx_unlock(&sc->hs_lock);
 	}
 }
 #endif /* HVS_TIMEOUT_TEST */
 
 #ifdef notyet
 /**
  * @brief timeout handler for requests
  *
  * This function is called as a result of a callout expiring.
  *
  * @param arg pointer to a request
  */
 static void
 storvsc_timeout(void *arg)
 {
 	struct hv_storvsc_request *reqp = arg;
 	struct storvsc_softc *sc = reqp->softc;
 	union ccb *ccb = reqp->ccb;
 
 	if (reqp->retries == 0) {
 		mtx_lock(&sc->hs_lock);
 		xpt_print(ccb->ccb_h.path,
 		    "%u: IO timed out (req=0x%p), wait for another %u secs.\n",
 		    ticks, reqp, ccb->ccb_h.timeout / 1000);
 		cam_error_print(ccb, CAM_ESF_ALL, CAM_EPF_ALL);
 		mtx_unlock(&sc->hs_lock);
 
 		reqp->retries++;
 		callout_reset_sbt(&reqp->callout, SBT_1MS * ccb->ccb_h.timeout,
 		    0, storvsc_timeout, reqp, 0);
 #if HVS_TIMEOUT_TEST
 		storvsc_timeout_test(reqp, SEND_DIAGNOSTIC, 0);
 #endif
 		return;
 	}
 
 	mtx_lock(&sc->hs_lock);
 	xpt_print(ccb->ccb_h.path,
 		"%u: IO (reqp = 0x%p) did not return for %u seconds, %s.\n",
 		ticks, reqp, ccb->ccb_h.timeout * (reqp->retries+1) / 1000,
 		(sc->hs_frozen == 0)?
 		"freezing the queue" : "the queue is already frozen");
 	if (sc->hs_frozen == 0) {
 		sc->hs_frozen = 1;
 		xpt_freeze_simq(xpt_path_sim(ccb->ccb_h.path), 1);
 	}
 	mtx_unlock(&sc->hs_lock);
 	
 #if HVS_TIMEOUT_TEST
 	storvsc_timeout_test(reqp, MODE_SELECT_10, 1);
 #endif
 }
 #endif
 
 /**
  * @brief StorVSC device poll function
  *
  * This function is responsible for servicing requests when
  * interrupts are disabled (i.e when we are dumping core.)
  *
  * @param sim a pointer to a CAM SCSI interface module
  */
 static void
 storvsc_poll(struct cam_sim *sim)
 {
 	struct storvsc_softc *sc = cam_sim_softc(sim);
 
 	mtx_assert(&sc->hs_lock, MA_OWNED);
 	mtx_unlock(&sc->hs_lock);
 	hv_storvsc_on_channel_callback(sc->hs_chan, sc);
 	mtx_lock(&sc->hs_lock);
 }
 
 /**
  * @brief StorVSC device action function
  *
  * This function is responsible for handling SCSI operations which
  * are passed from the CAM layer.  The requests are in the form of
  * CAM control blocks which indicate the action being performed.
  * Not all actions require converting the request to a VSCSI protocol
  * message - these actions can be responded to by this driver.
  * Requests which are destined for a backend storage device are converted
  * to a VSCSI protocol message and sent on the channel connection associated
  * with this device.
  *
  * @param sim pointer to a CAM SCSI interface module
  * @param ccb pointer to a CAM control block
  */
 static void
 storvsc_action(struct cam_sim *sim, union ccb *ccb)
 {
 	struct storvsc_softc *sc = cam_sim_softc(sim);
 	int res;
 
 	mtx_assert(&sc->hs_lock, MA_OWNED);
 	switch (ccb->ccb_h.func_code) {
 	case XPT_PATH_INQ: {
 		struct ccb_pathinq *cpi = &ccb->cpi;
 
 		cpi->version_num = 1;
 		cpi->hba_inquiry = PI_TAG_ABLE|PI_SDTR_ABLE;
 		cpi->target_sprt = 0;
 		cpi->hba_misc = PIM_NOBUSRESET;
 		if (hv_storvsc_use_pim_unmapped)
 			cpi->hba_misc |= PIM_UNMAPPED;
 		cpi->maxio = STORVSC_DATA_SIZE_MAX;
 		cpi->hba_eng_cnt = 0;
 		cpi->max_target = STORVSC_MAX_TARGETS;
 		cpi->max_lun = sc->hs_drv_props->drv_max_luns_per_target;
 		cpi->initiator_id = cpi->max_target;
 		cpi->bus_id = cam_sim_bus(sim);
 		cpi->base_transfer_speed = 300000;
 		cpi->transport = XPORT_SAS;
 		cpi->transport_version = 0;
 		cpi->protocol = PROTO_SCSI;
 		cpi->protocol_version = SCSI_REV_SPC2;
 		strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
 		strlcpy(cpi->hba_vid, sc->hs_drv_props->drv_name, HBA_IDLEN);
 		strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
 		cpi->unit_number = cam_sim_unit(sim);
 
 		ccb->ccb_h.status = CAM_REQ_CMP;
 		xpt_done(ccb);
 		return;
 	}
 	case XPT_GET_TRAN_SETTINGS: {
 		struct  ccb_trans_settings *cts = &ccb->cts;
 
 		cts->transport = XPORT_SAS;
 		cts->transport_version = 0;
 		cts->protocol = PROTO_SCSI;
 		cts->protocol_version = SCSI_REV_SPC2;
 
 		/* enable tag queuing and disconnected mode */
 		cts->proto_specific.valid = CTS_SCSI_VALID_TQ;
 		cts->proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
 		cts->proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
 		cts->xport_specific.valid = CTS_SPI_VALID_DISC;
 		cts->xport_specific.spi.flags = CTS_SPI_FLAGS_DISC_ENB;
 			
 		ccb->ccb_h.status = CAM_REQ_CMP;
 		xpt_done(ccb);
 		return;
 	}
 	case XPT_SET_TRAN_SETTINGS:	{
 		ccb->ccb_h.status = CAM_REQ_CMP;
 		xpt_done(ccb);
 		return;
 	}
 	case XPT_CALC_GEOMETRY:{
 		cam_calc_geometry(&ccb->ccg, 1);
 		xpt_done(ccb);
 		return;
 	}
 	case  XPT_RESET_BUS:
 	case  XPT_RESET_DEV:{
 #if HVS_HOST_RESET
 		if ((res = hv_storvsc_host_reset(sc)) != 0) {
 			xpt_print(ccb->ccb_h.path,
 				"hv_storvsc_host_reset failed with %d\n", res);
 			ccb->ccb_h.status = CAM_PROVIDE_FAIL;
 			xpt_done(ccb);
 			return;
 		}
 		ccb->ccb_h.status = CAM_REQ_CMP;
 		xpt_done(ccb);
 		return;
 #else
 		xpt_print(ccb->ccb_h.path,
 				  "%s reset not supported.\n",
 				  (ccb->ccb_h.func_code == XPT_RESET_BUS)?
 				  "bus" : "dev");
 		ccb->ccb_h.status = CAM_REQ_INVALID;
 		xpt_done(ccb);
 		return;
 #endif	/* HVS_HOST_RESET */
 	}
 	case XPT_SCSI_IO:
 	case XPT_IMMED_NOTIFY: {
 		struct hv_storvsc_request *reqp = NULL;
 		bus_dmamap_t dmap_saved;
 
 		if (ccb->csio.cdb_len == 0) {
 			panic("cdl_len is 0\n");
 		}
 
 		if (LIST_EMPTY(&sc->hs_free_list)) {
 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
 			if (sc->hs_frozen == 0) {
 				sc->hs_frozen = 1;
 				xpt_freeze_simq(sim, /* count*/1);
 			}
 			xpt_done(ccb);
 			return;
 		}
 
 		reqp = LIST_FIRST(&sc->hs_free_list);
 		LIST_REMOVE(reqp, link);
 
 		/* Save the data_dmap before reset request */
 		dmap_saved = reqp->data_dmap;
 
 		/* XXX this is ugly */
 		bzero(reqp, sizeof(struct hv_storvsc_request));
 
 		/* Restore necessary bits */
 		reqp->data_dmap = dmap_saved;
 		reqp->softc = sc;
 		
 		ccb->ccb_h.status |= CAM_SIM_QUEUED;
 		if ((res = create_storvsc_request(ccb, reqp)) != 0) {
 			ccb->ccb_h.status = CAM_REQ_INVALID;
 			xpt_done(ccb);
 			return;
 		}
 
 #ifdef notyet
 		if (ccb->ccb_h.timeout != CAM_TIME_INFINITY) {
 			callout_init(&reqp->callout, 1);
 			callout_reset_sbt(&reqp->callout,
 			    SBT_1MS * ccb->ccb_h.timeout, 0,
 			    storvsc_timeout, reqp, 0);
 #if HVS_TIMEOUT_TEST
 			cv_init(&reqp->event.cv, "storvsc timeout cv");
 			mtx_init(&reqp->event.mtx, "storvsc timeout mutex",
 					NULL, MTX_DEF);
 			switch (reqp->vstor_packet.vm_srb.cdb[0]) {
 				case MODE_SELECT_10:
 				case SEND_DIAGNOSTIC:
 					/* To have timer send the request. */
 					return;
 				default:
 					break;
 			}
 #endif /* HVS_TIMEOUT_TEST */
 		}
 #endif
 
 		if ((res = hv_storvsc_io_request(sc, reqp)) != 0) {
 			xpt_print(ccb->ccb_h.path,
 				"hv_storvsc_io_request failed with %d\n", res);
 			ccb->ccb_h.status = CAM_PROVIDE_FAIL;
 			storvsc_free_request(sc, reqp);
 			xpt_done(ccb);
 			return;
 		}
 		return;
 	}
 
 	default:
 		ccb->ccb_h.status = CAM_REQ_INVALID;
 		xpt_done(ccb);
 		return;
 	}
 }
 
 /**
  * @brief destroy bounce buffer
  *
  * This function is responsible for destroy a Scatter/Gather list
  * that create by storvsc_create_bounce_buffer()
  *
  * @param sgl- the Scatter/Gather need be destroy
  * @param sg_count- page count of the SG list.
  *
  */
 static void
 storvsc_destroy_bounce_buffer(struct hv_sglist *sgl)
 {
 	struct hv_sgl_node *sgl_node = NULL;
 	if (LIST_EMPTY(&g_hv_sgl_page_pool.in_use_sgl_list)) {
 		printf("storvsc error: not enough in use sgl\n");
 		return;
 	}
 	sgl_node = LIST_FIRST(&g_hv_sgl_page_pool.in_use_sgl_list);
 	LIST_REMOVE(sgl_node, link);
 	sgl_node->sgl_data = sgl;
 	LIST_INSERT_HEAD(&g_hv_sgl_page_pool.free_sgl_list, sgl_node, link);
 }
 
 /**
  * @brief create bounce buffer
  *
  * This function is responsible for create a Scatter/Gather list,
  * which hold several pages that can be aligned with page size.
  *
  * @param seg_count- SG-list segments count
  * @param write - if WRITE_TYPE, set SG list page used size to 0,
  * otherwise set used size to page size.
  *
  * return NULL if create failed
  */
 static struct hv_sglist *
 storvsc_create_bounce_buffer(uint16_t seg_count, int write)
 {
 	int i = 0;
 	struct hv_sglist *bounce_sgl = NULL;
 	unsigned int buf_len = ((write == WRITE_TYPE) ? 0 : PAGE_SIZE);
 	struct hv_sgl_node *sgl_node = NULL;	
 
 	/* get struct hv_sglist from free_sgl_list */
 	if (LIST_EMPTY(&g_hv_sgl_page_pool.free_sgl_list)) {
 		printf("storvsc error: not enough free sgl\n");
 		return NULL;
 	}
 	sgl_node = LIST_FIRST(&g_hv_sgl_page_pool.free_sgl_list);
 	LIST_REMOVE(sgl_node, link);
 	bounce_sgl = sgl_node->sgl_data;
 	LIST_INSERT_HEAD(&g_hv_sgl_page_pool.in_use_sgl_list, sgl_node, link);
 
 	bounce_sgl->sg_maxseg = seg_count;
 
 	if (write == WRITE_TYPE)
 		bounce_sgl->sg_nseg = 0;
 	else
 		bounce_sgl->sg_nseg = seg_count;
 
 	for (i = 0; i < seg_count; i++)
 	        bounce_sgl->sg_iov[i].iov_len = buf_len;
 
 	return bounce_sgl;
 }
 
 /**
  * @brief copy data from SG list to bounce buffer
  *
  * This function is responsible for copy data from one SG list's segments
  * to another SG list which used as bounce buffer.
  *
  * @param bounce_sgl - the destination SG list
  * @param orig_sgl - the segment of the source SG list.
  * @param orig_sgl_count - the count of segments.
  * @param orig_sgl_count - indicate which segment need bounce buffer,
  *  set 1 means need.
  *
  */
 static void
 storvsc_copy_sgl_to_bounce_buf(struct hv_sglist *bounce_sgl,
 			       bus_dma_segment_t *orig_sgl,
 			       unsigned int orig_sgl_count,
 			       uint64_t seg_bits)
 {
 	int src_sgl_idx = 0;
 
 	for (src_sgl_idx = 0; src_sgl_idx < orig_sgl_count; src_sgl_idx++) {
 		if (seg_bits & (1 << src_sgl_idx)) {
 			memcpy(bounce_sgl->sg_iov[src_sgl_idx].iov_base,
 			    (void*)orig_sgl[src_sgl_idx].ds_addr,
 			    orig_sgl[src_sgl_idx].ds_len);
 
 			bounce_sgl->sg_iov[src_sgl_idx].iov_len =
 			    orig_sgl[src_sgl_idx].ds_len;
 		}
 	}
 }
 
 /**
  * @brief copy data from SG list which used as bounce to another SG list
  *
  * This function is responsible for copy data from one SG list with bounce
  * buffer to another SG list's segments.
  *
  * @param dest_sgl - the destination SG list's segments
  * @param dest_sgl_count - the count of destination SG list's segment.
  * @param src_sgl - the source SG list.
  * @param seg_bits - indicate which segment used bounce buffer of src SG-list.
  *
  */
 void
 storvsc_copy_from_bounce_buf_to_sgl(bus_dma_segment_t *dest_sgl,
 				    unsigned int dest_sgl_count,
 				    struct hv_sglist* src_sgl,
 				    uint64_t seg_bits)
 {
 	int sgl_idx = 0;
 	
 	for (sgl_idx = 0; sgl_idx < dest_sgl_count; sgl_idx++) {
 		if (seg_bits & (1 << sgl_idx)) {
 			memcpy((void*)(dest_sgl[sgl_idx].ds_addr),
 			    src_sgl->sg_iov[sgl_idx].iov_base,
 			    src_sgl->sg_iov[sgl_idx].iov_len);
 		}
 	}
 }
 
 /**
  * @brief check SG list with bounce buffer or not
  *
  * This function is responsible for check if need bounce buffer for SG list.
  *
  * @param sgl - the SG list's segments
  * @param sg_count - the count of SG list's segment.
  * @param bits - segmengs number that need bounce buffer
  *
  * return -1 if SG list needless bounce buffer
  */
 static int
 storvsc_check_bounce_buffer_sgl(bus_dma_segment_t *sgl,
 				unsigned int sg_count,
 				uint64_t *bits)
 {
 	int i = 0;
 	int offset = 0;
 	uint64_t phys_addr = 0;
 	uint64_t tmp_bits = 0;
 	boolean_t found_hole = FALSE;
 	boolean_t pre_aligned = TRUE;
 
 	if (sg_count < 2){
 		return -1;
 	}
 
 	*bits = 0;
 	
 	phys_addr = vtophys(sgl[0].ds_addr);
 	offset =  phys_addr - trunc_page(phys_addr);
 
 	if (offset != 0) {
 		pre_aligned = FALSE;
 		tmp_bits |= 1;
 	}
 
 	for (i = 1; i < sg_count; i++) {
 		phys_addr = vtophys(sgl[i].ds_addr);
 		offset =  phys_addr - trunc_page(phys_addr);
 
 		if (offset == 0) {
 			if (FALSE == pre_aligned){
 				/*
 				 * This segment is aligned, if the previous
 				 * one is not aligned, find a hole
 				 */
 				found_hole = TRUE;
 			}
 			pre_aligned = TRUE;
 		} else {
 			tmp_bits |= 1ULL << i;
 			if (!pre_aligned) {
 				if (phys_addr != vtophys(sgl[i-1].ds_addr +
 				    sgl[i-1].ds_len)) {
 					/*
 					 * Check whether connect to previous
 					 * segment,if not, find the hole
 					 */
 					found_hole = TRUE;
 				}
 			} else {
 				found_hole = TRUE;
 			}
 			pre_aligned = FALSE;
 		}
 	}
 
 	if (!found_hole) {
 		return (-1);
 	} else {
 		*bits = tmp_bits;
 		return 0;
 	}
 }
 
 /**
  * Copy bus_dma segments to multiple page buffer, which requires
  * the pages are compact composed except for the 1st and last pages.
  */
 static void
 storvsc_xferbuf_prepare(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
 {
 	struct hv_storvsc_request *reqp = arg;
 	union ccb *ccb = reqp->ccb;
 	struct ccb_scsiio *csio = &ccb->csio;
 	struct storvsc_gpa_range *prplist;
 	int i;
 
 	prplist = &reqp->prp_list;
 	prplist->gpa_range.gpa_len = csio->dxfer_len;
 	prplist->gpa_range.gpa_ofs = segs[0].ds_addr & PAGE_MASK;
 
 	for (i = 0; i < nsegs; i++) {
 #ifdef INVARIANTS
 		if (nsegs > 1) {
 			if (i == 0) {
 				KASSERT((segs[i].ds_addr & PAGE_MASK) +
 				    segs[i].ds_len == PAGE_SIZE,
 				    ("invalid 1st page, ofs 0x%jx, len %zu",
 				     (uintmax_t)segs[i].ds_addr,
 				     segs[i].ds_len));
 			} else if (i == nsegs - 1) {
 				KASSERT((segs[i].ds_addr & PAGE_MASK) == 0,
 				    ("invalid last page, ofs 0x%jx",
 				     (uintmax_t)segs[i].ds_addr));
 			} else {
 				KASSERT((segs[i].ds_addr & PAGE_MASK) == 0 &&
 				    segs[i].ds_len == PAGE_SIZE,
 				    ("not a full page, ofs 0x%jx, len %zu",
 				     (uintmax_t)segs[i].ds_addr,
 				     segs[i].ds_len));
 			}
 		}
 #endif
 		prplist->gpa_page[i] = atop(segs[i].ds_addr);
 	}
 	reqp->prp_cnt = nsegs;
 }
 
 /**
  * @brief Fill in a request structure based on a CAM control block
  *
  * Fills in a request structure based on the contents of a CAM control
  * block.  The request structure holds the payload information for
  * VSCSI protocol request.
  *
  * @param ccb pointer to a CAM contorl block
  * @param reqp pointer to a request structure
  */
 static int
 create_storvsc_request(union ccb *ccb, struct hv_storvsc_request *reqp)
 {
 	struct ccb_scsiio *csio = &ccb->csio;
 	uint64_t phys_addr;
 	uint32_t pfn;
 	uint64_t not_aligned_seg_bits = 0;
 	int error;
 	
 	/* refer to struct vmscsi_req for meanings of these two fields */
 	reqp->vstor_packet.u.vm_srb.port =
 		cam_sim_unit(xpt_path_sim(ccb->ccb_h.path));
 	reqp->vstor_packet.u.vm_srb.path_id =
 		cam_sim_bus(xpt_path_sim(ccb->ccb_h.path));
 
 	reqp->vstor_packet.u.vm_srb.target_id = ccb->ccb_h.target_id;
 	reqp->vstor_packet.u.vm_srb.lun = ccb->ccb_h.target_lun;
 
 	reqp->vstor_packet.u.vm_srb.cdb_len = csio->cdb_len;
 	if(ccb->ccb_h.flags & CAM_CDB_POINTER) {
 		memcpy(&reqp->vstor_packet.u.vm_srb.u.cdb, csio->cdb_io.cdb_ptr,
 			csio->cdb_len);
 	} else {
 		memcpy(&reqp->vstor_packet.u.vm_srb.u.cdb, csio->cdb_io.cdb_bytes,
 			csio->cdb_len);
 	}
 
 	if (hv_storvsc_use_win8ext_flags) {
 		reqp->vstor_packet.u.vm_srb.win8_extension.time_out_value = 60;
 		reqp->vstor_packet.u.vm_srb.win8_extension.srb_flags |=
 			SRB_FLAGS_DISABLE_SYNCH_TRANSFER;
 	}
 	switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
 	case CAM_DIR_OUT:
 		reqp->vstor_packet.u.vm_srb.data_in = WRITE_TYPE;
 		if (hv_storvsc_use_win8ext_flags) {
 			reqp->vstor_packet.u.vm_srb.win8_extension.srb_flags |=
 				SRB_FLAGS_DATA_OUT;
 		}
 		break;
 	case CAM_DIR_IN:
 		reqp->vstor_packet.u.vm_srb.data_in = READ_TYPE;
 		if (hv_storvsc_use_win8ext_flags) {
 			reqp->vstor_packet.u.vm_srb.win8_extension.srb_flags |=
 				SRB_FLAGS_DATA_IN;
 		}
 		break;
 	case CAM_DIR_NONE:
 		reqp->vstor_packet.u.vm_srb.data_in = UNKNOWN_TYPE;
 		if (hv_storvsc_use_win8ext_flags) {
 			reqp->vstor_packet.u.vm_srb.win8_extension.srb_flags |=
 				SRB_FLAGS_NO_DATA_TRANSFER;
 		}
 		break;
 	default:
 		printf("Error: unexpected data direction: 0x%x\n",
 			ccb->ccb_h.flags & CAM_DIR_MASK);
 		return (EINVAL);
 	}
 
 	reqp->sense_data     = &csio->sense_data;
 	reqp->sense_info_len = csio->sense_len;
 
 	reqp->ccb = ccb;
 	ccb->ccb_h.spriv_ptr0 = reqp;
 
 	if (0 == csio->dxfer_len) {
 		return (0);
 	}
 
 	switch (ccb->ccb_h.flags & CAM_DATA_MASK) {
 	case CAM_DATA_BIO:
 	case CAM_DATA_VADDR:
 		error = bus_dmamap_load_ccb(reqp->softc->storvsc_req_dtag,
 		    reqp->data_dmap, ccb, storvsc_xferbuf_prepare, reqp,
 		    BUS_DMA_NOWAIT);
 		if (error) {
 			xpt_print(ccb->ccb_h.path,
 			    "bus_dmamap_load_ccb failed: %d\n", error);
 			return (error);
 		}
 		if ((ccb->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_BIO)
 			reqp->softc->sysctl_data.data_bio_cnt++;
 		else
 			reqp->softc->sysctl_data.data_vaddr_cnt++;
 		break;
 
 	case CAM_DATA_SG:
 	{
 		struct storvsc_gpa_range *prplist;
 		int i = 0;
 		int offset = 0;
 		int ret;
 
 		bus_dma_segment_t *storvsc_sglist =
 		    (bus_dma_segment_t *)ccb->csio.data_ptr;
 		u_int16_t storvsc_sg_count = ccb->csio.sglist_cnt;
 
 		prplist = &reqp->prp_list;
 		prplist->gpa_range.gpa_len = csio->dxfer_len;
 
 		printf("Storvsc: get SG I/O operation, %d\n",
 		    reqp->vstor_packet.u.vm_srb.data_in);
 
 		if (storvsc_sg_count > STORVSC_DATA_SEGCNT_MAX){
 			printf("Storvsc: %d segments is too much, "
 			    "only support %d segments\n",
 			    storvsc_sg_count, STORVSC_DATA_SEGCNT_MAX);
 			return (EINVAL);
 		}
 
 		/*
 		 * We create our own bounce buffer function currently. Idealy
 		 * we should use BUS_DMA(9) framework. But with current BUS_DMA
 		 * code there is no callback API to check the page alignment of
 		 * middle segments before busdma can decide if a bounce buffer
 		 * is needed for particular segment. There is callback,
 		 * "bus_dma_filter_t *filter", but the parrameters are not
 		 * sufficient for storvsc driver.
 		 * TODO:
 		 *	Add page alignment check in BUS_DMA(9) callback. Once
 		 *	this is complete, switch the following code to use
 		 *	BUS_DMA(9) for storvsc bounce buffer support.
 		 */
 		/* check if we need to create bounce buffer */
 		ret = storvsc_check_bounce_buffer_sgl(storvsc_sglist,
 		    storvsc_sg_count, &not_aligned_seg_bits);
 		if (ret != -1) {
 			reqp->bounce_sgl =
 			    storvsc_create_bounce_buffer(storvsc_sg_count,
 			    reqp->vstor_packet.u.vm_srb.data_in);
 			if (NULL == reqp->bounce_sgl) {
 				printf("Storvsc_error: "
 				    "create bounce buffer failed.\n");
 				return (ENOMEM);
 			}
 
 			reqp->bounce_sgl_count = storvsc_sg_count;
 			reqp->not_aligned_seg_bits = not_aligned_seg_bits;
 
 			/*
 			 * if it is write, we need copy the original data
 			 *to bounce buffer
 			 */
 			if (WRITE_TYPE == reqp->vstor_packet.u.vm_srb.data_in) {
 				storvsc_copy_sgl_to_bounce_buf(
 				    reqp->bounce_sgl,
 				    storvsc_sglist,
 				    storvsc_sg_count,
 				    reqp->not_aligned_seg_bits);
 			}
 
 			/* transfer virtual address to physical frame number */
 			if (reqp->not_aligned_seg_bits & 0x1){
  				phys_addr =
 				    vtophys(reqp->bounce_sgl->sg_iov[0].iov_base);
 			}else{
  				phys_addr =
 					vtophys(storvsc_sglist[0].ds_addr);
 			}
 			prplist->gpa_range.gpa_ofs = phys_addr & PAGE_MASK;
 
 			pfn = phys_addr >> PAGE_SHIFT;
 			prplist->gpa_page[0] = pfn;
 			
 			for (i = 1; i < storvsc_sg_count; i++) {
 				if (reqp->not_aligned_seg_bits & (1 << i)) {
 					phys_addr =
 					    vtophys(reqp->bounce_sgl->sg_iov[i].iov_base);
 				} else {
 					phys_addr =
 					    vtophys(storvsc_sglist[i].ds_addr);
 				}
 
 				pfn = phys_addr >> PAGE_SHIFT;
 				prplist->gpa_page[i] = pfn;
 			}
 			reqp->prp_cnt = i;
 		} else {
 			phys_addr = vtophys(storvsc_sglist[0].ds_addr);
 
 			prplist->gpa_range.gpa_ofs = phys_addr & PAGE_MASK;
 
 			for (i = 0; i < storvsc_sg_count; i++) {
 				phys_addr = vtophys(storvsc_sglist[i].ds_addr);
 				pfn = phys_addr >> PAGE_SHIFT;
 				prplist->gpa_page[i] = pfn;
 			}
 			reqp->prp_cnt = i;
 
 			/* check the last segment cross boundary or not */
 			offset = phys_addr & PAGE_MASK;
 			if (offset) {
 				/* Add one more PRP entry */
 				phys_addr =
 				    vtophys(storvsc_sglist[i-1].ds_addr +
 				    PAGE_SIZE - offset);
 				pfn = phys_addr >> PAGE_SHIFT;
 				prplist->gpa_page[i] = pfn;
 				reqp->prp_cnt++;
 			}
 			
 			reqp->bounce_sgl_count = 0;
 		}
 		reqp->softc->sysctl_data.data_sg_cnt++;
 		break;
 	}
 	default:
 		printf("Unknow flags: %d\n", ccb->ccb_h.flags);
 		return(EINVAL);
 	}
 
 	return(0);
 }
 
 static uint32_t
 is_scsi_valid(const struct scsi_inquiry_data *inq_data)
 {
 	u_int8_t type;
 
 	type = SID_TYPE(inq_data);
 	if (type == T_NODEVICE)
 		return (0);
 	if (SID_QUAL(inq_data) == SID_QUAL_BAD_LU)
 		return (0);
 	return (1);
 }
 
 /**
  * @brief completion function before returning to CAM
  *
  * I/O process has been completed and the result needs
  * to be passed to the CAM layer.
  * Free resources related to this request.
  *
  * @param reqp pointer to a request structure
  */
 static void
 storvsc_io_done(struct hv_storvsc_request *reqp)
 {
 	union ccb *ccb = reqp->ccb;
 	struct ccb_scsiio *csio = &ccb->csio;
 	struct storvsc_softc *sc = reqp->softc;
 	struct vmscsi_req *vm_srb = &reqp->vstor_packet.u.vm_srb;
 	bus_dma_segment_t *ori_sglist = NULL;
 	int ori_sg_count = 0;
 	const struct scsi_generic *cmd;
 
 	/* destroy bounce buffer if it is used */
 	if (reqp->bounce_sgl_count) {
 		ori_sglist = (bus_dma_segment_t *)ccb->csio.data_ptr;
 		ori_sg_count = ccb->csio.sglist_cnt;
 
 		/*
 		 * If it is READ operation, we should copy back the data
 		 * to original SG list.
 		 */
 		if (READ_TYPE == reqp->vstor_packet.u.vm_srb.data_in) {
 			storvsc_copy_from_bounce_buf_to_sgl(ori_sglist,
 			    ori_sg_count,
 			    reqp->bounce_sgl,
 			    reqp->not_aligned_seg_bits);
 		}
 
 		storvsc_destroy_bounce_buffer(reqp->bounce_sgl);
 		reqp->bounce_sgl_count = 0;
 	}
 		
 	if (reqp->retries > 0) {
 		mtx_lock(&sc->hs_lock);
 #if HVS_TIMEOUT_TEST
 		xpt_print(ccb->ccb_h.path,
 			"%u: IO returned after timeout, "
 			"waking up timer handler if any.\n", ticks);
 		mtx_lock(&reqp->event.mtx);
 		cv_signal(&reqp->event.cv);
 		mtx_unlock(&reqp->event.mtx);
 #endif
 		reqp->retries = 0;
 		xpt_print(ccb->ccb_h.path,
 			"%u: IO returned after timeout, "
 			"stopping timer if any.\n", ticks);
 		mtx_unlock(&sc->hs_lock);
 	}
 
 #ifdef notyet
 	/*
 	 * callout_drain() will wait for the timer handler to finish
 	 * if it is running. So we don't need any lock to synchronize
 	 * between this routine and the timer handler.
 	 * Note that we need to make sure reqp is not freed when timer
 	 * handler is using or will use it.
 	 */
 	if (ccb->ccb_h.timeout != CAM_TIME_INFINITY) {
 		callout_drain(&reqp->callout);
 	}
 #endif
 	cmd = (const struct scsi_generic *)
 	    ((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
 	     csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes);
 
 	ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
 	ccb->ccb_h.status &= ~CAM_STATUS_MASK;
 	int srb_status = SRB_STATUS(vm_srb->srb_status);
 #ifdef DIAGNOSTIC
 	if (hv_storvsc_srb_status != -1) {
 		srb_status = SRB_STATUS(hv_storvsc_srb_status & 0x3f);
 		hv_storvsc_srb_status = -1;
 	}
 #endif /* DIAGNOSTIC */
 	if (vm_srb->scsi_status == SCSI_STATUS_OK) {
 		if (srb_status != SRB_STATUS_SUCCESS) {
 			bool log_error = true;
 			switch (srb_status) {
 				case SRB_STATUS_PENDING:
 					/* We should never get this */
 					panic("storvsc_io_done: SRB_STATUS_PENDING");
 					break;
 				case SRB_STATUS_ABORTED:
 					/*
 					 * storvsc doesn't support aborts yet
 					 * but if we ever get this status
 					 * the I/O is complete - treat it as a
 					 * timeout
 					 */
 					ccb->ccb_h.status |= CAM_CMD_TIMEOUT;
 					break;
 				case SRB_STATUS_ABORT_FAILED:
 					/* We should never get this */
 					panic("storvsc_io_done: SRB_STATUS_ABORT_FAILED");
 					break;
 				case SRB_STATUS_ERROR:
 					/*
 					 * We should never get this.
 					 * Treat it as a CAM_UNREC_HBA_ERROR.
 					 * It will be retried
 					 */
 					ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
 					break;
 				case SRB_STATUS_BUSY:
 					/* Host is busy. Delay and retry */
 					ccb->ccb_h.status |= CAM_BUSY;
 					break;
 				case SRB_STATUS_INVALID_REQUEST:
 				case SRB_STATUS_INVALID_PATH_ID:
 				case SRB_STATUS_NO_DEVICE:
 				case SRB_STATUS_INVALID_TARGET_ID:
 					/*
 					 * These indicate an invalid address
 					 * and really should never be seen.
 					 * A CAM_PATH_INVALID could be
 					 * used here but I want to run
 					 * down retries.  Do a CAM_BUSY
 					 * since the host might be having issues.
 					 */
 					ccb->ccb_h.status |= CAM_BUSY;
 					break;
 				case SRB_STATUS_TIMEOUT:
 				case SRB_STATUS_COMMAND_TIMEOUT:
 					/* The backend has timed this out */
 					ccb->ccb_h.status |= CAM_BUSY;
 					break;
 				/* Some old pSCSI errors below */
 				case SRB_STATUS_SELECTION_TIMEOUT:
 				case SRB_STATUS_MESSAGE_REJECTED:
 				case SRB_STATUS_PARITY_ERROR:
 				case SRB_STATUS_NO_HBA:
 				case SRB_STATUS_DATA_OVERRUN:
 				case SRB_STATUS_UNEXPECTED_BUS_FREE:
 				case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
 					/*
 					 * Old pSCSI responses, should never get.
 					 * If we do treat as a CAM_UNREC_HBA_ERROR
 					 * which will be retried
 					 */
 					ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
 					break;
 				case SRB_STATUS_BUS_RESET:
 					ccb->ccb_h.status |= CAM_SCSI_BUS_RESET;
 					break;
 				case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
 					/*
 					 * The request block is malformed and
 					 * I doubt it is from the guest. Just retry.
 					 */
 					ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
 					break;
 				/* Not used statuses just retry */
 				case SRB_STATUS_REQUEST_FLUSHED:
 				case SRB_STATUS_BAD_FUNCTION:
 				case SRB_STATUS_NOT_POWERED:
 					ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
 					break;
 				case SRB_STATUS_INVALID_LUN:
 					/*
 					 * Don't log an EMS for this response since
 					 * there is no device at this LUN. This is a
 					 * normal and expected response when a device
 					 * is detached.
 					 */
 					ccb->ccb_h.status |= CAM_DEV_NOT_THERE;
 					log_error = false;
 					break;
 				case SRB_STATUS_ERROR_RECOVERY:
 				case SRB_STATUS_LINK_DOWN:
 					/*
 					 * I don't ever expect these from
 					 * the host but if we ever get
 					 * retry after a delay
 					 */
 					ccb->ccb_h.status |= CAM_BUSY;
 					break;
 				default:
 					/*
 					 * An undefined response assert on
 					 * on debug builds else retry
 					 */
 					ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
 					KASSERT(srb_status <= SRB_STATUS_LINK_DOWN,
 					    ("storvsc: %s, unexpected srb_status of 0x%x",
 					    __func__, srb_status));
 					break;
 			}
 			if (log_error) {
 				xpt_print(ccb->ccb_h.path, "The hypervisor's I/O adapter "
 					"driver received an unexpected response code 0x%x "
 					"for operation: %s. If this continues to occur, "
 					"report the condition to your hypervisor vendor so "
 					"they can rectify the issue.\n", srb_status,
 					scsi_op_desc(cmd->opcode, NULL));
 			}
 		} else {
 			ccb->ccb_h.status |= CAM_REQ_CMP;
 		}
 
 		if (cmd->opcode == INQUIRY &&
 		    srb_status == SRB_STATUS_SUCCESS) {
 			int resp_xfer_len, resp_buf_len, data_len;
 			uint8_t *resp_buf = (uint8_t *)csio->data_ptr;
 			struct scsi_inquiry_data *inq_data =
 			    (struct scsi_inquiry_data *)csio->data_ptr;
 
 			/* Get the buffer length reported by host */
 			resp_xfer_len = vm_srb->transfer_len;
 
 			/* Get the available buffer length */
 			resp_buf_len = resp_xfer_len >= 5 ? resp_buf[4] + 5 : 0;
 			data_len = (resp_buf_len < resp_xfer_len) ?
 			    resp_buf_len : resp_xfer_len;
 			if (bootverbose && data_len >= 5) {
 				xpt_print(ccb->ccb_h.path, "storvsc inquiry "
 				    "(%d) [%x %x %x %x %x ... ]\n", data_len,
 				    resp_buf[0], resp_buf[1], resp_buf[2],
 				    resp_buf[3], resp_buf[4]);
 			}
 			/*
 			 * XXX: Hyper-V (since win2012r2) responses inquiry with
 			 * unknown version (0) for GEN-2 DVD device.
 			 * Manually set the version number to SPC3 in order to
 			 * ask CAM to continue probing with "PROBE_REPORT_LUNS".
 			 * see probedone() in scsi_xpt.c
 			 */
 			if (SID_TYPE(inq_data) == T_CDROM &&
 			    inq_data->version == 0 &&
 			    (vmstor_proto_version >= VMSTOR_PROTOCOL_VERSION_WIN8)) {
 				inq_data->version = SCSI_REV_SPC3;
 				if (bootverbose) {
 					xpt_print(ccb->ccb_h.path,
 					    "set version from 0 to %d\n",
 					    inq_data->version);
 				}
 			}
 			/*
 			 * XXX: Manually fix the wrong response returned from WS2012
 			 */
 			if (!is_scsi_valid(inq_data) &&
 			    (vmstor_proto_version == VMSTOR_PROTOCOL_VERSION_WIN8_1 ||
 			    vmstor_proto_version == VMSTOR_PROTOCOL_VERSION_WIN8 ||
 			    vmstor_proto_version == VMSTOR_PROTOCOL_VERSION_WIN7)) {
 				if (data_len >= 4 &&
 				    (resp_buf[2] == 0 || resp_buf[3] == 0)) {
 					resp_buf[2] = SCSI_REV_SPC3;
 					resp_buf[3] = 2; // resp fmt must be 2
 					if (bootverbose)
 						xpt_print(ccb->ccb_h.path,
 						    "fix version and resp fmt for 0x%x\n",
 						    vmstor_proto_version);
 				}
 			} else if (data_len >= SHORT_INQUIRY_LENGTH) {
 				char vendor[16];
 
 				cam_strvis(vendor, inq_data->vendor,
 				    sizeof(inq_data->vendor), sizeof(vendor));
 				/*
 				 * XXX: Upgrade SPC2 to SPC3 if host is WIN8 or
 				 * WIN2012 R2 in order to support UNMAP feature.
 				 */
 				if (!strncmp(vendor, "Msft", 4) &&
 				    SID_ANSI_REV(inq_data) == SCSI_REV_SPC2 &&
 				    (vmstor_proto_version ==
 				     VMSTOR_PROTOCOL_VERSION_WIN8_1 ||
 				     vmstor_proto_version ==
 				     VMSTOR_PROTOCOL_VERSION_WIN8)) {
 					inq_data->version = SCSI_REV_SPC3;
 					if (bootverbose) {
 						xpt_print(ccb->ccb_h.path,
 						    "storvsc upgrades "
 						    "SPC2 to SPC3\n");
 					}
 				}
 			}
 		}
 	} else {
 		/**
 		 * On Some Windows hosts TEST_UNIT_READY command can return
 		 * SRB_STATUS_ERROR and sense data, for example, asc=0x3a,1
 		 * "(Medium not present - tray closed)". This error can be
 		 * ignored since it will be sent to host periodically.
 		 */
 		boolean_t unit_not_ready = \
 		    vm_srb->scsi_status == SCSI_STATUS_CHECK_COND &&
 		    cmd->opcode == TEST_UNIT_READY &&
 		    srb_status == SRB_STATUS_ERROR;
 		if (!unit_not_ready && bootverbose) {
 			mtx_lock(&sc->hs_lock);
 			xpt_print(ccb->ccb_h.path,
 				"storvsc scsi_status = %d, srb_status = %d\n",
 				vm_srb->scsi_status, srb_status);
 			mtx_unlock(&sc->hs_lock);
 		}
 		ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
 	}
 
 	ccb->csio.scsi_status = (vm_srb->scsi_status & 0xFF);
 	if (srb_status == SRB_STATUS_SUCCESS ||
 	    srb_status == SRB_STATUS_DATA_OVERRUN)
 		ccb->csio.resid = ccb->csio.dxfer_len - vm_srb->transfer_len;
 	else
 		ccb->csio.resid = ccb->csio.dxfer_len;
 
 	if ((vm_srb->srb_status & SRB_STATUS_AUTOSENSE_VALID) != 0 &&
 	    reqp->sense_info_len != 0) {
 		csio->sense_resid = csio->sense_len - reqp->sense_info_len;
 		ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
 	}
 
 	mtx_lock(&sc->hs_lock);
 	if (reqp->softc->hs_frozen == 1) {
 		xpt_print(ccb->ccb_h.path,
 			"%u: storvsc unfreezing softc 0x%p.\n",
 			ticks, reqp->softc);
 		ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
 		reqp->softc->hs_frozen = 0;
 	}
 	storvsc_free_request(sc, reqp);
 	mtx_unlock(&sc->hs_lock);
 
 	xpt_done_direct(ccb);
 }
 
 /**
  * @brief Free a request structure
  *
  * Free a request structure by returning it to the free list
  *
  * @param sc pointer to a softc
  * @param reqp pointer to a request structure
  */	
 static void
 storvsc_free_request(struct storvsc_softc *sc, struct hv_storvsc_request *reqp)
 {
 
 	LIST_INSERT_HEAD(&sc->hs_free_list, reqp, link);
 }
 
 /**
  * @brief Determine type of storage device from GUID
  *
  * Using the type GUID, determine if this is a StorVSC (paravirtual
  * SCSI or BlkVSC (paravirtual IDE) device.
  *
  * @param dev a device
  * returns an enum
  */
 static enum hv_storage_type
 storvsc_get_storage_type(device_t dev)
 {
 	device_t parent = device_get_parent(dev);
 
 	if (VMBUS_PROBE_GUID(parent, dev, &gBlkVscDeviceType) == 0)
 		return DRIVER_BLKVSC;
 	if (VMBUS_PROBE_GUID(parent, dev, &gStorVscDeviceType) == 0)
 		return DRIVER_STORVSC;
 	return DRIVER_UNKNOWN;
 }
 
 #define	PCI_VENDOR_INTEL	0x8086
 #define	PCI_PRODUCT_PIIX4	0x7111
 
 static void
 storvsc_ada_probe_veto(void *arg __unused, struct cam_path *path,
     struct ata_params *ident_buf __unused, int *veto)
 {
 
 	/*
 	 * The ATA disks are shared with the controllers managed
 	 * by this driver, so veto the ATA disks' attachment; the
 	 * ATA disks will be attached as SCSI disks once this driver
 	 * attached.
 	 */
 	if (path->device->protocol == PROTO_ATA) {
 		struct ccb_pathinq cpi;
 
 		xpt_path_inq(&cpi, path);
 		if (cpi.ccb_h.status == CAM_REQ_CMP &&
 		    cpi.hba_vendor == PCI_VENDOR_INTEL &&
 		    cpi.hba_device == PCI_PRODUCT_PIIX4) {
 			(*veto)++;
 			if (bootverbose) {
 				xpt_print(path,
 				    "Disable ATA disks on "
 				    "simulated ATA controller (0x%04x%04x)\n",
 				    cpi.hba_device, cpi.hba_vendor);
 			}
 		}
 	}
 }
 
 static void
 storvsc_sysinit(void *arg __unused)
 {
 	if (vm_guest == VM_GUEST_HV) {
 		storvsc_handler_tag = EVENTHANDLER_REGISTER(ada_probe_veto,
 		    storvsc_ada_probe_veto, NULL, EVENTHANDLER_PRI_ANY);
 	}
 }
 SYSINIT(storvsc_sys_init, SI_SUB_DRIVERS, SI_ORDER_SECOND, storvsc_sysinit,
     NULL);
 
 static void
 storvsc_sysuninit(void *arg __unused)
 {
 	if (storvsc_handler_tag != NULL)
 		EVENTHANDLER_DEREGISTER(ada_probe_veto, storvsc_handler_tag);
 }
 SYSUNINIT(storvsc_sys_uninit, SI_SUB_DRIVERS, SI_ORDER_SECOND,
     storvsc_sysuninit, NULL);
diff --git a/sys/dev/vt/vt_core.c b/sys/dev/vt/vt_core.c
index 860cc0b00781..a623f50c9c06 100644
--- a/sys/dev/vt/vt_core.c
+++ b/sys/dev/vt/vt_core.c
@@ -1,3213 +1,3214 @@
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
  *
  * Copyright (c) 2009, 2013 The FreeBSD Foundation
  *
  * This software was developed by Ed Schouten under sponsorship from the
  * FreeBSD Foundation.
  *
  * Portions of this software were developed by Oleksandr Rybalko
  * under sponsorship from the FreeBSD Foundation.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  * 1. Redistributions of source code must retain the above copyright
  *    notice, this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
  *
  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
 
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 
 #include <sys/param.h>
 #include <sys/consio.h>
 #include <sys/devctl.h>
 #include <sys/eventhandler.h>
 #include <sys/fbio.h>
 #include <sys/font.h>
 #include <sys/kbio.h>
 #include <sys/kdb.h>
 #include <sys/kernel.h>
 #include <sys/linker.h>
 #include <sys/lock.h>
 #include <sys/malloc.h>
 #include <sys/mutex.h>
 #include <sys/power.h>
 #include <sys/priv.h>
 #include <sys/proc.h>
 #include <sys/random.h>
 #include <sys/reboot.h>
 #include <sys/sbuf.h>
 #include <sys/systm.h>
 #include <sys/terminal.h>
 
 #include <dev/kbd/kbdreg.h>
 #include <dev/vt/vt.h>
 
 #if defined(__i386__) || defined(__amd64__)
 #include <machine/psl.h>
 #include <machine/frame.h>
 #endif
 
 static int vtterm_cngrab_noswitch(struct vt_device *, struct vt_window *);
 static int vtterm_cnungrab_noswitch(struct vt_device *, struct vt_window *);
 
 static tc_bell_t	vtterm_bell;
 static tc_cursor_t	vtterm_cursor;
 static tc_putchar_t	vtterm_putchar;
 static tc_fill_t	vtterm_fill;
 static tc_copy_t	vtterm_copy;
 static tc_pre_input_t	vtterm_pre_input;
 static tc_post_input_t	vtterm_post_input;
 static tc_param_t	vtterm_param;
 static tc_done_t	vtterm_done;
 
 static tc_cnprobe_t	vtterm_cnprobe;
 static tc_cngetc_t	vtterm_cngetc;
 
 static tc_cngrab_t	vtterm_cngrab;
 static tc_cnungrab_t	vtterm_cnungrab;
 
 static tc_opened_t	vtterm_opened;
 static tc_ioctl_t	vtterm_ioctl;
 static tc_mmap_t	vtterm_mmap;
 
 static const struct terminal_class vt_termclass = {
 	.tc_bell	= vtterm_bell,
 	.tc_cursor	= vtterm_cursor,
 	.tc_putchar	= vtterm_putchar,
 	.tc_fill	= vtterm_fill,
 	.tc_copy	= vtterm_copy,
 	.tc_pre_input	= vtterm_pre_input,
 	.tc_post_input	= vtterm_post_input,
 	.tc_param	= vtterm_param,
 	.tc_done	= vtterm_done,
 
 	.tc_cnprobe	= vtterm_cnprobe,
 	.tc_cngetc	= vtterm_cngetc,
 
 	.tc_cngrab	= vtterm_cngrab,
 	.tc_cnungrab	= vtterm_cnungrab,
 
 	.tc_opened	= vtterm_opened,
 	.tc_ioctl	= vtterm_ioctl,
 	.tc_mmap	= vtterm_mmap,
 };
 
 /*
  * Use a constant timer of 25 Hz to redraw the screen.
  *
  * XXX: In theory we should only fire up the timer when there is really
  * activity. Unfortunately we cannot always start timers. We really
  * don't want to process kernel messages synchronously, because it
  * really slows down the system.
  */
 #define	VT_TIMERFREQ	25
 
 /* Bell pitch/duration. */
 #define	VT_BELLDURATION	(SBT_1S / 20)
 #define	VT_BELLPITCH	(1193182 / 800) /* Approx 1491Hz */
 
 #define	VT_UNIT(vw)	((vw)->vw_device->vd_unit * VT_MAXWINDOWS + \
 			(vw)->vw_number)
 
 static SYSCTL_NODE(_kern, OID_AUTO, vt, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
     "vt(9) parameters");
 static VT_SYSCTL_INT(enable_altgr, 1, "Enable AltGr key (Do not assume R.Alt as Alt)");
 static VT_SYSCTL_INT(enable_bell, 0, "Enable bell");
 static VT_SYSCTL_INT(debug, 0, "vt(9) debug level");
 static VT_SYSCTL_INT(deadtimer, 15, "Time to wait busy process in VT_PROCESS mode");
 static VT_SYSCTL_INT(suspendswitch, 1, "Switch to VT0 before suspend");
 
 /* Allow to disable some keyboard combinations. */
 static VT_SYSCTL_INT(kbd_halt, 1, "Enable halt keyboard combination.  "
     "See kbdmap(5) to configure.");
 static VT_SYSCTL_INT(kbd_poweroff, 1, "Enable Power Off keyboard combination.  "
     "See kbdmap(5) to configure.");
 static VT_SYSCTL_INT(kbd_reboot, 1, "Enable reboot keyboard combination.  "
     "See kbdmap(5) to configure (typically Ctrl-Alt-Delete).");
 static VT_SYSCTL_INT(kbd_debug, 1, "Enable key combination to enter debugger.  "
     "See kbdmap(5) to configure (typically Ctrl-Alt-Esc).");
 static VT_SYSCTL_INT(kbd_panic, 0, "Enable request to panic.  "
     "See kbdmap(5) to configure.");
 
 /* Used internally, not a tunable. */
 int vt_draw_logo_cpus;
 VT_SYSCTL_INT(splash_cpu, 0, "Show logo CPUs during boot");
 VT_SYSCTL_INT(splash_ncpu, 0, "Override number of logos displayed "
     "(0 = do not override)");
 VT_SYSCTL_INT(splash_cpu_style, 2, "Draw logo style "
     "(0 = Alternate beastie, 1 = Beastie, 2 = Orb)");
 VT_SYSCTL_INT(splash_cpu_duration, 10, "Hide logos after (seconds)");
 
 static unsigned int vt_unit = 0;
 static MALLOC_DEFINE(M_VT, "vt", "vt device");
 struct vt_device *main_vd = &vt_consdev;
 
 /* Boot logo. */
 extern unsigned int vt_logo_width;
 extern unsigned int vt_logo_height;
 extern unsigned int vt_logo_depth;
 extern unsigned char vt_logo_image[];
 #ifndef DEV_SPLASH
 #define	vtterm_draw_cpu_logos(...)	do {} while (0)
 const unsigned int vt_logo_sprite_height;
 #endif
 
 /*
  * Console font. vt_font_loader will be filled with font data passed
  * by loader. If there is no font passed by boot loader, we use built in
  * default.
  */
 extern struct vt_font vt_font_default;
 static struct vt_font vt_font_loader;
 static struct vt_font *vt_font_assigned = &vt_font_default;
 
 #ifndef SC_NO_CUTPASTE
 extern struct vt_mouse_cursor vt_default_mouse_pointer;
 #endif
 
 static int signal_vt_rel(struct vt_window *);
 static int signal_vt_acq(struct vt_window *);
 static int finish_vt_rel(struct vt_window *, int, int *);
 static int finish_vt_acq(struct vt_window *);
 static int vt_window_switch(struct vt_window *);
 static int vt_late_window_switch(struct vt_window *);
 static int vt_proc_alive(struct vt_window *);
 static void vt_resize(struct vt_device *);
 static void vt_update_static(void *);
 #ifndef SC_NO_CUTPASTE
 static void vt_mouse_paste(void);
 #endif
 static void vt_suspend_handler(void *priv);
 static void vt_resume_handler(void *priv);
 
 SET_DECLARE(vt_drv_set, struct vt_driver);
 
 #define	_VTDEFH	MAX(100, PIXEL_HEIGHT(VT_FB_MAX_HEIGHT))
 #define	_VTDEFW	MAX(200, PIXEL_WIDTH(VT_FB_MAX_WIDTH))
 
 static struct terminal	vt_consterm;
 static struct vt_window	vt_conswindow;
 #ifndef SC_NO_CONSDRAWN
 static term_char_t vt_consdrawn[PIXEL_HEIGHT(VT_FB_MAX_HEIGHT) * PIXEL_WIDTH(VT_FB_MAX_WIDTH)];
 static term_color_t vt_consdrawnfg[PIXEL_HEIGHT(VT_FB_MAX_HEIGHT) * PIXEL_WIDTH(VT_FB_MAX_WIDTH)];
 static term_color_t vt_consdrawnbg[PIXEL_HEIGHT(VT_FB_MAX_HEIGHT) * PIXEL_WIDTH(VT_FB_MAX_WIDTH)];
 #endif
 struct vt_device	vt_consdev = {
 	.vd_driver = NULL,
 	.vd_softc = NULL,
 	.vd_prev_driver = NULL,
 	.vd_prev_softc = NULL,
 	.vd_flags = VDF_INVALID,
 	.vd_windows = { [VT_CONSWINDOW] =  &vt_conswindow, },
 	.vd_curwindow = &vt_conswindow,
 	.vd_kbstate = 0,
 
 #ifndef SC_NO_CUTPASTE
 	.vd_pastebuf = {
 		.vpb_buf = NULL,
 		.vpb_bufsz = 0,
 		.vpb_len = 0
 	},
 	.vd_mcursor = &vt_default_mouse_pointer,
 	.vd_mcursor_fg = TC_WHITE,
 	.vd_mcursor_bg = TC_BLACK,
 #endif
 
 #ifndef SC_NO_CONSDRAWN
 	.vd_drawn = vt_consdrawn,
 	.vd_drawnfg = vt_consdrawnfg,
 	.vd_drawnbg = vt_consdrawnbg,
 #endif
 };
 static term_char_t vt_constextbuf[(_VTDEFW) * (VBF_DEFAULT_HISTORY_SIZE)];
 static term_char_t *vt_constextbufrows[VBF_DEFAULT_HISTORY_SIZE];
 static struct vt_window	vt_conswindow = {
 	.vw_number = VT_CONSWINDOW,
 	.vw_flags = VWF_CONSOLE,
 	.vw_buf = {
 		.vb_buffer = &vt_constextbuf[0],
 		.vb_rows = &vt_constextbufrows[0],
 		.vb_history_size = VBF_DEFAULT_HISTORY_SIZE,
 		.vb_curroffset = 0,
 		.vb_roffset = 0,
 		.vb_flags = VBF_STATIC,
 		.vb_mark_start = {.tp_row = 0, .tp_col = 0,},
 		.vb_mark_end = {.tp_row = 0, .tp_col = 0,},
 		.vb_scr_size = {
 			.tp_row = _VTDEFH,
 			.tp_col = _VTDEFW,
 		},
 	},
 	.vw_device = &vt_consdev,
 	.vw_terminal = &vt_consterm,
 	.vw_kbdmode = K_XLATE,
 	.vw_grabbed = 0,
 	.vw_bell_pitch = VT_BELLPITCH,
 	.vw_bell_duration = VT_BELLDURATION,
 };
 
 /* Add to set of consoles. */
 TERMINAL_DECLARE_EARLY(vt_consterm, vt_termclass, &vt_conswindow);
 
 /*
  * Right after kmem is done to allow early drivers to use locking and allocate
  * memory.
  */
 SYSINIT(vt_update_static, SI_SUB_KMEM, SI_ORDER_ANY, vt_update_static,
     &vt_consdev);
 /* Delay until all devices attached, to not waste time. */
 SYSINIT(vt_early_cons, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_ANY, vt_upgrade,
     &vt_consdev);
 
 /* Initialize locks/mem depended members. */
 static void
 vt_update_static(void *dummy)
 {
 
 	if (!vty_enabled(VTY_VT))
 		return;
 	if (main_vd->vd_driver != NULL)
 		printf("VT(%s): %s %ux%u\n", main_vd->vd_driver->vd_name,
 		    (main_vd->vd_flags & VDF_TEXTMODE) ? "text" : "resolution",
 		    main_vd->vd_width, main_vd->vd_height);
 	else
 		printf("VT: init without driver.\n");
 
 	mtx_init(&main_vd->vd_lock, "vtdev", NULL, MTX_DEF);
 	cv_init(&main_vd->vd_winswitch, "vtwswt");
 }
 
 static void
 vt_schedule_flush(struct vt_device *vd, int ms)
 {
 
 	if (ms <= 0)
 		/* Default to initial value. */
 		ms = 1000 / VT_TIMERFREQ;
 
 	callout_schedule(&vd->vd_timer, hz / (1000 / ms));
 }
 
 void
 vt_resume_flush_timer(struct vt_window *vw, int ms)
 {
 	struct vt_device *vd = vw->vw_device;
 
 	if (vd->vd_curwindow != vw)
 		return;
 
 	if (!(vd->vd_flags & VDF_ASYNC) ||
 	    !atomic_cmpset_int(&vd->vd_timer_armed, 0, 1))
 		return;
 
 	vt_schedule_flush(vd, ms);
 }
 
 static void
 vt_suspend_flush_timer(struct vt_device *vd)
 {
 	/*
 	 * As long as this function is called locked, callout_stop()
 	 * has the same effect like callout_drain() with regard to
 	 * preventing the callback function from executing.
 	 */
 	VT_LOCK_ASSERT(vd, MA_OWNED);
 
 	if (!(vd->vd_flags & VDF_ASYNC) ||
 	    !atomic_cmpset_int(&vd->vd_timer_armed, 1, 0))
 		return;
 
 	callout_stop(&vd->vd_timer);
 }
 
 static void
 vt_switch_timer(void *arg)
 {
 
 	(void)vt_late_window_switch((struct vt_window *)arg);
 }
 
 static int
 vt_save_kbd_mode(struct vt_window *vw, keyboard_t *kbd)
 {
 	int mode, ret;
 
 	mode = 0;
 	ret = kbdd_ioctl(kbd, KDGKBMODE, (caddr_t)&mode);
 	if (ret == ENOIOCTL)
 		ret = ENODEV;
 	if (ret != 0)
 		return (ret);
 
 	vw->vw_kbdmode = mode;
 
 	return (0);
 }
 
 static int
 vt_update_kbd_mode(struct vt_window *vw, keyboard_t *kbd)
 {
 	int ret;
 
 	ret = kbdd_ioctl(kbd, KDSKBMODE, (caddr_t)&vw->vw_kbdmode);
 	if (ret == ENOIOCTL)
 		ret = ENODEV;
 
 	return (ret);
 }
 
 static int
 vt_save_kbd_state(struct vt_window *vw, keyboard_t *kbd)
 {
 	int state, ret;
 
 	state = 0;
 	ret = kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
 	if (ret == ENOIOCTL)
 		ret = ENODEV;
 	if (ret != 0)
 		return (ret);
 
 	vw->vw_kbdstate &= ~LOCK_MASK;
 	vw->vw_kbdstate |= state & LOCK_MASK;
 
 	return (0);
 }
 
 static int
 vt_update_kbd_state(struct vt_window *vw, keyboard_t *kbd)
 {
 	int state, ret;
 
 	state = vw->vw_kbdstate & LOCK_MASK;
 	ret = kbdd_ioctl(kbd, KDSKBSTATE, (caddr_t)&state);
 	if (ret == ENOIOCTL)
 		ret = ENODEV;
 
 	return (ret);
 }
 
 static int
 vt_save_kbd_leds(struct vt_window *vw, keyboard_t *kbd)
 {
 	int leds, ret;
 
 	leds = 0;
 	ret = kbdd_ioctl(kbd, KDGETLED, (caddr_t)&leds);
 	if (ret == ENOIOCTL)
 		ret = ENODEV;
 	if (ret != 0)
 		return (ret);
 
 	vw->vw_kbdstate &= ~LED_MASK;
 	vw->vw_kbdstate |= leds & LED_MASK;
 
 	return (0);
 }
 
 static int
 vt_update_kbd_leds(struct vt_window *vw, keyboard_t *kbd)
 {
 	int leds, ret;
 
 	leds = vw->vw_kbdstate & LED_MASK;
 	ret = kbdd_ioctl(kbd, KDSETLED, (caddr_t)&leds);
 	if (ret == ENOIOCTL)
 		ret = ENODEV;
 
 	return (ret);
 }
 
 static int
 vt_window_preswitch(struct vt_window *vw, struct vt_window *curvw)
 {
 
 	DPRINTF(40, "%s\n", __func__);
 	curvw->vw_switch_to = vw;
 	/* Set timer to allow switch in case when process hang. */
 	callout_reset(&vw->vw_proc_dead_timer, hz * vt_deadtimer,
 	    vt_switch_timer, (void *)vw);
 	/* Notify process about vt switch attempt. */
 	DPRINTF(30, "%s: Notify process.\n", __func__);
 	signal_vt_rel(curvw);
 
 	return (0);
 }
 
 static int
 vt_window_postswitch(struct vt_window *vw)
 {
 
 	signal_vt_acq(vw);
 	return (0);
 }
 
 /* vt_late_window_switch will do VT switching for regular case. */
 static int
 vt_late_window_switch(struct vt_window *vw)
 {
 	struct vt_window *curvw;
 	int ret;
 
 	callout_stop(&vw->vw_proc_dead_timer);
 
 	ret = vt_window_switch(vw);
 	if (ret != 0) {
 		/*
 		 * If the switch hasn't happened, then return the VT
 		 * to the current owner, if any.
 		 */
 		curvw = vw->vw_device->vd_curwindow;
 		if (curvw->vw_smode.mode == VT_PROCESS)
 			(void)vt_window_postswitch(curvw);
 		return (ret);
 	}
 
 	/* Notify owner process about terminal availability. */
 	if (vw->vw_smode.mode == VT_PROCESS) {
 		ret = vt_window_postswitch(vw);
 	}
 	return (ret);
 }
 
 /* Switch window. */
 static int
 vt_proc_window_switch(struct vt_window *vw)
 {
 	struct vt_window *curvw;
 	struct vt_device *vd;
 	int ret;
 
 	/* Prevent switching to NULL */
 	if (vw == NULL) {
 		DPRINTF(30, "%s: Cannot switch: vw is NULL.", __func__);
 		return (EINVAL);
 	}
 	vd = vw->vw_device;
 	curvw = vd->vd_curwindow;
 
 	/* Check if virtual terminal is locked */
 	if (curvw->vw_flags & VWF_VTYLOCK)
 		return (EBUSY);
 
 	/* Check if switch already in progress */
 	if (curvw->vw_flags & VWF_SWWAIT_REL) {
 		/* Check if switching to same window */
 		if (curvw->vw_switch_to == vw) {
 			DPRINTF(30, "%s: Switch in progress to same vw.", __func__);
 			return (0);	/* success */
 		}
 		DPRINTF(30, "%s: Switch in progress to different vw.", __func__);
 		return (EBUSY);
 	}
 
 	/* Avoid switching to already selected window */
 	if (vw == curvw) {
 		DPRINTF(30, "%s: Cannot switch: vw == curvw.", __func__);
 		return (0);	/* success */
 	}
 
 	/*
 	 * Early check for an attempt to switch to a non-functional VT.
 	 * The same check is done in vt_window_switch(), but it's better
 	 * to fail as early as possible to avoid needless pre-switch
 	 * actions.
 	 */
 	VT_LOCK(vd);
 	if ((vw->vw_flags & (VWF_OPENED|VWF_CONSOLE)) == 0) {
 		VT_UNLOCK(vd);
 		return (EINVAL);
 	}
 	VT_UNLOCK(vd);
 
 	/* Ask current process permission to switch away. */
 	if (curvw->vw_smode.mode == VT_PROCESS) {
 		DPRINTF(30, "%s: VT_PROCESS ", __func__);
 		if (vt_proc_alive(curvw) == FALSE) {
 			DPRINTF(30, "Dead. Cleaning.");
 			/* Dead */
 		} else {
 			DPRINTF(30, "%s: Signaling process.\n", __func__);
 			/* Alive, try to ask him. */
 			ret = vt_window_preswitch(vw, curvw);
 			/* Wait for process answer or timeout. */
 			return (ret);
 		}
 		DPRINTF(30, "\n");
 	}
 
 	ret = vt_late_window_switch(vw);
 	return (ret);
 }
 
 /* Switch window ignoring process locking. */
 static int
 vt_window_switch(struct vt_window *vw)
 {
 	struct vt_device *vd = vw->vw_device;
 	struct vt_window *curvw = vd->vd_curwindow;
 	keyboard_t *kbd;
 
 	if (kdb_active) {
 		/*
 		 * When grabbing the console for the debugger, avoid
 		 * locks as that can result in deadlock.  While this
 		 * could use try locks, that wouldn't really make a
 		 * difference as there are sufficient barriers in
 		 * debugger entry/exit to be equivalent to
 		 * successfully try-locking here.
 		 */
 		if (curvw == vw)
 			return (0);
 		if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE)))
 			return (EINVAL);
 
 		vd->vd_curwindow = vw;
 		vd->vd_flags |= VDF_INVALID;
 		if (vd->vd_driver->vd_postswitch)
 			vd->vd_driver->vd_postswitch(vd);
 		return (0);
 	}
 
 	VT_LOCK(vd);
 	if (curvw == vw) {
 		/*
 		 * Nothing to do, except ensure the driver has the opportunity to
 		 * switch to console mode when panicking, making sure the panic
 		 * is readable (even when a GUI was using ttyv0).
 		 */
-		if ((kdb_active || panicstr) && vd->vd_driver->vd_postswitch)
+		if ((kdb_active || KERNEL_PANICKED()) &&
+		    vd->vd_driver->vd_postswitch)
 			vd->vd_driver->vd_postswitch(vd);
 		VT_UNLOCK(vd);
 		return (0);
 	}
 	if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE))) {
 		VT_UNLOCK(vd);
 		return (EINVAL);
 	}
 
 	vt_suspend_flush_timer(vd);
 
 	vd->vd_curwindow = vw;
 	vd->vd_flags |= VDF_INVALID;
 	cv_broadcast(&vd->vd_winswitch);
 	VT_UNLOCK(vd);
 
 	if (vd->vd_driver->vd_postswitch)
 		vd->vd_driver->vd_postswitch(vd);
 
 	vt_resume_flush_timer(vw, 0);
 
 	/* Restore per-window keyboard mode. */
 	mtx_lock(&Giant);
 	if ((kbd = vd->vd_keyboard) != NULL) {
 		if (curvw->vw_kbdmode == K_XLATE)
 			vt_save_kbd_state(curvw, kbd);
 
 		vt_update_kbd_mode(vw, kbd);
 		vt_update_kbd_state(vw, kbd);
 	}
 	mtx_unlock(&Giant);
 	DPRINTF(10, "%s(ttyv%d) done\n", __func__, vw->vw_number);
 
 	return (0);
 }
 
 void
 vt_termsize(struct vt_device *vd, struct vt_font *vf, term_pos_t *size)
 {
 
 	size->tp_row = vd->vd_height;
 	if (vt_draw_logo_cpus)
 		size->tp_row -= vt_logo_sprite_height;
 	size->tp_col = vd->vd_width;
 	if (vf != NULL) {
 		size->tp_row = MIN(size->tp_row / vf->vf_height,
 		    PIXEL_HEIGHT(VT_FB_MAX_HEIGHT));
 		size->tp_col = MIN(size->tp_col / vf->vf_width,
 		    PIXEL_WIDTH(VT_FB_MAX_WIDTH));
 	}
 }
 
 static inline void
 vt_termrect(struct vt_device *vd, struct vt_font *vf, term_rect_t *rect)
 {
 
 	rect->tr_begin.tp_row = rect->tr_begin.tp_col = 0;
 	if (vt_draw_logo_cpus)
 		rect->tr_begin.tp_row = vt_logo_sprite_height;
 
 	rect->tr_end.tp_row = vd->vd_height;
 	rect->tr_end.tp_col = vd->vd_width;
 
 	if (vf != NULL) {
 		rect->tr_begin.tp_row =
 		    howmany(rect->tr_begin.tp_row, vf->vf_height);
 
 		rect->tr_end.tp_row = MIN(rect->tr_end.tp_row / vf->vf_height,
 		    PIXEL_HEIGHT(VT_FB_MAX_HEIGHT));
 		rect->tr_end.tp_col = MIN(rect->tr_end.tp_col / vf->vf_width,
 		    PIXEL_WIDTH(VT_FB_MAX_WIDTH));
 	}
 }
 
 void
 vt_winsize(struct vt_device *vd, struct vt_font *vf, struct winsize *size)
 {
 
 	size->ws_ypixel = vd->vd_height;
 	if (vt_draw_logo_cpus)
 		size->ws_ypixel -= vt_logo_sprite_height;
 	size->ws_row = size->ws_ypixel;
 	size->ws_col = size->ws_xpixel = vd->vd_width;
 	if (vf != NULL) {
 		size->ws_row = MIN(size->ws_row / vf->vf_height,
 		    PIXEL_HEIGHT(VT_FB_MAX_HEIGHT));
 		size->ws_col = MIN(size->ws_col / vf->vf_width,
 		    PIXEL_WIDTH(VT_FB_MAX_WIDTH));
 	}
 }
 
 void
 vt_compute_drawable_area(struct vt_window *vw)
 {
 	struct vt_device *vd;
 	struct vt_font *vf;
 	vt_axis_t height;
 
 	vd = vw->vw_device;
 
 	if (vw->vw_font == NULL) {
 		vw->vw_draw_area.tr_begin.tp_col = 0;
 		vw->vw_draw_area.tr_begin.tp_row = 0;
 		if (vt_draw_logo_cpus)
 			vw->vw_draw_area.tr_begin.tp_row = vt_logo_sprite_height;
 		vw->vw_draw_area.tr_end.tp_col = vd->vd_width;
 		vw->vw_draw_area.tr_end.tp_row = vd->vd_height;
 		return;
 	}
 
 	vf = vw->vw_font;
 
 	/*
 	 * Compute the drawable area, so that the text is centered on
 	 * the screen.
 	 */
 
 	height = vd->vd_height;
 	if (vt_draw_logo_cpus)
 		height -= vt_logo_sprite_height;
 	vw->vw_draw_area.tr_begin.tp_col = (vd->vd_width % vf->vf_width) / 2;
 	vw->vw_draw_area.tr_begin.tp_row = (height % vf->vf_height) / 2;
 	if (vt_draw_logo_cpus)
 		vw->vw_draw_area.tr_begin.tp_row += vt_logo_sprite_height;
 	vw->vw_draw_area.tr_end.tp_col = vw->vw_draw_area.tr_begin.tp_col +
 	    rounddown(vd->vd_width, vf->vf_width);
 	vw->vw_draw_area.tr_end.tp_row = vw->vw_draw_area.tr_begin.tp_row +
 	    rounddown(height, vf->vf_height);
 }
 
 static void
 vt_scroll(struct vt_window *vw, int offset, int whence)
 {
 	int diff;
 	term_pos_t size;
 
 	if ((vw->vw_flags & VWF_SCROLL) == 0)
 		return;
 
 	vt_termsize(vw->vw_device, vw->vw_font, &size);
 
 	diff = vthistory_seek(&vw->vw_buf, offset, whence);
 	if (diff)
 		vw->vw_device->vd_flags |= VDF_INVALID;
 	vt_resume_flush_timer(vw, 0);
 }
 
 static int
 vt_machine_kbdevent(struct vt_device *vd, int c)
 {
 
 	switch (c) {
 	case SPCLKEY | DBG: /* kbdmap(5) keyword `debug`. */
 		if (vt_kbd_debug) {
 			kdb_enter(KDB_WHY_BREAK, "manual escape to debugger");
 #if VT_ALT_TO_ESC_HACK
 			/*
 			 * There's an unfortunate conflict between SPCLKEY|DBG
 			 * and VT_ALT_TO_ESC_HACK.  Just assume they didn't mean
 			 * it if we got to here.
 			 */
 			vd->vd_kbstate &= ~ALKED;
 #endif
 		}
 		return (1);
 	case SPCLKEY | HALT: /* kbdmap(5) keyword `halt`. */
 		if (vt_kbd_halt)
 			shutdown_nice(RB_HALT);
 		return (1);
 	case SPCLKEY | PASTE: /* kbdmap(5) keyword `paste`. */
 #ifndef SC_NO_CUTPASTE
 		/* Insert text from cut-paste buffer. */
 		vt_mouse_paste();
 #endif
 		break;
 	case SPCLKEY | PDWN: /* kbdmap(5) keyword `pdwn`. */
 		if (vt_kbd_poweroff)
 			shutdown_nice(RB_HALT|RB_POWEROFF);
 		return (1);
 	case SPCLKEY | PNC: /* kbdmap(5) keyword `panic`. */
 		/*
 		 * Request to immediate panic if sysctl
 		 * kern.vt.enable_panic_key allow it.
 		 */
 		if (vt_kbd_panic)
 			panic("Forced by the panic key");
 		return (1);
 	case SPCLKEY | RBT: /* kbdmap(5) keyword `boot`. */
 		if (vt_kbd_reboot)
 			shutdown_nice(RB_AUTOBOOT);
 		return (1);
 	case SPCLKEY | SPSC: /* kbdmap(5) keyword `spsc`. */
 		/* Force activatation/deactivation of the screen saver. */
 		/* TODO */
 		return (1);
 	case SPCLKEY | STBY: /* XXX Not present in kbdcontrol parser. */
 		/* Put machine into Stand-By mode. */
 		power_pm_suspend(POWER_SLEEP_STATE_STANDBY);
 		return (1);
 	case SPCLKEY | SUSP: /* kbdmap(5) keyword `susp`. */
 		/* Suspend machine. */
 		power_pm_suspend(POWER_SLEEP_STATE_SUSPEND);
 		return (1);
 	}
 
 	return (0);
 }
 
 static void
 vt_scrollmode_kbdevent(struct vt_window *vw, int c, int console)
 {
 	struct vt_device *vd;
 	term_pos_t size;
 
 	vd = vw->vw_device;
 	/* Only special keys handled in ScrollLock mode */
 	if ((c & SPCLKEY) == 0)
 		return;
 
 	c &= ~SPCLKEY;
 
 	if (console == 0) {
 		if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
 			vw = vd->vd_windows[c - F_SCR];
 			vt_proc_window_switch(vw);
 			return;
 		}
 		VT_LOCK(vd);
 	}
 
 	switch (c) {
 	case SLK: {
 		/* Turn scrolling off. */
 		vt_scroll(vw, 0, VHS_END);
 		VTBUF_SLCK_DISABLE(&vw->vw_buf);
 		vw->vw_flags &= ~VWF_SCROLL;
 		break;
 	}
 	case FKEY | F(49): /* Home key. */
 		vt_scroll(vw, 0, VHS_SET);
 		break;
 	case FKEY | F(50): /* Arrow up. */
 		vt_scroll(vw, -1, VHS_CUR);
 		break;
 	case FKEY | F(51): /* Page up. */
 		vt_termsize(vd, vw->vw_font, &size);
 		vt_scroll(vw, -size.tp_row, VHS_CUR);
 		break;
 	case FKEY | F(57): /* End key. */
 		vt_scroll(vw, 0, VHS_END);
 		break;
 	case FKEY | F(58): /* Arrow down. */
 		vt_scroll(vw, 1, VHS_CUR);
 		break;
 	case FKEY | F(59): /* Page down. */
 		vt_termsize(vd, vw->vw_font, &size);
 		vt_scroll(vw, size.tp_row, VHS_CUR);
 		break;
 	}
 
 	if (console == 0)
 		VT_UNLOCK(vd);
 }
 
 static int
 vt_processkey(keyboard_t *kbd, struct vt_device *vd, int c)
 {
 	struct vt_window *vw = vd->vd_curwindow;
 
 	random_harvest_queue(&c, sizeof(c), RANDOM_KEYBOARD);
 #if VT_ALT_TO_ESC_HACK
 	if (c & RELKEY) {
 		switch (c & ~RELKEY) {
 		case (SPCLKEY | RALT):
 			if (vt_enable_altgr != 0)
 				break;
 		case (SPCLKEY | LALT):
 			vd->vd_kbstate &= ~ALKED;
 		}
 		/* Other keys ignored for RELKEY event. */
 		return (0);
 	} else {
 		switch (c & ~RELKEY) {
 		case (SPCLKEY | RALT):
 			if (vt_enable_altgr != 0)
 				break;
 		case (SPCLKEY | LALT):
 			vd->vd_kbstate |= ALKED;
 		}
 	}
 #else
 	if (c & RELKEY)
 		/* Other keys ignored for RELKEY event. */
 		return (0);
 #endif
 
 	if (vt_machine_kbdevent(vd, c))
 		return (0);
 
 	if (vw->vw_flags & VWF_SCROLL) {
 		vt_scrollmode_kbdevent(vw, c, 0/* Not a console */);
 		/* Scroll mode keys handled, nothing to do more. */
 		return (0);
 	}
 
 	if (c & SPCLKEY) {
 		c &= ~SPCLKEY;
 
 		if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
 			vw = vd->vd_windows[c - F_SCR];
 			vt_proc_window_switch(vw);
 			return (0);
 		}
 
 		switch (c) {
 		case NEXT:
 			/* Switch to next VT. */
 			c = (vw->vw_number + 1) % VT_MAXWINDOWS;
 			vw = vd->vd_windows[c];
 			vt_proc_window_switch(vw);
 			return (0);
 		case PREV:
 			/* Switch to previous VT. */
 			c = (vw->vw_number + VT_MAXWINDOWS - 1) % VT_MAXWINDOWS;
 			vw = vd->vd_windows[c];
 			vt_proc_window_switch(vw);
 			return (0);
 		case SLK: {
 			vt_save_kbd_state(vw, kbd);
 			VT_LOCK(vd);
 			if (vw->vw_kbdstate & SLKED) {
 				/* Turn scrolling on. */
 				vw->vw_flags |= VWF_SCROLL;
 				VTBUF_SLCK_ENABLE(&vw->vw_buf);
 			} else {
 				/* Turn scrolling off. */
 				vw->vw_flags &= ~VWF_SCROLL;
 				VTBUF_SLCK_DISABLE(&vw->vw_buf);
 				vt_scroll(vw, 0, VHS_END);
 			}
 			VT_UNLOCK(vd);
 			break;
 		}
 		case FKEY | F(1):  case FKEY | F(2):  case FKEY | F(3):
 		case FKEY | F(4):  case FKEY | F(5):  case FKEY | F(6):
 		case FKEY | F(7):  case FKEY | F(8):  case FKEY | F(9):
 		case FKEY | F(10): case FKEY | F(11): case FKEY | F(12):
 			/* F1 through F12 keys. */
 			terminal_input_special(vw->vw_terminal,
 			    TKEY_F1 + c - (FKEY | F(1)));
 			break;
 		case FKEY | F(49): /* Home key. */
 			terminal_input_special(vw->vw_terminal, TKEY_HOME);
 			break;
 		case FKEY | F(50): /* Arrow up. */
 			terminal_input_special(vw->vw_terminal, TKEY_UP);
 			break;
 		case FKEY | F(51): /* Page up. */
 			terminal_input_special(vw->vw_terminal, TKEY_PAGE_UP);
 			break;
 		case FKEY | F(53): /* Arrow left. */
 			terminal_input_special(vw->vw_terminal, TKEY_LEFT);
 			break;
 		case FKEY | F(55): /* Arrow right. */
 			terminal_input_special(vw->vw_terminal, TKEY_RIGHT);
 			break;
 		case FKEY | F(57): /* End key. */
 			terminal_input_special(vw->vw_terminal, TKEY_END);
 			break;
 		case FKEY | F(58): /* Arrow down. */
 			terminal_input_special(vw->vw_terminal, TKEY_DOWN);
 			break;
 		case FKEY | F(59): /* Page down. */
 			terminal_input_special(vw->vw_terminal, TKEY_PAGE_DOWN);
 			break;
 		case FKEY | F(60): /* Insert key. */
 			terminal_input_special(vw->vw_terminal, TKEY_INSERT);
 			break;
 		case FKEY | F(61): /* Delete key. */
 			terminal_input_special(vw->vw_terminal, TKEY_DELETE);
 			break;
 		}
 	} else if (KEYFLAGS(c) == 0) {
 		/* Don't do UTF-8 conversion when doing raw mode. */
 		if (vw->vw_kbdmode == K_XLATE) {
 #if VT_ALT_TO_ESC_HACK
 			if (vd->vd_kbstate & ALKED) {
 				/*
 				 * Prepend ESC sequence if one of ALT keys down.
 				 */
 				terminal_input_char(vw->vw_terminal, 0x1b);
 			}
 #endif
 #if defined(KDB)
 			kdb_alt_break(c, &vd->vd_altbrk);
 #endif
 			terminal_input_char(vw->vw_terminal, KEYCHAR(c));
 		} else
 			terminal_input_raw(vw->vw_terminal, c);
 	}
 	return (0);
 }
 
 static int
 vt_kbdevent(keyboard_t *kbd, int event, void *arg)
 {
 	struct vt_device *vd = arg;
 	int c;
 
 	switch (event) {
 	case KBDIO_KEYINPUT:
 		break;
 	case KBDIO_UNLOADING:
 		mtx_lock(&Giant);
 		vd->vd_keyboard = NULL;
 		kbd_release(kbd, (void *)vd);
 		mtx_unlock(&Giant);
 		return (0);
 	default:
 		return (EINVAL);
 	}
 
 	while ((c = kbdd_read_char(kbd, 0)) != NOKEY)
 		vt_processkey(kbd, vd, c);
 
 	return (0);
 }
 
 static int
 vt_allocate_keyboard(struct vt_device *vd)
 {
 	int		 grabbed, i, idx0, idx;
 	keyboard_t	*k0, *k;
 	keyboard_info_t	 ki;
 
 	/*
 	 * If vt_upgrade() happens while the console is grabbed, we are
 	 * potentially going to switch keyboard devices while the keyboard is in
 	 * use. Unwind the grabbing of the current keyboard first, then we will
 	 * re-grab the new keyboard below, before we return.
 	 */
 	if (vd->vd_curwindow == &vt_conswindow) {
 		grabbed = vd->vd_curwindow->vw_grabbed;
 		for (i = 0; i < grabbed; ++i)
 			vtterm_cnungrab_noswitch(vd, vd->vd_curwindow);
 	}
 
 	idx0 = kbd_allocate("kbdmux", -1, vd, vt_kbdevent, vd);
 	if (idx0 >= 0) {
 		DPRINTF(20, "%s: kbdmux allocated, idx = %d\n", __func__, idx0);
 		k0 = kbd_get_keyboard(idx0);
 
 		for (idx = kbd_find_keyboard2("*", -1, 0);
 		     idx != -1;
 		     idx = kbd_find_keyboard2("*", -1, idx + 1)) {
 			k = kbd_get_keyboard(idx);
 
 			if (idx == idx0 || KBD_IS_BUSY(k))
 				continue;
 
 			bzero(&ki, sizeof(ki));
 			strncpy(ki.kb_name, k->kb_name, sizeof(ki.kb_name));
 			ki.kb_name[sizeof(ki.kb_name) - 1] = '\0';
 			ki.kb_unit = k->kb_unit;
 
 			kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
 		}
 	} else {
 		DPRINTF(20, "%s: no kbdmux allocated\n", __func__);
 		idx0 = kbd_allocate("*", -1, vd, vt_kbdevent, vd);
 		if (idx0 < 0) {
 			DPRINTF(10, "%s: No keyboard found.\n", __func__);
 			return (-1);
 		}
 		k0 = kbd_get_keyboard(idx0);
 	}
 	vd->vd_keyboard = k0;
 	DPRINTF(20, "%s: vd_keyboard = %d\n", __func__,
 	    vd->vd_keyboard->kb_index);
 
 	if (vd->vd_curwindow == &vt_conswindow) {
 		for (i = 0; i < grabbed; ++i)
 			vtterm_cngrab_noswitch(vd, vd->vd_curwindow);
 	}
 
 	return (idx0);
 }
 
 #define DEVCTL_LEN 64
 static void
 vtterm_devctl(bool enabled, bool hushed, int hz, sbintime_t duration)
 {
 	struct sbuf sb;
 	char *buf;
 
 	buf = malloc(DEVCTL_LEN, M_VT, M_NOWAIT);
 	if (buf == NULL)
 		return;
 	sbuf_new(&sb, buf, DEVCTL_LEN, SBUF_FIXEDLEN);
 	sbuf_printf(&sb, "enabled=%s hushed=%s hz=%d duration_ms=%d",
 	    enabled ? "true" : "false", hushed ? "true" : "false",
 	    hz, (int)(duration / SBT_1MS));
 	sbuf_finish(&sb);
 	if (sbuf_error(&sb) == 0)
 		devctl_notify("VT", "BELL", "RING", sbuf_data(&sb));
 	sbuf_delete(&sb);
 	free(buf, M_VT);
 }
 
 static void
 vtterm_bell(struct terminal *tm)
 {
 	struct vt_window *vw = tm->tm_softc;
 	struct vt_device *vd = vw->vw_device;
 
 	vtterm_devctl(vt_enable_bell, vd->vd_flags & VDF_QUIET_BELL,
 	    vw->vw_bell_pitch, vw->vw_bell_duration);
 
 	if (!vt_enable_bell)
 		return;
 
 	if (vd->vd_flags & VDF_QUIET_BELL)
 		return;
 
 	if (vw->vw_bell_pitch == 0 ||
 	    vw->vw_bell_duration == 0)
 		return;
 
 	sysbeep(vw->vw_bell_pitch, vw->vw_bell_duration);
 }
 
 static void
 vtterm_beep(struct terminal *tm, u_int param)
 {
 	u_int freq;
 	sbintime_t period;
 	struct vt_window *vw = tm->tm_softc;
 	struct vt_device *vd = vw->vw_device;
 
 	if ((param == 0) || ((param & 0xffff) == 0)) {
 		vtterm_bell(tm);
 		return;
 	}
 
 	period = ((param >> 16) & 0xffff) * SBT_1MS;
 	freq = 1193182 / (param & 0xffff);
 
 	vtterm_devctl(vt_enable_bell, vd->vd_flags & VDF_QUIET_BELL,
 	    freq, period);
 
 	if (!vt_enable_bell)
 		return;
 
 	sysbeep(freq, period);
 }
 
 static void
 vtterm_cursor(struct terminal *tm, const term_pos_t *p)
 {
 	struct vt_window *vw = tm->tm_softc;
 
 	vtbuf_cursor_position(&vw->vw_buf, p);
 }
 
 static void
 vtterm_putchar(struct terminal *tm, const term_pos_t *p, term_char_t c)
 {
 	struct vt_window *vw = tm->tm_softc;
 
 	vtbuf_putchar(&vw->vw_buf, p, c);
 }
 
 static void
 vtterm_fill(struct terminal *tm, const term_rect_t *r, term_char_t c)
 {
 	struct vt_window *vw = tm->tm_softc;
 
 	vtbuf_fill(&vw->vw_buf, r, c);
 }
 
 static void
 vtterm_copy(struct terminal *tm, const term_rect_t *r,
     const term_pos_t *p)
 {
 	struct vt_window *vw = tm->tm_softc;
 
 	vtbuf_copy(&vw->vw_buf, r, p);
 }
 
 static void
 vtterm_param(struct terminal *tm, int cmd, unsigned int arg)
 {
 	struct vt_window *vw = tm->tm_softc;
 
 	switch (cmd) {
 	case TP_SETLOCALCURSOR:
 		/*
 		 * 0 means normal (usually block), 1 means hidden, and
 		 * 2 means blinking (always block) for compatibility with
 		 * syscons.  We don't support any changes except hiding,
 		 * so must map 2 to 0.
 		 */
 		arg = (arg == 1) ? 0 : 1;
 		/* FALLTHROUGH */
 	case TP_SHOWCURSOR:
 		vtbuf_cursor_visibility(&vw->vw_buf, arg);
 		vt_resume_flush_timer(vw, 0);
 		break;
 	case TP_MOUSE:
 		vw->vw_mouse_level = arg;
 		break;
 	case TP_SETBELLPD:
 		vw->vw_bell_pitch = TP_SETBELLPD_PITCH(arg);
 		vw->vw_bell_duration =
 		    TICKS_2_MSEC(TP_SETBELLPD_DURATION(arg)) * SBT_1MS;
 		break;
 	}
 }
 
 void
 vt_determine_colors(term_char_t c, int cursor,
     term_color_t *fg, term_color_t *bg)
 {
 	term_color_t tmp;
 	int invert;
 
 	invert = 0;
 
 	*fg = TCHAR_FGCOLOR(c);
 	if (TCHAR_FORMAT(c) & TF_BOLD)
 		*fg = TCOLOR_LIGHT(*fg);
 	*bg = TCHAR_BGCOLOR(c);
 	if (TCHAR_FORMAT(c) & TF_BLINK)
 		*bg = TCOLOR_LIGHT(*bg);
 
 	if (TCHAR_FORMAT(c) & TF_REVERSE)
 		invert ^= 1;
 	if (cursor)
 		invert ^= 1;
 
 	if (invert) {
 		tmp = *fg;
 		*fg = *bg;
 		*bg = tmp;
 	}
 }
 
 #ifndef SC_NO_CUTPASTE
 int
 vt_is_cursor_in_area(const struct vt_device *vd, const term_rect_t *area)
 {
 	unsigned int mx, my;
 
 	/*
 	 * We use the cursor position saved during the current refresh,
 	 * in case the cursor moved since.
 	 */
 	mx = vd->vd_mx_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_col;
 	my = vd->vd_my_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_row;
 
 	if (mx >= area->tr_end.tp_col ||
 	    mx + vd->vd_mcursor->width <= area->tr_begin.tp_col ||
 	    my >= area->tr_end.tp_row ||
 	    my + vd->vd_mcursor->height <= area->tr_begin.tp_row)
 		return (0);
 	return (1);
 }
 
 static void
 vt_mark_mouse_position_as_dirty(struct vt_device *vd, int locked)
 {
 	term_rect_t area;
 	struct vt_window *vw;
 	struct vt_font *vf;
 	int x, y;
 
 	vw = vd->vd_curwindow;
 	vf = vw->vw_font;
 
 	x = vd->vd_mx_drawn;
 	y = vd->vd_my_drawn;
 
 	if (vf != NULL) {
 		area.tr_begin.tp_col = x / vf->vf_width;
 		area.tr_begin.tp_row = y / vf->vf_height;
 		area.tr_end.tp_col =
 		    ((x + vd->vd_mcursor->width) / vf->vf_width) + 1;
 		area.tr_end.tp_row =
 		    ((y + vd->vd_mcursor->height) / vf->vf_height) + 1;
 	} else {
 		/*
 		 * No font loaded (ie. vt_vga operating in textmode).
 		 *
 		 * FIXME: This fake area needs to be revisited once the
 		 * mouse cursor is supported in vt_vga's textmode.
 		 */
 		area.tr_begin.tp_col = x;
 		area.tr_begin.tp_row = y;
 		area.tr_end.tp_col = x + 2;
 		area.tr_end.tp_row = y + 2;
 	}
 
 	if (!locked)
 		vtbuf_lock(&vw->vw_buf);
 	if (vd->vd_driver->vd_invalidate_text)
 		vd->vd_driver->vd_invalidate_text(vd, &area);
 	vtbuf_dirty(&vw->vw_buf, &area);
 	if (!locked)
 		vtbuf_unlock(&vw->vw_buf);
 }
 #endif
 
 static void
 vt_set_border(struct vt_device *vd, const term_rect_t *area,
     term_color_t c)
 {
 	vd_drawrect_t *drawrect = vd->vd_driver->vd_drawrect;
 
 	if (drawrect == NULL)
 		return;
 
 	/* Top bar */
 	if (area->tr_begin.tp_row > 0)
 		drawrect(vd, 0, 0, vd->vd_width - 1,
 		    area->tr_begin.tp_row - 1, 1, c);
 
 	/* Left bar */
 	if (area->tr_begin.tp_col > 0)
 		drawrect(vd, 0, area->tr_begin.tp_row,
 		    area->tr_begin.tp_col - 1, area->tr_end.tp_row - 1, 1, c);
 
 	/* Right bar */
 	if (area->tr_end.tp_col < vd->vd_width)
 		drawrect(vd, area->tr_end.tp_col, area->tr_begin.tp_row,
 		    vd->vd_width - 1, area->tr_end.tp_row - 1, 1, c);
 
 	/* Bottom bar */
 	if (area->tr_end.tp_row < vd->vd_height)
 		drawrect(vd, 0, area->tr_end.tp_row, vd->vd_width - 1,
 		    vd->vd_height - 1, 1, c);
 }
 
 static int
 vt_flush(struct vt_device *vd)
 {
 	struct vt_window *vw;
 	struct vt_font *vf;
 	term_rect_t tarea;
 #ifndef SC_NO_CUTPASTE
 	int cursor_was_shown, cursor_moved;
 #endif
 
 	vw = vd->vd_curwindow;
 	if (vw == NULL)
 		return (0);
 
 	if (vd->vd_flags & VDF_SPLASH || vw->vw_flags & VWF_BUSY)
 		return (0);
 
 	vf = vw->vw_font;
 	if (((vd->vd_flags & VDF_TEXTMODE) == 0) && (vf == NULL))
 		return (0);
 
 	vtbuf_lock(&vw->vw_buf);
 
 #ifndef SC_NO_CUTPASTE
 	cursor_was_shown = vd->vd_mshown;
 	cursor_moved = (vd->vd_mx != vd->vd_mx_drawn ||
 	    vd->vd_my != vd->vd_my_drawn);
 
 	/* Check if the cursor should be displayed or not. */
 	if ((vd->vd_flags & VDF_MOUSECURSOR) && /* Mouse support enabled. */
 	    !(vw->vw_flags & VWF_MOUSE_HIDE) && /* Cursor displayed.      */
 	    !kdb_active && !KERNEL_PANICKED()) {  /* DDB inactive.          */
 		vd->vd_mshown = 1;
 	} else {
 		vd->vd_mshown = 0;
 	}
 
 	/*
 	 * If the cursor changed display state or moved, we must mark
 	 * the old position as dirty, so that it's erased.
 	 */
 	if (cursor_was_shown != vd->vd_mshown ||
 	    (vd->vd_mshown && cursor_moved))
 		vt_mark_mouse_position_as_dirty(vd, true);
 
 	/*
          * Save position of the mouse cursor. It's used by backends to
          * know where to draw the cursor and during the next refresh to
          * erase the previous position.
 	 */
 	vd->vd_mx_drawn = vd->vd_mx;
 	vd->vd_my_drawn = vd->vd_my;
 
 	/*
 	 * If the cursor is displayed and has moved since last refresh,
 	 * mark the new position as dirty.
 	 */
 	if (vd->vd_mshown && cursor_moved)
 		vt_mark_mouse_position_as_dirty(vd, true);
 #endif
 
 	vtbuf_undirty(&vw->vw_buf, &tarea);
 
 	/* Force a full redraw when the screen contents might be invalid. */
 	if (vd->vd_flags & (VDF_INVALID | VDF_SUSPENDED)) {
 		const teken_attr_t *a;
 
 		vd->vd_flags &= ~VDF_INVALID;
 
 		a = teken_get_curattr(&vw->vw_terminal->tm_emulator);
 		vt_set_border(vd, &vw->vw_draw_area, a->ta_bgcolor);
 		vt_termrect(vd, vf, &tarea);
 		if (vd->vd_driver->vd_invalidate_text)
 			vd->vd_driver->vd_invalidate_text(vd, &tarea);
 		if (vt_draw_logo_cpus)
 			vtterm_draw_cpu_logos(vd);
 	}
 
 	if (tarea.tr_begin.tp_col < tarea.tr_end.tp_col) {
 		vd->vd_driver->vd_bitblt_text(vd, vw, &tarea);
 		vtbuf_unlock(&vw->vw_buf);
 		return (1);
 	}
 
 	vtbuf_unlock(&vw->vw_buf);
 	return (0);
 }
 
 static void
 vt_timer(void *arg)
 {
 	struct vt_device *vd;
 	int changed;
 
 	vd = arg;
 	/* Update screen if required. */
 	changed = vt_flush(vd);
 
 	/* Schedule for next update. */
 	if (changed)
 		vt_schedule_flush(vd, 0);
 	else
 		vd->vd_timer_armed = 0;
 }
 
 static void
 vtterm_pre_input(struct terminal *tm)
 {
 	struct vt_window *vw = tm->tm_softc;
 
 	vtbuf_lock(&vw->vw_buf);
 }
 
 static void
 vtterm_post_input(struct terminal *tm)
 {
 	struct vt_window *vw = tm->tm_softc;
 
 	vtbuf_unlock(&vw->vw_buf);
 	vt_resume_flush_timer(vw, 0);
 }
 
 static void
 vtterm_done(struct terminal *tm)
 {
 	struct vt_window *vw = tm->tm_softc;
 	struct vt_device *vd = vw->vw_device;
 
 	if (kdb_active || KERNEL_PANICKED()) {
 		/* Switch to the debugger. */
 		if (vd->vd_curwindow != vw) {
 			vd->vd_curwindow = vw;
 			vd->vd_flags |= VDF_INVALID;
 			if (vd->vd_driver->vd_postswitch)
 				vd->vd_driver->vd_postswitch(vd);
 		}
 		vd->vd_flags &= ~VDF_SPLASH;
 		vt_flush(vd);
 	} else if (!(vd->vd_flags & VDF_ASYNC)) {
 		vt_flush(vd);
 	}
 }
 
 #ifdef DEV_SPLASH
 static void
 vtterm_splash(struct vt_device *vd)
 {
 	vt_axis_t top, left;
 
 	/* Display a nice boot splash. */
 	if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) {
 		top = (vd->vd_height - vt_logo_height) / 2;
 		left = (vd->vd_width - vt_logo_width) / 2;
 		switch (vt_logo_depth) {
 		case 1:
 			/* XXX: Unhardcode colors! */
 			vd->vd_driver->vd_bitblt_bmp(vd, vd->vd_curwindow,
 			    vt_logo_image, NULL, vt_logo_width, vt_logo_height,
 			    left, top, TC_WHITE, TC_BLACK);
 		}
 		vd->vd_flags |= VDF_SPLASH;
 	}
 }
 #endif
 
 static struct vt_font *
 parse_font_info_static(struct font_info *fi)
 {
 	struct vt_font *vfp;
 	uintptr_t ptr;
 	uint32_t checksum;
 
 	if (fi == NULL)
 		return (NULL);
 
 	ptr = (uintptr_t)fi;
 	/*
 	 * Compute and verify checksum. The total sum of all the fields
 	 * must be 0.
 	 */
 	checksum = fi->fi_width;
 	checksum += fi->fi_height;
 	checksum += fi->fi_bitmap_size;
 	for (unsigned i = 0; i < VFNT_MAPS; i++)
 		checksum += fi->fi_map_count[i];
 
 	if (checksum + fi->fi_checksum != 0)
 		return (NULL);
 
 	ptr += sizeof(struct font_info);
 	ptr = roundup2(ptr, 8);
 
 	vfp = &vt_font_loader;
 	vfp->vf_height = fi->fi_height;
 	vfp->vf_width = fi->fi_width;
 	/* This is default font, set refcount 1 to disable removal. */
 	vfp->vf_refcount = 1;
 	for (unsigned i = 0; i < VFNT_MAPS; i++) {
 		if (fi->fi_map_count[i] == 0)
 			continue;
 		vfp->vf_map_count[i] = fi->fi_map_count[i];
 		vfp->vf_map[i] = (vfnt_map_t *)ptr;
 		ptr += (fi->fi_map_count[i] * sizeof(vfnt_map_t));
 		ptr = roundup2(ptr, 8);
 	}
 	vfp->vf_bytes = (uint8_t *)ptr;
 	return (vfp);
 }
 
 /*
  * Set up default font with allocated data structures.
  * However, we can not set refcount here, because it is already set and
  * incremented in vtterm_cnprobe() to avoid being released by font load from
  * userland.
  */
 static struct vt_font *
 parse_font_info(struct font_info *fi)
 {
 	struct vt_font *vfp;
 	uintptr_t ptr;
 	uint32_t checksum;
 	size_t size;
 
 	if (fi == NULL)
 		return (NULL);
 
 	ptr = (uintptr_t)fi;
 	/*
 	 * Compute and verify checksum. The total sum of all the fields
 	 * must be 0.
 	 */
 	checksum = fi->fi_width;
 	checksum += fi->fi_height;
 	checksum += fi->fi_bitmap_size;
 	for (unsigned i = 0; i < VFNT_MAPS; i++)
 		checksum += fi->fi_map_count[i];
 
 	if (checksum + fi->fi_checksum != 0)
 		return (NULL);
 
 	ptr += sizeof(struct font_info);
 	ptr = roundup2(ptr, 8);
 
 	vfp = &vt_font_loader;
 	vfp->vf_height = fi->fi_height;
 	vfp->vf_width = fi->fi_width;
 	for (unsigned i = 0; i < VFNT_MAPS; i++) {
 		if (fi->fi_map_count[i] == 0)
 			continue;
 		vfp->vf_map_count[i] = fi->fi_map_count[i];
 		size = fi->fi_map_count[i] * sizeof(vfnt_map_t);
 		vfp->vf_map[i] = malloc(size, M_VT, M_WAITOK | M_ZERO);
 		bcopy((vfnt_map_t *)ptr, vfp->vf_map[i], size);
 		ptr += size;
 		ptr = roundup2(ptr, 8);
 	}
 	vfp->vf_bytes = malloc(fi->fi_bitmap_size, M_VT, M_WAITOK | M_ZERO);
 	bcopy((uint8_t *)ptr, vfp->vf_bytes, fi->fi_bitmap_size);
 	return (vfp);
 }
 
 static void
 vt_init_font(void *arg)
 {
 	caddr_t kmdp;
 	struct font_info *fi;
 	struct vt_font *font;
 
 	kmdp = preload_search_by_type("elf kernel");
 	if (kmdp == NULL)
 		kmdp = preload_search_by_type("elf64 kernel");
 	fi = MD_FETCH(kmdp, MODINFOMD_FONT, struct font_info *);
 
 	font = parse_font_info(fi);
 	if (font != NULL)
 		vt_font_assigned = font;
 }
 
 SYSINIT(vt_init_font, SI_SUB_KMEM, SI_ORDER_ANY, vt_init_font, &vt_consdev);
 
 static void
 vt_init_font_static(void)
 {
 	caddr_t kmdp;
 	struct font_info *fi;
 	struct vt_font *font;
 
 	kmdp = preload_search_by_type("elf kernel");
 	if (kmdp == NULL)
 		kmdp = preload_search_by_type("elf64 kernel");
 	fi = MD_FETCH(kmdp, MODINFOMD_FONT, struct font_info *);
 
 	font = parse_font_info_static(fi);
 	if (font != NULL)
 		vt_font_assigned = font;
 }
 
 static void
 vtterm_cnprobe(struct terminal *tm, struct consdev *cp)
 {
 	struct vt_driver *vtd, **vtdlist, *vtdbest = NULL;
 	struct vt_window *vw = tm->tm_softc;
 	struct vt_device *vd = vw->vw_device;
 	struct winsize wsz;
 	const term_attr_t *a;
 
 	if (!vty_enabled(VTY_VT))
 		return;
 
 	if (vd->vd_flags & VDF_INITIALIZED)
 		/* Initialization already done. */
 		return;
 
 	SET_FOREACH(vtdlist, vt_drv_set) {
 		vtd = *vtdlist;
 		if (vtd->vd_probe == NULL)
 			continue;
 		if (vtd->vd_probe(vd) == CN_DEAD)
 			continue;
 		if ((vtdbest == NULL) ||
 		    (vtd->vd_priority > vtdbest->vd_priority))
 			vtdbest = vtd;
 	}
 	if (vtdbest == NULL) {
 		cp->cn_pri = CN_DEAD;
 		vd->vd_flags |= VDF_DEAD;
 	} else {
 		vd->vd_driver = vtdbest;
 		cp->cn_pri = vd->vd_driver->vd_init(vd);
 	}
 
 	/* Check if driver's vt_init return CN_DEAD. */
 	if (cp->cn_pri == CN_DEAD) {
 		vd->vd_flags |= VDF_DEAD;
 	}
 
 	/* Initialize any early-boot keyboard drivers */
 	kbd_configure(KB_CONF_PROBE_ONLY);
 
 	vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1);
 	vd->vd_windows[VT_CONSWINDOW] = vw;
 	sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw));
 
 	vt_init_font_static();
 
 	/* Attach default font if not in TEXTMODE. */
 	if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
 		vw->vw_font = vtfont_ref(vt_font_assigned);
 		vt_compute_drawable_area(vw);
 	}
 
 	/*
 	 * The original screen size was faked (_VTDEFW x _VTDEFH). Now
 	 * that we have the real viewable size, fix it in the static
 	 * buffer.
 	 */
 	if (vd->vd_width != 0 && vd->vd_height != 0)
 		vt_termsize(vd, vw->vw_font, &vw->vw_buf.vb_scr_size);
 
 	/* We need to access terminal attributes from vtbuf */
 	vw->vw_buf.vb_terminal = tm;
 	vtbuf_init_early(&vw->vw_buf);
 	vt_winsize(vd, vw->vw_font, &wsz);
 	a = teken_get_curattr(&tm->tm_emulator);
 	terminal_set_winsize_blank(tm, &wsz, 1, a);
 
 	if (vtdbest != NULL) {
 #ifdef DEV_SPLASH
 		if (!vt_splash_cpu)
 			vtterm_splash(vd);
 #endif
 		vd->vd_flags |= VDF_INITIALIZED;
 	}
 }
 
 static int
 vtterm_cngetc(struct terminal *tm)
 {
 	struct vt_window *vw = tm->tm_softc;
 	struct vt_device *vd = vw->vw_device;
 	keyboard_t *kbd;
 	u_int c;
 
 	if (vw->vw_kbdsq && *vw->vw_kbdsq)
 		return (*vw->vw_kbdsq++);
 
 	/* Make sure the splash screen is not there. */
 	if (vd->vd_flags & VDF_SPLASH) {
 		/* Remove splash */
 		vd->vd_flags &= ~VDF_SPLASH;
 		/* Mark screen as invalid to force update */
 		vd->vd_flags |= VDF_INVALID;
 		vt_flush(vd);
 	}
 
 	/* Stripped down keyboard handler. */
 	if ((kbd = vd->vd_keyboard) == NULL)
 		return (-1);
 
 	/* Force keyboard input mode to K_XLATE */
 	vw->vw_kbdmode = K_XLATE;
 	vt_update_kbd_mode(vw, kbd);
 
 	/* Switch the keyboard to polling to make it work here. */
 	kbdd_poll(kbd, TRUE);
 	c = kbdd_read_char(kbd, 0);
 	kbdd_poll(kbd, FALSE);
 	if (c & RELKEY)
 		return (-1);
 
 	if (vw->vw_flags & VWF_SCROLL) {
 		vt_scrollmode_kbdevent(vw, c, 1/* Console mode */);
 		vt_flush(vd);
 		return (-1);
 	}
 
 	/* Stripped down handling of vt_kbdevent(), without locking, etc. */
 	if (c & SPCLKEY) {
 		switch (c) {
 		case SPCLKEY | SLK:
 			vt_save_kbd_state(vw, kbd);
 			if (vw->vw_kbdstate & SLKED) {
 				/* Turn scrolling on. */
 				vw->vw_flags |= VWF_SCROLL;
 				VTBUF_SLCK_ENABLE(&vw->vw_buf);
 			} else {
 				/* Turn scrolling off. */
 				vt_scroll(vw, 0, VHS_END);
 				vw->vw_flags &= ~VWF_SCROLL;
 				VTBUF_SLCK_DISABLE(&vw->vw_buf);
 			}
 			break;
 		/* XXX: KDB can handle history. */
 		case SPCLKEY | FKEY | F(50): /* Arrow up. */
 			vw->vw_kbdsq = "\x1b[A";
 			break;
 		case SPCLKEY | FKEY | F(58): /* Arrow down. */
 			vw->vw_kbdsq = "\x1b[B";
 			break;
 		case SPCLKEY | FKEY | F(55): /* Arrow right. */
 			vw->vw_kbdsq = "\x1b[C";
 			break;
 		case SPCLKEY | FKEY | F(53): /* Arrow left. */
 			vw->vw_kbdsq = "\x1b[D";
 			break;
 		}
 
 		/* Force refresh to make scrollback work. */
 		vt_flush(vd);
 	} else if (KEYFLAGS(c) == 0) {
 		return (KEYCHAR(c));
 	}
 
 	if (vw->vw_kbdsq && *vw->vw_kbdsq)
 		return (*vw->vw_kbdsq++);
 
 	return (-1);
 }
 
 /*
  * These two do most of what we want to do in vtterm_cnungrab, but without
  * actually switching windows.  This is necessary for, e.g.,
  * vt_allocate_keyboard() to get the current keyboard into the state it needs to
  * be in without damaging the device's window state.
  *
  * Both return the current grab count, though it's only used in vtterm_cnungrab.
  */
 static int
 vtterm_cngrab_noswitch(struct vt_device *vd, struct vt_window *vw)
 {
 	keyboard_t *kbd;
 
 	if (vw->vw_grabbed++ > 0)
 		return (vw->vw_grabbed);
 
 	if ((kbd = vd->vd_keyboard) == NULL)
 		return (1);
 
 	/*
 	 * Make sure the keyboard is accessible even when the kbd device
 	 * driver is disabled.
 	 */
 	kbdd_enable(kbd);
 
 	/* We shall always use the keyboard in the XLATE mode here. */
 	vw->vw_prev_kbdmode = vw->vw_kbdmode;
 	vw->vw_kbdmode = K_XLATE;
 	vt_update_kbd_mode(vw, kbd);
 
 	kbdd_poll(kbd, TRUE);
 	return (1);
 }
 
 static int
 vtterm_cnungrab_noswitch(struct vt_device *vd, struct vt_window *vw)
 {
 	keyboard_t *kbd;
 
 	if (--vw->vw_grabbed > 0)
 		return (vw->vw_grabbed);
 
 	if ((kbd = vd->vd_keyboard) == NULL)
 		return (0);
 
 	kbdd_poll(kbd, FALSE);
 
 	vw->vw_kbdmode = vw->vw_prev_kbdmode;
 	vt_update_kbd_mode(vw, kbd);
 	kbdd_disable(kbd);
 	return (0);
 }
 
 static void
 vtterm_cngrab(struct terminal *tm)
 {
 	struct vt_device *vd;
 	struct vt_window *vw;
 
 	vw = tm->tm_softc;
 	vd = vw->vw_device;
 
 	/* To be restored after we ungrab. */
 	if (vd->vd_grabwindow == NULL)
 		vd->vd_grabwindow = vd->vd_curwindow;
 
 	if (!cold)
 		vt_window_switch(vw);
 
 	vtterm_cngrab_noswitch(vd, vw);
 }
 
 static void
 vtterm_cnungrab(struct terminal *tm)
 {
 	struct vt_device *vd;
 	struct vt_window *vw;
 
 	vw = tm->tm_softc;
 	vd = vw->vw_device;
 
 	MPASS(vd->vd_grabwindow != NULL);
 	if (vtterm_cnungrab_noswitch(vd, vw) != 0)
 		return;
 
 	if (!cold && vd->vd_grabwindow != vw)
 		vt_window_switch(vd->vd_grabwindow);
 
 	vd->vd_grabwindow = NULL;
 }
 
 static void
 vtterm_opened(struct terminal *tm, int opened)
 {
 	struct vt_window *vw = tm->tm_softc;
 	struct vt_device *vd = vw->vw_device;
 
 	VT_LOCK(vd);
 	vd->vd_flags &= ~VDF_SPLASH;
 	if (opened)
 		vw->vw_flags |= VWF_OPENED;
 	else {
 		vw->vw_flags &= ~VWF_OPENED;
 		/* TODO: finish ACQ/REL */
 	}
 	VT_UNLOCK(vd);
 }
 
 static int
 vt_change_font(struct vt_window *vw, struct vt_font *vf)
 {
 	struct vt_device *vd = vw->vw_device;
 	struct terminal *tm = vw->vw_terminal;
 	term_pos_t size;
 	struct winsize wsz;
 
 	/*
 	 * Changing fonts.
 	 *
 	 * Changing fonts is a little tricky.  We must prevent
 	 * simultaneous access to the device, so we must stop
 	 * the display timer and the terminal from accessing.
 	 * We need to switch fonts and grow our screen buffer.
 	 *
 	 * XXX: Right now the code uses terminal_mute() to
 	 * prevent data from reaching the console driver while
 	 * resizing the screen buffer.  This isn't elegant...
 	 */
 
 	VT_LOCK(vd);
 	if (vw->vw_flags & VWF_BUSY) {
 		/* Another process is changing the font. */
 		VT_UNLOCK(vd);
 		return (EBUSY);
 	}
 	vw->vw_flags |= VWF_BUSY;
 	VT_UNLOCK(vd);
 
 	vt_termsize(vd, vf, &size);
 	vt_winsize(vd, vf, &wsz);
 
 	/* Grow the screen buffer and terminal. */
 	terminal_mute(tm, 1);
 	vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
 	terminal_set_winsize_blank(tm, &wsz, 0, NULL);
 	terminal_set_cursor(tm, &vw->vw_buf.vb_cursor);
 	terminal_mute(tm, 0);
 
 	/* Actually apply the font to the current window. */
 	VT_LOCK(vd);
 	if (vw->vw_font != vf && vw->vw_font != NULL && vf != NULL) {
 		/*
 		 * In case vt_change_font called to update size we don't need
 		 * to update font link.
 		 */
 		vtfont_unref(vw->vw_font);
 		vw->vw_font = vtfont_ref(vf);
 	}
 
 	/*
 	 * Compute the drawable area and move the mouse cursor inside
 	 * it, in case the new area is smaller than the previous one.
 	 */
 	vt_compute_drawable_area(vw);
 	vd->vd_mx = min(vd->vd_mx,
 	    vw->vw_draw_area.tr_end.tp_col -
 	    vw->vw_draw_area.tr_begin.tp_col - 1);
 	vd->vd_my = min(vd->vd_my,
 	    vw->vw_draw_area.tr_end.tp_row -
 	    vw->vw_draw_area.tr_begin.tp_row - 1);
 
 	/* Force a full redraw the next timer tick. */
 	if (vd->vd_curwindow == vw) {
 		vd->vd_flags |= VDF_INVALID;
 		vt_resume_flush_timer(vw, 0);
 	}
 	vw->vw_flags &= ~VWF_BUSY;
 	VT_UNLOCK(vd);
 	return (0);
 }
 
 static int
 vt_proc_alive(struct vt_window *vw)
 {
 	struct proc *p;
 
 	if (vw->vw_smode.mode != VT_PROCESS)
 		return (FALSE);
 
 	if (vw->vw_proc) {
 		if ((p = pfind(vw->vw_pid)) != NULL)
 			PROC_UNLOCK(p);
 		if (vw->vw_proc == p)
 			return (TRUE);
 		vw->vw_proc = NULL;
 		vw->vw_smode.mode = VT_AUTO;
 		DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid);
 		vw->vw_pid = 0;
 	}
 	return (FALSE);
 }
 
 static int
 signal_vt_rel(struct vt_window *vw)
 {
 
 	if (vw->vw_smode.mode != VT_PROCESS)
 		return (FALSE);
 	if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
 		vw->vw_proc = NULL;
 		vw->vw_pid = 0;
 		return (TRUE);
 	}
 	vw->vw_flags |= VWF_SWWAIT_REL;
 	PROC_LOCK(vw->vw_proc);
 	kern_psignal(vw->vw_proc, vw->vw_smode.relsig);
 	PROC_UNLOCK(vw->vw_proc);
 	DPRINTF(1, "sending relsig to %d\n", vw->vw_pid);
 	return (TRUE);
 }
 
 static int
 signal_vt_acq(struct vt_window *vw)
 {
 
 	if (vw->vw_smode.mode != VT_PROCESS)
 		return (FALSE);
 	if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
 		cnavailable(vw->vw_terminal->consdev, FALSE);
 	if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
 		vw->vw_proc = NULL;
 		vw->vw_pid = 0;
 		return (TRUE);
 	}
 	vw->vw_flags |= VWF_SWWAIT_ACQ;
 	PROC_LOCK(vw->vw_proc);
 	kern_psignal(vw->vw_proc, vw->vw_smode.acqsig);
 	PROC_UNLOCK(vw->vw_proc);
 	DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid);
 	return (TRUE);
 }
 
 static int
 finish_vt_rel(struct vt_window *vw, int release, int *s)
 {
 
 	if (vw->vw_flags & VWF_SWWAIT_REL) {
 		vw->vw_flags &= ~VWF_SWWAIT_REL;
 		if (release) {
 			callout_drain(&vw->vw_proc_dead_timer);
 			(void)vt_late_window_switch(vw->vw_switch_to);
 		}
 		return (0);
 	}
 	return (EINVAL);
 }
 
 static int
 finish_vt_acq(struct vt_window *vw)
 {
 
 	if (vw->vw_flags & VWF_SWWAIT_ACQ) {
 		vw->vw_flags &= ~VWF_SWWAIT_ACQ;
 		return (0);
 	}
 	return (EINVAL);
 }
 
 #ifndef SC_NO_CUTPASTE
 static void
 vt_mouse_terminput_button(struct vt_device *vd, int button)
 {
 	struct vt_window *vw;
 	struct vt_font *vf;
 	char mouseb[6] = "\x1B[M";
 	int i, x, y;
 
 	vw = vd->vd_curwindow;
 	vf = vw->vw_font;
 
 	/* Translate to char position. */
 	x = vd->vd_mx / vf->vf_width;
 	y = vd->vd_my / vf->vf_height;
 	/* Avoid overflow. */
 	x = MIN(x, 255 - '!');
 	y = MIN(y, 255 - '!');
 
 	mouseb[3] = ' ' + button;
 	mouseb[4] = '!' + x;
 	mouseb[5] = '!' + y;
 
 	for (i = 0; i < sizeof(mouseb); i++)
 		terminal_input_char(vw->vw_terminal, mouseb[i]);
 }
 
 static void
 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event,
     int cnt)
 {
 
 	switch (type) {
 	case MOUSE_BUTTON_EVENT:
 		if (cnt > 0) {
 			/* Mouse button pressed. */
 			if (event & MOUSE_BUTTON1DOWN)
 				vt_mouse_terminput_button(vd, 0);
 			if (event & MOUSE_BUTTON2DOWN)
 				vt_mouse_terminput_button(vd, 1);
 			if (event & MOUSE_BUTTON3DOWN)
 				vt_mouse_terminput_button(vd, 2);
 		} else {
 			/* Mouse button released. */
 			vt_mouse_terminput_button(vd, 3);
 		}
 		break;
 #ifdef notyet
 	case MOUSE_MOTION_EVENT:
 		if (mouse->u.data.z < 0) {
 			/* Scroll up. */
 			sc_mouse_input_button(vd, 64);
 		} else if (mouse->u.data.z > 0) {
 			/* Scroll down. */
 			sc_mouse_input_button(vd, 65);
 		}
 		break;
 #endif
 	}
 }
 
 static void
 vt_mouse_paste()
 {
 	term_char_t *buf;
 	int i, len;
 
 	len = VD_PASTEBUFLEN(main_vd);
 	buf = VD_PASTEBUF(main_vd);
 	len /= sizeof(term_char_t);
 	for (i = 0; i < len; i++) {
 		if (buf[i] == '\0')
 			continue;
 		terminal_input_char(main_vd->vd_curwindow->vw_terminal,
 		    buf[i]);
 	}
 }
 
 void
 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel)
 {
 	struct vt_device *vd;
 	struct vt_window *vw;
 	struct vt_font *vf;
 	term_pos_t size;
 	int len, mark;
 
 	vd = main_vd;
 	vw = vd->vd_curwindow;
 	vf = vw->vw_font;
 	mark = 0;
 
 	if (vw->vw_flags & (VWF_MOUSE_HIDE | VWF_GRAPHICS))
 		/*
 		 * Either the mouse is disabled, or the window is in
 		 * "graphics mode". The graphics mode is usually set by
 		 * an X server, using the KDSETMODE ioctl.
 		 */
 		return;
 
 	if (vf == NULL)	/* Text mode. */
 		return;
 
 	/*
 	 * TODO: add flag about pointer position changed, to not redraw chars
 	 * under mouse pointer when nothing changed.
 	 */
 
 	if (vw->vw_mouse_level > 0)
 		vt_mouse_terminput(vd, type, x, y, event, cnt);
 
 	switch (type) {
 	case MOUSE_ACTION:
 	case MOUSE_MOTION_EVENT:
 		/* Movement */
 		x += vd->vd_mx;
 		y += vd->vd_my;
 
 		vt_termsize(vd, vf, &size);
 
 		/* Apply limits. */
 		x = MAX(x, 0);
 		y = MAX(y, 0);
 		x = MIN(x, (size.tp_col * vf->vf_width) - 1);
 		y = MIN(y, (size.tp_row * vf->vf_height) - 1);
 
 		vd->vd_mx = x;
 		vd->vd_my = y;
 		if (vd->vd_mstate & MOUSE_BUTTON1DOWN)
 			vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE,
 			    vd->vd_mx / vf->vf_width,
 			    vd->vd_my / vf->vf_height);
 
 		vt_resume_flush_timer(vw, 0);
 		return; /* Done */
 	case MOUSE_BUTTON_EVENT:
 		/* Buttons */
 		break;
 	default:
 		return; /* Done */
 	}
 
 	switch (event) {
 	case MOUSE_BUTTON1DOWN:
 		switch (cnt % 4) {
 		case 0:	/* up */
 			mark = VTB_MARK_END;
 			break;
 		case 1: /* single click: start cut operation */
 			mark = VTB_MARK_START;
 			break;
 		case 2:	/* double click: cut a word */
 			mark = VTB_MARK_WORD;
 			break;
 		case 3:	/* triple click: cut a line */
 			mark = VTB_MARK_ROW;
 			break;
 		}
 		break;
 	case VT_MOUSE_PASTEBUTTON:
 		switch (cnt) {
 		case 0:	/* up */
 			break;
 		default:
 			vt_mouse_paste();
 			break;
 		}
 		return; /* Done */
 	case VT_MOUSE_EXTENDBUTTON:
 		switch (cnt) {
 		case 0:	/* up */
 			if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN))
 				mark = VTB_MARK_EXTEND;
 			else
 				mark = 0;
 			break;
 		default:
 			mark = VTB_MARK_EXTEND;
 			break;
 		}
 		break;
 	default:
 		return; /* Done */
 	}
 
 	/* Save buttons state. */
 	if (cnt > 0)
 		vd->vd_mstate |= event;
 	else
 		vd->vd_mstate &= ~event;
 
 	if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width,
 	    vd->vd_my / vf->vf_height) == 1) {
 		/*
 		 * We have something marked to copy, so update pointer to
 		 * window with selection.
 		 */
 		vt_resume_flush_timer(vw, 0);
 
 		switch (mark) {
 		case VTB_MARK_END:
 		case VTB_MARK_WORD:
 		case VTB_MARK_ROW:
 		case VTB_MARK_EXTEND:
 			break;
 		default:
 			/* Other types of mark do not require to copy data. */
 			return;
 		}
 
 		/* Get current selection size in bytes. */
 		len = vtbuf_get_marked_len(&vw->vw_buf);
 		if (len <= 0)
 			return;
 
 		/* Reallocate buffer only if old one is too small. */
 		if (len > VD_PASTEBUFSZ(vd)) {
 			VD_PASTEBUF(vd) = realloc(VD_PASTEBUF(vd), len, M_VT,
 			    M_WAITOK | M_ZERO);
 			/* Update buffer size. */
 			VD_PASTEBUFSZ(vd) = len;
 		}
 		/* Request copy/paste buffer data, no more than `len' */
 		vtbuf_extract_marked(&vw->vw_buf, VD_PASTEBUF(vd),
 		    VD_PASTEBUFSZ(vd));
 
 		VD_PASTEBUFLEN(vd) = len;
 
 		/* XXX VD_PASTEBUF(vd) have to be freed on shutdown/unload. */
 	}
 }
 
 void
 vt_mouse_state(int show)
 {
 	struct vt_device *vd;
 	struct vt_window *vw;
 
 	vd = main_vd;
 	vw = vd->vd_curwindow;
 
 	switch (show) {
 	case VT_MOUSE_HIDE:
 		vw->vw_flags |= VWF_MOUSE_HIDE;
 		break;
 	case VT_MOUSE_SHOW:
 		vw->vw_flags &= ~VWF_MOUSE_HIDE;
 		break;
 	}
 
 	/* Mark mouse position as dirty. */
 	vt_mark_mouse_position_as_dirty(vd, false);
 	vt_resume_flush_timer(vw, 0);
 }
 #endif
 
 static int
 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr,
     int nprot, vm_memattr_t *memattr)
 {
 	struct vt_window *vw = tm->tm_softc;
 	struct vt_device *vd = vw->vw_device;
 
 	if (vd->vd_driver->vd_fb_mmap)
 		return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot,
 		    memattr));
 
 	return (ENXIO);
 }
 
 static int
 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data,
     struct thread *td)
 {
 	struct vt_window *vw = tm->tm_softc;
 	struct vt_device *vd = vw->vw_device;
 	keyboard_t *kbd;
 	int error, i, s;
 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
 	int ival;
 
 	switch (cmd) {
 	case _IO('v', 4):
 		cmd = VT_RELDISP;
 		break;
 	case _IO('v', 5):
 		cmd = VT_ACTIVATE;
 		break;
 	case _IO('v', 6):
 		cmd = VT_WAITACTIVE;
 		break;
 	case _IO('K', 20):
 		cmd = KDSKBSTATE;
 		break;
 	case _IO('K', 67):
 		cmd = KDSETRAD;
 		break;
 	case _IO('K', 7):
 		cmd = KDSKBMODE;
 		break;
 	case _IO('K', 8):
 		cmd = KDMKTONE;
 		break;
 	case _IO('K', 10):
 		cmd = KDSETMODE;
 		break;
 	case _IO('K', 13):
 		cmd = KDSBORDER;
 		break;
 	case _IO('K', 63):
 		cmd = KIOCSOUND;
 		break;
 	case _IO('K', 66):
 		cmd = KDSETLED;
 		break;
 	case _IO('c', 104):
 		cmd = CONS_SETWINORG;
 		break;
 	case _IO('c', 110):
 		cmd = CONS_SETKBD;
 		break;
 	default:
 		goto skip_thunk;
 	}
 	ival = IOCPARM_IVAL(data);
 	data = (caddr_t)&ival;
 skip_thunk:
 #endif
 
 	switch (cmd) {
 	case KDSETRAD:		/* set keyboard repeat & delay rates (old) */
 		if (*(int *)data & ~0x7f)
 			return (EINVAL);
 		/* FALLTHROUGH */
 	case GIO_KEYMAP:
 	case PIO_KEYMAP:
 	case GIO_DEADKEYMAP:
 	case PIO_DEADKEYMAP:
 	case GETFKEY:
 	case SETFKEY:
 	case KDGKBINFO:
 	case KDGKBTYPE:
 	case KDGETREPEAT:	/* get keyboard repeat & delay rates */
 	case KDSETREPEAT:	/* set keyboard repeat & delay rates (new) */
 	case KBADDKBD:		/* add keyboard to mux */
 	case KBRELKBD: {	/* release keyboard from mux */
 		error = 0;
 
 		mtx_lock(&Giant);
 		if ((kbd = vd->vd_keyboard) != NULL)
 			error = kbdd_ioctl(kbd, cmd, data);
 		mtx_unlock(&Giant);
 		if (error == ENOIOCTL) {
 			if (cmd == KDGKBTYPE) {
 				/* always return something? XXX */
 				*(int *)data = 0;
 			} else {
 				return (ENODEV);
 			}
 		}
 		return (error);
 	}
 	case KDGKBSTATE: {	/* get keyboard state (locks) */
 		error = 0;
 
 		if (vw == vd->vd_curwindow) {
 			mtx_lock(&Giant);
 			if ((kbd = vd->vd_keyboard) != NULL)
 				error = vt_save_kbd_state(vw, kbd);
 			mtx_unlock(&Giant);
 
 			if (error != 0)
 				return (error);
 		}
 
 		*(int *)data = vw->vw_kbdstate & LOCK_MASK;
 
 		return (error);
 	}
 	case KDSKBSTATE: {	/* set keyboard state (locks) */
 		int state;
 
 		state = *(int *)data;
 		if (state & ~LOCK_MASK)
 			return (EINVAL);
 
 		vw->vw_kbdstate &= ~LOCK_MASK;
 		vw->vw_kbdstate |= state;
 
 		error = 0;
 		if (vw == vd->vd_curwindow) {
 			mtx_lock(&Giant);
 			if ((kbd = vd->vd_keyboard) != NULL)
 				error = vt_update_kbd_state(vw, kbd);
 			mtx_unlock(&Giant);
 		}
 
 		return (error);
 	}
 	case KDGETLED: {	/* get keyboard LED status */
 		error = 0;
 
 		if (vw == vd->vd_curwindow) {
 			mtx_lock(&Giant);
 			if ((kbd = vd->vd_keyboard) != NULL)
 				error = vt_save_kbd_leds(vw, kbd);
 			mtx_unlock(&Giant);
 
 			if (error != 0)
 				return (error);
 		}
 
 		*(int *)data = vw->vw_kbdstate & LED_MASK;
 
 		return (error);
 	}
 	case KDSETLED: {	/* set keyboard LED status */
 		int leds;
 
 		leds = *(int *)data;
 		if (leds & ~LED_MASK)
 			return (EINVAL);
 
 		vw->vw_kbdstate &= ~LED_MASK;
 		vw->vw_kbdstate |= leds;
 
 		error = 0;
 		if (vw == vd->vd_curwindow) {
 			mtx_lock(&Giant);
 			if ((kbd = vd->vd_keyboard) != NULL)
 				error = vt_update_kbd_leds(vw, kbd);
 			mtx_unlock(&Giant);
 		}
 
 		return (error);
 	}
 	case KDGETMODE:
 		*(int *)data = (vw->vw_flags & VWF_GRAPHICS) ?
 		    KD_GRAPHICS : KD_TEXT;
 		return (0);
 	case KDGKBMODE: {
 		error = 0;
 
 		if (vw == vd->vd_curwindow) {
 			mtx_lock(&Giant);
 			if ((kbd = vd->vd_keyboard) != NULL)
 				error = vt_save_kbd_mode(vw, kbd);
 			mtx_unlock(&Giant);
 
 			if (error != 0)
 				return (error);
 		}
 
 		*(int *)data = vw->vw_kbdmode;
 
 		return (error);
 	}
 	case KDSKBMODE: {
 		int mode;
 
 		mode = *(int *)data;
 		switch (mode) {
 		case K_XLATE:
 		case K_RAW:
 		case K_CODE:
 			vw->vw_kbdmode = mode;
 
 			error = 0;
 			if (vw == vd->vd_curwindow) {
 				mtx_lock(&Giant);
 				if ((kbd = vd->vd_keyboard) != NULL)
 					error = vt_update_kbd_mode(vw, kbd);
 				mtx_unlock(&Giant);
 			}
 
 			return (error);
 		default:
 			return (EINVAL);
 		}
 	}
 	case FBIOGTYPE:
 	case FBIO_GETWINORG:	/* get frame buffer window origin */
 	case FBIO_GETDISPSTART:	/* get display start address */
 	case FBIO_GETLINEWIDTH:	/* get scan line width in bytes */
 	case FBIO_BLANK:	/* blank display */
 	case FBIO_GETRGBOFFS:	/* get RGB offsets */
 		if (vd->vd_driver->vd_fb_ioctl)
 			return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td));
 		break;
 	case CONS_BLANKTIME:
 		/* XXX */
 		return (0);
 	case CONS_HISTORY:
 		if (*(int *)data < 0)
 			return EINVAL;
 		if (*(int *)data != vw->vw_buf.vb_history_size)
 			vtbuf_sethistory_size(&vw->vw_buf, *(int *)data);
 		return (0);
 	case CONS_CLRHIST:
 		vtbuf_clearhistory(&vw->vw_buf);
 		/*
 		 * Invalidate the entire visible window; it is not guaranteed
 		 * that this operation will be immediately followed by a scroll
 		 * event, so it would otherwise be possible for prior artifacts
 		 * to remain visible.
 		 */
 		VT_LOCK(vd);
 		if (vw == vd->vd_curwindow) {
 			vd->vd_flags |= VDF_INVALID;
 			vt_resume_flush_timer(vw, 0);
 		}
 		VT_UNLOCK(vd);
 		return (0);
 	case CONS_GET:
 		/* XXX */
 		*(int *)data = M_CG640x480;
 		return (0);
 	case CONS_BELLTYPE:	/* set bell type sound */
 		if ((*(int *)data) & CONS_QUIET_BELL)
 			vd->vd_flags |= VDF_QUIET_BELL;
 		else
 			vd->vd_flags &= ~VDF_QUIET_BELL;
 		return (0);
 	case CONS_GETINFO: {
 		vid_info_t *vi = (vid_info_t *)data;
 		if (vi->size != sizeof(struct vid_info))
 			return (EINVAL);
 
 		if (vw == vd->vd_curwindow) {
 			mtx_lock(&Giant);
 			if ((kbd = vd->vd_keyboard) != NULL)
 				vt_save_kbd_state(vw, kbd);
 			mtx_unlock(&Giant);
 		}
 
 		vi->m_num = vd->vd_curwindow->vw_number + 1;
 		vi->mk_keylock = vw->vw_kbdstate & LOCK_MASK;
 		/* XXX: other fields! */
 		return (0);
 	}
 	case CONS_GETVERS:
 		*(int *)data = 0x200;
 		return (0);
 	case CONS_MODEINFO:
 		/* XXX */
 		return (0);
 	case CONS_MOUSECTL: {
 		mouse_info_t *mouse = (mouse_info_t*)data;
 
 		/*
 		 * All the commands except MOUSE_SHOW nd MOUSE_HIDE
 		 * should not be applied to individual TTYs, but only to
 		 * consolectl.
 		 */
 		switch (mouse->operation) {
 		case MOUSE_HIDE:
 			if (vd->vd_flags & VDF_MOUSECURSOR) {
 				vd->vd_flags &= ~VDF_MOUSECURSOR;
 #ifndef SC_NO_CUTPASTE
 				vt_mouse_state(VT_MOUSE_HIDE);
 #endif
 			}
 			return (0);
 		case MOUSE_SHOW:
 			if (!(vd->vd_flags & VDF_MOUSECURSOR)) {
 				vd->vd_flags |= VDF_MOUSECURSOR;
 				vd->vd_mx = vd->vd_width / 2;
 				vd->vd_my = vd->vd_height / 2;
 #ifndef SC_NO_CUTPASTE
 				vt_mouse_state(VT_MOUSE_SHOW);
 #endif
 			}
 			return (0);
 		default:
 			return (EINVAL);
 		}
 	}
 	case PIO_VFONT: {
 		struct vt_font *vf;
 
 		if (vd->vd_flags & VDF_TEXTMODE)
 			return (ENOTSUP);
 
 		error = vtfont_load((void *)data, &vf);
 		if (error != 0)
 			return (error);
 
 		error = vt_change_font(vw, vf);
 		vtfont_unref(vf);
 		return (error);
 	}
 	case PIO_VFONT_DEFAULT: {
 		/* Reset to default font. */
 		error = vt_change_font(vw, vt_font_assigned);
 		return (error);
 	}
 	case GIO_SCRNMAP: {
 		scrmap_t *sm = (scrmap_t *)data;
 
 		/* We don't have screen maps, so return a handcrafted one. */
 		for (i = 0; i < 256; i++)
 			sm->scrmap[i] = i;
 		return (0);
 	}
 	case KDSETMODE:
 		/*
 		 * FIXME: This implementation is incomplete compared to
 		 * syscons.
 		 */
 		switch (*(int *)data) {
 		case KD_TEXT:
 		case KD_TEXT1:
 		case KD_PIXEL:
 			vw->vw_flags &= ~VWF_GRAPHICS;
 			break;
 		case KD_GRAPHICS:
 			vw->vw_flags |= VWF_GRAPHICS;
 			break;
 		}
 		return (0);
 	case KDENABIO:		/* allow io operations */
 		error = priv_check(td, PRIV_IO);
 		if (error != 0)
 			return (error);
 		error = securelevel_gt(td->td_ucred, 0);
 		if (error != 0)
 			return (error);
 #if defined(__i386__)
 		td->td_frame->tf_eflags |= PSL_IOPL;
 #elif defined(__amd64__)
 		td->td_frame->tf_rflags |= PSL_IOPL;
 #endif
 		return (0);
 	case KDDISABIO:		/* disallow io operations (default) */
 #if defined(__i386__)
 		td->td_frame->tf_eflags &= ~PSL_IOPL;
 #elif defined(__amd64__)
 		td->td_frame->tf_rflags &= ~PSL_IOPL;
 #endif
 		return (0);
 	case KDMKTONE:		/* sound the bell */
 		vtterm_beep(tm, *(u_int *)data);
 		return (0);
 	case KIOCSOUND:		/* make tone (*data) hz */
 		/* TODO */
 		return (0);
 	case CONS_SETKBD:	/* set the new keyboard */
 		mtx_lock(&Giant);
 		error = 0;
 		if (vd->vd_keyboard == NULL ||
 		    vd->vd_keyboard->kb_index != *(int *)data) {
 			kbd = kbd_get_keyboard(*(int *)data);
 			if (kbd == NULL) {
 				mtx_unlock(&Giant);
 				return (EINVAL);
 			}
 			i = kbd_allocate(kbd->kb_name, kbd->kb_unit,
 			    (void *)vd, vt_kbdevent, vd);
 			if (i >= 0) {
 				if ((kbd = vd->vd_keyboard) != NULL) {
 					vt_save_kbd_state(vd->vd_curwindow, kbd);
 					kbd_release(kbd, (void *)vd);
 				}
 				kbd = vd->vd_keyboard = kbd_get_keyboard(i);
 
 				vt_update_kbd_mode(vd->vd_curwindow, kbd);
 				vt_update_kbd_state(vd->vd_curwindow, kbd);
 			} else {
 				error = EPERM;	/* XXX */
 			}
 		}
 		mtx_unlock(&Giant);
 		return (error);
 	case CONS_RELKBD:	/* release the current keyboard */
 		mtx_lock(&Giant);
 		error = 0;
 		if ((kbd = vd->vd_keyboard) != NULL) {
 			vt_save_kbd_state(vd->vd_curwindow, kbd);
 			error = kbd_release(kbd, (void *)vd);
 			if (error == 0) {
 				vd->vd_keyboard = NULL;
 			}
 		}
 		mtx_unlock(&Giant);
 		return (error);
 	case VT_ACTIVATE: {
 		int win;
 		win = *(int *)data - 1;
 		DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME,
 		    VT_UNIT(vw), win);
 		if ((win >= VT_MAXWINDOWS) || (win < 0))
 			return (EINVAL);
 		return (vt_proc_window_switch(vd->vd_windows[win]));
 	}
 	case VT_GETACTIVE:
 		*(int *)data = vd->vd_curwindow->vw_number + 1;
 		return (0);
 	case VT_GETINDEX:
 		*(int *)data = vw->vw_number + 1;
 		return (0);
 	case VT_LOCKSWITCH:
 		/* TODO: Check current state, switching can be in progress. */
 		if ((*(int *)data) == 0x01)
 			vw->vw_flags |= VWF_VTYLOCK;
 		else if ((*(int *)data) == 0x02)
 			vw->vw_flags &= ~VWF_VTYLOCK;
 		else
 			return (EINVAL);
 		return (0);
 	case VT_OPENQRY:
 		VT_LOCK(vd);
 		for (i = 0; i < VT_MAXWINDOWS; i++) {
 			vw = vd->vd_windows[i];
 			if (vw == NULL)
 				continue;
 			if (!(vw->vw_flags & VWF_OPENED)) {
 				*(int *)data = vw->vw_number + 1;
 				VT_UNLOCK(vd);
 				return (0);
 			}
 		}
 		VT_UNLOCK(vd);
 		return (EINVAL);
 	case VT_WAITACTIVE: {
 		unsigned int idx;
 
 		error = 0;
 
 		idx = *(unsigned int *)data;
 		if (idx > VT_MAXWINDOWS)
 			return (EINVAL);
 		if (idx > 0)
 			vw = vd->vd_windows[idx - 1];
 
 		VT_LOCK(vd);
 		while (vd->vd_curwindow != vw && error == 0)
 			error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
 		VT_UNLOCK(vd);
 		return (error);
 	}
 	case VT_SETMODE: {	/* set screen switcher mode */
 		struct vt_mode *mode;
 		struct proc *p1;
 
 		mode = (struct vt_mode *)data;
 		DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw));
 		if (vw->vw_smode.mode == VT_PROCESS) {
 			p1 = pfind(vw->vw_pid);
 			if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) {
 				if (p1)
 					PROC_UNLOCK(p1);
 				DPRINTF(5, "error EPERM\n");
 				return (EPERM);
 			}
 			if (p1)
 				PROC_UNLOCK(p1);
 		}
 		if (mode->mode == VT_AUTO) {
 			vw->vw_smode.mode = VT_AUTO;
 			vw->vw_proc = NULL;
 			vw->vw_pid = 0;
 			DPRINTF(5, "VT_AUTO, ");
 			if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
 				cnavailable(vw->vw_terminal->consdev, TRUE);
 			/* were we in the middle of the vty switching process? */
 			if (finish_vt_rel(vw, TRUE, &s) == 0)
 				DPRINTF(5, "reset WAIT_REL, ");
 			if (finish_vt_acq(vw) == 0)
 				DPRINTF(5, "reset WAIT_ACQ, ");
 			return (0);
 		} else if (mode->mode == VT_PROCESS) {
 			if (!ISSIGVALID(mode->relsig) ||
 			    !ISSIGVALID(mode->acqsig) ||
 			    !ISSIGVALID(mode->frsig)) {
 				DPRINTF(5, "error EINVAL\n");
 				return (EINVAL);
 			}
 			DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid);
 			bcopy(data, &vw->vw_smode, sizeof(struct vt_mode));
 			vw->vw_proc = td->td_proc;
 			vw->vw_pid = vw->vw_proc->p_pid;
 			if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
 				cnavailable(vw->vw_terminal->consdev, FALSE);
 		} else {
 			DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n",
 			    mode->mode);
 			return (EINVAL);
 		}
 		DPRINTF(5, "\n");
 		return (0);
 	}
 	case VT_GETMODE:	/* get screen switcher mode */
 		bcopy(&vw->vw_smode, data, sizeof(struct vt_mode));
 		return (0);
 
 	case VT_RELDISP:	/* screen switcher ioctl */
 		/*
 		 * This must be the current vty which is in the VT_PROCESS
 		 * switching mode...
 		 */
 		if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode !=
 		    VT_PROCESS)) {
 			return (EINVAL);
 		}
 		/* ...and this process is controlling it. */
 		if (vw->vw_proc != td->td_proc) {
 			return (EPERM);
 		}
 		error = EINVAL;
 		switch(*(int *)data) {
 		case VT_FALSE:	/* user refuses to release screen, abort */
 			if ((error = finish_vt_rel(vw, FALSE, &s)) == 0)
 				DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n",
 				    SC_DRIVER_NAME, VT_UNIT(vw));
 			break;
 		case VT_TRUE:	/* user has released screen, go on */
 			/* finish_vt_rel(..., TRUE, ...) should not be locked */
 			if (vw->vw_flags & VWF_SWWAIT_REL) {
 				if ((error = finish_vt_rel(vw, TRUE, &s)) == 0)
 					DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n",
 					    SC_DRIVER_NAME, VT_UNIT(vw));
 			} else {
 				error = EINVAL;
 			}
 			return (error);
 		case VT_ACKACQ:	/* acquire acknowledged, switch completed */
 			if ((error = finish_vt_acq(vw)) == 0)
 				DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n",
 				    SC_DRIVER_NAME, VT_UNIT(vw));
 			break;
 		default:
 			break;
 		}
 		return (error);
 	}
 
 	return (ENOIOCTL);
 }
 
 static struct vt_window *
 vt_allocate_window(struct vt_device *vd, unsigned int window)
 {
 	struct vt_window *vw;
 	struct terminal *tm;
 	term_pos_t size;
 	struct winsize wsz;
 
 	vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO);
 	vw->vw_device = vd;
 	vw->vw_number = window;
 	vw->vw_kbdmode = K_XLATE;
 
 	if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
 		vw->vw_font = vtfont_ref(vt_font_assigned);
 		vt_compute_drawable_area(vw);
 	}
 
 	vt_termsize(vd, vw->vw_font, &size);
 	vt_winsize(vd, vw->vw_font, &wsz);
 	tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw);
 	vw->vw_buf.vb_terminal = tm;	/* must be set before vtbuf_init() */
 	vtbuf_init(&vw->vw_buf, &size);
 
 	terminal_set_winsize(tm, &wsz);
 	vd->vd_windows[window] = vw;
 	callout_init(&vw->vw_proc_dead_timer, 1);
 
 	return (vw);
 }
 
 void
 vt_upgrade(struct vt_device *vd)
 {
 	struct vt_window *vw;
 	unsigned int i;
 	int register_handlers;
 
 	if (!vty_enabled(VTY_VT))
 		return;
 	if (main_vd->vd_driver == NULL)
 		return;
 
 	for (i = 0; i < VT_MAXWINDOWS; i++) {
 		vw = vd->vd_windows[i];
 		if (vw == NULL) {
 			/* New window. */
 			vw = vt_allocate_window(vd, i);
 		}
 		if (!(vw->vw_flags & VWF_READY)) {
 			callout_init(&vw->vw_proc_dead_timer, 1);
 			terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw));
 			vw->vw_flags |= VWF_READY;
 			if (vw->vw_flags & VWF_CONSOLE) {
 				/* For existing console window. */
 				EVENTHANDLER_REGISTER(shutdown_pre_sync,
 				    vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT);
 			}
 		}
 	}
 	VT_LOCK(vd);
 	if (vd->vd_curwindow == NULL)
 		vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW];
 
 	register_handlers = 0;
 	if (!(vd->vd_flags & VDF_ASYNC)) {
 		/* Attach keyboard. */
 		vt_allocate_keyboard(vd);
 
 		/* Init 25 Hz timer. */
 		callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0);
 
 		/*
 		 * Start timer when everything ready.
 		 * Note that the operations here are purposefully ordered.
 		 * We need to ensure vd_timer_armed is non-zero before we set
 		 * the VDF_ASYNC flag. That prevents this function from
 		 * racing with vt_resume_flush_timer() to update the
 		 * callout structure.
 		 */
 		atomic_add_acq_int(&vd->vd_timer_armed, 1);
 		vd->vd_flags |= VDF_ASYNC;
 		callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd);
 		register_handlers = 1;
 	}
 
 	VT_UNLOCK(vd);
 
 	/* Refill settings with new sizes. */
 	vt_resize(vd);
 
 	if (register_handlers) {
 		/* Register suspend/resume handlers. */
 		EVENTHANDLER_REGISTER(power_suspend_early, vt_suspend_handler,
 		    vd, EVENTHANDLER_PRI_ANY);
 		EVENTHANDLER_REGISTER(power_resume, vt_resume_handler, vd,
 		    EVENTHANDLER_PRI_ANY);
 	}
 }
 
 static void
 vt_resize(struct vt_device *vd)
 {
 	struct vt_window *vw;
 	int i;
 
 	for (i = 0; i < VT_MAXWINDOWS; i++) {
 		vw = vd->vd_windows[i];
 		VT_LOCK(vd);
 		/* Assign default font to window, if not textmode. */
 		if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL)
 			vw->vw_font = vtfont_ref(vt_font_assigned);
 		VT_UNLOCK(vd);
 
 		/* Resize terminal windows */
 		while (vt_change_font(vw, vw->vw_font) == EBUSY) {
 			DPRINTF(100, "%s: vt_change_font() is busy, "
 			    "window %d\n", __func__, i);
 		}
 	}
 }
 
 static void
 vt_replace_backend(const struct vt_driver *drv, void *softc)
 {
 	struct vt_device *vd;
 
 	vd = main_vd;
 
 	if (vd->vd_flags & VDF_ASYNC) {
 		/* Stop vt_flush periodic task. */
 		VT_LOCK(vd);
 		vt_suspend_flush_timer(vd);
 		VT_UNLOCK(vd);
 		/*
 		 * Mute current terminal until we done. vt_change_font (called
 		 * from vt_resize) will unmute it.
 		 */
 		terminal_mute(vd->vd_curwindow->vw_terminal, 1);
 	}
 
 	/*
 	 * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will
 	 * set it.
 	 */
 	VT_LOCK(vd);
 	vd->vd_flags &= ~VDF_TEXTMODE;
 
 	if (drv != NULL) {
 		/*
 		 * We want to upgrade from the current driver to the
 		 * given driver.
 		 */
 
 		vd->vd_prev_driver = vd->vd_driver;
 		vd->vd_prev_softc = vd->vd_softc;
 		vd->vd_driver = drv;
 		vd->vd_softc = softc;
 
 		vd->vd_driver->vd_init(vd);
 	} else if (vd->vd_prev_driver != NULL && vd->vd_prev_softc != NULL) {
 		/*
 		 * No driver given: we want to downgrade to the previous
 		 * driver.
 		 */
 		const struct vt_driver *old_drv;
 		void *old_softc;
 
 		old_drv = vd->vd_driver;
 		old_softc = vd->vd_softc;
 
 		vd->vd_driver = vd->vd_prev_driver;
 		vd->vd_softc = vd->vd_prev_softc;
 		vd->vd_prev_driver = NULL;
 		vd->vd_prev_softc = NULL;
 
 		vd->vd_flags |= VDF_DOWNGRADE;
 
 		vd->vd_driver->vd_init(vd);
 
 		if (old_drv->vd_fini)
 			old_drv->vd_fini(vd, old_softc);
 
 		vd->vd_flags &= ~VDF_DOWNGRADE;
 	}
 
 	VT_UNLOCK(vd);
 
 	/* Update windows sizes and initialize last items. */
 	vt_upgrade(vd);
 
 #ifdef DEV_SPLASH
 	if (vd->vd_flags & VDF_SPLASH)
 		vtterm_splash(vd);
 #endif
 
 	if (vd->vd_flags & VDF_ASYNC) {
 		/* Allow to put chars now. */
 		terminal_mute(vd->vd_curwindow->vw_terminal, 0);
 		/* Rerun timer for screen updates. */
 		vt_resume_flush_timer(vd->vd_curwindow, 0);
 	}
 
 	/*
 	 * Register as console. If it already registered, cnadd() will ignore
 	 * it.
 	 */
 	termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal);
 }
 
 static void
 vt_suspend_handler(void *priv)
 {
 	struct vt_device *vd;
 
 	vd = priv;
 	vd->vd_flags |= VDF_SUSPENDED;
 	if (vd->vd_driver != NULL && vd->vd_driver->vd_suspend != NULL)
 		vd->vd_driver->vd_suspend(vd);
 }
 
 static void
 vt_resume_handler(void *priv)
 {
 	struct vt_device *vd;
 
 	vd = priv;
 	if (vd->vd_driver != NULL && vd->vd_driver->vd_resume != NULL)
 		vd->vd_driver->vd_resume(vd);
 	vd->vd_flags &= ~VDF_SUSPENDED;
 }
 
 void
 vt_allocate(const struct vt_driver *drv, void *softc)
 {
 
 	if (!vty_enabled(VTY_VT))
 		return;
 
 	if (main_vd->vd_driver == NULL) {
 		main_vd->vd_driver = drv;
 		printf("VT: initialize with new VT driver \"%s\".\n",
 		    drv->vd_name);
 	} else {
 		/*
 		 * Check if have rights to replace current driver. For example:
 		 * it is bad idea to replace KMS driver with generic VGA one.
 		 */
 		if (drv->vd_priority <= main_vd->vd_driver->vd_priority) {
 			printf("VT: Driver priority %d too low. Current %d\n ",
 			    drv->vd_priority, main_vd->vd_driver->vd_priority);
 			return;
 		}
 		printf("VT: Replacing driver \"%s\" with new \"%s\".\n",
 		    main_vd->vd_driver->vd_name, drv->vd_name);
 	}
 
 	vt_replace_backend(drv, softc);
 }
 
 void
 vt_deallocate(const struct vt_driver *drv, void *softc)
 {
 
 	if (!vty_enabled(VTY_VT))
 		return;
 
 	if (main_vd->vd_prev_driver == NULL ||
 	    main_vd->vd_driver != drv ||
 	    main_vd->vd_softc != softc)
 		return;
 
 	printf("VT: Switching back from \"%s\" to \"%s\".\n",
 	    main_vd->vd_driver->vd_name, main_vd->vd_prev_driver->vd_name);
 
 	vt_replace_backend(NULL, NULL);
 }
 
 void
 vt_suspend(struct vt_device *vd)
 {
 	int error;
 
 	if (vt_suspendswitch == 0)
 		return;
 	/* Save current window. */
 	vd->vd_savedwindow = vd->vd_curwindow;
 	/* Ask holding process to free window and switch to console window */
 	vt_proc_window_switch(vd->vd_windows[VT_CONSWINDOW]);
 
 	/* Wait for the window switch to complete. */
 	error = 0;
 	VT_LOCK(vd);
 	while (vd->vd_curwindow != vd->vd_windows[VT_CONSWINDOW] && error == 0)
 		error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
 	VT_UNLOCK(vd);
 }
 
 void
 vt_resume(struct vt_device *vd)
 {
 
 	if (vt_suspendswitch == 0)
 		return;
 	/* Switch back to saved window, if any */
 	vt_proc_window_switch(vd->vd_savedwindow);
 	vd->vd_savedwindow = NULL;
 }
diff --git a/sys/kern/kern_shutdown.c b/sys/kern/kern_shutdown.c
index c78b19ca454a..8c2ec0e42d5d 100644
--- a/sys/kern/kern_shutdown.c
+++ b/sys/kern/kern_shutdown.c
@@ -1,1838 +1,1838 @@
 /*-
  * SPDX-License-Identifier: BSD-3-Clause
  *
  * Copyright (c) 1986, 1988, 1991, 1993
  *	The Regents of the University of California.  All rights reserved.
  * (c) UNIX System Laboratories, Inc.
  * All or some portions of this file are derived from material licensed
  * to the University of California by American Telephone and Telegraph
  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
  * the permission of UNIX System Laboratories, Inc.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  * 1. Redistributions of source code must retain the above copyright
  *    notice, this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
  * 3. Neither the name of the University nor the names of its contributors
  *    may be used to endorse or promote products derived from this software
  *    without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
  *	@(#)kern_shutdown.c	8.3 (Berkeley) 1/21/94
  */
 
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 
 #include "opt_ddb.h"
 #include "opt_ekcd.h"
 #include "opt_kdb.h"
 #include "opt_panic.h"
 #include "opt_printf.h"
 #include "opt_sched.h"
 #include "opt_watchdog.h"
 
 #include <sys/param.h>
 #include <sys/systm.h>
 #include <sys/bio.h>
 #include <sys/boottrace.h>
 #include <sys/buf.h>
 #include <sys/conf.h>
 #include <sys/compressor.h>
 #include <sys/cons.h>
 #include <sys/disk.h>
 #include <sys/eventhandler.h>
 #include <sys/filedesc.h>
 #include <sys/jail.h>
 #include <sys/kdb.h>
 #include <sys/kernel.h>
 #include <sys/kerneldump.h>
 #include <sys/kthread.h>
 #include <sys/ktr.h>
 #include <sys/malloc.h>
 #include <sys/mbuf.h>
 #include <sys/mount.h>
 #include <sys/priv.h>
 #include <sys/proc.h>
 #include <sys/reboot.h>
 #include <sys/resourcevar.h>
 #include <sys/rwlock.h>
 #include <sys/sbuf.h>
 #include <sys/sched.h>
 #include <sys/smp.h>
 #include <sys/sysctl.h>
 #include <sys/sysproto.h>
 #include <sys/taskqueue.h>
 #include <sys/vnode.h>
 #include <sys/watchdog.h>
 
 #include <crypto/chacha20/chacha.h>
 #include <crypto/rijndael/rijndael-api-fst.h>
 #include <crypto/sha2/sha256.h>
 
 #include <ddb/ddb.h>
 
 #include <machine/cpu.h>
 #include <machine/dump.h>
 #include <machine/pcb.h>
 #include <machine/smp.h>
 
 #include <security/mac/mac_framework.h>
 
 #include <vm/vm.h>
 #include <vm/vm_object.h>
 #include <vm/vm_page.h>
 #include <vm/vm_pager.h>
 #include <vm/swap_pager.h>
 
 #include <sys/signalvar.h>
 
 static MALLOC_DEFINE(M_DUMPER, "dumper", "dumper block buffer");
 
 #ifndef PANIC_REBOOT_WAIT_TIME
 #define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */
 #endif
 static int panic_reboot_wait_time = PANIC_REBOOT_WAIT_TIME;
 SYSCTL_INT(_kern, OID_AUTO, panic_reboot_wait_time, CTLFLAG_RWTUN,
     &panic_reboot_wait_time, 0,
     "Seconds to wait before rebooting after a panic");
 
 /*
  * Note that stdarg.h and the ANSI style va_start macro is used for both
  * ANSI and traditional C compilers.
  */
 #include <machine/stdarg.h>
 
 #ifdef KDB
 #ifdef KDB_UNATTENDED
 int debugger_on_panic = 0;
 #else
 int debugger_on_panic = 1;
 #endif
 SYSCTL_INT(_debug, OID_AUTO, debugger_on_panic,
     CTLFLAG_RWTUN | CTLFLAG_SECURE,
     &debugger_on_panic, 0, "Run debugger on kernel panic");
 
 static bool debugger_on_recursive_panic = false;
 SYSCTL_BOOL(_debug, OID_AUTO, debugger_on_recursive_panic,
     CTLFLAG_RWTUN | CTLFLAG_SECURE,
     &debugger_on_recursive_panic, 0, "Run debugger on recursive kernel panic");
 
 int debugger_on_trap = 0;
 SYSCTL_INT(_debug, OID_AUTO, debugger_on_trap,
     CTLFLAG_RWTUN | CTLFLAG_SECURE,
     &debugger_on_trap, 0, "Run debugger on kernel trap before panic");
 
 #ifdef KDB_TRACE
 static int trace_on_panic = 1;
 static bool trace_all_panics = true;
 #else
 static int trace_on_panic = 0;
 static bool trace_all_panics = false;
 #endif
 SYSCTL_INT(_debug, OID_AUTO, trace_on_panic,
     CTLFLAG_RWTUN | CTLFLAG_SECURE,
     &trace_on_panic, 0, "Print stack trace on kernel panic");
 SYSCTL_BOOL(_debug, OID_AUTO, trace_all_panics, CTLFLAG_RWTUN,
     &trace_all_panics, 0, "Print stack traces on secondary kernel panics");
 #endif /* KDB */
 
 static int sync_on_panic = 0;
 SYSCTL_INT(_kern, OID_AUTO, sync_on_panic, CTLFLAG_RWTUN,
 	&sync_on_panic, 0, "Do a sync before rebooting from a panic");
 
 static bool poweroff_on_panic = 0;
 SYSCTL_BOOL(_kern, OID_AUTO, poweroff_on_panic, CTLFLAG_RWTUN,
 	&poweroff_on_panic, 0, "Do a power off instead of a reboot on a panic");
 
 static bool powercycle_on_panic = 0;
 SYSCTL_BOOL(_kern, OID_AUTO, powercycle_on_panic, CTLFLAG_RWTUN,
 	&powercycle_on_panic, 0, "Do a power cycle instead of a reboot on a panic");
 
 static SYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
     "Shutdown environment");
 
 #ifndef DIAGNOSTIC
 static int show_busybufs;
 #else
 static int show_busybufs = 1;
 #endif
 SYSCTL_INT(_kern_shutdown, OID_AUTO, show_busybufs, CTLFLAG_RW,
     &show_busybufs, 0,
     "Show busy buffers during shutdown");
 
 int suspend_blocked = 0;
 SYSCTL_INT(_kern, OID_AUTO, suspend_blocked, CTLFLAG_RW,
 	&suspend_blocked, 0, "Block suspend due to a pending shutdown");
 
 #ifdef EKCD
 FEATURE(ekcd, "Encrypted kernel crash dumps support");
 
 MALLOC_DEFINE(M_EKCD, "ekcd", "Encrypted kernel crash dumps data");
 
 struct kerneldumpcrypto {
 	uint8_t			kdc_encryption;
 	uint8_t			kdc_iv[KERNELDUMP_IV_MAX_SIZE];
 	union {
 		struct {
 			keyInstance	aes_ki;
 			cipherInstance	aes_ci;
 		} u_aes;
 		struct chacha_ctx	u_chacha;
 	} u;
 #define	kdc_ki	u.u_aes.aes_ki
 #define	kdc_ci	u.u_aes.aes_ci
 #define	kdc_chacha	u.u_chacha
 	uint32_t		kdc_dumpkeysize;
 	struct kerneldumpkey	kdc_dumpkey[];
 };
 #endif
 
 struct kerneldumpcomp {
 	uint8_t			kdc_format;
 	struct compressor	*kdc_stream;
 	uint8_t			*kdc_buf;
 	size_t			kdc_resid;
 };
 
 static struct kerneldumpcomp *kerneldumpcomp_create(struct dumperinfo *di,
 		    uint8_t compression);
 static void	kerneldumpcomp_destroy(struct dumperinfo *di);
 static int	kerneldumpcomp_write_cb(void *base, size_t len, off_t off, void *arg);
 
 static int kerneldump_gzlevel = 6;
 SYSCTL_INT(_kern, OID_AUTO, kerneldump_gzlevel, CTLFLAG_RWTUN,
     &kerneldump_gzlevel, 0,
     "Kernel crash dump compression level");
 
 /*
  * Variable panicstr contains argument to first call to panic; used as flag
  * to indicate that the kernel has already called panic.
  */
 const char *panicstr;
 bool __read_frequently panicked;
 
 int __read_mostly dumping;		/* system is dumping */
 int rebooting;				/* system is rebooting */
 /*
  * Used to serialize between sysctl kern.shutdown.dumpdevname and list
  * modifications via ioctl.
  */
 static struct mtx dumpconf_list_lk;
 MTX_SYSINIT(dumper_configs, &dumpconf_list_lk, "dumper config list", MTX_DEF);
 
 /* Our selected dumper(s). */
 static TAILQ_HEAD(dumpconflist, dumperinfo) dumper_configs =
     TAILQ_HEAD_INITIALIZER(dumper_configs);
 
 /* Context information for dump-debuggers. */
 static struct pcb dumppcb;		/* Registers. */
 lwpid_t dumptid;			/* Thread ID. */
 
 static struct cdevsw reroot_cdevsw = {
      .d_version = D_VERSION,
      .d_name    = "reroot",
 };
 
 static void poweroff_wait(void *, int);
 static void shutdown_halt(void *junk, int howto);
 static void shutdown_panic(void *junk, int howto);
 static void shutdown_reset(void *junk, int howto);
 static int kern_reroot(void);
 
 /* register various local shutdown events */
 static void
 shutdown_conf(void *unused)
 {
 
 	EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL,
 	    SHUTDOWN_PRI_FIRST);
 	EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL,
 	    SHUTDOWN_PRI_LAST + 100);
 	EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL,
 	    SHUTDOWN_PRI_LAST + 100);
 	EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL,
 	    SHUTDOWN_PRI_LAST + 200);
 }
 
 SYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL);
 
 /*
  * The only reason this exists is to create the /dev/reroot/ directory,
  * used by reroot code in init(8) as a mountpoint for tmpfs.
  */
 static void
 reroot_conf(void *unused)
 {
 	int error;
 	struct cdev *cdev;
 
 	error = make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, &cdev,
 	    &reroot_cdevsw, NULL, UID_ROOT, GID_WHEEL, 0600, "reroot/reroot");
 	if (error != 0) {
 		printf("%s: failed to create device node, error %d",
 		    __func__, error);
 	}
 }
 
 SYSINIT(reroot_conf, SI_SUB_DEVFS, SI_ORDER_ANY, reroot_conf, NULL);
 
 /*
  * The system call that results in a reboot.
  */
 /* ARGSUSED */
 int
 sys_reboot(struct thread *td, struct reboot_args *uap)
 {
 	int error;
 
 	error = 0;
 #ifdef MAC
 	error = mac_system_check_reboot(td->td_ucred, uap->opt);
 #endif
 	if (error == 0)
 		error = priv_check(td, PRIV_REBOOT);
 	if (error == 0) {
 		if (uap->opt & RB_REROOT)
 			error = kern_reroot();
 		else
 			kern_reboot(uap->opt);
 	}
 	return (error);
 }
 
 static void
 shutdown_nice_task_fn(void *arg, int pending __unused)
 {
 	int howto;
 
 	howto = (uintptr_t)arg;
 	/* Send a signal to init(8) and have it shutdown the world. */
 	PROC_LOCK(initproc);
 	if ((howto & RB_POWEROFF) != 0) {
 		BOOTTRACE("SIGUSR2 to init(8)");
 		kern_psignal(initproc, SIGUSR2);
 	} else if ((howto & RB_POWERCYCLE) != 0) {
 		BOOTTRACE("SIGWINCH to init(8)");
 		kern_psignal(initproc, SIGWINCH);
 	} else if ((howto & RB_HALT) != 0) {
 		BOOTTRACE("SIGUSR1 to init(8)");
 		kern_psignal(initproc, SIGUSR1);
 	} else {
 		BOOTTRACE("SIGINT to init(8)");
 		kern_psignal(initproc, SIGINT);
 	}
 	PROC_UNLOCK(initproc);
 }
 
 static struct task shutdown_nice_task = TASK_INITIALIZER(0,
     &shutdown_nice_task_fn, NULL);
 
 /*
  * Called by events that want to shut down.. e.g  <CTL><ALT><DEL> on a PC
  */
 void
 shutdown_nice(int howto)
 {
 
 	if (initproc != NULL && !SCHEDULER_STOPPED()) {
 		BOOTTRACE("shutdown initiated");
 		shutdown_nice_task.ta_context = (void *)(uintptr_t)howto;
 		taskqueue_enqueue(taskqueue_fast, &shutdown_nice_task);
 	} else {
 		/*
 		 * No init(8) running, or scheduler would not allow it
 		 * to run, so simply reboot.
 		 */
 		kern_reboot(howto | RB_NOSYNC);
 	}
 }
 
 static void
 print_uptime(void)
 {
 	int f;
 	struct timespec ts;
 
 	getnanouptime(&ts);
 	printf("Uptime: ");
 	f = 0;
 	if (ts.tv_sec >= 86400) {
 		printf("%ldd", (long)ts.tv_sec / 86400);
 		ts.tv_sec %= 86400;
 		f = 1;
 	}
 	if (f || ts.tv_sec >= 3600) {
 		printf("%ldh", (long)ts.tv_sec / 3600);
 		ts.tv_sec %= 3600;
 		f = 1;
 	}
 	if (f || ts.tv_sec >= 60) {
 		printf("%ldm", (long)ts.tv_sec / 60);
 		ts.tv_sec %= 60;
 		f = 1;
 	}
 	printf("%lds\n", (long)ts.tv_sec);
 }
 
 /*
  * Set up a context that can be extracted from the dump.
  */
 void
 dump_savectx(void)
 {
 
 	savectx(&dumppcb);
 	dumptid = curthread->td_tid;
 }
 
 int
 doadump(boolean_t textdump)
 {
 	boolean_t coredump;
 	int error;
 
 	error = 0;
 	if (dumping)
 		return (EBUSY);
 	if (TAILQ_EMPTY(&dumper_configs))
 		return (ENXIO);
 
 	dump_savectx();
 	dumping++;
 
 	coredump = TRUE;
 #ifdef DDB
 	if (textdump && textdump_pending) {
 		coredump = FALSE;
 		textdump_dumpsys(TAILQ_FIRST(&dumper_configs));
 	}
 #endif
 	if (coredump) {
 		struct dumperinfo *di;
 
 		TAILQ_FOREACH(di, &dumper_configs, di_next) {
 			error = dumpsys(di);
 			if (error == 0)
 				break;
 		}
 	}
 
 	dumping--;
 	return (error);
 }
 
 /*
  * Trace the shutdown reason.
  */
 static void
 reboottrace(int howto)
 {
 	if ((howto & RB_DUMP) != 0) {
 		if ((howto & RB_HALT) != 0)
 			BOOTTRACE("system panic: halting...");
 		if ((howto & RB_POWEROFF) != 0)
 			BOOTTRACE("system panic: powering off...");
 		if ((howto & (RB_HALT|RB_POWEROFF)) == 0)
 			BOOTTRACE("system panic: rebooting...");
 	} else {
 		if ((howto & RB_HALT) != 0)
 			BOOTTRACE("system halting...");
 		if ((howto & RB_POWEROFF) != 0)
 			BOOTTRACE("system powering off...");
 		if ((howto & (RB_HALT|RB_POWEROFF)) == 0)
 			BOOTTRACE("system rebooting...");
 	}
 }
 
 /*
  * kern_reboot(9): Shut down the system cleanly to prepare for reboot, halt, or
  * power off.
  */
 void
 kern_reboot(int howto)
 {
 	static int once = 0;
 
 	if (initproc != NULL && curproc != initproc)
 		BOOTTRACE("kernel shutdown (dirty) started");
 	else
 		BOOTTRACE("kernel shutdown (clean) started");
 
 	/*
 	 * Normal paths here don't hold Giant, but we can wind up here
 	 * unexpectedly with it held.  Drop it now so we don't have to
 	 * drop and pick it up elsewhere. The paths it is locking will
 	 * never be returned to, and it is preferable to preclude
 	 * deadlock than to lock against code that won't ever
 	 * continue.
 	 */
 	while (mtx_owned(&Giant))
 		mtx_unlock(&Giant);
 
 #if defined(SMP)
 	/*
 	 * Bind us to the first CPU so that all shutdown code runs there.  Some
 	 * systems don't shutdown properly (i.e., ACPI power off) if we
 	 * run on another processor.
 	 */
 	if (!SCHEDULER_STOPPED()) {
 		thread_lock(curthread);
 		sched_bind(curthread, CPU_FIRST());
 		thread_unlock(curthread);
 		KASSERT(PCPU_GET(cpuid) == CPU_FIRST(),
 		    ("%s: not running on cpu 0", __func__));
 	}
 #endif
 	/* We're in the process of rebooting. */
 	rebooting = 1;
 	reboottrace(howto);
 
 	/* We are out of the debugger now. */
 	kdb_active = 0;
 
 	/*
 	 * Do any callouts that should be done BEFORE syncing the filesystems.
 	 */
 	EVENTHANDLER_INVOKE(shutdown_pre_sync, howto);
 	BOOTTRACE("shutdown pre sync complete");
 
 	/* 
 	 * Now sync filesystems
 	 */
 	if (!cold && (howto & RB_NOSYNC) == 0 && once == 0) {
 		once = 1;
 		BOOTTRACE("bufshutdown begin");
 		bufshutdown(show_busybufs);
 		BOOTTRACE("bufshutdown end");
 	}
 
 	print_uptime();
 
 	cngrab();
 
 	/*
 	 * Ok, now do things that assume all filesystem activity has
 	 * been completed.
 	 */
 	EVENTHANDLER_INVOKE(shutdown_post_sync, howto);
 	BOOTTRACE("shutdown post sync complete");
 
 	if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold && !dumping) 
 		doadump(TRUE);
 
 	/* Now that we're going to really halt the system... */
 	BOOTTRACE("shutdown final begin");
 
 	if (shutdown_trace)
 		boottrace_dump_console();
 
 	EVENTHANDLER_INVOKE(shutdown_final, howto);
 
 	for(;;) ;	/* safety against shutdown_reset not working */
 	/* NOTREACHED */
 }
 
 /*
  * The system call that results in changing the rootfs.
  */
 static int
 kern_reroot(void)
 {
 	struct vnode *oldrootvnode, *vp;
 	struct mount *mp, *devmp;
 	int error;
 
 	if (curproc != initproc)
 		return (EPERM);
 
 	/*
 	 * Mark the filesystem containing currently-running executable
 	 * (the temporary copy of init(8)) busy.
 	 */
 	vp = curproc->p_textvp;
 	error = vn_lock(vp, LK_SHARED);
 	if (error != 0)
 		return (error);
 	mp = vp->v_mount;
 	error = vfs_busy(mp, MBF_NOWAIT);
 	if (error != 0) {
 		vfs_ref(mp);
 		VOP_UNLOCK(vp);
 		error = vfs_busy(mp, 0);
 		vn_lock(vp, LK_SHARED | LK_RETRY);
 		vfs_rel(mp);
 		if (error != 0) {
 			VOP_UNLOCK(vp);
 			return (ENOENT);
 		}
 		if (VN_IS_DOOMED(vp)) {
 			VOP_UNLOCK(vp);
 			vfs_unbusy(mp);
 			return (ENOENT);
 		}
 	}
 	VOP_UNLOCK(vp);
 
 	/*
 	 * Remove the filesystem containing currently-running executable
 	 * from the mount list, to prevent it from being unmounted
 	 * by vfs_unmountall(), and to avoid confusing vfs_mountroot().
 	 *
 	 * Also preserve /dev - forcibly unmounting it could cause driver
 	 * reinitialization.
 	 */
 
 	vfs_ref(rootdevmp);
 	devmp = rootdevmp;
 	rootdevmp = NULL;
 
 	mtx_lock(&mountlist_mtx);
 	TAILQ_REMOVE(&mountlist, mp, mnt_list);
 	TAILQ_REMOVE(&mountlist, devmp, mnt_list);
 	mtx_unlock(&mountlist_mtx);
 
 	oldrootvnode = rootvnode;
 
 	/*
 	 * Unmount everything except for the two filesystems preserved above.
 	 */
 	vfs_unmountall();
 
 	/*
 	 * Add /dev back; vfs_mountroot() will move it into its new place.
 	 */
 	mtx_lock(&mountlist_mtx);
 	TAILQ_INSERT_HEAD(&mountlist, devmp, mnt_list);
 	mtx_unlock(&mountlist_mtx);
 	rootdevmp = devmp;
 	vfs_rel(rootdevmp);
 
 	/*
 	 * Mount the new rootfs.
 	 */
 	vfs_mountroot();
 
 	/*
 	 * Update all references to the old rootvnode.
 	 */
 	mountcheckdirs(oldrootvnode, rootvnode);
 
 	/*
 	 * Add the temporary filesystem back and unbusy it.
 	 */
 	mtx_lock(&mountlist_mtx);
 	TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
 	mtx_unlock(&mountlist_mtx);
 	vfs_unbusy(mp);
 
 	return (0);
 }
 
 /*
  * If the shutdown was a clean halt, behave accordingly.
  */
 static void
 shutdown_halt(void *junk, int howto)
 {
 
 	if (howto & RB_HALT) {
 		printf("\n");
 		printf("The operating system has halted.\n");
 		printf("Please press any key to reboot.\n\n");
 
 		wdog_kern_pat(WD_TO_NEVER);
 
 		switch (cngetc()) {
 		case -1:		/* No console, just die */
 			cpu_halt();
 			/* NOTREACHED */
 		default:
 			break;
 		}
 	}
 }
 
 /*
  * Check to see if the system panicked, pause and then reboot
  * according to the specified delay.
  */
 static void
 shutdown_panic(void *junk, int howto)
 {
 	int loop;
 
 	if (howto & RB_DUMP) {
 		if (panic_reboot_wait_time != 0) {
 			if (panic_reboot_wait_time != -1) {
 				printf("Automatic reboot in %d seconds - "
 				       "press a key on the console to abort\n",
 					panic_reboot_wait_time);
 				for (loop = panic_reboot_wait_time * 10;
 				     loop > 0; --loop) {
 					DELAY(1000 * 100); /* 1/10th second */
 					/* Did user type a key? */
 					if (cncheckc() != -1)
 						break;
 				}
 				if (!loop)
 					return;
 			}
 		} else { /* zero time specified - reboot NOW */
 			return;
 		}
 		printf("--> Press a key on the console to reboot,\n");
 		printf("--> or switch off the system now.\n");
 		cngetc();
 	}
 }
 
 /*
  * Everything done, now reset
  */
 static void
 shutdown_reset(void *junk, int howto)
 {
 
 	printf("Rebooting...\n");
 	DELAY(1000000);	/* wait 1 sec for printf's to complete and be read */
 
 	/*
 	 * Acquiring smp_ipi_mtx here has a double effect:
 	 * - it disables interrupts avoiding CPU0 preemption
 	 *   by fast handlers (thus deadlocking  against other CPUs)
 	 * - it avoids deadlocks against smp_rendezvous() or, more 
 	 *   generally, threads busy-waiting, with this spinlock held,
 	 *   and waiting for responses by threads on other CPUs
 	 *   (ie. smp_tlb_shootdown()).
 	 *
 	 * For the !SMP case it just needs to handle the former problem.
 	 */
 #ifdef SMP
 	mtx_lock_spin(&smp_ipi_mtx);
 #else
 	spinlock_enter();
 #endif
 
 	cpu_reset();
 	/* NOTREACHED */ /* assuming reset worked */
 }
 
 #if defined(WITNESS) || defined(INVARIANT_SUPPORT)
 static int kassert_warn_only = 0;
 #ifdef KDB
 static int kassert_do_kdb = 0;
 #endif
 #ifdef KTR
 static int kassert_do_ktr = 0;
 #endif
 static int kassert_do_log = 1;
 static int kassert_log_pps_limit = 4;
 static int kassert_log_mute_at = 0;
 static int kassert_log_panic_at = 0;
 static int kassert_suppress_in_panic = 0;
 static int kassert_warnings = 0;
 
 SYSCTL_NODE(_debug, OID_AUTO, kassert, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
     "kassert options");
 
 #ifdef KASSERT_PANIC_OPTIONAL
 #define KASSERT_RWTUN	CTLFLAG_RWTUN
 #else
 #define KASSERT_RWTUN	CTLFLAG_RDTUN
 #endif
 
 SYSCTL_INT(_debug_kassert, OID_AUTO, warn_only, KASSERT_RWTUN,
     &kassert_warn_only, 0,
     "KASSERT triggers a panic (0) or just a warning (1)");
 
 #ifdef KDB
 SYSCTL_INT(_debug_kassert, OID_AUTO, do_kdb, KASSERT_RWTUN,
     &kassert_do_kdb, 0, "KASSERT will enter the debugger");
 #endif
 
 #ifdef KTR
 SYSCTL_UINT(_debug_kassert, OID_AUTO, do_ktr, KASSERT_RWTUN,
     &kassert_do_ktr, 0,
     "KASSERT does a KTR, set this to the KTRMASK you want");
 #endif
 
 SYSCTL_INT(_debug_kassert, OID_AUTO, do_log, KASSERT_RWTUN,
     &kassert_do_log, 0,
     "If warn_only is enabled, log (1) or do not log (0) assertion violations");
 
 SYSCTL_INT(_debug_kassert, OID_AUTO, warnings, CTLFLAG_RD | CTLFLAG_STATS,
     &kassert_warnings, 0, "number of KASSERTs that have been triggered");
 
 SYSCTL_INT(_debug_kassert, OID_AUTO, log_panic_at, KASSERT_RWTUN,
     &kassert_log_panic_at, 0, "max number of KASSERTS before we will panic");
 
 SYSCTL_INT(_debug_kassert, OID_AUTO, log_pps_limit, KASSERT_RWTUN,
     &kassert_log_pps_limit, 0, "limit number of log messages per second");
 
 SYSCTL_INT(_debug_kassert, OID_AUTO, log_mute_at, KASSERT_RWTUN,
     &kassert_log_mute_at, 0, "max number of KASSERTS to log");
 
 SYSCTL_INT(_debug_kassert, OID_AUTO, suppress_in_panic, KASSERT_RWTUN,
     &kassert_suppress_in_panic, 0,
     "KASSERTs will be suppressed while handling a panic");
 #undef KASSERT_RWTUN
 
 static int kassert_sysctl_kassert(SYSCTL_HANDLER_ARGS);
 
 SYSCTL_PROC(_debug_kassert, OID_AUTO, kassert,
     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE | CTLFLAG_MPSAFE, NULL, 0,
     kassert_sysctl_kassert, "I",
     "set to trigger a test kassert");
 
 static int
 kassert_sysctl_kassert(SYSCTL_HANDLER_ARGS)
 {
 	int error, i;
 
 	error = sysctl_wire_old_buffer(req, sizeof(int));
 	if (error == 0) {
 		i = 0;
 		error = sysctl_handle_int(oidp, &i, 0, req);
 	}
 	if (error != 0 || req->newptr == NULL)
 		return (error);
 	KASSERT(0, ("kassert_sysctl_kassert triggered kassert %d", i));
 	return (0);
 }
 
 #ifdef KASSERT_PANIC_OPTIONAL
 /*
  * Called by KASSERT, this decides if we will panic
  * or if we will log via printf and/or ktr.
  */
 void
 kassert_panic(const char *fmt, ...)
 {
 	static char buf[256];
 	va_list ap;
 
 	va_start(ap, fmt);
 	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
 	va_end(ap);
 
 	/*
 	 * If we are suppressing secondary panics, log the warning but do not
 	 * re-enter panic/kdb.
 	 */
-	if (panicstr != NULL && kassert_suppress_in_panic) {
+	if (KERNEL_PANICKED() && kassert_suppress_in_panic) {
 		if (kassert_do_log) {
 			printf("KASSERT failed: %s\n", buf);
 #ifdef KDB
 			if (trace_all_panics && trace_on_panic)
 				kdb_backtrace();
 #endif
 		}
 		return;
 	}
 
 	/*
 	 * panic if we're not just warning, or if we've exceeded
 	 * kassert_log_panic_at warnings.
 	 */
 	if (!kassert_warn_only ||
 	    (kassert_log_panic_at > 0 &&
 	     kassert_warnings >= kassert_log_panic_at)) {
 		va_start(ap, fmt);
 		vpanic(fmt, ap);
 		/* NORETURN */
 	}
 #ifdef KTR
 	if (kassert_do_ktr)
 		CTR0(ktr_mask, buf);
 #endif /* KTR */
 	/*
 	 * log if we've not yet met the mute limit.
 	 */
 	if (kassert_do_log &&
 	    (kassert_log_mute_at == 0 ||
 	     kassert_warnings < kassert_log_mute_at)) {
 		static  struct timeval lasterr;
 		static  int curerr;
 
 		if (ppsratecheck(&lasterr, &curerr, kassert_log_pps_limit)) {
 			printf("KASSERT failed: %s\n", buf);
 			kdb_backtrace();
 		}
 	}
 #ifdef KDB
 	if (kassert_do_kdb) {
 		kdb_enter(KDB_WHY_KASSERT, buf);
 	}
 #endif
 	atomic_add_int(&kassert_warnings, 1);
 }
 #endif /* KASSERT_PANIC_OPTIONAL */
 #endif
 
 /*
  * Panic is called on unresolvable fatal errors.  It prints "panic: mesg",
  * and then reboots.  If we are called twice, then we avoid trying to sync
  * the disks as this often leads to recursive panics.
  */
 void
 panic(const char *fmt, ...)
 {
 	va_list ap;
 
 	va_start(ap, fmt);
 	vpanic(fmt, ap);
 }
 
 void
 vpanic(const char *fmt, va_list ap)
 {
 #ifdef SMP
 	cpuset_t other_cpus;
 #endif
 	struct thread *td = curthread;
 	int bootopt, newpanic;
 	static char buf[256];
 
 	spinlock_enter();
 
 #ifdef SMP
 	/*
 	 * stop_cpus_hard(other_cpus) should prevent multiple CPUs from
 	 * concurrently entering panic.  Only the winner will proceed
 	 * further.
 	 */
 	if (panicstr == NULL && !kdb_active) {
 		other_cpus = all_cpus;
 		CPU_CLR(PCPU_GET(cpuid), &other_cpus);
 		stop_cpus_hard(other_cpus);
 	}
 #endif
 
 	/*
 	 * Ensure that the scheduler is stopped while panicking, even if panic
 	 * has been entered from kdb.
 	 */
 	td->td_stopsched = 1;
 
 	bootopt = RB_AUTOBOOT;
 	newpanic = 0;
-	if (panicstr)
+	if (KERNEL_PANICKED())
 		bootopt |= RB_NOSYNC;
 	else {
 		bootopt |= RB_DUMP;
 		panicstr = fmt;
 		panicked = true;
 		newpanic = 1;
 	}
 
 	if (newpanic) {
 		(void)vsnprintf(buf, sizeof(buf), fmt, ap);
 		panicstr = buf;
 		cngrab();
 		printf("panic: %s\n", buf);
 	} else {
 		printf("panic: ");
 		vprintf(fmt, ap);
 		printf("\n");
 	}
 #ifdef SMP
 	printf("cpuid = %d\n", PCPU_GET(cpuid));
 #endif
 	printf("time = %jd\n", (intmax_t )time_second);
 #ifdef KDB
 	if ((newpanic || trace_all_panics) && trace_on_panic)
 		kdb_backtrace();
 	if (debugger_on_panic)
 		kdb_enter(KDB_WHY_PANIC, "panic");
 	else if (!newpanic && debugger_on_recursive_panic)
 		kdb_enter(KDB_WHY_PANIC, "re-panic");
 #endif
 	/*thread_lock(td); */
 	td->td_flags |= TDF_INPANIC;
 	/* thread_unlock(td); */
 	if (!sync_on_panic)
 		bootopt |= RB_NOSYNC;
 	if (poweroff_on_panic)
 		bootopt |= RB_POWEROFF;
 	if (powercycle_on_panic)
 		bootopt |= RB_POWERCYCLE;
 	kern_reboot(bootopt);
 }
 
 /*
  * Support for poweroff delay.
  *
  * Please note that setting this delay too short might power off your machine
  * before the write cache on your hard disk has been flushed, leading to
  * soft-updates inconsistencies.
  */
 #ifndef POWEROFF_DELAY
 # define POWEROFF_DELAY 5000
 #endif
 static int poweroff_delay = POWEROFF_DELAY;
 
 SYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW,
     &poweroff_delay, 0, "Delay before poweroff to write disk caches (msec)");
 
 static void
 poweroff_wait(void *junk, int howto)
 {
 
 	if ((howto & (RB_POWEROFF | RB_POWERCYCLE)) == 0 || poweroff_delay <= 0)
 		return;
 	DELAY(poweroff_delay * 1000);
 }
 
 /*
  * Some system processes (e.g. syncer) need to be stopped at appropriate
  * points in their main loops prior to a system shutdown, so that they
  * won't interfere with the shutdown process (e.g. by holding a disk buf
  * to cause sync to fail).  For each of these system processes, register
  * shutdown_kproc() as a handler for one of shutdown events.
  */
 static int kproc_shutdown_wait = 60;
 SYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW,
     &kproc_shutdown_wait, 0, "Max wait time (sec) to stop for each process");
 
 void
 kproc_shutdown(void *arg, int howto)
 {
 	struct proc *p;
 	int error;
 
-	if (panicstr)
+	if (KERNEL_PANICKED())
 		return;
 
 	p = (struct proc *)arg;
 	printf("Waiting (max %d seconds) for system process `%s' to stop... ",
 	    kproc_shutdown_wait, p->p_comm);
 	error = kproc_suspend(p, kproc_shutdown_wait * hz);
 
 	if (error == EWOULDBLOCK)
 		printf("timed out\n");
 	else
 		printf("done\n");
 }
 
 void
 kthread_shutdown(void *arg, int howto)
 {
 	struct thread *td;
 	int error;
 
-	if (panicstr)
+	if (KERNEL_PANICKED())
 		return;
 
 	td = (struct thread *)arg;
 	printf("Waiting (max %d seconds) for system thread `%s' to stop... ",
 	    kproc_shutdown_wait, td->td_name);
 	error = kthread_suspend(td, kproc_shutdown_wait * hz);
 
 	if (error == EWOULDBLOCK)
 		printf("timed out\n");
 	else
 		printf("done\n");
 }
 
 static int
 dumpdevname_sysctl_handler(SYSCTL_HANDLER_ARGS)
 {
 	char buf[256];
 	struct dumperinfo *di;
 	struct sbuf sb;
 	int error;
 
 	error = sysctl_wire_old_buffer(req, 0);
 	if (error != 0)
 		return (error);
 
 	sbuf_new_for_sysctl(&sb, buf, sizeof(buf), req);
 
 	mtx_lock(&dumpconf_list_lk);
 	TAILQ_FOREACH(di, &dumper_configs, di_next) {
 		if (di != TAILQ_FIRST(&dumper_configs))
 			sbuf_putc(&sb, ',');
 		sbuf_cat(&sb, di->di_devname);
 	}
 	mtx_unlock(&dumpconf_list_lk);
 
 	error = sbuf_finish(&sb);
 	sbuf_delete(&sb);
 	return (error);
 }
 SYSCTL_PROC(_kern_shutdown, OID_AUTO, dumpdevname,
     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, &dumper_configs, 0,
     dumpdevname_sysctl_handler, "A",
     "Device(s) for kernel dumps");
 
 static int _dump_append(struct dumperinfo *di, void *virtual, size_t length);
 
 #ifdef EKCD
 static struct kerneldumpcrypto *
 kerneldumpcrypto_create(size_t blocksize, uint8_t encryption,
     const uint8_t *key, uint32_t encryptedkeysize, const uint8_t *encryptedkey)
 {
 	struct kerneldumpcrypto *kdc;
 	struct kerneldumpkey *kdk;
 	uint32_t dumpkeysize;
 
 	dumpkeysize = roundup2(sizeof(*kdk) + encryptedkeysize, blocksize);
 	kdc = malloc(sizeof(*kdc) + dumpkeysize, M_EKCD, M_WAITOK | M_ZERO);
 
 	arc4rand(kdc->kdc_iv, sizeof(kdc->kdc_iv), 0);
 
 	kdc->kdc_encryption = encryption;
 	switch (kdc->kdc_encryption) {
 	case KERNELDUMP_ENC_AES_256_CBC:
 		if (rijndael_makeKey(&kdc->kdc_ki, DIR_ENCRYPT, 256, key) <= 0)
 			goto failed;
 		break;
 	case KERNELDUMP_ENC_CHACHA20:
 		chacha_keysetup(&kdc->kdc_chacha, key, 256);
 		break;
 	default:
 		goto failed;
 	}
 
 	kdc->kdc_dumpkeysize = dumpkeysize;
 	kdk = kdc->kdc_dumpkey;
 	kdk->kdk_encryption = kdc->kdc_encryption;
 	memcpy(kdk->kdk_iv, kdc->kdc_iv, sizeof(kdk->kdk_iv));
 	kdk->kdk_encryptedkeysize = htod32(encryptedkeysize);
 	memcpy(kdk->kdk_encryptedkey, encryptedkey, encryptedkeysize);
 
 	return (kdc);
 failed:
 	zfree(kdc, M_EKCD);
 	return (NULL);
 }
 
 static int
 kerneldumpcrypto_init(struct kerneldumpcrypto *kdc)
 {
 	uint8_t hash[SHA256_DIGEST_LENGTH];
 	SHA256_CTX ctx;
 	struct kerneldumpkey *kdk;
 	int error;
 
 	error = 0;
 
 	if (kdc == NULL)
 		return (0);
 
 	/*
 	 * When a user enters ddb it can write a crash dump multiple times.
 	 * Each time it should be encrypted using a different IV.
 	 */
 	SHA256_Init(&ctx);
 	SHA256_Update(&ctx, kdc->kdc_iv, sizeof(kdc->kdc_iv));
 	SHA256_Final(hash, &ctx);
 	bcopy(hash, kdc->kdc_iv, sizeof(kdc->kdc_iv));
 
 	switch (kdc->kdc_encryption) {
 	case KERNELDUMP_ENC_AES_256_CBC:
 		if (rijndael_cipherInit(&kdc->kdc_ci, MODE_CBC,
 		    kdc->kdc_iv) <= 0) {
 			error = EINVAL;
 			goto out;
 		}
 		break;
 	case KERNELDUMP_ENC_CHACHA20:
 		chacha_ivsetup(&kdc->kdc_chacha, kdc->kdc_iv, NULL);
 		break;
 	default:
 		error = EINVAL;
 		goto out;
 	}
 
 	kdk = kdc->kdc_dumpkey;
 	memcpy(kdk->kdk_iv, kdc->kdc_iv, sizeof(kdk->kdk_iv));
 out:
 	explicit_bzero(hash, sizeof(hash));
 	return (error);
 }
 
 static uint32_t
 kerneldumpcrypto_dumpkeysize(const struct kerneldumpcrypto *kdc)
 {
 
 	if (kdc == NULL)
 		return (0);
 	return (kdc->kdc_dumpkeysize);
 }
 #endif /* EKCD */
 
 static struct kerneldumpcomp *
 kerneldumpcomp_create(struct dumperinfo *di, uint8_t compression)
 {
 	struct kerneldumpcomp *kdcomp;
 	int format;
 
 	switch (compression) {
 	case KERNELDUMP_COMP_GZIP:
 		format = COMPRESS_GZIP;
 		break;
 	case KERNELDUMP_COMP_ZSTD:
 		format = COMPRESS_ZSTD;
 		break;
 	default:
 		return (NULL);
 	}
 
 	kdcomp = malloc(sizeof(*kdcomp), M_DUMPER, M_WAITOK | M_ZERO);
 	kdcomp->kdc_format = compression;
 	kdcomp->kdc_stream = compressor_init(kerneldumpcomp_write_cb,
 	    format, di->maxiosize, kerneldump_gzlevel, di);
 	if (kdcomp->kdc_stream == NULL) {
 		free(kdcomp, M_DUMPER);
 		return (NULL);
 	}
 	kdcomp->kdc_buf = malloc(di->maxiosize, M_DUMPER, M_WAITOK | M_NODUMP);
 	return (kdcomp);
 }
 
 static void
 kerneldumpcomp_destroy(struct dumperinfo *di)
 {
 	struct kerneldumpcomp *kdcomp;
 
 	kdcomp = di->kdcomp;
 	if (kdcomp == NULL)
 		return;
 	compressor_fini(kdcomp->kdc_stream);
 	zfree(kdcomp->kdc_buf, M_DUMPER);
 	free(kdcomp, M_DUMPER);
 }
 
 /*
  * Free a dumper. Must not be present on global list.
  */
 void
 dumper_destroy(struct dumperinfo *di)
 {
 
 	if (di == NULL)
 		return;
 
 	zfree(di->blockbuf, M_DUMPER);
 	kerneldumpcomp_destroy(di);
 #ifdef EKCD
 	zfree(di->kdcrypto, M_EKCD);
 #endif
 	zfree(di, M_DUMPER);
 }
 
 /*
  * Allocate and set up a new dumper from the provided template.
  */
 int
 dumper_create(const struct dumperinfo *di_template, const char *devname,
     const struct diocskerneldump_arg *kda, struct dumperinfo **dip)
 {
 	struct dumperinfo *newdi;
 	int error = 0;
 
 	if (dip == NULL)
 		return (EINVAL);
 
 	/* Allocate a new dumper */
 	newdi = malloc(sizeof(*newdi) + strlen(devname) + 1, M_DUMPER,
 	    M_WAITOK | M_ZERO);
 	memcpy(newdi, di_template, sizeof(*newdi));
 	newdi->blockbuf = NULL;
 	newdi->kdcrypto = NULL;
 	newdi->kdcomp = NULL;
 	strcpy(newdi->di_devname, devname);
 
 	if (kda->kda_encryption != KERNELDUMP_ENC_NONE) {
 #ifdef EKCD
 		newdi->kdcrypto = kerneldumpcrypto_create(newdi->blocksize,
 		    kda->kda_encryption, kda->kda_key,
 		    kda->kda_encryptedkeysize, kda->kda_encryptedkey);
 		if (newdi->kdcrypto == NULL) {
 			error = EINVAL;
 			goto cleanup;
 		}
 #else
 		error = EOPNOTSUPP;
 		goto cleanup;
 #endif
 	}
 	if (kda->kda_compression != KERNELDUMP_COMP_NONE) {
 #ifdef EKCD
 		/*
 		 * We can't support simultaneous unpadded block cipher
 		 * encryption and compression because there is no guarantee the
 		 * length of the compressed result is exactly a multiple of the
 		 * cipher block size.
 		 */
 		if (kda->kda_encryption == KERNELDUMP_ENC_AES_256_CBC) {
 			error = EOPNOTSUPP;
 			goto cleanup;
 		}
 #endif
 		newdi->kdcomp = kerneldumpcomp_create(newdi,
 		    kda->kda_compression);
 		if (newdi->kdcomp == NULL) {
 			error = EINVAL;
 			goto cleanup;
 		}
 	}
 	newdi->blockbuf = malloc(newdi->blocksize, M_DUMPER, M_WAITOK | M_ZERO);
 
 	*dip = newdi;
 	return (0);
 cleanup:
 	dumper_destroy(newdi);
 	return (error);
 }
 
 /*
  * Create a new dumper and register it in the global list.
  */
 int
 dumper_insert(const struct dumperinfo *di_template, const char *devname,
     const struct diocskerneldump_arg *kda)
 {
 	struct dumperinfo *newdi, *listdi;
 	bool inserted;
 	uint8_t index;
 	int error;
 
 	index = kda->kda_index;
 	MPASS(index != KDA_REMOVE && index != KDA_REMOVE_DEV &&
 	    index != KDA_REMOVE_ALL);
 
 	error = priv_check(curthread, PRIV_SETDUMPER);
 	if (error != 0)
 		return (error);
 
 	error = dumper_create(di_template, devname, kda, &newdi);
 	if (error != 0)
 		return (error);
 
 	/* Add the new configuration to the queue */
 	mtx_lock(&dumpconf_list_lk);
 	inserted = false;
 	TAILQ_FOREACH(listdi, &dumper_configs, di_next) {
 		if (index == 0) {
 			TAILQ_INSERT_BEFORE(listdi, newdi, di_next);
 			inserted = true;
 			break;
 		}
 		index--;
 	}
 	if (!inserted)
 		TAILQ_INSERT_TAIL(&dumper_configs, newdi, di_next);
 	mtx_unlock(&dumpconf_list_lk);
 
 	return (0);
 }
 
 #ifdef DDB
 void
 dumper_ddb_insert(struct dumperinfo *newdi)
 {
 	TAILQ_INSERT_HEAD(&dumper_configs, newdi, di_next);
 }
 
 void
 dumper_ddb_remove(struct dumperinfo *di)
 {
 	TAILQ_REMOVE(&dumper_configs, di, di_next);
 }
 #endif
 
 static bool
 dumper_config_match(const struct dumperinfo *di, const char *devname,
     const struct diocskerneldump_arg *kda)
 {
 	if (kda->kda_index == KDA_REMOVE_ALL)
 		return (true);
 
 	if (strcmp(di->di_devname, devname) != 0)
 		return (false);
 
 	/*
 	 * Allow wildcard removal of configs matching a device on g_dev_orphan.
 	 */
 	if (kda->kda_index == KDA_REMOVE_DEV)
 		return (true);
 
 	if (di->kdcomp != NULL) {
 		if (di->kdcomp->kdc_format != kda->kda_compression)
 			return (false);
 	} else if (kda->kda_compression != KERNELDUMP_COMP_NONE)
 		return (false);
 #ifdef EKCD
 	if (di->kdcrypto != NULL) {
 		if (di->kdcrypto->kdc_encryption != kda->kda_encryption)
 			return (false);
 		/*
 		 * Do we care to verify keys match to delete?  It seems weird
 		 * to expect multiple fallback dump configurations on the same
 		 * device that only differ in crypto key.
 		 */
 	} else
 #endif
 		if (kda->kda_encryption != KERNELDUMP_ENC_NONE)
 			return (false);
 
 	return (true);
 }
 
 /*
  * Remove and free the requested dumper(s) from the global list.
  */
 int
 dumper_remove(const char *devname, const struct diocskerneldump_arg *kda)
 {
 	struct dumperinfo *di, *sdi;
 	bool found;
 	int error;
 
 	error = priv_check(curthread, PRIV_SETDUMPER);
 	if (error != 0)
 		return (error);
 
 	/*
 	 * Try to find a matching configuration, and kill it.
 	 *
 	 * NULL 'kda' indicates remove any configuration matching 'devname',
 	 * which may remove multiple configurations in atypical configurations.
 	 */
 	found = false;
 	mtx_lock(&dumpconf_list_lk);
 	TAILQ_FOREACH_SAFE(di, &dumper_configs, di_next, sdi) {
 		if (dumper_config_match(di, devname, kda)) {
 			found = true;
 			TAILQ_REMOVE(&dumper_configs, di, di_next);
 			dumper_destroy(di);
 		}
 	}
 	mtx_unlock(&dumpconf_list_lk);
 
 	/* Only produce ENOENT if a more targeted match didn't match. */
 	if (!found && kda->kda_index == KDA_REMOVE)
 		return (ENOENT);
 	return (0);
 }
 
 static int
 dump_check_bounds(struct dumperinfo *di, off_t offset, size_t length)
 {
 
 	if (di->mediasize > 0 && length != 0 && (offset < di->mediaoffset ||
 	    offset - di->mediaoffset + length > di->mediasize)) {
 		if (di->kdcomp != NULL && offset >= di->mediaoffset) {
 			printf(
 		    "Compressed dump failed to fit in device boundaries.\n");
 			return (E2BIG);
 		}
 
 		printf("Attempt to write outside dump device boundaries.\n"
 	    "offset(%jd), mediaoffset(%jd), length(%ju), mediasize(%jd).\n",
 		    (intmax_t)offset, (intmax_t)di->mediaoffset,
 		    (uintmax_t)length, (intmax_t)di->mediasize);
 		return (ENOSPC);
 	}
 	if (length % di->blocksize != 0) {
 		printf("Attempt to write partial block of length %ju.\n",
 		    (uintmax_t)length);
 		return (EINVAL);
 	}
 	if (offset % di->blocksize != 0) {
 		printf("Attempt to write at unaligned offset %jd.\n",
 		    (intmax_t)offset);
 		return (EINVAL);
 	}
 
 	return (0);
 }
 
 #ifdef EKCD
 static int
 dump_encrypt(struct kerneldumpcrypto *kdc, uint8_t *buf, size_t size)
 {
 
 	switch (kdc->kdc_encryption) {
 	case KERNELDUMP_ENC_AES_256_CBC:
 		if (rijndael_blockEncrypt(&kdc->kdc_ci, &kdc->kdc_ki, buf,
 		    8 * size, buf) <= 0) {
 			return (EIO);
 		}
 		if (rijndael_cipherInit(&kdc->kdc_ci, MODE_CBC,
 		    buf + size - 16 /* IV size for AES-256-CBC */) <= 0) {
 			return (EIO);
 		}
 		break;
 	case KERNELDUMP_ENC_CHACHA20:
 		chacha_encrypt_bytes(&kdc->kdc_chacha, buf, buf, size);
 		break;
 	default:
 		return (EINVAL);
 	}
 
 	return (0);
 }
 
 /* Encrypt data and call dumper. */
 static int
 dump_encrypted_write(struct dumperinfo *di, void *virtual, off_t offset,
     size_t length)
 {
 	static uint8_t buf[KERNELDUMP_BUFFER_SIZE];
 	struct kerneldumpcrypto *kdc;
 	int error;
 	size_t nbytes;
 
 	kdc = di->kdcrypto;
 
 	while (length > 0) {
 		nbytes = MIN(length, sizeof(buf));
 		bcopy(virtual, buf, nbytes);
 
 		if (dump_encrypt(kdc, buf, nbytes) != 0)
 			return (EIO);
 
 		error = dump_write(di, buf, offset, nbytes);
 		if (error != 0)
 			return (error);
 
 		offset += nbytes;
 		virtual = (void *)((uint8_t *)virtual + nbytes);
 		length -= nbytes;
 	}
 
 	return (0);
 }
 #endif /* EKCD */
 
 static int
 kerneldumpcomp_write_cb(void *base, size_t length, off_t offset, void *arg)
 {
 	struct dumperinfo *di;
 	size_t resid, rlength;
 	int error;
 
 	di = arg;
 
 	if (length % di->blocksize != 0) {
 		/*
 		 * This must be the final write after flushing the compression
 		 * stream. Write as many full blocks as possible and stash the
 		 * residual data in the dumper's block buffer. It will be
 		 * padded and written in dump_finish().
 		 */
 		rlength = rounddown(length, di->blocksize);
 		if (rlength != 0) {
 			error = _dump_append(di, base, rlength);
 			if (error != 0)
 				return (error);
 		}
 		resid = length - rlength;
 		memmove(di->blockbuf, (uint8_t *)base + rlength, resid);
 		bzero((uint8_t *)di->blockbuf + resid, di->blocksize - resid);
 		di->kdcomp->kdc_resid = resid;
 		return (EAGAIN);
 	}
 	return (_dump_append(di, base, length));
 }
 
 /*
  * Write kernel dump headers at the beginning and end of the dump extent.
  * Write the kernel dump encryption key after the leading header if we were
  * configured to do so.
  */
 static int
 dump_write_headers(struct dumperinfo *di, struct kerneldumpheader *kdh)
 {
 #ifdef EKCD
 	struct kerneldumpcrypto *kdc;
 #endif
 	void *buf;
 	size_t hdrsz;
 	uint64_t extent;
 	uint32_t keysize;
 	int error;
 
 	hdrsz = sizeof(*kdh);
 	if (hdrsz > di->blocksize)
 		return (ENOMEM);
 
 #ifdef EKCD
 	kdc = di->kdcrypto;
 	keysize = kerneldumpcrypto_dumpkeysize(kdc);
 #else
 	keysize = 0;
 #endif
 
 	/*
 	 * If the dump device has special handling for headers, let it take care
 	 * of writing them out.
 	 */
 	if (di->dumper_hdr != NULL)
 		return (di->dumper_hdr(di, kdh));
 
 	if (hdrsz == di->blocksize)
 		buf = kdh;
 	else {
 		buf = di->blockbuf;
 		memset(buf, 0, di->blocksize);
 		memcpy(buf, kdh, hdrsz);
 	}
 
 	extent = dtoh64(kdh->dumpextent);
 #ifdef EKCD
 	if (kdc != NULL) {
 		error = dump_write(di, kdc->kdc_dumpkey,
 		    di->mediaoffset + di->mediasize - di->blocksize - extent -
 		    keysize, keysize);
 		if (error != 0)
 			return (error);
 	}
 #endif
 
 	error = dump_write(di, buf,
 	    di->mediaoffset + di->mediasize - 2 * di->blocksize - extent -
 	    keysize, di->blocksize);
 	if (error == 0)
 		error = dump_write(di, buf, di->mediaoffset + di->mediasize -
 		    di->blocksize, di->blocksize);
 	return (error);
 }
 
 /*
  * Don't touch the first SIZEOF_METADATA bytes on the dump device.  This is to
  * protect us from metadata and metadata from us.
  */
 #define	SIZEOF_METADATA		(64 * 1024)
 
 /*
  * Do some preliminary setup for a kernel dump: initialize state for encryption,
  * if requested, and make sure that we have enough space on the dump device.
  *
  * We set things up so that the dump ends before the last sector of the dump
  * device, at which the trailing header is written.
  *
  *     +-----------+------+-----+----------------------------+------+
  *     |           | lhdr | key |    ... kernel dump ...     | thdr |
  *     +-----------+------+-----+----------------------------+------+
  *                   1 blk  opt <------- dump extent --------> 1 blk
  *
  * Dumps written using dump_append() start at the beginning of the extent.
  * Uncompressed dumps will use the entire extent, but compressed dumps typically
  * will not. The true length of the dump is recorded in the leading and trailing
  * headers once the dump has been completed.
  *
  * The dump device may provide a callback, in which case it will initialize
  * dumpoff and take care of laying out the headers.
  */
 int
 dump_start(struct dumperinfo *di, struct kerneldumpheader *kdh)
 {
 #ifdef EKCD
 	struct kerneldumpcrypto *kdc;
 #endif
 	void *key;
 	uint64_t dumpextent, span;
 	uint32_t keysize;
 	int error;
 
 #ifdef EKCD
 	/* Send the key before the dump so a partial dump is still usable. */
 	kdc = di->kdcrypto;
 	error = kerneldumpcrypto_init(kdc);
 	if (error != 0)
 		return (error);
 	keysize = kerneldumpcrypto_dumpkeysize(kdc);
 	key = keysize > 0 ? kdc->kdc_dumpkey : NULL;
 #else
 	error = 0;
 	keysize = 0;
 	key = NULL;
 #endif
 
 	if (di->dumper_start != NULL) {
 		error = di->dumper_start(di, key, keysize);
 	} else {
 		dumpextent = dtoh64(kdh->dumpextent);
 		span = SIZEOF_METADATA + dumpextent + 2 * di->blocksize +
 		    keysize;
 		if (di->mediasize < span) {
 			if (di->kdcomp == NULL)
 				return (E2BIG);
 
 			/*
 			 * We don't yet know how much space the compressed dump
 			 * will occupy, so try to use the whole swap partition
 			 * (minus the first 64KB) in the hope that the
 			 * compressed dump will fit. If that doesn't turn out to
 			 * be enough, the bounds checking in dump_write()
 			 * will catch us and cause the dump to fail.
 			 */
 			dumpextent = di->mediasize - span + dumpextent;
 			kdh->dumpextent = htod64(dumpextent);
 		}
 
 		/*
 		 * The offset at which to begin writing the dump.
 		 */
 		di->dumpoff = di->mediaoffset + di->mediasize - di->blocksize -
 		    dumpextent;
 	}
 	di->origdumpoff = di->dumpoff;
 	return (error);
 }
 
 static int
 _dump_append(struct dumperinfo *di, void *virtual, size_t length)
 {
 	int error;
 
 #ifdef EKCD
 	if (di->kdcrypto != NULL)
 		error = dump_encrypted_write(di, virtual, di->dumpoff, length);
 	else
 #endif
 		error = dump_write(di, virtual, di->dumpoff, length);
 	if (error == 0)
 		di->dumpoff += length;
 	return (error);
 }
 
 /*
  * Write to the dump device starting at dumpoff. When compression is enabled,
  * writes to the device will be performed using a callback that gets invoked
  * when the compression stream's output buffer is full.
  */
 int
 dump_append(struct dumperinfo *di, void *virtual, size_t length)
 {
 	void *buf;
 
 	if (di->kdcomp != NULL) {
 		/* Bounce through a buffer to avoid CRC errors. */
 		if (length > di->maxiosize)
 			return (EINVAL);
 		buf = di->kdcomp->kdc_buf;
 		memmove(buf, virtual, length);
 		return (compressor_write(di->kdcomp->kdc_stream, buf, length));
 	}
 	return (_dump_append(di, virtual, length));
 }
 
 /*
  * Write to the dump device at the specified offset.
  */
 int
 dump_write(struct dumperinfo *di, void *virtual, off_t offset, size_t length)
 {
 	int error;
 
 	error = dump_check_bounds(di, offset, length);
 	if (error != 0)
 		return (error);
 	return (di->dumper(di->priv, virtual, offset, length));
 }
 
 /*
  * Perform kernel dump finalization: flush the compression stream, if necessary,
  * write the leading and trailing kernel dump headers now that we know the true
  * length of the dump, and optionally write the encryption key following the
  * leading header.
  */
 int
 dump_finish(struct dumperinfo *di, struct kerneldumpheader *kdh)
 {
 	int error;
 
 	if (di->kdcomp != NULL) {
 		error = compressor_flush(di->kdcomp->kdc_stream);
 		if (error == EAGAIN) {
 			/* We have residual data in di->blockbuf. */
 			error = _dump_append(di, di->blockbuf, di->blocksize);
 			if (error == 0)
 				/* Compensate for _dump_append()'s adjustment. */
 				di->dumpoff -= di->blocksize - di->kdcomp->kdc_resid;
 			di->kdcomp->kdc_resid = 0;
 		}
 		if (error != 0)
 			return (error);
 
 		/*
 		 * We now know the size of the compressed dump, so update the
 		 * header accordingly and recompute parity.
 		 */
 		kdh->dumplength = htod64(di->dumpoff - di->origdumpoff);
 		kdh->parity = 0;
 		kdh->parity = kerneldump_parity(kdh);
 
 		compressor_reset(di->kdcomp->kdc_stream);
 	}
 
 	error = dump_write_headers(di, kdh);
 	if (error != 0)
 		return (error);
 
 	(void)dump_write(di, NULL, 0, 0);
 	return (0);
 }
 
 void
 dump_init_header(const struct dumperinfo *di, struct kerneldumpheader *kdh,
     const char *magic, uint32_t archver, uint64_t dumplen)
 {
 	size_t dstsize;
 
 	bzero(kdh, sizeof(*kdh));
 	strlcpy(kdh->magic, magic, sizeof(kdh->magic));
 	strlcpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture));
 	kdh->version = htod32(KERNELDUMPVERSION);
 	kdh->architectureversion = htod32(archver);
 	kdh->dumplength = htod64(dumplen);
 	kdh->dumpextent = kdh->dumplength;
 	kdh->dumptime = htod64(time_second);
 #ifdef EKCD
 	kdh->dumpkeysize = htod32(kerneldumpcrypto_dumpkeysize(di->kdcrypto));
 #else
 	kdh->dumpkeysize = 0;
 #endif
 	kdh->blocksize = htod32(di->blocksize);
 	strlcpy(kdh->hostname, prison0.pr_hostname, sizeof(kdh->hostname));
 	dstsize = sizeof(kdh->versionstring);
 	if (strlcpy(kdh->versionstring, version, dstsize) >= dstsize)
 		kdh->versionstring[dstsize - 2] = '\n';
 	if (panicstr != NULL)
 		strlcpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring));
 	if (di->kdcomp != NULL)
 		kdh->compression = di->kdcomp->kdc_format;
 	kdh->parity = kerneldump_parity(kdh);
 }
 
 #ifdef DDB
 DB_SHOW_COMMAND(panic, db_show_panic)
 {
 
 	if (panicstr == NULL)
 		db_printf("panicstr not set\n");
 	else
 		db_printf("panic: %s\n", panicstr);
 }
 #endif
diff --git a/sys/kern/subr_asan.c b/sys/kern/subr_asan.c
index 19496346ce7e..003b89f888e9 100644
--- a/sys/kern/subr_asan.c
+++ b/sys/kern/subr_asan.c
@@ -1,1180 +1,1180 @@
 /*	$NetBSD: subr_asan.c,v 1.26 2020/09/10 14:10:46 maxv Exp $	*/
 
 /*
  * Copyright (c) 2018-2020 Maxime Villard, m00nbsd.net
  * All rights reserved.
  *
  * This code is part of the KASAN subsystem of the NetBSD kernel.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  * 1. Redistributions of source code must retain the above copyright
  *    notice, this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
  *
  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
 
 #define	SAN_RUNTIME
 
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 #if 0
 __KERNEL_RCSID(0, "$NetBSD: subr_asan.c,v 1.26 2020/09/10 14:10:46 maxv Exp $");
 #endif
 
 #include <sys/param.h>
 #include <sys/systm.h>
 #include <sys/asan.h>
 #include <sys/kernel.h>
 #include <sys/stack.h>
 #include <sys/sysctl.h>
 
 #include <machine/asan.h>
 
 /* ASAN constants. Part of the compiler ABI. */
 #define KASAN_SHADOW_MASK		(KASAN_SHADOW_SCALE - 1)
 #define KASAN_ALLOCA_SCALE_SIZE		32
 
 /* ASAN ABI version. */
 #if defined(__clang__) && (__clang_major__ - 0 >= 6)
 #define ASAN_ABI_VERSION	8
 #elif __GNUC_PREREQ__(7, 1) && !defined(__clang__)
 #define ASAN_ABI_VERSION	8
 #elif __GNUC_PREREQ__(6, 1) && !defined(__clang__)
 #define ASAN_ABI_VERSION	6
 #else
 #error "Unsupported compiler version"
 #endif
 
 #define __RET_ADDR	(unsigned long)__builtin_return_address(0)
 
 /* Global variable descriptor. Part of the compiler ABI.  */
 struct __asan_global_source_location {
 	const char *filename;
 	int line_no;
 	int column_no;
 };
 
 struct __asan_global {
 	const void *beg;		/* address of the global variable */
 	size_t size;			/* size of the global variable */
 	size_t size_with_redzone;	/* size with the redzone */
 	const void *name;		/* name of the variable */
 	const void *module_name;	/* name of the module where the var is declared */
 	unsigned long has_dynamic_init;	/* the var has dyn initializer (c++) */
 	struct __asan_global_source_location *location;
 #if ASAN_ABI_VERSION >= 7
 	uintptr_t odr_indicator;	/* the address of the ODR indicator symbol */
 #endif
 };
 
 FEATURE(kasan, "Kernel address sanitizer");
 
 static SYSCTL_NODE(_debug, OID_AUTO, kasan, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
     "KASAN options");
 
 static int panic_on_violation = 1;
 SYSCTL_INT(_debug_kasan, OID_AUTO, panic_on_violation, CTLFLAG_RDTUN,
     &panic_on_violation, 0,
     "Panic if an invalid access is detected");
 
 static bool kasan_enabled __read_mostly = false;
 
 /* -------------------------------------------------------------------------- */
 
 void
 kasan_shadow_map(vm_offset_t addr, size_t size)
 {
 	size_t sz, npages, i;
 	vm_offset_t sva, eva;
 
 	KASSERT(addr % KASAN_SHADOW_SCALE == 0,
 	    ("%s: invalid address %#lx", __func__, addr));
 
 	sz = roundup(size, KASAN_SHADOW_SCALE) / KASAN_SHADOW_SCALE;
 
 	sva = kasan_md_addr_to_shad(addr);
 	eva = kasan_md_addr_to_shad(addr) + sz;
 
 	sva = rounddown(sva, PAGE_SIZE);
 	eva = roundup(eva, PAGE_SIZE);
 
 	npages = (eva - sva) / PAGE_SIZE;
 
 	KASSERT(sva >= KASAN_MIN_ADDRESS && eva < KASAN_MAX_ADDRESS,
 	    ("%s: invalid address range %#lx-%#lx", __func__, sva, eva));
 
 	for (i = 0; i < npages; i++)
 		pmap_san_enter(sva + ptoa(i));
 }
 
 void
 kasan_init(void)
 {
 	int disabled;
 
 	disabled = 0;
 	TUNABLE_INT_FETCH("debug.kasan.disabled", &disabled);
 	if (disabled)
 		return;
 
 	/* MD initialization. */
 	kasan_md_init();
 
 	/* Now officially enabled. */
 	kasan_enabled = true;
 }
 
 static inline const char *
 kasan_code_name(uint8_t code)
 {
 	switch (code) {
 	case KASAN_GENERIC_REDZONE:
 		return "GenericRedZone";
 	case KASAN_MALLOC_REDZONE:
 		return "MallocRedZone";
 	case KASAN_KMEM_REDZONE:
 		return "KmemRedZone";
 	case KASAN_UMA_FREED:
 		return "UMAUseAfterFree";
 	case KASAN_KSTACK_FREED:
 		return "KernelStack";
 	case KASAN_EXEC_ARGS_FREED:
 		return "ExecKVA";
 	case 1 ... 7:
 		return "RedZonePartial";
 	case KASAN_STACK_LEFT:
 		return "StackLeft";
 	case KASAN_STACK_MID:
 		return "StackMiddle";
 	case KASAN_STACK_RIGHT:
 		return "StackRight";
 	case KASAN_USE_AFTER_RET:
 		return "UseAfterRet";
 	case KASAN_USE_AFTER_SCOPE:
 		return "UseAfterScope";
 	default:
 		return "Unknown";
 	}
 }
 
 #define	REPORT(f, ...) do {				\
 	if (panic_on_violation) {			\
 		kasan_enabled = false;			\
 		panic(f, __VA_ARGS__);			\
 	} else {					\
 		struct stack st;			\
 							\
 		stack_save(&st);			\
 		printf(f "\n", __VA_ARGS__);		\
 		stack_print_ddb(&st);			\
 	}						\
 } while (0)
 
 static void
 kasan_report(unsigned long addr, size_t size, bool write, unsigned long pc,
     uint8_t code)
 {
 	REPORT("ASan: Invalid access, %zu-byte %s at %#lx, %s(%x)",
 	    size, (write ? "write" : "read"), addr, kasan_code_name(code),
 	    code);
 }
 
 static __always_inline void
 kasan_shadow_1byte_markvalid(unsigned long addr)
 {
 	int8_t *byte = (int8_t *)kasan_md_addr_to_shad(addr);
 	int8_t last = (addr & KASAN_SHADOW_MASK) + 1;
 
 	*byte = last;
 }
 
 static __always_inline void
 kasan_shadow_Nbyte_markvalid(const void *addr, size_t size)
 {
 	size_t i;
 
 	for (i = 0; i < size; i++) {
 		kasan_shadow_1byte_markvalid((unsigned long)addr + i);
 	}
 }
 
 static __always_inline void
 kasan_shadow_Nbyte_fill(const void *addr, size_t size, uint8_t code)
 {
 	void *shad;
 
 	if (__predict_false(size == 0))
 		return;
 	if (__predict_false(kasan_md_unsupported((vm_offset_t)addr)))
 		return;
 
 	KASSERT((vm_offset_t)addr % KASAN_SHADOW_SCALE == 0,
 	    ("%s: invalid address %p", __func__, addr));
 	KASSERT(size % KASAN_SHADOW_SCALE == 0,
 	    ("%s: invalid size %zu", __func__, size));
 
 	shad = (void *)kasan_md_addr_to_shad((uintptr_t)addr);
 	size = size >> KASAN_SHADOW_SCALE_SHIFT;
 
 	__builtin_memset(shad, code, size);
 }
 
 /*
  * In an area of size 'sz_with_redz', mark the 'size' first bytes as valid,
  * and the rest as invalid. There are generally two use cases:
  *
  *  o kasan_mark(addr, origsize, size, code), with origsize < size. This marks
  *    the redzone at the end of the buffer as invalid. If the entire is to be
  *    marked invalid, origsize will be 0.
  *
  *  o kasan_mark(addr, size, size, 0). This marks the entire buffer as valid.
  */
 void
 kasan_mark(const void *addr, size_t size, size_t redzsize, uint8_t code)
 {
 	size_t i, n, redz;
 	int8_t *shad;
 
 	if ((vm_offset_t)addr >= DMAP_MIN_ADDRESS &&
 	    (vm_offset_t)addr < DMAP_MAX_ADDRESS)
 		return;
 
 	KASSERT((vm_offset_t)addr >= VM_MIN_KERNEL_ADDRESS &&
 	    (vm_offset_t)addr < VM_MAX_KERNEL_ADDRESS,
 	    ("%s: invalid address %p", __func__, addr));
 	KASSERT((vm_offset_t)addr % KASAN_SHADOW_SCALE == 0,
 	    ("%s: invalid address %p", __func__, addr));
 	redz = redzsize - roundup(size, KASAN_SHADOW_SCALE);
 	KASSERT(redz % KASAN_SHADOW_SCALE == 0,
 	    ("%s: invalid size %zu", __func__, redz));
 	shad = (int8_t *)kasan_md_addr_to_shad((uintptr_t)addr);
 
 	/* Chunks of 8 bytes, valid. */
 	n = size / KASAN_SHADOW_SCALE;
 	for (i = 0; i < n; i++) {
 		*shad++ = 0;
 	}
 
 	/* Possibly one chunk, mid. */
 	if ((size & KASAN_SHADOW_MASK) != 0) {
 		*shad++ = (size & KASAN_SHADOW_MASK);
 	}
 
 	/* Chunks of 8 bytes, invalid. */
 	n = redz / KASAN_SHADOW_SCALE;
 	for (i = 0; i < n; i++) {
 		*shad++ = code;
 	}
 }
 
 /* -------------------------------------------------------------------------- */
 
 #define ADDR_CROSSES_SCALE_BOUNDARY(addr, size) 		\
 	(addr >> KASAN_SHADOW_SCALE_SHIFT) !=			\
 	    ((addr + size - 1) >> KASAN_SHADOW_SCALE_SHIFT)
 
 static __always_inline bool
 kasan_shadow_1byte_isvalid(unsigned long addr, uint8_t *code)
 {
 	int8_t *byte = (int8_t *)kasan_md_addr_to_shad(addr);
 	int8_t last = (addr & KASAN_SHADOW_MASK) + 1;
 
 	if (__predict_true(*byte == 0 || last <= *byte)) {
 		return (true);
 	}
 	*code = *byte;
 	return (false);
 }
 
 static __always_inline bool
 kasan_shadow_2byte_isvalid(unsigned long addr, uint8_t *code)
 {
 	int8_t *byte, last;
 
 	if (ADDR_CROSSES_SCALE_BOUNDARY(addr, 2)) {
 		return (kasan_shadow_1byte_isvalid(addr, code) &&
 		    kasan_shadow_1byte_isvalid(addr+1, code));
 	}
 
 	byte = (int8_t *)kasan_md_addr_to_shad(addr);
 	last = ((addr + 1) & KASAN_SHADOW_MASK) + 1;
 
 	if (__predict_true(*byte == 0 || last <= *byte)) {
 		return (true);
 	}
 	*code = *byte;
 	return (false);
 }
 
 static __always_inline bool
 kasan_shadow_4byte_isvalid(unsigned long addr, uint8_t *code)
 {
 	int8_t *byte, last;
 
 	if (ADDR_CROSSES_SCALE_BOUNDARY(addr, 4)) {
 		return (kasan_shadow_2byte_isvalid(addr, code) &&
 		    kasan_shadow_2byte_isvalid(addr+2, code));
 	}
 
 	byte = (int8_t *)kasan_md_addr_to_shad(addr);
 	last = ((addr + 3) & KASAN_SHADOW_MASK) + 1;
 
 	if (__predict_true(*byte == 0 || last <= *byte)) {
 		return (true);
 	}
 	*code = *byte;
 	return (false);
 }
 
 static __always_inline bool
 kasan_shadow_8byte_isvalid(unsigned long addr, uint8_t *code)
 {
 	int8_t *byte, last;
 
 	if (ADDR_CROSSES_SCALE_BOUNDARY(addr, 8)) {
 		return (kasan_shadow_4byte_isvalid(addr, code) &&
 		    kasan_shadow_4byte_isvalid(addr+4, code));
 	}
 
 	byte = (int8_t *)kasan_md_addr_to_shad(addr);
 	last = ((addr + 7) & KASAN_SHADOW_MASK) + 1;
 
 	if (__predict_true(*byte == 0 || last <= *byte)) {
 		return (true);
 	}
 	*code = *byte;
 	return (false);
 }
 
 static __always_inline bool
 kasan_shadow_Nbyte_isvalid(unsigned long addr, size_t size, uint8_t *code)
 {
 	size_t i;
 
 	for (i = 0; i < size; i++) {
 		if (!kasan_shadow_1byte_isvalid(addr+i, code))
 			return (false);
 	}
 
 	return (true);
 }
 
 static __always_inline void
 kasan_shadow_check(unsigned long addr, size_t size, bool write,
     unsigned long retaddr)
 {
 	uint8_t code;
 	bool valid;
 
 	if (__predict_false(!kasan_enabled))
 		return;
 	if (__predict_false(size == 0))
 		return;
 	if (__predict_false(kasan_md_unsupported(addr)))
 		return;
-	if (__predict_false(panicstr != NULL))
+	if (KERNEL_PANICKED())
 		return;
 
 	if (__builtin_constant_p(size)) {
 		switch (size) {
 		case 1:
 			valid = kasan_shadow_1byte_isvalid(addr, &code);
 			break;
 		case 2:
 			valid = kasan_shadow_2byte_isvalid(addr, &code);
 			break;
 		case 4:
 			valid = kasan_shadow_4byte_isvalid(addr, &code);
 			break;
 		case 8:
 			valid = kasan_shadow_8byte_isvalid(addr, &code);
 			break;
 		default:
 			valid = kasan_shadow_Nbyte_isvalid(addr, size, &code);
 			break;
 		}
 	} else {
 		valid = kasan_shadow_Nbyte_isvalid(addr, size, &code);
 	}
 
 	if (__predict_false(!valid)) {
 		kasan_report(addr, size, write, retaddr, code);
 	}
 }
 
 /* -------------------------------------------------------------------------- */
 
 void *
 kasan_memcpy(void *dst, const void *src, size_t len)
 {
 	kasan_shadow_check((unsigned long)src, len, false, __RET_ADDR);
 	kasan_shadow_check((unsigned long)dst, len, true, __RET_ADDR);
 	return (__builtin_memcpy(dst, src, len));
 }
 
 int
 kasan_memcmp(const void *b1, const void *b2, size_t len)
 {
 	kasan_shadow_check((unsigned long)b1, len, false, __RET_ADDR);
 	kasan_shadow_check((unsigned long)b2, len, false, __RET_ADDR);
 	return (__builtin_memcmp(b1, b2, len));
 }
 
 void *
 kasan_memset(void *b, int c, size_t len)
 {
 	kasan_shadow_check((unsigned long)b, len, true, __RET_ADDR);
 	return (__builtin_memset(b, c, len));
 }
 
 void *
 kasan_memmove(void *dst, const void *src, size_t len)
 {
 	kasan_shadow_check((unsigned long)src, len, false, __RET_ADDR);
 	kasan_shadow_check((unsigned long)dst, len, true, __RET_ADDR);
 	return (__builtin_memmove(dst, src, len));
 }
 
 size_t
 kasan_strlen(const char *str)
 {
 	const char *s;
 
 	s = str;
 	while (1) {
 		kasan_shadow_check((unsigned long)s, 1, false, __RET_ADDR);
 		if (*s == '\0')
 			break;
 		s++;
 	}
 
 	return (s - str);
 }
 
 char *
 kasan_strcpy(char *dst, const char *src)
 {
 	char *save = dst;
 
 	while (1) {
 		kasan_shadow_check((unsigned long)src, 1, false, __RET_ADDR);
 		kasan_shadow_check((unsigned long)dst, 1, true, __RET_ADDR);
 		*dst = *src;
 		if (*src == '\0')
 			break;
 		src++, dst++;
 	}
 
 	return save;
 }
 
 int
 kasan_strcmp(const char *s1, const char *s2)
 {
 	while (1) {
 		kasan_shadow_check((unsigned long)s1, 1, false, __RET_ADDR);
 		kasan_shadow_check((unsigned long)s2, 1, false, __RET_ADDR);
 		if (*s1 != *s2)
 			break;
 		if (*s1 == '\0')
 			return 0;
 		s1++, s2++;
 	}
 
 	return (*(const unsigned char *)s1 - *(const unsigned char *)s2);
 }
 
 int
 kasan_copyin(const void *uaddr, void *kaddr, size_t len)
 {
 	kasan_shadow_check((unsigned long)kaddr, len, true, __RET_ADDR);
 	return (copyin(uaddr, kaddr, len));
 }
 
 int
 kasan_copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done)
 {
 	kasan_shadow_check((unsigned long)kaddr, len, true, __RET_ADDR);
 	return (copyinstr(uaddr, kaddr, len, done));
 }
 
 int
 kasan_copyout(const void *kaddr, void *uaddr, size_t len)
 {
 	kasan_shadow_check((unsigned long)kaddr, len, false, __RET_ADDR);
 	return (copyout(kaddr, uaddr, len));
 }
 
 /* -------------------------------------------------------------------------- */
 
 int
 kasan_fubyte(volatile const void *base)
 {
 	return (fubyte(base));
 }
 
 int
 kasan_fuword16(volatile const void *base)
 {
 	return (fuword16(base));
 }
 
 int
 kasan_fueword(volatile const void *base, long *val)
 {
 	kasan_shadow_check((unsigned long)val, sizeof(*val), true, __RET_ADDR);
 	return (fueword(base, val));
 }
 
 int
 kasan_fueword32(volatile const void *base, int32_t *val)
 {
 	kasan_shadow_check((unsigned long)val, sizeof(*val), true, __RET_ADDR);
 	return (fueword32(base, val));
 }
 
 int
 kasan_fueword64(volatile const void *base, int64_t *val)
 {
 	kasan_shadow_check((unsigned long)val, sizeof(*val), true, __RET_ADDR);
 	return (fueword64(base, val));
 }
 
 int
 kasan_subyte(volatile void *base, int byte)
 {
 	return (subyte(base, byte));
 }
 
 int
 kasan_suword(volatile void *base, long word)
 {
 	return (suword(base, word));
 }
 
 int
 kasan_suword16(volatile void *base, int word)
 {
 	return (suword16(base, word));
 }
 
 int
 kasan_suword32(volatile void *base, int32_t word)
 {
 	return (suword32(base, word));
 }
 
 int
 kasan_suword64(volatile void *base, int64_t word)
 {
 	return (suword64(base, word));
 }
 
 int
 kasan_casueword32(volatile uint32_t *base, uint32_t oldval, uint32_t *oldvalp,
     uint32_t newval)
 {
 	kasan_shadow_check((unsigned long)oldvalp, sizeof(*oldvalp), true,
 	    __RET_ADDR);
 	return (casueword32(base, oldval, oldvalp, newval));
 }
 
 int
 kasan_casueword(volatile u_long *base, u_long oldval, u_long *oldvalp,
     u_long newval)
 {
 	kasan_shadow_check((unsigned long)oldvalp, sizeof(*oldvalp), true,
 	    __RET_ADDR);
 	return (casueword(base, oldval, oldvalp, newval));
 }
 
 /* -------------------------------------------------------------------------- */
 
 #include <machine/atomic.h>
 #include <sys/atomic_san.h>
 
 #define _ASAN_ATOMIC_FUNC_ADD(name, type)				\
 	void kasan_atomic_add_##name(volatile type *ptr, type val)	\
 	{								\
 		kasan_shadow_check((uintptr_t)ptr, sizeof(type), true,	\
 		    __RET_ADDR);					\
 		atomic_add_##name(ptr, val);				\
 	}
 
 #define	ASAN_ATOMIC_FUNC_ADD(name, type)				\
 	_ASAN_ATOMIC_FUNC_ADD(name, type)				\
 	_ASAN_ATOMIC_FUNC_ADD(acq_##name, type)				\
 	_ASAN_ATOMIC_FUNC_ADD(rel_##name, type)
 
 #define _ASAN_ATOMIC_FUNC_SUBTRACT(name, type)				\
 	void kasan_atomic_subtract_##name(volatile type *ptr, type val)	\
 	{								\
 		kasan_shadow_check((uintptr_t)ptr, sizeof(type), true,	\
 		    __RET_ADDR);					\
 		atomic_subtract_##name(ptr, val);			\
 	}
 
 #define	ASAN_ATOMIC_FUNC_SUBTRACT(name, type)				\
 	_ASAN_ATOMIC_FUNC_SUBTRACT(name, type)				\
 	_ASAN_ATOMIC_FUNC_SUBTRACT(acq_##name, type)			\
 	_ASAN_ATOMIC_FUNC_SUBTRACT(rel_##name, type)
 
 #define _ASAN_ATOMIC_FUNC_SET(name, type)				\
 	void kasan_atomic_set_##name(volatile type *ptr, type val)	\
 	{								\
 		kasan_shadow_check((uintptr_t)ptr, sizeof(type), true,	\
 		    __RET_ADDR);					\
 		atomic_set_##name(ptr, val);				\
 	}
 
 #define	ASAN_ATOMIC_FUNC_SET(name, type)				\
 	_ASAN_ATOMIC_FUNC_SET(name, type)				\
 	_ASAN_ATOMIC_FUNC_SET(acq_##name, type)				\
 	_ASAN_ATOMIC_FUNC_SET(rel_##name, type)
 
 #define _ASAN_ATOMIC_FUNC_CLEAR(name, type)				\
 	void kasan_atomic_clear_##name(volatile type *ptr, type val)	\
 	{								\
 		kasan_shadow_check((uintptr_t)ptr, sizeof(type), true,	\
 		    __RET_ADDR);					\
 		atomic_clear_##name(ptr, val);				\
 	}
 
 #define	ASAN_ATOMIC_FUNC_CLEAR(name, type)				\
 	_ASAN_ATOMIC_FUNC_CLEAR(name, type)				\
 	_ASAN_ATOMIC_FUNC_CLEAR(acq_##name, type)			\
 	_ASAN_ATOMIC_FUNC_CLEAR(rel_##name, type)
 
 #define	ASAN_ATOMIC_FUNC_FETCHADD(name, type)				\
 	type kasan_atomic_fetchadd_##name(volatile type *ptr, type val)	\
 	{								\
 		kasan_shadow_check((uintptr_t)ptr, sizeof(type), true,	\
 		    __RET_ADDR);					\
 		return (atomic_fetchadd_##name(ptr, val));		\
 	}
 
 #define	ASAN_ATOMIC_FUNC_READANDCLEAR(name, type)			\
 	type kasan_atomic_readandclear_##name(volatile type *ptr)	\
 	{								\
 		kasan_shadow_check((uintptr_t)ptr, sizeof(type), true,	\
 		    __RET_ADDR);					\
 		return (atomic_readandclear_##name(ptr));		\
 	}
 
 #define	ASAN_ATOMIC_FUNC_TESTANDCLEAR(name, type)			\
 	int kasan_atomic_testandclear_##name(volatile type *ptr, u_int v) \
 	{								\
 		kasan_shadow_check((uintptr_t)ptr, sizeof(type), true,	\
 		    __RET_ADDR);					\
 		return (atomic_testandclear_##name(ptr, v));		\
 	}
 
 #define	ASAN_ATOMIC_FUNC_TESTANDSET(name, type)				\
 	int kasan_atomic_testandset_##name(volatile type *ptr, u_int v) \
 	{								\
 		kasan_shadow_check((uintptr_t)ptr, sizeof(type), true,	\
 		    __RET_ADDR);					\
 		return (atomic_testandset_##name(ptr, v));		\
 	}
 
 #define	ASAN_ATOMIC_FUNC_SWAP(name, type)				\
 	type kasan_atomic_swap_##name(volatile type *ptr, type val)	\
 	{								\
 		kasan_shadow_check((uintptr_t)ptr, sizeof(type), true,	\
 		    __RET_ADDR);					\
 		return (atomic_swap_##name(ptr, val));			\
 	}
 
 #define _ASAN_ATOMIC_FUNC_CMPSET(name, type)				\
 	int kasan_atomic_cmpset_##name(volatile type *ptr, type oval,	\
 	    type nval)							\
 	{								\
 		kasan_shadow_check((uintptr_t)ptr, sizeof(type), true,	\
 		    __RET_ADDR);					\
 		return (atomic_cmpset_##name(ptr, oval, nval));		\
 	}
 
 #define	ASAN_ATOMIC_FUNC_CMPSET(name, type)				\
 	_ASAN_ATOMIC_FUNC_CMPSET(name, type)				\
 	_ASAN_ATOMIC_FUNC_CMPSET(acq_##name, type)			\
 	_ASAN_ATOMIC_FUNC_CMPSET(rel_##name, type)
 
 #define _ASAN_ATOMIC_FUNC_FCMPSET(name, type)				\
 	int kasan_atomic_fcmpset_##name(volatile type *ptr, type *oval,	\
 	    type nval)							\
 	{								\
 		kasan_shadow_check((uintptr_t)ptr, sizeof(type), true,	\
 		    __RET_ADDR);					\
 		return (atomic_fcmpset_##name(ptr, oval, nval));	\
 	}
 
 #define	ASAN_ATOMIC_FUNC_FCMPSET(name, type)				\
 	_ASAN_ATOMIC_FUNC_FCMPSET(name, type)				\
 	_ASAN_ATOMIC_FUNC_FCMPSET(acq_##name, type)			\
 	_ASAN_ATOMIC_FUNC_FCMPSET(rel_##name, type)
 
 #define ASAN_ATOMIC_FUNC_THREAD_FENCE(name)				\
 	void kasan_atomic_thread_fence_##name(void)			\
 	{								\
 		atomic_thread_fence_##name();				\
 	}
 
 #define	_ASAN_ATOMIC_FUNC_LOAD(name, type)				\
 	type kasan_atomic_load_##name(volatile type *ptr)		\
 	{								\
 		kasan_shadow_check((uintptr_t)ptr, sizeof(type), true,	\
 		    __RET_ADDR);					\
 		return (atomic_load_##name(ptr));			\
 	}
 
 #define	ASAN_ATOMIC_FUNC_LOAD(name, type)				\
 	_ASAN_ATOMIC_FUNC_LOAD(name, type)				\
 	_ASAN_ATOMIC_FUNC_LOAD(acq_##name, type)
 
 #define	_ASAN_ATOMIC_FUNC_STORE(name, type)				\
 	void kasan_atomic_store_##name(volatile type *ptr, type val)	\
 	{								\
 		kasan_shadow_check((uintptr_t)ptr, sizeof(type), true,	\
 		    __RET_ADDR);					\
 		atomic_store_##name(ptr, val);				\
 	}
 
 #define	ASAN_ATOMIC_FUNC_STORE(name, type)				\
 	_ASAN_ATOMIC_FUNC_STORE(name, type)				\
 	_ASAN_ATOMIC_FUNC_STORE(rel_##name, type)
 
 ASAN_ATOMIC_FUNC_ADD(8, uint8_t);
 ASAN_ATOMIC_FUNC_ADD(16, uint16_t);
 ASAN_ATOMIC_FUNC_ADD(32, uint32_t);
 ASAN_ATOMIC_FUNC_ADD(64, uint64_t);
 ASAN_ATOMIC_FUNC_ADD(int, u_int);
 ASAN_ATOMIC_FUNC_ADD(long, u_long);
 ASAN_ATOMIC_FUNC_ADD(ptr, uintptr_t);
 
 ASAN_ATOMIC_FUNC_SUBTRACT(8, uint8_t);
 ASAN_ATOMIC_FUNC_SUBTRACT(16, uint16_t);
 ASAN_ATOMIC_FUNC_SUBTRACT(32, uint32_t);
 ASAN_ATOMIC_FUNC_SUBTRACT(64, uint64_t);
 ASAN_ATOMIC_FUNC_SUBTRACT(int, u_int);
 ASAN_ATOMIC_FUNC_SUBTRACT(long, u_long);
 ASAN_ATOMIC_FUNC_SUBTRACT(ptr, uintptr_t);
 
 ASAN_ATOMIC_FUNC_SET(8, uint8_t);
 ASAN_ATOMIC_FUNC_SET(16, uint16_t);
 ASAN_ATOMIC_FUNC_SET(32, uint32_t);
 ASAN_ATOMIC_FUNC_SET(64, uint64_t);
 ASAN_ATOMIC_FUNC_SET(int, u_int);
 ASAN_ATOMIC_FUNC_SET(long, u_long);
 ASAN_ATOMIC_FUNC_SET(ptr, uintptr_t);
 
 ASAN_ATOMIC_FUNC_CLEAR(8, uint8_t);
 ASAN_ATOMIC_FUNC_CLEAR(16, uint16_t);
 ASAN_ATOMIC_FUNC_CLEAR(32, uint32_t);
 ASAN_ATOMIC_FUNC_CLEAR(64, uint64_t);
 ASAN_ATOMIC_FUNC_CLEAR(int, u_int);
 ASAN_ATOMIC_FUNC_CLEAR(long, u_long);
 ASAN_ATOMIC_FUNC_CLEAR(ptr, uintptr_t);
 
 ASAN_ATOMIC_FUNC_FETCHADD(32, uint32_t);
 ASAN_ATOMIC_FUNC_FETCHADD(64, uint64_t);
 ASAN_ATOMIC_FUNC_FETCHADD(int, u_int);
 ASAN_ATOMIC_FUNC_FETCHADD(long, u_long);
 
 ASAN_ATOMIC_FUNC_READANDCLEAR(32, uint32_t);
 ASAN_ATOMIC_FUNC_READANDCLEAR(64, uint64_t);
 ASAN_ATOMIC_FUNC_READANDCLEAR(int, u_int);
 ASAN_ATOMIC_FUNC_READANDCLEAR(long, u_long);
 ASAN_ATOMIC_FUNC_READANDCLEAR(ptr, uintptr_t);
 
 ASAN_ATOMIC_FUNC_TESTANDCLEAR(32, uint32_t);
 ASAN_ATOMIC_FUNC_TESTANDCLEAR(64, uint64_t);
 ASAN_ATOMIC_FUNC_TESTANDCLEAR(int, u_int);
 ASAN_ATOMIC_FUNC_TESTANDCLEAR(long, u_long);
 
 ASAN_ATOMIC_FUNC_TESTANDSET(32, uint32_t);
 ASAN_ATOMIC_FUNC_TESTANDSET(64, uint64_t);
 ASAN_ATOMIC_FUNC_TESTANDSET(int, u_int);
 ASAN_ATOMIC_FUNC_TESTANDSET(long, u_long);
 
 ASAN_ATOMIC_FUNC_SWAP(32, uint32_t);
 ASAN_ATOMIC_FUNC_SWAP(64, uint64_t);
 ASAN_ATOMIC_FUNC_SWAP(int, u_int);
 ASAN_ATOMIC_FUNC_SWAP(long, u_long);
 ASAN_ATOMIC_FUNC_SWAP(ptr, uintptr_t);
 
 ASAN_ATOMIC_FUNC_CMPSET(8, uint8_t);
 ASAN_ATOMIC_FUNC_CMPSET(16, uint16_t);
 ASAN_ATOMIC_FUNC_CMPSET(32, uint32_t);
 ASAN_ATOMIC_FUNC_CMPSET(64, uint64_t);
 ASAN_ATOMIC_FUNC_CMPSET(int, u_int);
 ASAN_ATOMIC_FUNC_CMPSET(long, u_long);
 ASAN_ATOMIC_FUNC_CMPSET(ptr, uintptr_t);
 
 ASAN_ATOMIC_FUNC_FCMPSET(8, uint8_t);
 ASAN_ATOMIC_FUNC_FCMPSET(16, uint16_t);
 ASAN_ATOMIC_FUNC_FCMPSET(32, uint32_t);
 ASAN_ATOMIC_FUNC_FCMPSET(64, uint64_t);
 ASAN_ATOMIC_FUNC_FCMPSET(int, u_int);
 ASAN_ATOMIC_FUNC_FCMPSET(long, u_long);
 ASAN_ATOMIC_FUNC_FCMPSET(ptr, uintptr_t);
 
 ASAN_ATOMIC_FUNC_LOAD(8, uint8_t);
 ASAN_ATOMIC_FUNC_LOAD(16, uint16_t);
 ASAN_ATOMIC_FUNC_LOAD(32, uint32_t);
 ASAN_ATOMIC_FUNC_LOAD(64, uint64_t);
 ASAN_ATOMIC_FUNC_LOAD(char, u_char);
 ASAN_ATOMIC_FUNC_LOAD(short, u_short);
 ASAN_ATOMIC_FUNC_LOAD(int, u_int);
 ASAN_ATOMIC_FUNC_LOAD(long, u_long);
 ASAN_ATOMIC_FUNC_LOAD(ptr, uintptr_t);
 
 ASAN_ATOMIC_FUNC_STORE(8, uint8_t);
 ASAN_ATOMIC_FUNC_STORE(16, uint16_t);
 ASAN_ATOMIC_FUNC_STORE(32, uint32_t);
 ASAN_ATOMIC_FUNC_STORE(64, uint64_t);
 ASAN_ATOMIC_FUNC_STORE(char, u_char);
 ASAN_ATOMIC_FUNC_STORE(short, u_short);
 ASAN_ATOMIC_FUNC_STORE(int, u_int);
 ASAN_ATOMIC_FUNC_STORE(long, u_long);
 ASAN_ATOMIC_FUNC_STORE(ptr, uintptr_t);
 
 ASAN_ATOMIC_FUNC_THREAD_FENCE(acq);
 ASAN_ATOMIC_FUNC_THREAD_FENCE(rel);
 ASAN_ATOMIC_FUNC_THREAD_FENCE(acq_rel);
 ASAN_ATOMIC_FUNC_THREAD_FENCE(seq_cst);
 
 void
 kasan_atomic_interrupt_fence(void)
 {
 }
 
 /* -------------------------------------------------------------------------- */
 
 #include <sys/bus.h>
 #include <machine/bus.h>
 #include <sys/bus_san.h>
 
 int
 kasan_bus_space_map(bus_space_tag_t tag, bus_addr_t hnd, bus_size_t size,
     int flags, bus_space_handle_t *handlep)
 {
 	return (bus_space_map(tag, hnd, size, flags, handlep));
 }
 
 void
 kasan_bus_space_unmap(bus_space_tag_t tag, bus_space_handle_t hnd,
     bus_size_t size)
 {
 	bus_space_unmap(tag, hnd, size);
 }
 
 int
 kasan_bus_space_subregion(bus_space_tag_t tag, bus_space_handle_t hnd,
     bus_size_t offset, bus_size_t size, bus_space_handle_t *handlep)
 {
 	return (bus_space_subregion(tag, hnd, offset, size, handlep));
 }
 
 void
 kasan_bus_space_free(bus_space_tag_t tag, bus_space_handle_t hnd,
     bus_size_t size)
 {
 	bus_space_free(tag, hnd, size);
 }
 
 void
 kasan_bus_space_barrier(bus_space_tag_t tag, bus_space_handle_t hnd,
     bus_size_t offset, bus_size_t size, int flags)
 {
 	bus_space_barrier(tag, hnd, offset, size, flags);
 }
 
 #define ASAN_BUS_READ_FUNC(func, width, type)				\
 	type kasan_bus_space_read##func##_##width(bus_space_tag_t tag,	\
 	    bus_space_handle_t hnd, bus_size_t offset)			\
 	{								\
 		return (bus_space_read##func##_##width(tag, hnd,	\
 		    offset));						\
 	}								\
 
 #define ASAN_BUS_READ_PTR_FUNC(func, width, type)			\
 	void kasan_bus_space_read_##func##_##width(bus_space_tag_t tag,	\
 	    bus_space_handle_t hnd, bus_size_t size, type *buf,		\
 	    bus_size_t count)						\
 	{								\
 		kasan_shadow_check((uintptr_t)buf, sizeof(type) * count,\
 		    false, __RET_ADDR);					\
 		bus_space_read_##func##_##width(tag, hnd, size, buf, 	\
 		    count);						\
 	}
 
 ASAN_BUS_READ_FUNC(, 1, uint8_t)
 ASAN_BUS_READ_FUNC(_stream, 1, uint8_t)
 ASAN_BUS_READ_PTR_FUNC(multi, 1, uint8_t)
 ASAN_BUS_READ_PTR_FUNC(multi_stream, 1, uint8_t)
 ASAN_BUS_READ_PTR_FUNC(region, 1, uint8_t)
 ASAN_BUS_READ_PTR_FUNC(region_stream, 1, uint8_t)
 
 ASAN_BUS_READ_FUNC(, 2, uint16_t)
 ASAN_BUS_READ_FUNC(_stream, 2, uint16_t)
 ASAN_BUS_READ_PTR_FUNC(multi, 2, uint16_t)
 ASAN_BUS_READ_PTR_FUNC(multi_stream, 2, uint16_t)
 ASAN_BUS_READ_PTR_FUNC(region, 2, uint16_t)
 ASAN_BUS_READ_PTR_FUNC(region_stream, 2, uint16_t)
 
 ASAN_BUS_READ_FUNC(, 4, uint32_t)
 ASAN_BUS_READ_FUNC(_stream, 4, uint32_t)
 ASAN_BUS_READ_PTR_FUNC(multi, 4, uint32_t)
 ASAN_BUS_READ_PTR_FUNC(multi_stream, 4, uint32_t)
 ASAN_BUS_READ_PTR_FUNC(region, 4, uint32_t)
 ASAN_BUS_READ_PTR_FUNC(region_stream, 4, uint32_t)
 
 ASAN_BUS_READ_FUNC(, 8, uint64_t)
 
 #define	ASAN_BUS_WRITE_FUNC(func, width, type)				\
 	void kasan_bus_space_write##func##_##width(bus_space_tag_t tag,	\
 	    bus_space_handle_t hnd, bus_size_t offset, type value)	\
 	{								\
 		bus_space_write##func##_##width(tag, hnd, offset, value);\
 	}								\
 
 #define	ASAN_BUS_WRITE_PTR_FUNC(func, width, type)			\
 	void kasan_bus_space_write_##func##_##width(bus_space_tag_t tag,\
 	    bus_space_handle_t hnd, bus_size_t size, const type *buf,	\
 	    bus_size_t count)						\
 	{								\
 		kasan_shadow_check((uintptr_t)buf, sizeof(type) * count,\
 		    true, __RET_ADDR);					\
 		bus_space_write_##func##_##width(tag, hnd, size, buf, 	\
 		    count);						\
 	}
 
 ASAN_BUS_WRITE_FUNC(, 1, uint8_t)
 ASAN_BUS_WRITE_FUNC(_stream, 1, uint8_t)
 ASAN_BUS_WRITE_PTR_FUNC(multi, 1, uint8_t)
 ASAN_BUS_WRITE_PTR_FUNC(multi_stream, 1, uint8_t)
 ASAN_BUS_WRITE_PTR_FUNC(region, 1, uint8_t)
 ASAN_BUS_WRITE_PTR_FUNC(region_stream, 1, uint8_t)
 
 ASAN_BUS_WRITE_FUNC(, 2, uint16_t)
 ASAN_BUS_WRITE_FUNC(_stream, 2, uint16_t)
 ASAN_BUS_WRITE_PTR_FUNC(multi, 2, uint16_t)
 ASAN_BUS_WRITE_PTR_FUNC(multi_stream, 2, uint16_t)
 ASAN_BUS_WRITE_PTR_FUNC(region, 2, uint16_t)
 ASAN_BUS_WRITE_PTR_FUNC(region_stream, 2, uint16_t)
 
 ASAN_BUS_WRITE_FUNC(, 4, uint32_t)
 ASAN_BUS_WRITE_FUNC(_stream, 4, uint32_t)
 ASAN_BUS_WRITE_PTR_FUNC(multi, 4, uint32_t)
 ASAN_BUS_WRITE_PTR_FUNC(multi_stream, 4, uint32_t)
 ASAN_BUS_WRITE_PTR_FUNC(region, 4, uint32_t)
 ASAN_BUS_WRITE_PTR_FUNC(region_stream, 4, uint32_t)
 
 ASAN_BUS_WRITE_FUNC(, 8, uint64_t)
 
 #define	ASAN_BUS_SET_FUNC(func, width, type)				\
 	void kasan_bus_space_set_##func##_##width(bus_space_tag_t tag,	\
 	    bus_space_handle_t hnd, bus_size_t offset, type value,	\
 	    bus_size_t count)						\
 	{								\
 		bus_space_set_##func##_##width(tag, hnd, offset, value,	\
 		    count);						\
 	}
 
 ASAN_BUS_SET_FUNC(multi, 1, uint8_t)
 ASAN_BUS_SET_FUNC(region, 1, uint8_t)
 ASAN_BUS_SET_FUNC(multi_stream, 1, uint8_t)
 ASAN_BUS_SET_FUNC(region_stream, 1, uint8_t)
 
 ASAN_BUS_SET_FUNC(multi, 2, uint16_t)
 ASAN_BUS_SET_FUNC(region, 2, uint16_t)
 ASAN_BUS_SET_FUNC(multi_stream, 2, uint16_t)
 ASAN_BUS_SET_FUNC(region_stream, 2, uint16_t)
 
 ASAN_BUS_SET_FUNC(multi, 4, uint32_t)
 ASAN_BUS_SET_FUNC(region, 4, uint32_t)
 ASAN_BUS_SET_FUNC(multi_stream, 4, uint32_t)
 ASAN_BUS_SET_FUNC(region_stream, 4, uint32_t)
 
 /* -------------------------------------------------------------------------- */
 
 void __asan_register_globals(struct __asan_global *, size_t);
 void __asan_unregister_globals(struct __asan_global *, size_t);
 
 void
 __asan_register_globals(struct __asan_global *globals, size_t n)
 {
 	size_t i;
 
 	for (i = 0; i < n; i++) {
 		kasan_mark(globals[i].beg, globals[i].size,
 		    globals[i].size_with_redzone, KASAN_GENERIC_REDZONE);
 	}
 }
 
 void
 __asan_unregister_globals(struct __asan_global *globals, size_t n)
 {
 	size_t i;
 
 	for (i = 0; i < n; i++) {
 		kasan_mark(globals[i].beg, globals[i].size_with_redzone,
 		    globals[i].size_with_redzone, 0);
 	}
 }
 
 #define ASAN_LOAD_STORE(size)					\
 	void __asan_load##size(unsigned long);			\
 	void __asan_load##size(unsigned long addr)		\
 	{							\
 		kasan_shadow_check(addr, size, false, __RET_ADDR);\
 	} 							\
 	void __asan_load##size##_noabort(unsigned long);	\
 	void __asan_load##size##_noabort(unsigned long addr)	\
 	{							\
 		kasan_shadow_check(addr, size, false, __RET_ADDR);\
 	}							\
 	void __asan_store##size(unsigned long);			\
 	void __asan_store##size(unsigned long addr)		\
 	{							\
 		kasan_shadow_check(addr, size, true, __RET_ADDR);\
 	}							\
 	void __asan_store##size##_noabort(unsigned long);	\
 	void __asan_store##size##_noabort(unsigned long addr)	\
 	{							\
 		kasan_shadow_check(addr, size, true, __RET_ADDR);\
 	}
 
 ASAN_LOAD_STORE(1);
 ASAN_LOAD_STORE(2);
 ASAN_LOAD_STORE(4);
 ASAN_LOAD_STORE(8);
 ASAN_LOAD_STORE(16);
 
 void __asan_loadN(unsigned long, size_t);
 void __asan_loadN_noabort(unsigned long, size_t);
 void __asan_storeN(unsigned long, size_t);
 void __asan_storeN_noabort(unsigned long, size_t);
 void __asan_handle_no_return(void);
 
 void
 __asan_loadN(unsigned long addr, size_t size)
 {
 	kasan_shadow_check(addr, size, false, __RET_ADDR);
 }
 
 void
 __asan_loadN_noabort(unsigned long addr, size_t size)
 {
 	kasan_shadow_check(addr, size, false, __RET_ADDR);
 }
 
 void
 __asan_storeN(unsigned long addr, size_t size)
 {
 	kasan_shadow_check(addr, size, true, __RET_ADDR);
 }
 
 void
 __asan_storeN_noabort(unsigned long addr, size_t size)
 {
 	kasan_shadow_check(addr, size, true, __RET_ADDR);
 }
 
 void
 __asan_handle_no_return(void)
 {
 	/* nothing */
 }
 
 #define ASAN_SET_SHADOW(byte) \
 	void __asan_set_shadow_##byte(void *, size_t);			\
 	void __asan_set_shadow_##byte(void *addr, size_t size)		\
 	{								\
 		__builtin_memset((void *)addr, 0x##byte, size);		\
 	}
 
 ASAN_SET_SHADOW(00);
 ASAN_SET_SHADOW(f1);
 ASAN_SET_SHADOW(f2);
 ASAN_SET_SHADOW(f3);
 ASAN_SET_SHADOW(f5);
 ASAN_SET_SHADOW(f8);
 
 void __asan_poison_stack_memory(const void *, size_t);
 void __asan_unpoison_stack_memory(const void *, size_t);
 
 void
 __asan_poison_stack_memory(const void *addr, size_t size)
 {
 	size = roundup(size, KASAN_SHADOW_SCALE);
 	kasan_shadow_Nbyte_fill(addr, size, KASAN_USE_AFTER_SCOPE);
 }
 
 void
 __asan_unpoison_stack_memory(const void *addr, size_t size)
 {
 	kasan_shadow_Nbyte_markvalid(addr, size);
 }
 
 void __asan_alloca_poison(const void *, size_t);
 void __asan_allocas_unpoison(const void *, const void *);
 
 void
 __asan_alloca_poison(const void *addr, size_t size)
 {
 	const void *l, *r;
 
 	KASSERT((vm_offset_t)addr % KASAN_ALLOCA_SCALE_SIZE == 0,
 	    ("%s: invalid address %p", __func__, addr));
 
 	l = (const uint8_t *)addr - KASAN_ALLOCA_SCALE_SIZE;
 	r = (const uint8_t *)addr + roundup(size, KASAN_ALLOCA_SCALE_SIZE);
 
 	kasan_shadow_Nbyte_fill(l, KASAN_ALLOCA_SCALE_SIZE, KASAN_STACK_LEFT);
 	kasan_mark(addr, size, roundup(size, KASAN_ALLOCA_SCALE_SIZE),
 	    KASAN_STACK_MID);
 	kasan_shadow_Nbyte_fill(r, KASAN_ALLOCA_SCALE_SIZE, KASAN_STACK_RIGHT);
 }
 
 void
 __asan_allocas_unpoison(const void *stkbegin, const void *stkend)
 {
 	size_t size;
 
 	if (__predict_false(!stkbegin))
 		return;
 	if (__predict_false((uintptr_t)stkbegin > (uintptr_t)stkend))
 		return;
 	size = (uintptr_t)stkend - (uintptr_t)stkbegin;
 
 	kasan_shadow_Nbyte_fill(stkbegin, size, 0);
 }
 
 void __asan_poison_memory_region(const void *addr, size_t size);
 void __asan_unpoison_memory_region(const void *addr, size_t size);
 
 void
 __asan_poison_memory_region(const void *addr, size_t size)
 {
 }
 
 void
 __asan_unpoison_memory_region(const void *addr, size_t size)
 {
 }
diff --git a/sys/kern/subr_msan.c b/sys/kern/subr_msan.c
index 10ccc842012a..816f38fc74cb 100644
--- a/sys/kern/subr_msan.c
+++ b/sys/kern/subr_msan.c
@@ -1,1619 +1,1619 @@
 /*	$NetBSD: subr_msan.c,v 1.14 2020/09/09 16:29:59 maxv Exp $	*/
 
 /*
  * Copyright (c) 2019-2020 Maxime Villard, m00nbsd.net
  * All rights reserved.
  * Copyright (c) 2021 The FreeBSD Foundation
  *
  * Portions of this software were developed by Mark Johnston under sponsorship
  * from the FreeBSD Foundation.
  *
  * This code is part of the KMSAN subsystem of the NetBSD kernel.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  * 1. Redistributions of source code must retain the above copyright
  *    notice, this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
  *
  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
 
 #define	SAN_RUNTIME
 
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 #if 0
 __KERNEL_RCSID(0, "$NetBSD: subr_msan.c,v 1.14 2020/09/09 16:29:59 maxv Exp $");
 #endif
 
 #include <sys/param.h>
 #include <sys/systm.h>
 #include <sys/bio.h>
 #include <sys/buf.h>
 #include <sys/conf.h>
 #include <sys/kdb.h>
 #include <sys/kernel.h>
 #include <sys/linker.h>
 #include <sys/malloc.h>
 #include <sys/mbuf.h>
 #include <sys/memdesc.h>
 #include <sys/msan.h>
 #include <sys/proc.h>
 #include <sys/stack.h>
 #include <sys/sysctl.h>
 #include <sys/uio.h>
 
 #include <cam/cam.h>
 #include <cam/cam_ccb.h>
 
 #include <vm/vm.h>
 #include <vm/pmap.h>
 
 #include <machine/msan.h>
 #include <machine/stdarg.h>
 
 void kmsan_init_arg(size_t);
 void kmsan_init_ret(size_t);
 
 /* -------------------------------------------------------------------------- */
 
 /*
  * Part of the compiler ABI.
  */
 
 typedef struct {
 	uint8_t *shad;
 	msan_orig_t *orig;
 } msan_meta_t;
 
 #define MSAN_PARAM_SIZE		800
 #define MSAN_RETVAL_SIZE	800
 typedef struct {
 	uint8_t param_shadow[MSAN_PARAM_SIZE];
 	uint8_t retval_shadow[MSAN_RETVAL_SIZE];
 	uint8_t va_arg_shadow[MSAN_PARAM_SIZE];
 	uint8_t va_arg_origin[MSAN_PARAM_SIZE];
 	uint64_t va_arg_overflow_size;
 	msan_orig_t param_origin[MSAN_PARAM_SIZE / sizeof(msan_orig_t)];
 	msan_orig_t retval_origin;
 } msan_tls_t;
 
 /* -------------------------------------------------------------------------- */
 
 #define MSAN_NCONTEXT	4
 #define MSAN_ORIG_MASK	(~0x3)
 
 typedef struct kmsan_td {
 	size_t ctx;
 	msan_tls_t tls[MSAN_NCONTEXT];
 } msan_td_t;
 
 static msan_tls_t dummy_tls;
 
 /*
  * Use separate dummy regions for loads and stores: stores may mark the region
  * as uninitialized, and that can trigger false positives.
  */
 static uint8_t msan_dummy_shad[PAGE_SIZE] __aligned(PAGE_SIZE);
 static uint8_t msan_dummy_write_shad[PAGE_SIZE] __aligned(PAGE_SIZE);
 static uint8_t msan_dummy_orig[PAGE_SIZE] __aligned(PAGE_SIZE);
 static msan_td_t msan_thread0;
 static bool kmsan_enabled __read_mostly;
 
 static bool kmsan_reporting = false;
 
 /*
  * Avoid clobbering any thread-local state before we panic.
  */
 #define	kmsan_panic(f, ...) do {			\
 	kmsan_enabled = false;				\
 	panic(f, __VA_ARGS__);				\
 } while (0)
 
 #define	REPORT(f, ...) do {				\
 	if (panic_on_violation) {			\
 		kmsan_panic(f, __VA_ARGS__);		\
 	} else {					\
 		struct stack st;			\
 							\
 		stack_save(&st);			\
 		printf(f "\n", __VA_ARGS__);		\
 		stack_print_ddb(&st);			\
 	}						\
 } while (0)
 
 FEATURE(kmsan, "Kernel memory sanitizer");
 
 static SYSCTL_NODE(_debug, OID_AUTO, kmsan, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
     "KMSAN options");
 
 static bool panic_on_violation = 1;
 SYSCTL_BOOL(_debug_kmsan, OID_AUTO, panic_on_violation, CTLFLAG_RWTUN,
     &panic_on_violation, 0,
     "Panic if an invalid access is detected");
 
 static MALLOC_DEFINE(M_KMSAN, "kmsan", "Kernel memory sanitizer");
 
 /* -------------------------------------------------------------------------- */
 
 static inline const char *
 kmsan_orig_name(int type)
 {
 	switch (type) {
 	case KMSAN_TYPE_STACK:
 		return ("stack");
 	case KMSAN_TYPE_KMEM:
 		return ("kmem");
 	case KMSAN_TYPE_MALLOC:
 		return ("malloc");
 	case KMSAN_TYPE_UMA:
 		return ("UMA");
 	default:
 		return ("unknown");
 	}
 }
 
 static void
 kmsan_report_hook(const void *addr, size_t size, size_t off, const char *hook)
 {
 	msan_orig_t *orig;
 	const char *typename;
 	char *var, *fn;
 	uintptr_t ptr;
 	long foff;
 	char buf[128];
 	int type;
 
-	if (__predict_false(panicstr != NULL || kdb_active || kmsan_reporting))
+	if (__predict_false(KERNEL_PANICKED() || kdb_active || kmsan_reporting))
 		return;
 
 	kmsan_reporting = true;
 	__compiler_membar();
 
 	orig = (msan_orig_t *)kmsan_md_addr_to_orig((vm_offset_t)addr);
 	orig = (msan_orig_t *)((uintptr_t)orig & MSAN_ORIG_MASK);
 
 	if (*orig == 0) {
 		REPORT("MSan: Uninitialized memory in %s, offset %zu",
 		    hook, off);
 		goto out;
 	}
 
 	kmsan_md_orig_decode(*orig, &type, &ptr);
 	typename = kmsan_orig_name(type);
 
 	if (linker_ddb_search_symbol_name((caddr_t)ptr, buf,
 	    sizeof(buf), &foff) == 0) {
 		REPORT("MSan: Uninitialized %s memory in %s, "
 		    "offset %zu/%zu, addr %p, from %s+%#lx",
 		    typename, hook, off, size, addr, buf, foff);
 	} else if (__builtin_memcmp((void *)ptr, "----", 4) == 0) {
 		/*
 		 * The format of the string is: "----var@function". Parse it to
 		 * display a nice warning.
 		 */
 		var = (char *)ptr + 4;
 		strlcpy(buf, var, sizeof(buf));
 		var = buf;
 		fn = strchr(buf, '@');
 		*fn++ = '\0';
 		REPORT("MSan: Uninitialized %s memory in %s, offset %zu, "
 		    "variable '%s' from %s", typename, hook, off, var, fn);
 	} else {
 		REPORT("MSan: Uninitialized %s memory in %s, "
 		    "offset %zu/%zu, addr %p, PC %p",
 		    typename, hook, off, size, addr, (void *)ptr);
 	}
 
 out:
 	__compiler_membar();
 	kmsan_reporting = false;
 }
 
 static void
 kmsan_report_inline(msan_orig_t orig, unsigned long pc)
 {
 	const char *typename;
 	char *var, *fn;
 	uintptr_t ptr;
 	char buf[128];
 	long foff;
 	int type;
 
-	if (__predict_false(panicstr != NULL || kdb_active || kmsan_reporting))
+	if (__predict_false(KERNEL_PANICKED() || kdb_active || kmsan_reporting))
 		return;
 
 	kmsan_reporting = true;
 	__compiler_membar();
 
 	if (orig == 0) {
 		REPORT("MSan: uninitialized variable in %p", (void *)pc);
 		goto out;
 	}
 
 	kmsan_md_orig_decode(orig, &type, &ptr);
 	typename = kmsan_orig_name(type);
 
 	if (linker_ddb_search_symbol_name((caddr_t)ptr, buf,
 	    sizeof(buf), &foff) == 0) {
 		REPORT("MSan: Uninitialized %s memory from %s+%#lx",
 		    typename, buf, foff);
 	} else if (__builtin_memcmp((void *)ptr, "----", 4) == 0) {
 		/*
 		 * The format of the string is: "----var@function". Parse it to
 		 * display a nice warning.
 		 */
 		var = (char *)ptr + 4;
 		strlcpy(buf, var, sizeof(buf));
 		var = buf;
 		fn = strchr(buf, '@');
 		*fn++ = '\0';
 		REPORT("MSan: Uninitialized variable '%s' from %s", var, fn);
 	} else {
 		REPORT("MSan: Uninitialized %s memory, origin %x",
 		    typename, orig);
 	}
 
 out:
 	__compiler_membar();
 	kmsan_reporting = false;
 }
 
 /* -------------------------------------------------------------------------- */
 
 static inline msan_meta_t
 kmsan_meta_get(const void *addr, size_t size, const bool write)
 {
 	msan_meta_t ret;
 
 	if (__predict_false(!kmsan_enabled)) {
 		ret.shad = write ? msan_dummy_write_shad : msan_dummy_shad;
 		ret.orig = (msan_orig_t *)msan_dummy_orig;
 	} else if (__predict_false(kmsan_md_unsupported((vm_offset_t)addr))) {
 		ret.shad = write ? msan_dummy_write_shad : msan_dummy_shad;
 		ret.orig = (msan_orig_t *)msan_dummy_orig;
 	} else {
 		ret.shad = (void *)kmsan_md_addr_to_shad((vm_offset_t)addr);
 		ret.orig =
 		    (msan_orig_t *)kmsan_md_addr_to_orig((vm_offset_t)addr);
 		ret.orig = (msan_orig_t *)((uintptr_t)ret.orig &
 		    MSAN_ORIG_MASK);
 	}
 
 	return (ret);
 }
 
 static inline void
 kmsan_origin_fill(const void *addr, msan_orig_t o, size_t size)
 {
 	msan_orig_t *orig;
 	size_t i;
 
 	if (__predict_false(!kmsan_enabled))
 		return;
 	if (__predict_false(kmsan_md_unsupported((vm_offset_t)addr)))
 		return;
 
 	orig = (msan_orig_t *)kmsan_md_addr_to_orig((vm_offset_t)addr);
 	size += ((uintptr_t)orig & (sizeof(*orig) - 1));
 	orig = (msan_orig_t *)((uintptr_t)orig & MSAN_ORIG_MASK);
 
 	for (i = 0; i < size; i += 4) {
 		orig[i / 4] = o;
 	}
 }
 
 static inline void
 kmsan_shadow_fill(uintptr_t addr, uint8_t c, size_t size)
 {
 	uint8_t *shad;
 
 	if (__predict_false(!kmsan_enabled))
 		return;
 	if (__predict_false(kmsan_md_unsupported(addr)))
 		return;
 
 	shad = (uint8_t *)kmsan_md_addr_to_shad(addr);
 	__builtin_memset(shad, c, size);
 }
 
 static inline void
 kmsan_meta_copy(void *dst, const void *src, size_t size)
 {
 	uint8_t *orig_src, *orig_dst;
 	uint8_t *shad_src, *shad_dst;
 	msan_orig_t *_src, *_dst;
 	size_t i;
 
 	if (__predict_false(!kmsan_enabled))
 		return;
 	if (__predict_false(kmsan_md_unsupported((vm_offset_t)dst)))
 		return;
 	if (__predict_false(kmsan_md_unsupported((vm_offset_t)src))) {
 		kmsan_shadow_fill((uintptr_t)dst, KMSAN_STATE_INITED, size);
 		return;
 	}
 
 	shad_src = (uint8_t *)kmsan_md_addr_to_shad((vm_offset_t)src);
 	shad_dst = (uint8_t *)kmsan_md_addr_to_shad((vm_offset_t)dst);
 	__builtin_memmove(shad_dst, shad_src, size);
 
 	orig_src = (uint8_t *)kmsan_md_addr_to_orig((vm_offset_t)src);
 	orig_dst = (uint8_t *)kmsan_md_addr_to_orig((vm_offset_t)dst);
 	for (i = 0; i < size; i++) {
 		_src = (msan_orig_t *)((uintptr_t)orig_src & MSAN_ORIG_MASK);
 		_dst = (msan_orig_t *)((uintptr_t)orig_dst & MSAN_ORIG_MASK);
 		*_dst = *_src;
 		orig_src++;
 		orig_dst++;
 	}
 }
 
 static inline void
 kmsan_shadow_check(uintptr_t addr, size_t size, const char *hook)
 {
 	uint8_t *shad;
 	size_t i;
 
 	if (__predict_false(!kmsan_enabled))
 		return;
 	if (__predict_false(kmsan_md_unsupported(addr)))
 		return;
 
 	shad = (uint8_t *)kmsan_md_addr_to_shad(addr);
 	for (i = 0; i < size; i++) {
 		if (__predict_true(shad[i] == 0))
 			continue;
 		kmsan_report_hook((const char *)addr + i, size, i, hook);
 		break;
 	}
 }
 
 void
 kmsan_init_arg(size_t n)
 {
 	msan_td_t *mtd;
 	uint8_t *arg;
 
 	if (__predict_false(!kmsan_enabled))
 		return;
 	if (__predict_false(curthread == NULL))
 		return;
 	mtd = curthread->td_kmsan;
 	arg = mtd->tls[mtd->ctx].param_shadow;
 	__builtin_memset(arg, 0, n);
 }
 
 void
 kmsan_init_ret(size_t n)
 {
 	msan_td_t *mtd;
 	uint8_t *arg;
 
 	if (__predict_false(!kmsan_enabled))
 		return;
 	if (__predict_false(curthread == NULL))
 		return;
 	mtd = curthread->td_kmsan;
 	arg = mtd->tls[mtd->ctx].retval_shadow;
 	__builtin_memset(arg, 0, n);
 }
 
 static void
 kmsan_check_arg(size_t size, const char *hook)
 {
 	msan_td_t *mtd;
 	uint8_t *arg;
 	size_t i;
 
 	if (__predict_false(!kmsan_enabled))
 		return;
 	if (__predict_false(curthread == NULL))
 		return;
 	mtd = curthread->td_kmsan;
 	arg = mtd->tls[mtd->ctx].param_shadow;
 
 	for (i = 0; i < size; i++) {
 		if (__predict_true(arg[i] == 0))
 			continue;
 		kmsan_report_hook((const char *)arg + i, size, i, hook);
 		break;
 	}
 }
 
 void
 kmsan_thread_alloc(struct thread *td)
 {
 	msan_td_t *mtd;
 
 	if (!kmsan_enabled)
 		return;
 
 	mtd = td->td_kmsan;
 	if (mtd == NULL) {
 		/* We might be recycling a thread. */
 		kmsan_init_arg(sizeof(size_t) + sizeof(struct malloc_type *) +
 		    sizeof(int));
 		mtd = malloc(sizeof(*mtd), M_KMSAN, M_WAITOK);
 	}
 	kmsan_memset(mtd, 0, sizeof(*mtd));
 	mtd->ctx = 0;
 
 	if (td->td_kstack != 0)
 		kmsan_mark((void *)td->td_kstack, ptoa(td->td_kstack_pages),
 		    KMSAN_STATE_UNINIT);
 
 	td->td_kmsan = mtd;
 }
 
 void
 kmsan_thread_free(struct thread *td)
 {
 	msan_td_t *mtd;
 
 	if (!kmsan_enabled)
 		return;
 	if (__predict_false(td == curthread))
 		kmsan_panic("%s: freeing KMSAN TLS for curthread", __func__);
 
 	mtd = td->td_kmsan;
 	kmsan_init_arg(sizeof(void *) + sizeof(struct malloc_type *));
 	free(mtd, M_KMSAN);
 	td->td_kmsan = NULL;
 }
 
 void kmsan_intr_enter(void);
 void kmsan_intr_leave(void);
 
 void
 kmsan_intr_enter(void)
 {
 	msan_td_t *mtd;
 
 	if (__predict_false(!kmsan_enabled))
 		return;
 
 	mtd = curthread->td_kmsan;
 	mtd->ctx++;
 	if (__predict_false(mtd->ctx >= MSAN_NCONTEXT))
 		kmsan_panic("%s: mtd->ctx = %zu", __func__, mtd->ctx);
 }
 
 void
 kmsan_intr_leave(void)
 {
 	msan_td_t *mtd;
 
 	if (__predict_false(!kmsan_enabled))
 		return;
 
 	mtd = curthread->td_kmsan;
 	if (__predict_false(mtd->ctx == 0))
 		kmsan_panic("%s: mtd->ctx = %zu", __func__, mtd->ctx);
 	mtd->ctx--;
 }
 
 /* -------------------------------------------------------------------------- */
 
 void
 kmsan_shadow_map(vm_offset_t addr, size_t size)
 {
 	size_t npages, i;
 	vm_offset_t va;
 
 	MPASS(addr % PAGE_SIZE == 0);
 	MPASS(size % PAGE_SIZE == 0);
 
 	if (!kmsan_enabled)
 		return;
 
 	npages = atop(size);
 
 	va = kmsan_md_addr_to_shad(addr);
 	for (i = 0; i < npages; i++) {
 		pmap_san_enter(va + ptoa(i));
 	}
 
 	va = kmsan_md_addr_to_orig(addr);
 	for (i = 0; i < npages; i++) {
 		pmap_san_enter(va + ptoa(i));
 	}
 }
 
 void
 kmsan_orig(const void *addr, size_t size, int type, uintptr_t pc)
 {
 	msan_orig_t orig;
 
 	orig = kmsan_md_orig_encode(type, pc);
 	kmsan_origin_fill(addr, orig, size);
 }
 
 void
 kmsan_mark(const void *addr, size_t size, uint8_t c)
 {
 	kmsan_shadow_fill((uintptr_t)addr, c, size);
 }
 
 void
 kmsan_mark_bio(const struct bio *bp, uint8_t c)
 {
 	kmsan_mark(bp->bio_data, bp->bio_length, c);
 }
 
 static void
 kmsan_mark_ccb(const union ccb *ccb, uint8_t c)
 {
 	if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_IN)
 		return;
 	if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
 		return;
 
 	switch (ccb->ccb_h.func_code) {
 	case XPT_SCSI_IO: {
 		const struct ccb_scsiio *scsiio;
 
 		scsiio = &ccb->ctio;
 		kmsan_mark(scsiio->data_ptr, scsiio->dxfer_len, c);
 		break;
 	}
 	case XPT_ATA_IO: {
 		const struct ccb_ataio *ataio;
 
 		ataio = &ccb->ataio;
 		kmsan_mark(ataio->data_ptr, ataio->dxfer_len, c);
 		break;
 	}
 	case XPT_NVME_IO: {
 		const struct ccb_nvmeio *nvmeio;
 
 		nvmeio = &ccb->nvmeio;
 		kmsan_mark(nvmeio->data_ptr, nvmeio->dxfer_len, c);
 		break;
 	}
 	default:
 		kmsan_panic("%s: unhandled CCB type %d", __func__,
 		    ccb->ccb_h.func_code);
 	}
 }
 
 void
 kmsan_mark_mbuf(const struct mbuf *m, uint8_t c)
 {
 	do {
 		if ((m->m_flags & M_EXTPG) == 0)
 			kmsan_mark(m->m_data, m->m_len, c);
 		m = m->m_next;
 	} while (m != NULL);
 }
 
 void
 kmsan_check(const void *p, size_t sz, const char *descr)
 {
 	kmsan_shadow_check((uintptr_t)p, sz, descr);
 }
 
 void
 kmsan_check_bio(const struct bio *bp, const char *descr)
 {
 	kmsan_shadow_check((uintptr_t)bp->bio_data, bp->bio_length, descr);
 }
 
 void
 kmsan_check_ccb(const union ccb *ccb, const char *descr)
 {
 	if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_OUT)
 		return;
 	switch (ccb->ccb_h.func_code) {
 	case XPT_SCSI_IO: {
 		const struct ccb_scsiio *scsiio;
 
 		scsiio = &ccb->ctio;
 		kmsan_check(scsiio->data_ptr, scsiio->dxfer_len, descr);
 		break;
 	}
 	case XPT_ATA_IO: {
 		const struct ccb_ataio *ataio;
 
 		ataio = &ccb->ataio;
 		kmsan_check(ataio->data_ptr, ataio->dxfer_len, descr);
 		break;
 	}
 	case XPT_NVME_IO: {
 		const struct ccb_nvmeio *nvmeio;
 
 		nvmeio = &ccb->nvmeio;
 		kmsan_check(nvmeio->data_ptr, nvmeio->dxfer_len, descr);
 		break;
 	}
 	default:
 		kmsan_panic("%s: unhandled CCB type %d", __func__,
 		    ccb->ccb_h.func_code);
 	}
 }
 
 void
 kmsan_check_mbuf(const struct mbuf *m, const char *descr)
 {
 	do {
 		kmsan_shadow_check((uintptr_t)mtod(m, void *), m->m_len, descr);
 	} while ((m = m->m_next) != NULL);
 }
 
 void
 kmsan_init(void)
 {
 	int disabled;
 
 	disabled = 0;
 	TUNABLE_INT_FETCH("debug.kmsan.disabled", &disabled);
 	if (disabled)
 		return;
 
 	/* Initialize the TLS for curthread. */
 	msan_thread0.ctx = 0;
 	thread0.td_kmsan = &msan_thread0;
 
 	/* Now officially enabled. */
 	kmsan_enabled = true;
 }
 
 /* -------------------------------------------------------------------------- */
 
 msan_meta_t __msan_metadata_ptr_for_load_n(void *, size_t);
 msan_meta_t __msan_metadata_ptr_for_store_n(void *, size_t);
 
 msan_meta_t
 __msan_metadata_ptr_for_load_n(void *addr, size_t size)
 {
 	return (kmsan_meta_get(addr, size, false));
 }
 
 msan_meta_t
 __msan_metadata_ptr_for_store_n(void *addr, size_t size)
 {
 	return (kmsan_meta_get(addr, size, true));
 }
 
 #define MSAN_META_FUNC(size)						\
 	msan_meta_t __msan_metadata_ptr_for_load_##size(void *);	\
 	msan_meta_t __msan_metadata_ptr_for_load_##size(void *addr)	\
 	{								\
 		return (kmsan_meta_get(addr, size, false));		\
 	}								\
 	msan_meta_t __msan_metadata_ptr_for_store_##size(void *);	\
 	msan_meta_t __msan_metadata_ptr_for_store_##size(void *addr)	\
 	{								\
 		return (kmsan_meta_get(addr, size, true));		\
 	}
 
 MSAN_META_FUNC(1)
 MSAN_META_FUNC(2)
 MSAN_META_FUNC(4)
 MSAN_META_FUNC(8)
 
 void __msan_instrument_asm_store(const void *, size_t);
 msan_orig_t __msan_chain_origin(msan_orig_t);
 void __msan_poison(const void *, size_t);
 void __msan_unpoison(const void *, size_t);
 void __msan_poison_alloca(const void *, uint64_t, const char *);
 void __msan_unpoison_alloca(const void *, uint64_t);
 void __msan_warning(msan_orig_t);
 msan_tls_t *__msan_get_context_state(void);
 
 void
 __msan_instrument_asm_store(const void *addr, size_t size)
 {
 	kmsan_shadow_fill((uintptr_t)addr, KMSAN_STATE_INITED, size);
 }
 
 msan_orig_t
 __msan_chain_origin(msan_orig_t origin)
 {
 	return (origin);
 }
 
 void
 __msan_poison(const void *addr, size_t size)
 {
 	kmsan_shadow_fill((uintptr_t)addr, KMSAN_STATE_UNINIT, size);
 }
 
 void
 __msan_unpoison(const void *addr, size_t size)
 {
 	kmsan_shadow_fill((uintptr_t)addr, KMSAN_STATE_INITED, size);
 }
 
 void
 __msan_poison_alloca(const void *addr, uint64_t size, const char *descr)
 {
 	msan_orig_t orig;
 
 	orig = kmsan_md_orig_encode(KMSAN_TYPE_STACK, (uintptr_t)descr);
 	kmsan_origin_fill(addr, orig, size);
 	kmsan_shadow_fill((uintptr_t)addr, KMSAN_STATE_UNINIT, size);
 }
 
 void
 __msan_unpoison_alloca(const void *addr, uint64_t size)
 {
 	kmsan_shadow_fill((uintptr_t)addr, KMSAN_STATE_INITED, size);
 }
 
 void
 __msan_warning(msan_orig_t origin)
 {
 	if (__predict_false(!kmsan_enabled))
 		return;
 	kmsan_report_inline(origin, KMSAN_RET_ADDR);
 }
 
 msan_tls_t *
 __msan_get_context_state(void)
 {
 	msan_td_t *mtd;
 
 	/*
 	 * When APs are started, they execute some C code before curthread is
 	 * set.  We have to handle that here.
 	 */
 	if (__predict_false(!kmsan_enabled || curthread == NULL))
 		return (&dummy_tls);
 	mtd = curthread->td_kmsan;
 	return (&mtd->tls[mtd->ctx]);
 }
 
 /* -------------------------------------------------------------------------- */
 
 /*
  * Function hooks. Mostly ASM functions which need KMSAN wrappers to handle
  * initialized areas properly.
  */
 
 void *
 kmsan_memcpy(void *dst, const void *src, size_t len)
 {
 	/* No kmsan_check_arg, because inlined. */
 	kmsan_init_ret(sizeof(void *));
 	if (__predict_true(len != 0)) {
 		kmsan_meta_copy(dst, src, len);
 	}
 	return (__builtin_memcpy(dst, src, len));
 }
 
 int
 kmsan_memcmp(const void *b1, const void *b2, size_t len)
 {
 	const uint8_t *_b1 = b1, *_b2 = b2;
 	size_t i;
 
 	kmsan_check_arg(sizeof(b1) + sizeof(b2) + sizeof(len),
 	    "memcmp():args");
 	kmsan_init_ret(sizeof(int));
 
 	for (i = 0; i < len; i++) {
 		if (*_b1 != *_b2) {
 			kmsan_shadow_check((uintptr_t)b1, i + 1,
 			    "memcmp():arg1");
 			kmsan_shadow_check((uintptr_t)b2, i + 1,
 			    "memcmp():arg2");
 			return (*_b1 - *_b2);
 		}
 		_b1++, _b2++;
 	}
 
 	return (0);
 }
 
 void *
 kmsan_memset(void *dst, int c, size_t len)
 {
 	/* No kmsan_check_arg, because inlined. */
 	kmsan_shadow_fill((uintptr_t)dst, KMSAN_STATE_INITED, len);
 	kmsan_init_ret(sizeof(void *));
 	return (__builtin_memset(dst, c, len));
 }
 
 void *
 kmsan_memmove(void *dst, const void *src, size_t len)
 {
 	/* No kmsan_check_arg, because inlined. */
 	if (__predict_true(len != 0)) {
 		kmsan_meta_copy(dst, src, len);
 	}
 	kmsan_init_ret(sizeof(void *));
 	return (__builtin_memmove(dst, src, len));
 }
 
 __strong_reference(kmsan_memcpy, __msan_memcpy);
 __strong_reference(kmsan_memset, __msan_memset);
 __strong_reference(kmsan_memmove, __msan_memmove);
 
 char *
 kmsan_strcpy(char *dst, const char *src)
 {
 	const char *_src = src;
 	char *_dst = dst;
 	size_t len = 0;
 
 	kmsan_check_arg(sizeof(dst) + sizeof(src), "strcpy():args");
 
 	while (1) {
 		len++;
 		*dst = *src;
 		if (*src == '\0')
 			break;
 		src++, dst++;
 	}
 
 	kmsan_shadow_check((uintptr_t)_src, len, "strcpy():arg2");
 	kmsan_shadow_fill((uintptr_t)_dst, KMSAN_STATE_INITED, len);
 	kmsan_init_ret(sizeof(char *));
 	return (_dst);
 }
 
 int
 kmsan_strcmp(const char *s1, const char *s2)
 {
 	const char *_s1 = s1, *_s2 = s2;
 	size_t len = 0;
 
 	kmsan_check_arg(sizeof(s1) + sizeof(s2), "strcmp():args");
 	kmsan_init_ret(sizeof(int));
 
 	while (1) {
 		len++;
 		if (*s1 != *s2)
 			break;
 		if (*s1 == '\0') {
 			kmsan_shadow_check((uintptr_t)_s1, len, "strcmp():arg1");
 			kmsan_shadow_check((uintptr_t)_s2, len, "strcmp():arg2");
 			return (0);
 		}
 		s1++, s2++;
 	}
 
 	kmsan_shadow_check((uintptr_t)_s1, len, "strcmp():arg1");
 	kmsan_shadow_check((uintptr_t)_s2, len, "strcmp():arg2");
 
 	return (*(const unsigned char *)s1 - *(const unsigned char *)s2);
 }
 
 size_t
 kmsan_strlen(const char *str)
 {
 	const char *s;
 
 	kmsan_check_arg(sizeof(str), "strlen():args");
 
 	s = str;
 	while (1) {
 		if (*s == '\0')
 			break;
 		s++;
 	}
 
 	kmsan_shadow_check((uintptr_t)str, (size_t)(s - str) + 1, "strlen():arg1");
 	kmsan_init_ret(sizeof(size_t));
 	return (s - str);
 }
 
 int	kmsan_copyin(const void *, void *, size_t);
 int	kmsan_copyout(const void *, void *, size_t);
 int	kmsan_copyinstr(const void *, void *, size_t, size_t *);
 
 int
 kmsan_copyin(const void *uaddr, void *kaddr, size_t len)
 {
 	int ret;
 
 	kmsan_check_arg(sizeof(uaddr) + sizeof(kaddr) + sizeof(len),
 	    "copyin():args");
 	ret = copyin(uaddr, kaddr, len);
 	if (ret == 0)
 		kmsan_shadow_fill((uintptr_t)kaddr, KMSAN_STATE_INITED, len);
 	kmsan_init_ret(sizeof(int));
 	return (ret);
 }
 
 int
 kmsan_copyout(const void *kaddr, void *uaddr, size_t len)
 {
 	kmsan_check_arg(sizeof(kaddr) + sizeof(uaddr) + sizeof(len),
 	    "copyout():args");
 	kmsan_shadow_check((uintptr_t)kaddr, len, "copyout():arg1");
 	kmsan_init_ret(sizeof(int));
 	return (copyout(kaddr, uaddr, len));
 }
 
 int
 kmsan_copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done)
 {
 	size_t _done;
 	int ret;
 
 	kmsan_check_arg(sizeof(uaddr) + sizeof(kaddr) +
 	    sizeof(len) + sizeof(done), "copyinstr():args");
 	ret = copyinstr(uaddr, kaddr, len, &_done);
 	if (ret == 0)
 		kmsan_shadow_fill((uintptr_t)kaddr, KMSAN_STATE_INITED, _done);
 	if (done != NULL) {
 		*done = _done;
 		kmsan_shadow_fill((uintptr_t)done, KMSAN_STATE_INITED, sizeof(size_t));
 	}
 	kmsan_init_ret(sizeof(int));
 	return (ret);
 }
 
 /* -------------------------------------------------------------------------- */
 
 int
 kmsan_fubyte(volatile const void *base)
 {
 	int ret;
 
 	kmsan_check_arg(sizeof(base), "fubyte(): args");
 	ret = fubyte(base);
 	kmsan_init_ret(sizeof(int));
 	return (ret);
 }
 
 int
 kmsan_fuword16(volatile const void *base)
 {
 	int ret;
 
 	kmsan_check_arg(sizeof(base), "fuword16(): args");
 	ret = fuword16(base);
 	kmsan_init_ret(sizeof(int));
 	return (ret);
 }
 
 int
 kmsan_fueword(volatile const void *base, long *val)
 {
 	int ret;
 
 	kmsan_check_arg(sizeof(base) + sizeof(val), "fueword(): args");
 	ret = fueword(base, val);
 	if (ret == 0)
 		kmsan_shadow_fill((uintptr_t)val, KMSAN_STATE_INITED,
 		    sizeof(*val));
 	kmsan_init_ret(sizeof(int));
 	return (ret);
 }
 
 int
 kmsan_fueword32(volatile const void *base, int32_t *val)
 {
 	int ret;
 
 	kmsan_check_arg(sizeof(base) + sizeof(val), "fueword32(): args");
 	ret = fueword32(base, val);
 	if (ret == 0)
 		kmsan_shadow_fill((uintptr_t)val, KMSAN_STATE_INITED,
 		    sizeof(*val));
 	kmsan_init_ret(sizeof(int));
 	return (ret);
 }
 
 int
 kmsan_fueword64(volatile const void *base, int64_t *val)
 {
 	int ret;
 
 	kmsan_check_arg(sizeof(base) + sizeof(val), "fueword64(): args");
 	ret = fueword64(base, val);
 	if (ret == 0)
 		kmsan_shadow_fill((uintptr_t)val, KMSAN_STATE_INITED,
 		    sizeof(*val));
 	kmsan_init_ret(sizeof(int));
 	return (ret);
 }
 
 int
 kmsan_subyte(volatile void *base, int byte)
 {
 	int ret;
 
 	kmsan_check_arg(sizeof(base) + sizeof(byte), "subyte():args");
 	ret = subyte(base, byte);
 	kmsan_init_ret(sizeof(int));
 	return (ret);
 }
 
 int
 kmsan_suword(volatile void *base, long word)
 {
 	int ret;
 
 	kmsan_check_arg(sizeof(base) + sizeof(word), "suword():args");
 	ret = suword(base, word);
 	kmsan_init_ret(sizeof(int));
 	return (ret);
 }
 
 int
 kmsan_suword16(volatile void *base, int word)
 {
 	int ret;
 
 	kmsan_check_arg(sizeof(base) + sizeof(word), "suword16():args");
 	ret = suword16(base, word);
 	kmsan_init_ret(sizeof(int));
 	return (ret);
 }
 
 int
 kmsan_suword32(volatile void *base, int32_t word)
 {
 	int ret;
 
 	kmsan_check_arg(sizeof(base) + sizeof(word), "suword32():args");
 	ret = suword32(base, word);
 	kmsan_init_ret(sizeof(int));
 	return (ret);
 }
 
 int
 kmsan_suword64(volatile void *base, int64_t word)
 {
 	int ret;
 
 	kmsan_check_arg(sizeof(base) + sizeof(word), "suword64():args");
 	ret = suword64(base, word);
 	kmsan_init_ret(sizeof(int));
 	return (ret);
 }
 
 int
 kmsan_casueword32(volatile uint32_t *base, uint32_t oldval, uint32_t *oldvalp,
     uint32_t newval)
 {
 	int ret;
 
 	kmsan_check_arg(sizeof(base) + sizeof(oldval) + sizeof(oldvalp) +
 	    sizeof(newval), "casueword32(): args");
 	ret = casueword32(base, oldval, oldvalp, newval);
 	kmsan_shadow_fill((uintptr_t)oldvalp, KMSAN_STATE_INITED,
 	    sizeof(*oldvalp));
 	kmsan_init_ret(sizeof(int));
 	return (ret);
 }
 
 int
 kmsan_casueword(volatile u_long *base, u_long oldval, u_long *oldvalp,
     u_long newval)
 {
 	int ret;
 
 	kmsan_check_arg(sizeof(base) + sizeof(oldval) + sizeof(oldvalp) +
 	    sizeof(newval), "casueword32(): args");
 	ret = casueword(base, oldval, oldvalp, newval);
 	kmsan_shadow_fill((uintptr_t)oldvalp, KMSAN_STATE_INITED,
 	    sizeof(*oldvalp));
 	kmsan_init_ret(sizeof(int));
 	return (ret);
 }
 
 /* -------------------------------------------------------------------------- */
 
 #include <machine/atomic.h>
 #include <sys/atomic_san.h>
 
 #define _MSAN_ATOMIC_FUNC_ADD(name, type)				\
 	void kmsan_atomic_add_##name(volatile type *ptr, type val)	\
 	{								\
 		kmsan_check_arg(sizeof(ptr) + sizeof(val),		\
 		    "atomic_add_" #name "():args");			\
 		kmsan_shadow_check((uintptr_t)ptr, sizeof(type),	\
 		    "atomic_add_" #name "():ptr");			\
 		atomic_add_##name(ptr, val);				\
 	}
 
 #define	MSAN_ATOMIC_FUNC_ADD(name, type)				\
 	_MSAN_ATOMIC_FUNC_ADD(name, type)				\
 	_MSAN_ATOMIC_FUNC_ADD(acq_##name, type)				\
 	_MSAN_ATOMIC_FUNC_ADD(rel_##name, type)
 
 #define _MSAN_ATOMIC_FUNC_SUBTRACT(name, type)				\
 	void kmsan_atomic_subtract_##name(volatile type *ptr, type val)	\
 	{								\
 		kmsan_check_arg(sizeof(ptr) + sizeof(val),		\
 		    "atomic_subtract_" #name "():args");		\
 		kmsan_shadow_check((uintptr_t)ptr, sizeof(type),	\
 		    "atomic_subtract_" #name "():ptr");			\
 		atomic_subtract_##name(ptr, val);			\
 	}
 
 #define	MSAN_ATOMIC_FUNC_SUBTRACT(name, type)				\
 	_MSAN_ATOMIC_FUNC_SUBTRACT(name, type)				\
 	_MSAN_ATOMIC_FUNC_SUBTRACT(acq_##name, type)			\
 	_MSAN_ATOMIC_FUNC_SUBTRACT(rel_##name, type)
 
 #define _MSAN_ATOMIC_FUNC_SET(name, type)				\
 	void kmsan_atomic_set_##name(volatile type *ptr, type val)	\
 	{								\
 		kmsan_check_arg(sizeof(ptr) + sizeof(val),		\
 		    "atomic_set_" #name "():args");			\
 		kmsan_shadow_check((uintptr_t)ptr, sizeof(type),	\
 		    "atomic_set_" #name "():ptr");			\
 		atomic_set_##name(ptr, val);				\
 	}
 
 #define	MSAN_ATOMIC_FUNC_SET(name, type)				\
 	_MSAN_ATOMIC_FUNC_SET(name, type)				\
 	_MSAN_ATOMIC_FUNC_SET(acq_##name, type)				\
 	_MSAN_ATOMIC_FUNC_SET(rel_##name, type)
 
 #define _MSAN_ATOMIC_FUNC_CLEAR(name, type)				\
 	void kmsan_atomic_clear_##name(volatile type *ptr, type val)	\
 	{								\
 		kmsan_check_arg(sizeof(ptr) + sizeof(val),		\
 		    "atomic_clear_" #name "():args");			\
 		kmsan_shadow_check((uintptr_t)ptr, sizeof(type),	\
 		    "atomic_clear_" #name "():ptr");			\
 		atomic_clear_##name(ptr, val);				\
 	}
 
 #define	MSAN_ATOMIC_FUNC_CLEAR(name, type)				\
 	_MSAN_ATOMIC_FUNC_CLEAR(name, type)				\
 	_MSAN_ATOMIC_FUNC_CLEAR(acq_##name, type)			\
 	_MSAN_ATOMIC_FUNC_CLEAR(rel_##name, type)
 
 #define	MSAN_ATOMIC_FUNC_FETCHADD(name, type)				\
 	type kmsan_atomic_fetchadd_##name(volatile type *ptr, type val)	\
 	{								\
 		kmsan_check_arg(sizeof(ptr) + sizeof(val),		\
 		    "atomic_fetchadd_" #name "():args");		\
 		kmsan_shadow_check((uintptr_t)ptr, sizeof(type),	\
 		    "atomic_fetchadd_" #name "():ptr");			\
 		kmsan_init_ret(sizeof(type));				\
 		return (atomic_fetchadd_##name(ptr, val));		\
 	}
 
 #define	MSAN_ATOMIC_FUNC_READANDCLEAR(name, type)			\
 	type kmsan_atomic_readandclear_##name(volatile type *ptr)	\
 	{								\
 		kmsan_check_arg(sizeof(ptr),				\
 		    "atomic_readandclear_" #name "():args");		\
 		kmsan_shadow_check((uintptr_t)ptr, sizeof(type),	\
 		    "atomic_readandclear_" #name "():ptr");		\
 		kmsan_init_ret(sizeof(type));				\
 		return (atomic_readandclear_##name(ptr));		\
 	}
 
 #define	MSAN_ATOMIC_FUNC_TESTANDCLEAR(name, type)			\
 	int kmsan_atomic_testandclear_##name(volatile type *ptr, u_int v) \
 	{								\
 		kmsan_check_arg(sizeof(ptr) + sizeof(v),		\
 		    "atomic_testandclear_" #name "():args");		\
 		kmsan_shadow_check((uintptr_t)ptr, sizeof(type),	\
 		    "atomic_testandclear_" #name "():ptr");		\
 		kmsan_init_ret(sizeof(int));				\
 		return (atomic_testandclear_##name(ptr, v));		\
 	}
 
 #define	MSAN_ATOMIC_FUNC_TESTANDSET(name, type)				\
 	int kmsan_atomic_testandset_##name(volatile type *ptr, u_int v) \
 	{								\
 		kmsan_check_arg(sizeof(ptr) + sizeof(v),		\
 		    "atomic_testandset_" #name "():args");		\
 		kmsan_shadow_check((uintptr_t)ptr, sizeof(type),	\
 		    "atomic_testandset_" #name "():ptr");		\
 		kmsan_init_ret(sizeof(int));				\
 		return (atomic_testandset_##name(ptr, v));		\
 	}
 
 #define	MSAN_ATOMIC_FUNC_SWAP(name, type)				\
 	type kmsan_atomic_swap_##name(volatile type *ptr, type val)	\
 	{								\
 		kmsan_check_arg(sizeof(ptr) + sizeof(val),		\
 		    "atomic_swap_" #name "():args");			\
 		kmsan_shadow_check((uintptr_t)ptr, sizeof(type),	\
 		    "atomic_swap_" #name "():ptr");			\
 		kmsan_init_ret(sizeof(type));				\
 		return (atomic_swap_##name(ptr, val));			\
 	}
 
 #define _MSAN_ATOMIC_FUNC_CMPSET(name, type)				\
 	int kmsan_atomic_cmpset_##name(volatile type *ptr, type oval,	\
 	    type nval)							\
 	{								\
 		kmsan_check_arg(sizeof(ptr) + sizeof(oval) +		\
 		    sizeof(nval), "atomic_cmpset_" #name "():args");	\
 		kmsan_shadow_check((uintptr_t)ptr, sizeof(type),	\
 		    "atomic_cmpset_" #name "():ptr");			\
 		kmsan_init_ret(sizeof(int));				\
 		return (atomic_cmpset_##name(ptr, oval, nval));		\
 	}
 
 #define	MSAN_ATOMIC_FUNC_CMPSET(name, type)				\
 	_MSAN_ATOMIC_FUNC_CMPSET(name, type)				\
 	_MSAN_ATOMIC_FUNC_CMPSET(acq_##name, type)			\
 	_MSAN_ATOMIC_FUNC_CMPSET(rel_##name, type)
 
 #define _MSAN_ATOMIC_FUNC_FCMPSET(name, type)				\
 	int kmsan_atomic_fcmpset_##name(volatile type *ptr, type *oval,	\
 	    type nval)							\
 	{								\
 		kmsan_check_arg(sizeof(ptr) + sizeof(oval) +		\
 		    sizeof(nval), "atomic_fcmpset_" #name "():args");	\
 		kmsan_shadow_check((uintptr_t)ptr, sizeof(type),	\
 		    "atomic_fcmpset_" #name "():ptr");			\
 		kmsan_init_ret(sizeof(int));				\
 		return (atomic_fcmpset_##name(ptr, oval, nval));	\
 	}
 
 #define	MSAN_ATOMIC_FUNC_FCMPSET(name, type)				\
 	_MSAN_ATOMIC_FUNC_FCMPSET(name, type)				\
 	_MSAN_ATOMIC_FUNC_FCMPSET(acq_##name, type)			\
 	_MSAN_ATOMIC_FUNC_FCMPSET(rel_##name, type)
 
 #define MSAN_ATOMIC_FUNC_THREAD_FENCE(name)				\
 	void kmsan_atomic_thread_fence_##name(void)			\
 	{								\
 		atomic_thread_fence_##name();				\
 	}
 
 #define	_MSAN_ATOMIC_FUNC_LOAD(name, type)				\
 	type kmsan_atomic_load_##name(volatile type *ptr)		\
 	{								\
 		kmsan_check_arg(sizeof(ptr),				\
 		    "atomic_load_" #name "():args");			\
 		kmsan_shadow_check((uintptr_t)ptr, sizeof(type),	\
 		    "atomic_load_" #name "():ptr");			\
 		kmsan_init_ret(sizeof(type));				\
 		return (atomic_load_##name(ptr));			\
 	}
 
 #define	MSAN_ATOMIC_FUNC_LOAD(name, type)				\
 	_MSAN_ATOMIC_FUNC_LOAD(name, type)				\
 	_MSAN_ATOMIC_FUNC_LOAD(acq_##name, type)
 
 #define	_MSAN_ATOMIC_FUNC_STORE(name, type)				\
 	void kmsan_atomic_store_##name(volatile type *ptr, type val)	\
 	{								\
 		kmsan_check_arg(sizeof(ptr) + sizeof(val),		\
 		    "atomic_store_" #name "():args");			\
 		kmsan_shadow_fill((uintptr_t)ptr, KMSAN_STATE_INITED,	\
 		    sizeof(type));					\
 		atomic_store_##name(ptr, val);				\
 	}
 
 #define	MSAN_ATOMIC_FUNC_STORE(name, type)				\
 	_MSAN_ATOMIC_FUNC_STORE(name, type)				\
 	_MSAN_ATOMIC_FUNC_STORE(rel_##name, type)
 
 MSAN_ATOMIC_FUNC_ADD(8, uint8_t);
 MSAN_ATOMIC_FUNC_ADD(16, uint16_t);
 MSAN_ATOMIC_FUNC_ADD(32, uint32_t);
 MSAN_ATOMIC_FUNC_ADD(64, uint64_t);
 MSAN_ATOMIC_FUNC_ADD(int, u_int);
 MSAN_ATOMIC_FUNC_ADD(long, u_long);
 MSAN_ATOMIC_FUNC_ADD(ptr, uintptr_t);
 
 MSAN_ATOMIC_FUNC_SUBTRACT(8, uint8_t);
 MSAN_ATOMIC_FUNC_SUBTRACT(16, uint16_t);
 MSAN_ATOMIC_FUNC_SUBTRACT(32, uint32_t);
 MSAN_ATOMIC_FUNC_SUBTRACT(64, uint64_t);
 MSAN_ATOMIC_FUNC_SUBTRACT(int, u_int);
 MSAN_ATOMIC_FUNC_SUBTRACT(long, u_long);
 MSAN_ATOMIC_FUNC_SUBTRACT(ptr, uintptr_t);
 
 MSAN_ATOMIC_FUNC_SET(8, uint8_t);
 MSAN_ATOMIC_FUNC_SET(16, uint16_t);
 MSAN_ATOMIC_FUNC_SET(32, uint32_t);
 MSAN_ATOMIC_FUNC_SET(64, uint64_t);
 MSAN_ATOMIC_FUNC_SET(int, u_int);
 MSAN_ATOMIC_FUNC_SET(long, u_long);
 MSAN_ATOMIC_FUNC_SET(ptr, uintptr_t);
 
 MSAN_ATOMIC_FUNC_CLEAR(8, uint8_t);
 MSAN_ATOMIC_FUNC_CLEAR(16, uint16_t);
 MSAN_ATOMIC_FUNC_CLEAR(32, uint32_t);
 MSAN_ATOMIC_FUNC_CLEAR(64, uint64_t);
 MSAN_ATOMIC_FUNC_CLEAR(int, u_int);
 MSAN_ATOMIC_FUNC_CLEAR(long, u_long);
 MSAN_ATOMIC_FUNC_CLEAR(ptr, uintptr_t);
 
 MSAN_ATOMIC_FUNC_FETCHADD(32, uint32_t);
 MSAN_ATOMIC_FUNC_FETCHADD(64, uint64_t);
 MSAN_ATOMIC_FUNC_FETCHADD(int, u_int);
 MSAN_ATOMIC_FUNC_FETCHADD(long, u_long);
 
 MSAN_ATOMIC_FUNC_READANDCLEAR(32, uint32_t);
 MSAN_ATOMIC_FUNC_READANDCLEAR(64, uint64_t);
 MSAN_ATOMIC_FUNC_READANDCLEAR(int, u_int);
 MSAN_ATOMIC_FUNC_READANDCLEAR(long, u_long);
 MSAN_ATOMIC_FUNC_READANDCLEAR(ptr, uintptr_t);
 
 MSAN_ATOMIC_FUNC_TESTANDCLEAR(32, uint32_t);
 MSAN_ATOMIC_FUNC_TESTANDCLEAR(64, uint64_t);
 MSAN_ATOMIC_FUNC_TESTANDCLEAR(int, u_int);
 MSAN_ATOMIC_FUNC_TESTANDCLEAR(long, u_long);
 
 MSAN_ATOMIC_FUNC_TESTANDSET(32, uint32_t);
 MSAN_ATOMIC_FUNC_TESTANDSET(64, uint64_t);
 MSAN_ATOMIC_FUNC_TESTANDSET(int, u_int);
 MSAN_ATOMIC_FUNC_TESTANDSET(long, u_long);
 
 MSAN_ATOMIC_FUNC_SWAP(32, uint32_t);
 MSAN_ATOMIC_FUNC_SWAP(64, uint64_t);
 MSAN_ATOMIC_FUNC_SWAP(int, u_int);
 MSAN_ATOMIC_FUNC_SWAP(long, u_long);
 MSAN_ATOMIC_FUNC_SWAP(ptr, uintptr_t);
 
 MSAN_ATOMIC_FUNC_CMPSET(8, uint8_t);
 MSAN_ATOMIC_FUNC_CMPSET(16, uint16_t);
 MSAN_ATOMIC_FUNC_CMPSET(32, uint32_t);
 MSAN_ATOMIC_FUNC_CMPSET(64, uint64_t);
 MSAN_ATOMIC_FUNC_CMPSET(int, u_int);
 MSAN_ATOMIC_FUNC_CMPSET(long, u_long);
 MSAN_ATOMIC_FUNC_CMPSET(ptr, uintptr_t);
 
 MSAN_ATOMIC_FUNC_FCMPSET(8, uint8_t);
 MSAN_ATOMIC_FUNC_FCMPSET(16, uint16_t);
 MSAN_ATOMIC_FUNC_FCMPSET(32, uint32_t);
 MSAN_ATOMIC_FUNC_FCMPSET(64, uint64_t);
 MSAN_ATOMIC_FUNC_FCMPSET(int, u_int);
 MSAN_ATOMIC_FUNC_FCMPSET(long, u_long);
 MSAN_ATOMIC_FUNC_FCMPSET(ptr, uintptr_t);
 
 MSAN_ATOMIC_FUNC_LOAD(8, uint8_t);
 MSAN_ATOMIC_FUNC_LOAD(16, uint16_t);
 MSAN_ATOMIC_FUNC_LOAD(32, uint32_t);
 MSAN_ATOMIC_FUNC_LOAD(64, uint64_t);
 MSAN_ATOMIC_FUNC_LOAD(char, u_char);
 MSAN_ATOMIC_FUNC_LOAD(short, u_short);
 MSAN_ATOMIC_FUNC_LOAD(int, u_int);
 MSAN_ATOMIC_FUNC_LOAD(long, u_long);
 MSAN_ATOMIC_FUNC_LOAD(ptr, uintptr_t);
 
 MSAN_ATOMIC_FUNC_STORE(8, uint8_t);
 MSAN_ATOMIC_FUNC_STORE(16, uint16_t);
 MSAN_ATOMIC_FUNC_STORE(32, uint32_t);
 MSAN_ATOMIC_FUNC_STORE(64, uint64_t);
 MSAN_ATOMIC_FUNC_STORE(char, u_char);
 MSAN_ATOMIC_FUNC_STORE(short, u_short);
 MSAN_ATOMIC_FUNC_STORE(int, u_int);
 MSAN_ATOMIC_FUNC_STORE(long, u_long);
 MSAN_ATOMIC_FUNC_STORE(ptr, uintptr_t);
 
 MSAN_ATOMIC_FUNC_THREAD_FENCE(acq);
 MSAN_ATOMIC_FUNC_THREAD_FENCE(rel);
 MSAN_ATOMIC_FUNC_THREAD_FENCE(acq_rel);
 MSAN_ATOMIC_FUNC_THREAD_FENCE(seq_cst);
 
 void
 kmsan_atomic_interrupt_fence(void)
 {
 	atomic_interrupt_fence();
 }
 
 /* -------------------------------------------------------------------------- */
 
 #include <sys/bus.h>
 #include <machine/bus.h>
 #include <sys/bus_san.h>
 
 int
 kmsan_bus_space_map(bus_space_tag_t tag, bus_addr_t hnd, bus_size_t size,
     int flags, bus_space_handle_t *handlep)
 {
 	return (bus_space_map(tag, hnd, size, flags, handlep));
 }
 
 void
 kmsan_bus_space_unmap(bus_space_tag_t tag, bus_space_handle_t hnd,
     bus_size_t size)
 {
 	bus_space_unmap(tag, hnd, size);
 }
 
 int
 kmsan_bus_space_subregion(bus_space_tag_t tag, bus_space_handle_t hnd,
     bus_size_t offset, bus_size_t size, bus_space_handle_t *handlep)
 {
 	return (bus_space_subregion(tag, hnd, offset, size, handlep));
 }
 
 void
 kmsan_bus_space_free(bus_space_tag_t tag, bus_space_handle_t hnd,
     bus_size_t size)
 {
 	bus_space_free(tag, hnd, size);
 }
 
 void
 kmsan_bus_space_barrier(bus_space_tag_t tag, bus_space_handle_t hnd,
     bus_size_t offset, bus_size_t size, int flags)
 {
 	bus_space_barrier(tag, hnd, offset, size, flags);
 }
 
 /* XXXMJ x86-specific */
 #define MSAN_BUS_READ_FUNC(func, width, type)				\
 	type kmsan_bus_space_read##func##_##width(bus_space_tag_t tag,	\
 	    bus_space_handle_t hnd, bus_size_t offset)			\
 	{								\
 		type ret;						\
 		if ((tag) != X86_BUS_SPACE_IO)				\
 			kmsan_shadow_fill((uintptr_t)(hnd + offset),	\
 			    KMSAN_STATE_INITED, (width));		\
 		ret = bus_space_read##func##_##width(tag, hnd, offset);	\
 		kmsan_init_ret(sizeof(type));				\
 		return (ret);						\
 	}								\
 
 #define MSAN_BUS_READ_PTR_FUNC(func, width, type)			\
 	void kmsan_bus_space_read_##func##_##width(bus_space_tag_t tag,	\
 	    bus_space_handle_t hnd, bus_size_t size, type *buf,		\
 	    bus_size_t count)						\
 	{								\
 		kmsan_shadow_fill((uintptr_t)buf, KMSAN_STATE_INITED,	\
 		    (width) * count);					\
 		bus_space_read_##func##_##width(tag, hnd, size, buf, 	\
 		    count);						\
 	}
 
 MSAN_BUS_READ_FUNC(, 1, uint8_t)
 MSAN_BUS_READ_FUNC(_stream, 1, uint8_t)
 MSAN_BUS_READ_PTR_FUNC(multi, 1, uint8_t)
 MSAN_BUS_READ_PTR_FUNC(multi_stream, 1, uint8_t)
 MSAN_BUS_READ_PTR_FUNC(region, 1, uint8_t)
 MSAN_BUS_READ_PTR_FUNC(region_stream, 1, uint8_t)
 
 MSAN_BUS_READ_FUNC(, 2, uint16_t)
 MSAN_BUS_READ_FUNC(_stream, 2, uint16_t)
 MSAN_BUS_READ_PTR_FUNC(multi, 2, uint16_t)
 MSAN_BUS_READ_PTR_FUNC(multi_stream, 2, uint16_t)
 MSAN_BUS_READ_PTR_FUNC(region, 2, uint16_t)
 MSAN_BUS_READ_PTR_FUNC(region_stream, 2, uint16_t)
 
 MSAN_BUS_READ_FUNC(, 4, uint32_t)
 MSAN_BUS_READ_FUNC(_stream, 4, uint32_t)
 MSAN_BUS_READ_PTR_FUNC(multi, 4, uint32_t)
 MSAN_BUS_READ_PTR_FUNC(multi_stream, 4, uint32_t)
 MSAN_BUS_READ_PTR_FUNC(region, 4, uint32_t)
 MSAN_BUS_READ_PTR_FUNC(region_stream, 4, uint32_t)
 
 MSAN_BUS_READ_FUNC(, 8, uint64_t)
 
 #define	MSAN_BUS_WRITE_FUNC(func, width, type)				\
 	void kmsan_bus_space_write##func##_##width(bus_space_tag_t tag,	\
 	    bus_space_handle_t hnd, bus_size_t offset, type value)	\
 	{								\
 		bus_space_write##func##_##width(tag, hnd, offset, value);\
 	}								\
 
 #define	MSAN_BUS_WRITE_PTR_FUNC(func, width, type)			\
 	void kmsan_bus_space_write_##func##_##width(bus_space_tag_t tag,\
 	    bus_space_handle_t hnd, bus_size_t size, const type *buf,	\
 	    bus_size_t count)						\
 	{								\
 		kmsan_shadow_check((uintptr_t)buf, sizeof(type) * count,\
 		    "bus_space_write()");				\
 		bus_space_write_##func##_##width(tag, hnd, size, buf, 	\
 		    count);						\
 	}
 
 MSAN_BUS_WRITE_FUNC(, 1, uint8_t)
 MSAN_BUS_WRITE_FUNC(_stream, 1, uint8_t)
 MSAN_BUS_WRITE_PTR_FUNC(multi, 1, uint8_t)
 MSAN_BUS_WRITE_PTR_FUNC(multi_stream, 1, uint8_t)
 MSAN_BUS_WRITE_PTR_FUNC(region, 1, uint8_t)
 MSAN_BUS_WRITE_PTR_FUNC(region_stream, 1, uint8_t)
 
 MSAN_BUS_WRITE_FUNC(, 2, uint16_t)
 MSAN_BUS_WRITE_FUNC(_stream, 2, uint16_t)
 MSAN_BUS_WRITE_PTR_FUNC(multi, 2, uint16_t)
 MSAN_BUS_WRITE_PTR_FUNC(multi_stream, 2, uint16_t)
 MSAN_BUS_WRITE_PTR_FUNC(region, 2, uint16_t)
 MSAN_BUS_WRITE_PTR_FUNC(region_stream, 2, uint16_t)
 
 MSAN_BUS_WRITE_FUNC(, 4, uint32_t)
 MSAN_BUS_WRITE_FUNC(_stream, 4, uint32_t)
 MSAN_BUS_WRITE_PTR_FUNC(multi, 4, uint32_t)
 MSAN_BUS_WRITE_PTR_FUNC(multi_stream, 4, uint32_t)
 MSAN_BUS_WRITE_PTR_FUNC(region, 4, uint32_t)
 MSAN_BUS_WRITE_PTR_FUNC(region_stream, 4, uint32_t)
 
 MSAN_BUS_WRITE_FUNC(, 8, uint64_t)
 
 #define	MSAN_BUS_SET_FUNC(func, width, type)				\
 	void kmsan_bus_space_set_##func##_##width(bus_space_tag_t tag,	\
 	    bus_space_handle_t hnd, bus_size_t offset, type value,	\
 	    bus_size_t count)						\
 	{								\
 		bus_space_set_##func##_##width(tag, hnd, offset, value,	\
 		    count);						\
 	}
 
 MSAN_BUS_SET_FUNC(multi, 1, uint8_t)
 MSAN_BUS_SET_FUNC(region, 1, uint8_t)
 MSAN_BUS_SET_FUNC(multi_stream, 1, uint8_t)
 MSAN_BUS_SET_FUNC(region_stream, 1, uint8_t)
 
 MSAN_BUS_SET_FUNC(multi, 2, uint16_t)
 MSAN_BUS_SET_FUNC(region, 2, uint16_t)
 MSAN_BUS_SET_FUNC(multi_stream, 2, uint16_t)
 MSAN_BUS_SET_FUNC(region_stream, 2, uint16_t)
 
 MSAN_BUS_SET_FUNC(multi, 4, uint32_t)
 MSAN_BUS_SET_FUNC(region, 4, uint32_t)
 MSAN_BUS_SET_FUNC(multi_stream, 4, uint32_t)
 MSAN_BUS_SET_FUNC(region_stream, 4, uint32_t)
 
 /* -------------------------------------------------------------------------- */
 
 void
 kmsan_bus_dmamap_sync(struct memdesc *desc, bus_dmasync_op_t op)
 {
 	/*
 	 * Some drivers, e.g., nvme, use the same code path for loading device
 	 * read and write requests, and will thus specify both flags.  In this
 	 * case we should not do any checking since it will generally lead to
 	 * false positives.
 	 */
 	if ((op & (BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE)) ==
 	    BUS_DMASYNC_PREWRITE) {
 		switch (desc->md_type) {
 		case MEMDESC_VADDR:
 			kmsan_check(desc->u.md_vaddr, desc->md_opaque,
 			    "dmasync");
 			break;
 		case MEMDESC_BIO:
 			kmsan_check_bio(desc->u.md_bio, "dmasync");
 			break;
 		case MEMDESC_MBUF:
 			kmsan_check_mbuf(desc->u.md_mbuf, "dmasync");
 			break;
 		case MEMDESC_CCB:
 			kmsan_check_ccb(desc->u.md_ccb, "dmasync");
 			break;
 		case 0:
 			break;
 		default:
 			kmsan_panic("%s: unhandled memdesc type %d", __func__,
 			    desc->md_type);
 		}
 	}
 	if ((op & BUS_DMASYNC_POSTREAD) != 0) {
 		switch (desc->md_type) {
 		case MEMDESC_VADDR:
 			kmsan_mark(desc->u.md_vaddr, desc->md_opaque,
 			    KMSAN_STATE_INITED);
 			break;
 		case MEMDESC_BIO:
 			kmsan_mark_bio(desc->u.md_bio, KMSAN_STATE_INITED);
 			break;
 		case MEMDESC_MBUF:
 			kmsan_mark_mbuf(desc->u.md_mbuf, KMSAN_STATE_INITED);
 			break;
 		case MEMDESC_CCB:
 			kmsan_mark_ccb(desc->u.md_ccb, KMSAN_STATE_INITED);
 			break;
 		case 0:
 			break;
 		default:
 			kmsan_panic("%s: unhandled memdesc type %d", __func__,
 			    desc->md_type);
 		}
 	}
 }