17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ad4023c4Sdp  * Common Development and Distribution License (the "License").
6ad4023c4Sdp  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2199fd1a49Sahl 
227c478bd9Sstevel@tonic-gate /*
2328406508Ssudheer  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27f484800dSBryan Cantrill /*
2856a20711SPatrick Mooney  * Copyright 2017 Joyent, Inc.
29e5803b76SAdam H. Leventhal  * Copyright (c) 2012 by Delphix. All rights reserved.
30f484800dSBryan Cantrill  */
31f484800dSBryan Cantrill 
327c478bd9Sstevel@tonic-gate #ifndef _SYS_DTRACE_IMPL_H
337c478bd9Sstevel@tonic-gate #define	_SYS_DTRACE_IMPL_H
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
367c478bd9Sstevel@tonic-gate extern "C" {
377c478bd9Sstevel@tonic-gate #endif
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  * DTrace Dynamic Tracing Software: Kernel Implementation Interfaces
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  * Note: The contents of this file are private to the implementation of the
437c478bd9Sstevel@tonic-gate  * Solaris system and DTrace subsystem and are subject to change at any time
447c478bd9Sstevel@tonic-gate  * without notice.  Applications and drivers using these interfaces will fail
457c478bd9Sstevel@tonic-gate  * to run on future releases.  These interfaces should not be used for any
46*bbf21555SRichard Lowe  * purpose except those expressly outlined in dtrace(4D) and libdtrace(3LIB).
477c478bd9Sstevel@tonic-gate  * Please refer to the "Solaris Dynamic Tracing Guide" for more information.
487c478bd9Sstevel@tonic-gate  */
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #include <sys/dtrace.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate  * DTrace Implementation Constants and Typedefs
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@tonic-gate #define	DTRACE_MAXPROPLEN		128
567c478bd9Sstevel@tonic-gate #define	DTRACE_DYNVAR_CHUNKSIZE		256
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate struct dtrace_probe;
597c478bd9Sstevel@tonic-gate struct dtrace_ecb;
607c478bd9Sstevel@tonic-gate struct dtrace_predicate;
617c478bd9Sstevel@tonic-gate struct dtrace_action;
627c478bd9Sstevel@tonic-gate struct dtrace_provider;
637c478bd9Sstevel@tonic-gate struct dtrace_state;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate typedef struct dtrace_probe dtrace_probe_t;
667c478bd9Sstevel@tonic-gate typedef struct dtrace_ecb dtrace_ecb_t;
677c478bd9Sstevel@tonic-gate typedef struct dtrace_predicate dtrace_predicate_t;
687c478bd9Sstevel@tonic-gate typedef struct dtrace_action dtrace_action_t;
697c478bd9Sstevel@tonic-gate typedef struct dtrace_provider dtrace_provider_t;
707c478bd9Sstevel@tonic-gate typedef struct dtrace_meta dtrace_meta_t;
717c478bd9Sstevel@tonic-gate typedef struct dtrace_state dtrace_state_t;
727c478bd9Sstevel@tonic-gate typedef uint32_t dtrace_optid_t;
737c478bd9Sstevel@tonic-gate typedef uint32_t dtrace_specid_t;
747c478bd9Sstevel@tonic-gate typedef uint64_t dtrace_genid_t;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate  * DTrace Probes
787c478bd9Sstevel@tonic-gate  *
797c478bd9Sstevel@tonic-gate  * The probe is the fundamental unit of the DTrace architecture.  Probes are
807c478bd9Sstevel@tonic-gate  * created by DTrace providers, and managed by the DTrace framework.  A probe
817c478bd9Sstevel@tonic-gate  * is identified by a unique <provider, module, function, name> tuple, and has
827c478bd9Sstevel@tonic-gate  * a unique probe identifier assigned to it.  (Some probes are not associated
837c478bd9Sstevel@tonic-gate  * with a specific point in text; these are called _unanchored probes_ and have
847c478bd9Sstevel@tonic-gate  * no module or function associated with them.)  Probes are represented as a
857c478bd9Sstevel@tonic-gate  * dtrace_probe structure.  To allow quick lookups based on each element of the
867c478bd9Sstevel@tonic-gate  * probe tuple, probes are hashed by each of provider, module, function and
877c478bd9Sstevel@tonic-gate  * name.  (If a lookup is performed based on a regular expression, a
887c478bd9Sstevel@tonic-gate  * dtrace_probekey is prepared, and a linear search is performed.) Each probe
897c478bd9Sstevel@tonic-gate  * is additionally pointed to by a linear array indexed by its identifier.  The
907c478bd9Sstevel@tonic-gate  * identifier is the provider's mechanism for indicating to the DTrace
917c478bd9Sstevel@tonic-gate  * framework that a probe has fired:  the identifier is passed as the first
927c478bd9Sstevel@tonic-gate  * argument to dtrace_probe(), where it is then mapped into the corresponding
937c478bd9Sstevel@tonic-gate  * dtrace_probe structure.  From the dtrace_probe structure, dtrace_probe() can
947c478bd9Sstevel@tonic-gate  * iterate over the probe's list of enabling control blocks; see "DTrace
957c478bd9Sstevel@tonic-gate  * Enabling Control Blocks", below.)
967c478bd9Sstevel@tonic-gate  */
977c478bd9Sstevel@tonic-gate struct dtrace_probe {
987c478bd9Sstevel@tonic-gate 	dtrace_id_t dtpr_id;			/* probe identifier */
997c478bd9Sstevel@tonic-gate 	dtrace_ecb_t *dtpr_ecb;			/* ECB list; see below */
1007c478bd9Sstevel@tonic-gate 	dtrace_ecb_t *dtpr_ecb_last;		/* last ECB in list */
1017c478bd9Sstevel@tonic-gate 	void *dtpr_arg;				/* provider argument */
1027c478bd9Sstevel@tonic-gate 	dtrace_cacheid_t dtpr_predcache;	/* predicate cache ID */
1037c478bd9Sstevel@tonic-gate 	int dtpr_aframes;			/* artificial frames */
1047c478bd9Sstevel@tonic-gate 	dtrace_provider_t *dtpr_provider;	/* pointer to provider */
1057c478bd9Sstevel@tonic-gate 	char *dtpr_mod;				/* probe's module name */
1067c478bd9Sstevel@tonic-gate 	char *dtpr_func;			/* probe's function name */
1077c478bd9Sstevel@tonic-gate 	char *dtpr_name;			/* probe's name */
1087c478bd9Sstevel@tonic-gate 	dtrace_probe_t *dtpr_nextmod;		/* next in module hash */
1097c478bd9Sstevel@tonic-gate 	dtrace_probe_t *dtpr_prevmod;		/* previous in module hash */
1107c478bd9Sstevel@tonic-gate 	dtrace_probe_t *dtpr_nextfunc;		/* next in function hash */
1117c478bd9Sstevel@tonic-gate 	dtrace_probe_t *dtpr_prevfunc;		/* previous in function hash */
1127c478bd9Sstevel@tonic-gate 	dtrace_probe_t *dtpr_nextname;		/* next in name hash */
1137c478bd9Sstevel@tonic-gate 	dtrace_probe_t *dtpr_prevname;		/* previous in name hash */
1147c478bd9Sstevel@tonic-gate 	dtrace_genid_t dtpr_gen;		/* probe generation ID */
1157c478bd9Sstevel@tonic-gate };
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate typedef int dtrace_probekey_f(const char *, const char *, int);
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate typedef struct dtrace_probekey {
1207c478bd9Sstevel@tonic-gate 	const char *dtpk_prov;			/* provider name to match */
1217c478bd9Sstevel@tonic-gate 	dtrace_probekey_f *dtpk_pmatch;		/* provider matching function */
1227c478bd9Sstevel@tonic-gate 	const char *dtpk_mod;			/* module name to match */
1237c478bd9Sstevel@tonic-gate 	dtrace_probekey_f *dtpk_mmatch;		/* module matching function */
1247c478bd9Sstevel@tonic-gate 	const char *dtpk_func;			/* func name to match */
1257c478bd9Sstevel@tonic-gate 	dtrace_probekey_f *dtpk_fmatch;		/* func matching function */
1267c478bd9Sstevel@tonic-gate 	const char *dtpk_name;			/* name to match */
1277c478bd9Sstevel@tonic-gate 	dtrace_probekey_f *dtpk_nmatch;		/* name matching function */
1287c478bd9Sstevel@tonic-gate 	dtrace_id_t dtpk_id;			/* identifier to match */
1297c478bd9Sstevel@tonic-gate } dtrace_probekey_t;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate typedef struct dtrace_hashbucket {
1327c478bd9Sstevel@tonic-gate 	struct dtrace_hashbucket *dthb_next;	/* next on hash chain */
1337c478bd9Sstevel@tonic-gate 	dtrace_probe_t *dthb_chain;		/* chain of probes */
1347c478bd9Sstevel@tonic-gate 	int dthb_len;				/* number of probes here */
1357c478bd9Sstevel@tonic-gate } dtrace_hashbucket_t;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate typedef struct dtrace_hash {
1387c478bd9Sstevel@tonic-gate 	dtrace_hashbucket_t **dth_tab;		/* hash table */
1397c478bd9Sstevel@tonic-gate 	int dth_size;				/* size of hash table */
1407c478bd9Sstevel@tonic-gate 	int dth_mask;				/* mask to index into table */
1417c478bd9Sstevel@tonic-gate 	int dth_nbuckets;			/* total number of buckets */
1427c478bd9Sstevel@tonic-gate 	uintptr_t dth_nextoffs;			/* offset of next in probe */
1437c478bd9Sstevel@tonic-gate 	uintptr_t dth_prevoffs;			/* offset of prev in probe */
1447c478bd9Sstevel@tonic-gate 	uintptr_t dth_stroffs;			/* offset of str in probe */
1457c478bd9Sstevel@tonic-gate } dtrace_hash_t;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate  * DTrace Enabling Control Blocks
1497c478bd9Sstevel@tonic-gate  *
1507c478bd9Sstevel@tonic-gate  * When a provider wishes to fire a probe, it calls into dtrace_probe(),
1517c478bd9Sstevel@tonic-gate  * passing the probe identifier as the first argument.  As described above,
1527c478bd9Sstevel@tonic-gate  * dtrace_probe() maps the identifier into a pointer to a dtrace_probe_t
1537c478bd9Sstevel@tonic-gate  * structure.  This structure contains information about the probe, and a
1547c478bd9Sstevel@tonic-gate  * pointer to the list of Enabling Control Blocks (ECBs).  Each ECB points to
1557c478bd9Sstevel@tonic-gate  * DTrace consumer state, and contains an optional predicate, and a list of
1567c478bd9Sstevel@tonic-gate  * actions.  (Shown schematically below.)  The ECB abstraction allows a single
1577c478bd9Sstevel@tonic-gate  * probe to be multiplexed across disjoint consumers, or across disjoint
1587c478bd9Sstevel@tonic-gate  * enablings of a single probe within one consumer.
1597c478bd9Sstevel@tonic-gate  *
1607c478bd9Sstevel@tonic-gate  *   Enabling Control Block
1617c478bd9Sstevel@tonic-gate  *        dtrace_ecb_t
1627c478bd9Sstevel@tonic-gate  * +------------------------+
1637c478bd9Sstevel@tonic-gate  * | dtrace_epid_t ---------+--------------> Enabled Probe ID (EPID)
1647c478bd9Sstevel@tonic-gate  * | dtrace_state_t * ------+--------------> State associated with this ECB
1657c478bd9Sstevel@tonic-gate  * | dtrace_predicate_t * --+---------+
1667c478bd9Sstevel@tonic-gate  * | dtrace_action_t * -----+----+    |
1677c478bd9Sstevel@tonic-gate  * | dtrace_ecb_t * ---+    |    |    |       Predicate (if any)
1687c478bd9Sstevel@tonic-gate  * +-------------------+----+    |    |       dtrace_predicate_t
1697c478bd9Sstevel@tonic-gate  *                     |         |    +---> +--------------------+
1707c478bd9Sstevel@tonic-gate  *                     |         |          | dtrace_difo_t * ---+----> DIFO
1717c478bd9Sstevel@tonic-gate  *                     |         |          +--------------------+
1727c478bd9Sstevel@tonic-gate  *                     |         |
1737c478bd9Sstevel@tonic-gate  *            Next ECB |         |           Action
1747c478bd9Sstevel@tonic-gate  *            (if any) |         |       dtrace_action_t
1757c478bd9Sstevel@tonic-gate  *                     :         +--> +-------------------+
1767c478bd9Sstevel@tonic-gate  *                     :              | dtrace_actkind_t -+------> kind
1777c478bd9Sstevel@tonic-gate  *                     v              | dtrace_difo_t * --+------> DIFO (if any)
1787c478bd9Sstevel@tonic-gate  *                                    | dtrace_recdesc_t -+------> record descr.
1797c478bd9Sstevel@tonic-gate  *                                    | dtrace_action_t * +------+
1807c478bd9Sstevel@tonic-gate  *                                    +-------------------+      |
1817c478bd9Sstevel@tonic-gate  *                                                               | Next action
1827c478bd9Sstevel@tonic-gate  *                               +-------------------------------+  (if any)
1837c478bd9Sstevel@tonic-gate  *                               |
1847c478bd9Sstevel@tonic-gate  *                               |           Action
1857c478bd9Sstevel@tonic-gate  *                               |       dtrace_action_t
1867c478bd9Sstevel@tonic-gate  *                               +--> +-------------------+
1877c478bd9Sstevel@tonic-gate  *                                    | dtrace_actkind_t -+------> kind
1887c478bd9Sstevel@tonic-gate  *                                    | dtrace_difo_t * --+------> DIFO (if any)
1897c478bd9Sstevel@tonic-gate  *                                    | dtrace_action_t * +------+
1907c478bd9Sstevel@tonic-gate  *                                    +-------------------+      |
1917c478bd9Sstevel@tonic-gate  *                                                               | Next action
1927c478bd9Sstevel@tonic-gate  *                               +-------------------------------+  (if any)
1937c478bd9Sstevel@tonic-gate  *                               |
1947c478bd9Sstevel@tonic-gate  *                               :
1957c478bd9Sstevel@tonic-gate  *                               v
1967c478bd9Sstevel@tonic-gate  *
1977c478bd9Sstevel@tonic-gate  *
1987c478bd9Sstevel@tonic-gate  * dtrace_probe() iterates over the ECB list.  If the ECB needs less space
1997c478bd9Sstevel@tonic-gate  * than is available in the principal buffer, the ECB is processed:  if the
2007c478bd9Sstevel@tonic-gate  * predicate is non-NULL, the DIF object is executed.  If the result is
2017c478bd9Sstevel@tonic-gate  * non-zero, the action list is processed, with each action being executed
2027c478bd9Sstevel@tonic-gate  * accordingly.  When the action list has been completely executed, processing
203e5803b76SAdam H. Leventhal  * advances to the next ECB. The ECB abstraction allows disjoint consumers
204e5803b76SAdam H. Leventhal  * to multiplex on single probes.
205e5803b76SAdam H. Leventhal  *
206e5803b76SAdam H. Leventhal  * Execution of the ECB results in consuming dte_size bytes in the buffer
207e5803b76SAdam H. Leventhal  * to record data.  During execution, dte_needed bytes must be available in
208e5803b76SAdam H. Leventhal  * the buffer.  This space is used for both recorded data and tuple data.
2097c478bd9Sstevel@tonic-gate  */
2107c478bd9Sstevel@tonic-gate struct dtrace_ecb {
2117c478bd9Sstevel@tonic-gate 	dtrace_epid_t dte_epid;			/* enabled probe ID */
2127c478bd9Sstevel@tonic-gate 	uint32_t dte_alignment;			/* required alignment */
213e5803b76SAdam H. Leventhal 	size_t dte_needed;			/* space needed for execution */
214e5803b76SAdam H. Leventhal 	size_t dte_size;			/* size of recorded payload */
2157c478bd9Sstevel@tonic-gate 	dtrace_predicate_t *dte_predicate;	/* predicate, if any */
2167c478bd9Sstevel@tonic-gate 	dtrace_action_t *dte_action;		/* actions, if any */
2177c478bd9Sstevel@tonic-gate 	dtrace_ecb_t *dte_next;			/* next ECB on probe */
2187c478bd9Sstevel@tonic-gate 	dtrace_state_t *dte_state;		/* pointer to state */
2197c478bd9Sstevel@tonic-gate 	uint32_t dte_cond;			/* security condition */
2207c478bd9Sstevel@tonic-gate 	dtrace_probe_t *dte_probe;		/* pointer to probe */
2217c478bd9Sstevel@tonic-gate 	dtrace_action_t *dte_action_last;	/* last action on ECB */
2227c478bd9Sstevel@tonic-gate 	uint64_t dte_uarg;			/* library argument */
2237c478bd9Sstevel@tonic-gate };
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate struct dtrace_predicate {
2267c478bd9Sstevel@tonic-gate 	dtrace_difo_t *dtp_difo;		/* DIF object */
2277c478bd9Sstevel@tonic-gate 	dtrace_cacheid_t dtp_cacheid;		/* cache identifier */
2287c478bd9Sstevel@tonic-gate 	int dtp_refcnt;				/* reference count */
2297c478bd9Sstevel@tonic-gate };
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate struct dtrace_action {
2327c478bd9Sstevel@tonic-gate 	dtrace_actkind_t dta_kind;		/* kind of action */
2337c478bd9Sstevel@tonic-gate 	uint16_t dta_intuple;			/* boolean:  in aggregation */
2347c478bd9Sstevel@tonic-gate 	uint32_t dta_refcnt;			/* reference count */
2357c478bd9Sstevel@tonic-gate 	dtrace_difo_t *dta_difo;		/* pointer to DIFO */
2367c478bd9Sstevel@tonic-gate 	dtrace_recdesc_t dta_rec;		/* record description */
2377c478bd9Sstevel@tonic-gate 	dtrace_action_t *dta_prev;		/* previous action */
2387c478bd9Sstevel@tonic-gate 	dtrace_action_t *dta_next;		/* next action */
2397c478bd9Sstevel@tonic-gate };
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate typedef struct dtrace_aggregation {
2427c478bd9Sstevel@tonic-gate 	dtrace_action_t dtag_action;		/* action; must be first */
2437c478bd9Sstevel@tonic-gate 	dtrace_aggid_t dtag_id;			/* identifier */
2447c478bd9Sstevel@tonic-gate 	dtrace_ecb_t *dtag_ecb;			/* corresponding ECB */
2457c478bd9Sstevel@tonic-gate 	dtrace_action_t *dtag_first;		/* first action in tuple */
2467c478bd9Sstevel@tonic-gate 	uint32_t dtag_base;			/* base of aggregation */
247a1b5e537Sbmc 	uint8_t dtag_hasarg;			/* boolean:  has argument */
2487c478bd9Sstevel@tonic-gate 	uint64_t dtag_initial;			/* initial value */
249a1b5e537Sbmc 	void (*dtag_aggregate)(uint64_t *, uint64_t, uint64_t);
2507c478bd9Sstevel@tonic-gate } dtrace_aggregation_t;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate /*
2537c478bd9Sstevel@tonic-gate  * DTrace Buffers
2547c478bd9Sstevel@tonic-gate  *
2557c478bd9Sstevel@tonic-gate  * Principal buffers, aggregation buffers, and speculative buffers are all
2567c478bd9Sstevel@tonic-gate  * managed with the dtrace_buffer structure.  By default, this structure
2577c478bd9Sstevel@tonic-gate  * includes twin data buffers -- dtb_tomax and dtb_xamot -- that serve as the
2587c478bd9Sstevel@tonic-gate  * active and passive buffers, respectively.  For speculative buffers,
2597c478bd9Sstevel@tonic-gate  * dtb_xamot will be NULL; for "ring" and "fill" buffers, dtb_xamot will point
2607c478bd9Sstevel@tonic-gate  * to a scratch buffer.  For all buffer types, the dtrace_buffer structure is
2617c478bd9Sstevel@tonic-gate  * always allocated on a per-CPU basis; a single dtrace_buffer structure is
2627c478bd9Sstevel@tonic-gate  * never shared among CPUs.  (That is, there is never true sharing of the
2637c478bd9Sstevel@tonic-gate  * dtrace_buffer structure; to prevent false sharing of the structure, it must
2647c478bd9Sstevel@tonic-gate  * always be aligned to the coherence granularity -- generally 64 bytes.)
2657c478bd9Sstevel@tonic-gate  *
2667c478bd9Sstevel@tonic-gate  * One of the critical design decisions of DTrace is that a given ECB always
2677c478bd9Sstevel@tonic-gate  * stores the same quantity and type of data.  This is done to assure that the
2687c478bd9Sstevel@tonic-gate  * only metadata required for an ECB's traced data is the EPID.  That is, from
2697c478bd9Sstevel@tonic-gate  * the EPID, the consumer can determine the data layout.  (The data buffer
2707c478bd9Sstevel@tonic-gate  * layout is shown schematically below.)  By assuring that one can determine
2717c478bd9Sstevel@tonic-gate  * data layout from the EPID, the metadata stream can be separated from the
272e5803b76SAdam H. Leventhal  * data stream -- simplifying the data stream enormously.  The ECB always
273e5803b76SAdam H. Leventhal  * proceeds the recorded data as part of the dtrace_rechdr_t structure that
274e5803b76SAdam H. Leventhal  * includes the EPID and a high-resolution timestamp used for output ordering
275e5803b76SAdam H. Leventhal  * consistency.
2767c478bd9Sstevel@tonic-gate  *
277e5803b76SAdam H. Leventhal  *      base of data buffer --->  +--------+--------------------+--------+
278e5803b76SAdam H. Leventhal  *                                | rechdr | data               | rechdr |
279e5803b76SAdam H. Leventhal  *                                +--------+------+--------+----+--------+
280e5803b76SAdam H. Leventhal  *                                | data          | rechdr | data        |
281e5803b76SAdam H. Leventhal  *                                +---------------+--------+-------------+
2827c478bd9Sstevel@tonic-gate  *                                | data, cont.                          |
283e5803b76SAdam H. Leventhal  *                                +--------+--------------------+--------+
284e5803b76SAdam H. Leventhal  *                                | rechdr | data               |        |
285e5803b76SAdam H. Leventhal  *                                +--------+--------------------+        |
2867c478bd9Sstevel@tonic-gate  *                                |                ||                    |
2877c478bd9Sstevel@tonic-gate  *                                |                ||                    |
2887c478bd9Sstevel@tonic-gate  *                                |                \/                    |
2897c478bd9Sstevel@tonic-gate  *                                :                                      :
2907c478bd9Sstevel@tonic-gate  *                                .                                      .
2917c478bd9Sstevel@tonic-gate  *                                .                                      .
2927c478bd9Sstevel@tonic-gate  *                                .                                      .
2937c478bd9Sstevel@tonic-gate  *                                :                                      :
2947c478bd9Sstevel@tonic-gate  *                                |                                      |
295e5803b76SAdam H. Leventhal  *     limit of data buffer --->  +--------------------------------------+
2967c478bd9Sstevel@tonic-gate  *
2977c478bd9Sstevel@tonic-gate  * When evaluating an ECB, dtrace_probe() determines if the ECB's needs of the
2987c478bd9Sstevel@tonic-gate  * principal buffer (both scratch and payload) exceed the available space.  If
2997c478bd9Sstevel@tonic-gate  * the ECB's needs exceed available space (and if the principal buffer policy
3007c478bd9Sstevel@tonic-gate  * is the default "switch" policy), the ECB is dropped, the buffer's drop count
3017c478bd9Sstevel@tonic-gate  * is incremented, and processing advances to the next ECB.  If the ECB's needs
3027c478bd9Sstevel@tonic-gate  * can be met with the available space, the ECB is processed, but the offset in
3037c478bd9Sstevel@tonic-gate  * the principal buffer is only advanced if the ECB completes processing
3047c478bd9Sstevel@tonic-gate  * without error.
3057c478bd9Sstevel@tonic-gate  *
3067c478bd9Sstevel@tonic-gate  * When a buffer is to be switched (either because the buffer is the principal
3077c478bd9Sstevel@tonic-gate  * buffer with a "switch" policy or because it is an aggregation buffer), a
3087c478bd9Sstevel@tonic-gate  * cross call is issued to the CPU associated with the buffer.  In the cross
3097c478bd9Sstevel@tonic-gate  * call context, interrupts are disabled, and the active and the inactive
3107c478bd9Sstevel@tonic-gate  * buffers are atomically switched.  This involves switching the data pointers,
3117c478bd9Sstevel@tonic-gate  * copying the various state fields (offset, drops, errors, etc.) into their
3127c478bd9Sstevel@tonic-gate  * inactive equivalents, and clearing the state fields.  Because interrupts are
3137c478bd9Sstevel@tonic-gate  * disabled during this procedure, the switch is guaranteed to appear atomic to
3147c478bd9Sstevel@tonic-gate  * dtrace_probe().
3157c478bd9Sstevel@tonic-gate  *
3167c478bd9Sstevel@tonic-gate  * DTrace Ring Buffering
3177c478bd9Sstevel@tonic-gate  *
3187c478bd9Sstevel@tonic-gate  * To process a ring buffer correctly, one must know the oldest valid record.
3197c478bd9Sstevel@tonic-gate  * Processing starts at the oldest record in the buffer and continues until
3207c478bd9Sstevel@tonic-gate  * the end of the buffer is reached.  Processing then resumes starting with
3217c478bd9Sstevel@tonic-gate  * the record stored at offset 0 in the buffer, and continues until the
3227c478bd9Sstevel@tonic-gate  * youngest record is processed.  If trace records are of a fixed-length,
3237c478bd9Sstevel@tonic-gate  * determining the oldest record is trivial:
3247c478bd9Sstevel@tonic-gate  *
3257c478bd9Sstevel@tonic-gate  *   - If the ring buffer has not wrapped, the oldest record is the record
3267c478bd9Sstevel@tonic-gate  *     stored at offset 0.
3277c478bd9Sstevel@tonic-gate  *
3287c478bd9Sstevel@tonic-gate  *   - If the ring buffer has wrapped, the oldest record is the record stored
3297c478bd9Sstevel@tonic-gate  *     at the current offset.
3307c478bd9Sstevel@tonic-gate  *
3317c478bd9Sstevel@tonic-gate  * With variable length records, however, just knowing the current offset
3327c478bd9Sstevel@tonic-gate  * doesn't suffice for determining the oldest valid record:  assuming that one
3337c478bd9Sstevel@tonic-gate  * allows for arbitrary data, one has no way of searching forward from the
3347c478bd9Sstevel@tonic-gate  * current offset to find the oldest valid record.  (That is, one has no way
3357c478bd9Sstevel@tonic-gate  * of separating data from metadata.) It would be possible to simply refuse to
3367c478bd9Sstevel@tonic-gate  * process any data in the ring buffer between the current offset and the
3377c478bd9Sstevel@tonic-gate  * limit, but this leaves (potentially) an enormous amount of otherwise valid
3387c478bd9Sstevel@tonic-gate  * data unprocessed.
3397c478bd9Sstevel@tonic-gate  *
3407c478bd9Sstevel@tonic-gate  * To effect ring buffering, we track two offsets in the buffer:  the current
3417c478bd9Sstevel@tonic-gate  * offset and the _wrapped_ offset.  If a request is made to reserve some
3427c478bd9Sstevel@tonic-gate  * amount of data, and the buffer has wrapped, the wrapped offset is
3437c478bd9Sstevel@tonic-gate  * incremented until the wrapped offset minus the current offset is greater
3447c478bd9Sstevel@tonic-gate  * than or equal to the reserve request.  This is done by repeatedly looking
3457c478bd9Sstevel@tonic-gate  * up the ECB corresponding to the EPID at the current wrapped offset, and
3467c478bd9Sstevel@tonic-gate  * incrementing the wrapped offset by the size of the data payload
3477c478bd9Sstevel@tonic-gate  * corresponding to that ECB.  If this offset is greater than or equal to the
3487c478bd9Sstevel@tonic-gate  * limit of the data buffer, the wrapped offset is set to 0.  Thus, the
3497c478bd9Sstevel@tonic-gate  * current offset effectively "chases" the wrapped offset around the buffer.
3507c478bd9Sstevel@tonic-gate  * Schematically:
3517c478bd9Sstevel@tonic-gate  *
3527c478bd9Sstevel@tonic-gate  *      base of data buffer --->  +------+--------------------+------+
3537c478bd9Sstevel@tonic-gate  *                                | EPID | data               | EPID |
3547c478bd9Sstevel@tonic-gate  *                                +------+--------+------+----+------+
3557c478bd9Sstevel@tonic-gate  *                                | data          | EPID | data      |
3567c478bd9Sstevel@tonic-gate  *                                +---------------+------+-----------+
3577c478bd9Sstevel@tonic-gate  *                                | data, cont.                      |
3587c478bd9Sstevel@tonic-gate  *                                +------+---------------------------+
3597c478bd9Sstevel@tonic-gate  *                                | EPID | data                      |
3607c478bd9Sstevel@tonic-gate  *           current offset --->  +------+---------------------------+
3617c478bd9Sstevel@tonic-gate  *                                | invalid data                     |
3627c478bd9Sstevel@tonic-gate  *           wrapped offset --->  +------+--------------------+------+
3637c478bd9Sstevel@tonic-gate  *                                | EPID | data               | EPID |
3647c478bd9Sstevel@tonic-gate  *                                +------+--------+------+----+------+
3657c478bd9Sstevel@tonic-gate  *                                | data          | EPID | data      |
3667c478bd9Sstevel@tonic-gate  *                                +---------------+------+-----------+
3677c478bd9Sstevel@tonic-gate  *                                :                                  :
3687c478bd9Sstevel@tonic-gate  *                                .                                  .
3697c478bd9Sstevel@tonic-gate  *                                .        ... valid data ...        .
3707c478bd9Sstevel@tonic-gate  *                                .                                  .
3717c478bd9Sstevel@tonic-gate  *                                :                                  :
3727c478bd9Sstevel@tonic-gate  *                                +------+-------------+------+------+
3737c478bd9Sstevel@tonic-gate  *                                | EPID | data        | EPID | data |
3747c478bd9Sstevel@tonic-gate  *                                +------+------------++------+------+
3757c478bd9Sstevel@tonic-gate  *                                | data, cont.       | leftover     |
3767c478bd9Sstevel@tonic-gate  *     limit of data buffer --->  +-------------------+--------------+
3777c478bd9Sstevel@tonic-gate  *
3787c478bd9Sstevel@tonic-gate  * If the amount of requested buffer space exceeds the amount of space
3797c478bd9Sstevel@tonic-gate  * available between the current offset and the end of the buffer:
3807c478bd9Sstevel@tonic-gate  *
3817c478bd9Sstevel@tonic-gate  *  (1)  all words in the data buffer between the current offset and the limit
3827c478bd9Sstevel@tonic-gate  *       of the data buffer (marked "leftover", above) are set to
3837c478bd9Sstevel@tonic-gate  *       DTRACE_EPIDNONE
3847c478bd9Sstevel@tonic-gate  *
3857c478bd9Sstevel@tonic-gate  *  (2)  the wrapped offset is set to zero
3867c478bd9Sstevel@tonic-gate  *
3877c478bd9Sstevel@tonic-gate  *  (3)  the iteration process described above occurs until the wrapped offset
3887c478bd9Sstevel@tonic-gate  *       is greater than the amount of desired space.
3897c478bd9Sstevel@tonic-gate  *
3907c478bd9Sstevel@tonic-gate  * The wrapped offset is implemented by (re-)using the inactive offset.
3917c478bd9Sstevel@tonic-gate  * In a "switch" buffer policy, the inactive offset stores the offset in
3927c478bd9Sstevel@tonic-gate  * the inactive buffer; in a "ring" buffer policy, it stores the wrapped
3937c478bd9Sstevel@tonic-gate  * offset.
3947c478bd9Sstevel@tonic-gate  *
3957c478bd9Sstevel@tonic-gate  * DTrace Scratch Buffering
3967c478bd9Sstevel@tonic-gate  *
3977c478bd9Sstevel@tonic-gate  * Some ECBs may wish to allocate dynamically-sized temporary scratch memory.
3987c478bd9Sstevel@tonic-gate  * To accommodate such requests easily, scratch memory may be allocated in
3997c478bd9Sstevel@tonic-gate  * the buffer beyond the current offset plus the needed memory of the current
4007c478bd9Sstevel@tonic-gate  * ECB.  If there isn't sufficient room in the buffer for the requested amount
4017c478bd9Sstevel@tonic-gate  * of scratch space, the allocation fails and an error is generated.  Scratch
4027c478bd9Sstevel@tonic-gate  * memory is tracked in the dtrace_mstate_t and is automatically freed when
4037c478bd9Sstevel@tonic-gate  * the ECB ceases processing.  Note that ring buffers cannot allocate their
4047c478bd9Sstevel@tonic-gate  * scratch from the principal buffer -- lest they needlessly overwrite older,
4057c478bd9Sstevel@tonic-gate  * valid data.  Ring buffers therefore have their own dedicated scratch buffer
4067c478bd9Sstevel@tonic-gate  * from which scratch is allocated.
4077c478bd9Sstevel@tonic-gate  */
4087c478bd9Sstevel@tonic-gate #define	DTRACEBUF_RING		0x0001		/* bufpolicy set to "ring" */
4097c478bd9Sstevel@tonic-gate #define	DTRACEBUF_FILL		0x0002		/* bufpolicy set to "fill" */
4107c478bd9Sstevel@tonic-gate #define	DTRACEBUF_NOSWITCH	0x0004		/* do not switch buffer */
4117c478bd9Sstevel@tonic-gate #define	DTRACEBUF_WRAPPED	0x0008		/* ring buffer has wrapped */
4127c478bd9Sstevel@tonic-gate #define	DTRACEBUF_DROPPED	0x0010		/* drops occurred */
4137c478bd9Sstevel@tonic-gate #define	DTRACEBUF_ERROR		0x0020		/* errors occurred */
4147c478bd9Sstevel@tonic-gate #define	DTRACEBUF_FULL		0x0040		/* "fill" buffer is full */
4157c478bd9Sstevel@tonic-gate #define	DTRACEBUF_CONSUMED	0x0080		/* buffer has been consumed */
4167c478bd9Sstevel@tonic-gate #define	DTRACEBUF_INACTIVE	0x0100		/* buffer is not yet active */
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate typedef struct dtrace_buffer {
4197c478bd9Sstevel@tonic-gate 	uint64_t dtb_offset;			/* current offset in buffer */
4207c478bd9Sstevel@tonic-gate 	uint64_t dtb_size;			/* size of buffer */
4217c478bd9Sstevel@tonic-gate 	uint32_t dtb_flags;			/* flags */
4227c478bd9Sstevel@tonic-gate 	uint32_t dtb_drops;			/* number of drops */
4237c478bd9Sstevel@tonic-gate 	caddr_t dtb_tomax;			/* active buffer */
4247c478bd9Sstevel@tonic-gate 	caddr_t dtb_xamot;			/* inactive buffer */
4257c478bd9Sstevel@tonic-gate 	uint32_t dtb_xamot_flags;		/* inactive flags */
4267c478bd9Sstevel@tonic-gate 	uint32_t dtb_xamot_drops;		/* drops in inactive buffer */
4277c478bd9Sstevel@tonic-gate 	uint64_t dtb_xamot_offset;		/* offset in inactive buffer */
4287c478bd9Sstevel@tonic-gate 	uint32_t dtb_errors;			/* number of errors */
4297c478bd9Sstevel@tonic-gate 	uint32_t dtb_xamot_errors;		/* errors in inactive buffer */
4307c478bd9Sstevel@tonic-gate #ifndef _LP64
431f484800dSBryan Cantrill 	uint64_t dtb_pad1;			/* pad out to 64 bytes */
4327c478bd9Sstevel@tonic-gate #endif
433f484800dSBryan Cantrill 	uint64_t dtb_switched;			/* time of last switch */
434f484800dSBryan Cantrill 	uint64_t dtb_interval;			/* observed switch interval */
435f484800dSBryan Cantrill 	uint64_t dtb_pad2[6];			/* pad to avoid false sharing */
4367c478bd9Sstevel@tonic-gate } dtrace_buffer_t;
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate /*
4397c478bd9Sstevel@tonic-gate  * DTrace Aggregation Buffers
4407c478bd9Sstevel@tonic-gate  *
4417c478bd9Sstevel@tonic-gate  * Aggregation buffers use much of the same mechanism as described above
4427c478bd9Sstevel@tonic-gate  * ("DTrace Buffers").  However, because an aggregation is fundamentally a
4437c478bd9Sstevel@tonic-gate  * hash, there exists dynamic metadata associated with an aggregation buffer
4447c478bd9Sstevel@tonic-gate  * that is not associated with other kinds of buffers.  This aggregation
4457c478bd9Sstevel@tonic-gate  * metadata is _only_ relevant for the in-kernel implementation of
4467c478bd9Sstevel@tonic-gate  * aggregations; it is not actually relevant to user-level consumers.  To do
4477c478bd9Sstevel@tonic-gate  * this, we allocate dynamic aggregation data (hash keys and hash buckets)
4487c478bd9Sstevel@tonic-gate  * starting below the _limit_ of the buffer, and we allocate data from the
4497c478bd9Sstevel@tonic-gate  * _base_ of the buffer.  When the aggregation buffer is copied out, _only_ the
4507c478bd9Sstevel@tonic-gate  * data is copied out; the metadata is simply discarded.  Schematically,
4517c478bd9Sstevel@tonic-gate  * aggregation buffers look like:
4527c478bd9Sstevel@tonic-gate  *
4537c478bd9Sstevel@tonic-gate  *      base of data buffer --->  +-------+------+-----------+-------+
4547c478bd9Sstevel@tonic-gate  *                                | aggid | key  | value     | aggid |
4557c478bd9Sstevel@tonic-gate  *                                +-------+------+-----------+-------+
4567c478bd9Sstevel@tonic-gate  *                                | key                              |
4577c478bd9Sstevel@tonic-gate  *                                +-------+-------+-----+------------+
4587c478bd9Sstevel@tonic-gate  *                                | value | aggid | key | value      |
4597c478bd9Sstevel@tonic-gate  *                                +-------+------++-----+------+-----+
4607c478bd9Sstevel@tonic-gate  *                                | aggid | key  | value       |     |
4617c478bd9Sstevel@tonic-gate  *                                +-------+------+-------------+     |
4627c478bd9Sstevel@tonic-gate  *                                |                ||                |
4637c478bd9Sstevel@tonic-gate  *                                |                ||                |
4647c478bd9Sstevel@tonic-gate  *                                |                \/                |
4657c478bd9Sstevel@tonic-gate  *                                :                                  :
4667c478bd9Sstevel@tonic-gate  *                                .                                  .
4677c478bd9Sstevel@tonic-gate  *                                .                                  .
4687c478bd9Sstevel@tonic-gate  *                                .                                  .
4697c478bd9Sstevel@tonic-gate  *                                :                                  :
4707c478bd9Sstevel@tonic-gate  *                                |                /\                |
4717c478bd9Sstevel@tonic-gate  *                                |                ||   +------------+
4727c478bd9Sstevel@tonic-gate  *                                |                ||   |            |
4737c478bd9Sstevel@tonic-gate  *                                +---------------------+            |
4747c478bd9Sstevel@tonic-gate  *                                | hash keys                        |
4757c478bd9Sstevel@tonic-gate  *                                | (dtrace_aggkey structures)       |
4767c478bd9Sstevel@tonic-gate  *                                |                                  |
4777c478bd9Sstevel@tonic-gate  *                                +----------------------------------+
4787c478bd9Sstevel@tonic-gate  *                                | hash buckets                     |
4797c478bd9Sstevel@tonic-gate  *                                | (dtrace_aggbuffer structure)     |
4807c478bd9Sstevel@tonic-gate  *                                |                                  |
4817c478bd9Sstevel@tonic-gate  *     limit of data buffer --->  +----------------------------------+
4827c478bd9Sstevel@tonic-gate  *
4837c478bd9Sstevel@tonic-gate  *
4847c478bd9Sstevel@tonic-gate  * As implied above, just as we assure that ECBs always store a constant
4857c478bd9Sstevel@tonic-gate  * amount of data, we assure that a given aggregation -- identified by its
4867c478bd9Sstevel@tonic-gate  * aggregation ID -- always stores data of a constant quantity and type.
4877c478bd9Sstevel@tonic-gate  * As with EPIDs, this allows the aggregation ID to serve as the metadata for a
4887c478bd9Sstevel@tonic-gate  * given record.
4897c478bd9Sstevel@tonic-gate  *
4907c478bd9Sstevel@tonic-gate  * Note that the size of the dtrace_aggkey structure must be sizeof (uintptr_t)
4917c478bd9Sstevel@tonic-gate  * aligned.  (If this the structure changes such that this becomes false, an
4927c478bd9Sstevel@tonic-gate  * assertion will fail in dtrace_aggregate().)
4937c478bd9Sstevel@tonic-gate  */
4947c478bd9Sstevel@tonic-gate typedef struct dtrace_aggkey {
4957c478bd9Sstevel@tonic-gate 	uint32_t dtak_hashval;			/* hash value */
4967c478bd9Sstevel@tonic-gate 	uint32_t dtak_action:4;			/* action -- 4 bits */
4977c478bd9Sstevel@tonic-gate 	uint32_t dtak_size:28;			/* size -- 28 bits */
4987c478bd9Sstevel@tonic-gate 	caddr_t dtak_data;			/* data pointer */
4997c478bd9Sstevel@tonic-gate 	struct dtrace_aggkey *dtak_next;	/* next in hash chain */
5007c478bd9Sstevel@tonic-gate } dtrace_aggkey_t;
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate typedef struct dtrace_aggbuffer {
5037c478bd9Sstevel@tonic-gate 	uintptr_t dtagb_hashsize;		/* number of buckets */
5047c478bd9Sstevel@tonic-gate 	uintptr_t dtagb_free;			/* free list of keys */
5057c478bd9Sstevel@tonic-gate 	dtrace_aggkey_t **dtagb_hash;		/* hash table */
5067c478bd9Sstevel@tonic-gate } dtrace_aggbuffer_t;
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate /*
5097c478bd9Sstevel@tonic-gate  * DTrace Speculations
5107c478bd9Sstevel@tonic-gate  *
5117c478bd9Sstevel@tonic-gate  * Speculations have a per-CPU buffer and a global state.  Once a speculation
5127c478bd9Sstevel@tonic-gate  * buffer has been comitted or discarded, it cannot be reused until all CPUs
5137c478bd9Sstevel@tonic-gate  * have taken the same action (commit or discard) on their respective
5147c478bd9Sstevel@tonic-gate  * speculative buffer.  However, because DTrace probes may execute in arbitrary
5157c478bd9Sstevel@tonic-gate  * context, other CPUs cannot simply be cross-called at probe firing time to
5167c478bd9Sstevel@tonic-gate  * perform the necessary commit or discard.  The speculation states thus
5177c478bd9Sstevel@tonic-gate  * optimize for the case that a speculative buffer is only active on one CPU at
5187c478bd9Sstevel@tonic-gate  * the time of a commit() or discard() -- for if this is the case, other CPUs
5197c478bd9Sstevel@tonic-gate  * need not take action, and the speculation is immediately available for
5207c478bd9Sstevel@tonic-gate  * reuse.  If the speculation is active on multiple CPUs, it must be
5217c478bd9Sstevel@tonic-gate  * asynchronously cleaned -- potentially leading to a higher rate of dirty
5227c478bd9Sstevel@tonic-gate  * speculative drops.  The speculation states are as follows:
5237c478bd9Sstevel@tonic-gate  *
5247c478bd9Sstevel@tonic-gate  *  DTRACESPEC_INACTIVE       <= Initial state; inactive speculation
5257c478bd9Sstevel@tonic-gate  *  DTRACESPEC_ACTIVE         <= Allocated, but not yet speculatively traced to
5267c478bd9Sstevel@tonic-gate  *  DTRACESPEC_ACTIVEONE      <= Speculatively traced to on one CPU
5277c478bd9Sstevel@tonic-gate  *  DTRACESPEC_ACTIVEMANY     <= Speculatively traced to on more than one CPU
5287c478bd9Sstevel@tonic-gate  *  DTRACESPEC_COMMITTING     <= Currently being commited on one CPU
5297c478bd9Sstevel@tonic-gate  *  DTRACESPEC_COMMITTINGMANY <= Currently being commited on many CPUs
5307c478bd9Sstevel@tonic-gate  *  DTRACESPEC_DISCARDING     <= Currently being discarded on many CPUs
5317c478bd9Sstevel@tonic-gate  *
5327c478bd9Sstevel@tonic-gate  * The state transition diagram is as follows:
5337c478bd9Sstevel@tonic-gate  *
5347c478bd9Sstevel@tonic-gate  *     +----------------------------------------------------------+
5357c478bd9Sstevel@tonic-gate  *     |                                                          |
5367c478bd9Sstevel@tonic-gate  *     |                      +------------+                      |
5377c478bd9Sstevel@tonic-gate  *     |  +-------------------| COMMITTING |<-----------------+   |
5387c478bd9Sstevel@tonic-gate  *     |  |                   +------------+                  |   |
5397c478bd9Sstevel@tonic-gate  *     |  | copied spec.            ^             commit() on |   | discard() on
5407c478bd9Sstevel@tonic-gate  *     |  | into principal          |              active CPU |   | active CPU
5417c478bd9Sstevel@tonic-gate  *     |  |                         | commit()                |   |
5427c478bd9Sstevel@tonic-gate  *     V  V                         |                         |   |
5437c478bd9Sstevel@tonic-gate  * +----------+                 +--------+                +-----------+
5447c478bd9Sstevel@tonic-gate  * | INACTIVE |---------------->| ACTIVE |--------------->| ACTIVEONE |
5457c478bd9Sstevel@tonic-gate  * +----------+  speculation()  +--------+  speculate()   +-----------+
5467c478bd9Sstevel@tonic-gate  *     ^  ^                         |                         |   |
5477c478bd9Sstevel@tonic-gate  *     |  |                         | discard()               |   |
5487c478bd9Sstevel@tonic-gate  *     |  | asynchronously          |            discard() on |   | speculate()
5497c478bd9Sstevel@tonic-gate  *     |  | cleaned                 V            inactive CPU |   | on inactive
5507c478bd9Sstevel@tonic-gate  *     |  |                   +------------+                  |   | CPU
5517c478bd9Sstevel@tonic-gate  *     |  +-------------------| DISCARDING |<-----------------+   |
5527c478bd9Sstevel@tonic-gate  *     |                      +------------+                      |
5537c478bd9Sstevel@tonic-gate  *     | asynchronously             ^                             |
5547c478bd9Sstevel@tonic-gate  *     | copied spec.               |       discard()             |
5557c478bd9Sstevel@tonic-gate  *     | into principal             +------------------------+    |
5567c478bd9Sstevel@tonic-gate  *     |                                                     |    V
5577c478bd9Sstevel@tonic-gate  *  +----------------+             commit()              +------------+
5587c478bd9Sstevel@tonic-gate  *  | COMMITTINGMANY |<----------------------------------| ACTIVEMANY |
5597c478bd9Sstevel@tonic-gate  *  +----------------+                                   +------------+
5607c478bd9Sstevel@tonic-gate  */
5617c478bd9Sstevel@tonic-gate typedef enum dtrace_speculation_state {
5627c478bd9Sstevel@tonic-gate 	DTRACESPEC_INACTIVE = 0,
5637c478bd9Sstevel@tonic-gate 	DTRACESPEC_ACTIVE,
5647c478bd9Sstevel@tonic-gate 	DTRACESPEC_ACTIVEONE,
5657c478bd9Sstevel@tonic-gate 	DTRACESPEC_ACTIVEMANY,
5667c478bd9Sstevel@tonic-gate 	DTRACESPEC_COMMITTING,
5677c478bd9Sstevel@tonic-gate 	DTRACESPEC_COMMITTINGMANY,
5687c478bd9Sstevel@tonic-gate 	DTRACESPEC_DISCARDING
5697c478bd9Sstevel@tonic-gate } dtrace_speculation_state_t;
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate typedef struct dtrace_speculation {
5727c478bd9Sstevel@tonic-gate 	dtrace_speculation_state_t dtsp_state;	/* current speculation state */
5737c478bd9Sstevel@tonic-gate 	int dtsp_cleaning;			/* non-zero if being cleaned */
5747c478bd9Sstevel@tonic-gate 	dtrace_buffer_t *dtsp_buffer;		/* speculative buffer */
5757c478bd9Sstevel@tonic-gate } dtrace_speculation_t;
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate /*
5787c478bd9Sstevel@tonic-gate  * DTrace Dynamic Variables
5797c478bd9Sstevel@tonic-gate  *
5807c478bd9Sstevel@tonic-gate  * The dynamic variable problem is obviously decomposed into two subproblems:
5817c478bd9Sstevel@tonic-gate  * allocating new dynamic storage, and freeing old dynamic storage.  The
5827c478bd9Sstevel@tonic-gate  * presence of the second problem makes the first much more complicated -- or
5837c478bd9Sstevel@tonic-gate  * rather, the absence of the second renders the first trivial.  This is the
5847c478bd9Sstevel@tonic-gate  * case with aggregations, for which there is effectively no deallocation of
5857c478bd9Sstevel@tonic-gate  * dynamic storage.  (Or more accurately, all dynamic storage is deallocated
5867c478bd9Sstevel@tonic-gate  * when a snapshot is taken of the aggregation.)  As DTrace dynamic variables
5877c478bd9Sstevel@tonic-gate  * allow for both dynamic allocation and dynamic deallocation, the
5887c478bd9Sstevel@tonic-gate  * implementation of dynamic variables is quite a bit more complicated than
5897c478bd9Sstevel@tonic-gate  * that of their aggregation kin.
5907c478bd9Sstevel@tonic-gate  *
5917c478bd9Sstevel@tonic-gate  * We observe that allocating new dynamic storage is tricky only because the
5927c478bd9Sstevel@tonic-gate  * size can vary -- the allocation problem is much easier if allocation sizes
5937c478bd9Sstevel@tonic-gate  * are uniform.  We further observe that in D, the size of dynamic variables is
5947c478bd9Sstevel@tonic-gate  * actually _not_ dynamic -- dynamic variable sizes may be determined by static
5957c478bd9Sstevel@tonic-gate  * analysis of DIF text.  (This is true even of putatively dynamically-sized
5967c478bd9Sstevel@tonic-gate  * objects like strings and stacks, the sizes of which are dictated by the
5977c478bd9Sstevel@tonic-gate  * "stringsize" and "stackframes" variables, respectively.)  We exploit this by
5987c478bd9Sstevel@tonic-gate  * performing this analysis on all DIF before enabling any probes.  For each
5997c478bd9Sstevel@tonic-gate  * dynamic load or store, we calculate the dynamically-allocated size plus the
6007c478bd9Sstevel@tonic-gate  * size of the dtrace_dynvar structure plus the storage required to key the
6017c478bd9Sstevel@tonic-gate  * data.  For all DIF, we take the largest value and dub it the _chunksize_.
6027c478bd9Sstevel@tonic-gate  * We then divide dynamic memory into two parts:  a hash table that is wide
6037c478bd9Sstevel@tonic-gate  * enough to have every chunk in its own bucket, and a larger region of equal
6047c478bd9Sstevel@tonic-gate  * chunksize units.  Whenever we wish to dynamically allocate a variable, we
6057c478bd9Sstevel@tonic-gate  * always allocate a single chunk of memory.  Depending on the uniformity of
6067c478bd9Sstevel@tonic-gate  * allocation, this will waste some amount of memory -- but it eliminates the
6077c478bd9Sstevel@tonic-gate  * non-determinism inherent in traditional heap fragmentation.
6087c478bd9Sstevel@tonic-gate  *
6097c478bd9Sstevel@tonic-gate  * Dynamic objects are allocated by storing a non-zero value to them; they are
6107c478bd9Sstevel@tonic-gate  * deallocated by storing a zero value to them.  Dynamic variables are
6117c478bd9Sstevel@tonic-gate  * complicated enormously by being shared between CPUs.  In particular,
6127c478bd9Sstevel@tonic-gate  * consider the following scenario:
6137c478bd9Sstevel@tonic-gate  *
6147c478bd9Sstevel@tonic-gate  *                 CPU A                                 CPU B
6157c478bd9Sstevel@tonic-gate  *  +---------------------------------+   +---------------------------------+
6167c478bd9Sstevel@tonic-gate  *  |                                 |   |                                 |
6177c478bd9Sstevel@tonic-gate  *  | allocates dynamic object a[123] |   |                                 |
6187c478bd9Sstevel@tonic-gate  *  | by storing the value 345 to it  |   |                                 |
6197c478bd9Sstevel@tonic-gate  *  |                               --------->                              |
6207c478bd9Sstevel@tonic-gate  *  |                                 |   | wishing to load from object     |
6217c478bd9Sstevel@tonic-gate  *  |                                 |   | a[123], performs lookup in      |
6227c478bd9Sstevel@tonic-gate  *  |                                 |   | dynamic variable space          |
6237c478bd9Sstevel@tonic-gate  *  |                               <---------                              |
6247c478bd9Sstevel@tonic-gate  *  | deallocates object a[123] by    |   |                                 |
6257c478bd9Sstevel@tonic-gate  *  | storing 0 to it                 |   |                                 |
6267c478bd9Sstevel@tonic-gate  *  |                                 |   |                                 |
6277c478bd9Sstevel@tonic-gate  *  | allocates dynamic object b[567] |   | performs load from a[123]       |
6287c478bd9Sstevel@tonic-gate  *  | by storing the value 789 to it  |   |                                 |
6297c478bd9Sstevel@tonic-gate  *  :                                 :   :                                 :
6307c478bd9Sstevel@tonic-gate  *  .                                 .   .                                 .
6317c478bd9Sstevel@tonic-gate  *
6327c478bd9Sstevel@tonic-gate  * This is obviously a race in the D program, but there are nonetheless only
6337c478bd9Sstevel@tonic-gate  * two valid values for CPU B's load from a[123]:  345 or 0.  Most importantly,
6347c478bd9Sstevel@tonic-gate  * CPU B may _not_ see the value 789 for a[123].
6357c478bd9Sstevel@tonic-gate  *
6367c478bd9Sstevel@tonic-gate  * There are essentially two ways to deal with this:
6377c478bd9Sstevel@tonic-gate  *
6387c478bd9Sstevel@tonic-gate  *  (1)  Explicitly spin-lock variables.  That is, if CPU B wishes to load
6397c478bd9Sstevel@tonic-gate  *       from a[123], it needs to lock a[123] and hold the lock for the
6407c478bd9Sstevel@tonic-gate  *       duration that it wishes to manipulate it.
6417c478bd9Sstevel@tonic-gate  *
6427c478bd9Sstevel@tonic-gate  *  (2)  Avoid reusing freed chunks until it is known that no CPU is referring
6437c478bd9Sstevel@tonic-gate  *       to them.
6447c478bd9Sstevel@tonic-gate  *
6457c478bd9Sstevel@tonic-gate  * The implementation of (1) is rife with complexity, because it requires the
6467c478bd9Sstevel@tonic-gate  * user of a dynamic variable to explicitly decree when they are done using it.
6477c478bd9Sstevel@tonic-gate  * Were all variables by value, this perhaps wouldn't be debilitating -- but
6487c478bd9Sstevel@tonic-gate  * dynamic variables of non-scalar types are tracked by reference.  That is, if
6497c478bd9Sstevel@tonic-gate  * a dynamic variable is, say, a string, and that variable is to be traced to,
6507c478bd9Sstevel@tonic-gate  * say, the principal buffer, the DIF emulation code returns to the main
6517c478bd9Sstevel@tonic-gate  * dtrace_probe() loop a pointer to the underlying storage, not the contents of
6527c478bd9Sstevel@tonic-gate  * the storage.  Further, code calling on DIF emulation would have to be aware
6537c478bd9Sstevel@tonic-gate  * that the DIF emulation has returned a reference to a dynamic variable that
6547c478bd9Sstevel@tonic-gate  * has been potentially locked.  The variable would have to be unlocked after
6557c478bd9Sstevel@tonic-gate  * the main dtrace_probe() loop is finished with the variable, and the main
6567c478bd9Sstevel@tonic-gate  * dtrace_probe() loop would have to be careful to not call any further DIF
6577c478bd9Sstevel@tonic-gate  * emulation while the variable is locked to avoid deadlock.  More generally,
6587c478bd9Sstevel@tonic-gate  * if one were to implement (1), DIF emulation code dealing with dynamic
6597c478bd9Sstevel@tonic-gate  * variables could only deal with one dynamic variable at a time (lest deadlock
6607c478bd9Sstevel@tonic-gate  * result).  To sum, (1) exports too much subtlety to the users of dynamic
6617c478bd9Sstevel@tonic-gate  * variables -- increasing maintenance burden and imposing serious constraints
6627c478bd9Sstevel@tonic-gate  * on future DTrace development.
6637c478bd9Sstevel@tonic-gate  *
6647c478bd9Sstevel@tonic-gate  * The implementation of (2) is also complex, but the complexity is more
6657c478bd9Sstevel@tonic-gate  * manageable.  We need to be sure that when a variable is deallocated, it is
6667c478bd9Sstevel@tonic-gate  * not placed on a traditional free list, but rather on a _dirty_ list.  Once a
6677c478bd9Sstevel@tonic-gate  * variable is on a dirty list, it cannot be found by CPUs performing a
6687c478bd9Sstevel@tonic-gate  * subsequent lookup of the variable -- but it may still be in use by other
6697c478bd9Sstevel@tonic-gate  * CPUs.  To assure that all CPUs that may be seeing the old variable have
6707c478bd9Sstevel@tonic-gate  * cleared out of probe context, a dtrace_sync() can be issued.  Once the
6717c478bd9Sstevel@tonic-gate  * dtrace_sync() has completed, it can be known that all CPUs are done
6727c478bd9Sstevel@tonic-gate  * manipulating the dynamic variable -- the dirty list can be atomically
6737c478bd9Sstevel@tonic-gate  * appended to the free list.  Unfortunately, there's a slight hiccup in this
6747c478bd9Sstevel@tonic-gate  * mechanism:  dtrace_sync() may not be issued from probe context.  The
6757c478bd9Sstevel@tonic-gate  * dtrace_sync() must be therefore issued asynchronously from non-probe
6767c478bd9Sstevel@tonic-gate  * context.  For this we rely on the DTrace cleaner, a cyclic that runs at the
6777c478bd9Sstevel@tonic-gate  * "cleanrate" frequency.  To ease this implementation, we define several chunk
6787c478bd9Sstevel@tonic-gate  * lists:
6797c478bd9Sstevel@tonic-gate  *
6807c478bd9Sstevel@tonic-gate  *   - Dirty.  Deallocated chunks, not yet cleaned.  Not available.
6817c478bd9Sstevel@tonic-gate  *
6827c478bd9Sstevel@tonic-gate  *   - Rinsing.  Formerly dirty chunks that are currently being asynchronously
6837c478bd9Sstevel@tonic-gate  *     cleaned.  Not available, but will be shortly.  Dynamic variable
6847c478bd9Sstevel@tonic-gate  *     allocation may not spin or block for availability, however.
6857c478bd9Sstevel@tonic-gate  *
6867c478bd9Sstevel@tonic-gate  *   - Clean.  Clean chunks, ready for allocation -- but not on the free list.
6877c478bd9Sstevel@tonic-gate  *
6887c478bd9Sstevel@tonic-gate  *   - Free.  Available for allocation.
6897c478bd9Sstevel@tonic-gate  *
6907c478bd9Sstevel@tonic-gate  * Moreover, to avoid absurd contention, _each_ of these lists is implemented
6917c478bd9Sstevel@tonic-gate  * on a per-CPU basis.  This is only for performance, not correctness; chunks
6927c478bd9Sstevel@tonic-gate  * may be allocated from another CPU's free list.  The algorithm for allocation
6937c478bd9Sstevel@tonic-gate  * then is this:
6947c478bd9Sstevel@tonic-gate  *
6957c478bd9Sstevel@tonic-gate  *   (1)  Attempt to atomically allocate from current CPU's free list.  If list
6967c478bd9Sstevel@tonic-gate  *        is non-empty and allocation is successful, allocation is complete.
6977c478bd9Sstevel@tonic-gate  *
6987c478bd9Sstevel@tonic-gate  *   (2)  If the clean list is non-empty, atomically move it to the free list,
6997c478bd9Sstevel@tonic-gate  *        and reattempt (1).
7007c478bd9Sstevel@tonic-gate  *
7017c478bd9Sstevel@tonic-gate  *   (3)  If the dynamic variable space is in the CLEAN state, look for free
7027c478bd9Sstevel@tonic-gate  *        and clean lists on other CPUs by setting the current CPU to the next
7037c478bd9Sstevel@tonic-gate  *        CPU, and reattempting (1).  If the next CPU is the current CPU (that
7047c478bd9Sstevel@tonic-gate  *        is, if all CPUs have been checked), atomically switch the state of
7057c478bd9Sstevel@tonic-gate  *        the dynamic variable space based on the following:
7067c478bd9Sstevel@tonic-gate  *
7077c478bd9Sstevel@tonic-gate  *        - If no free chunks were found and no dirty chunks were found,
7087c478bd9Sstevel@tonic-gate  *          atomically set the state to EMPTY.
7097c478bd9Sstevel@tonic-gate  *
7107c478bd9Sstevel@tonic-gate  *        - If dirty chunks were found, atomically set the state to DIRTY.
7117c478bd9Sstevel@tonic-gate  *
7127c478bd9Sstevel@tonic-gate  *        - If rinsing chunks were found, atomically set the state to RINSING.
7137c478bd9Sstevel@tonic-gate  *
7147c478bd9Sstevel@tonic-gate  *   (4)  Based on state of dynamic variable space state, increment appropriate
7157c478bd9Sstevel@tonic-gate  *        counter to indicate dynamic drops (if in EMPTY state) vs. dynamic
7167c478bd9Sstevel@tonic-gate  *        dirty drops (if in DIRTY state) vs. dynamic rinsing drops (if in
7177c478bd9Sstevel@tonic-gate  *        RINSING state).  Fail the allocation.
7187c478bd9Sstevel@tonic-gate  *
7197c478bd9Sstevel@tonic-gate  * The cleaning cyclic operates with the following algorithm:  for all CPUs
7207c478bd9Sstevel@tonic-gate  * with a non-empty dirty list, atomically move the dirty list to the rinsing
7217c478bd9Sstevel@tonic-gate  * list.  Perform a dtrace_sync().  For all CPUs with a non-empty rinsing list,
7227c478bd9Sstevel@tonic-gate  * atomically move the rinsing list to the clean list.  Perform another
7237c478bd9Sstevel@tonic-gate  * dtrace_sync().  By this point, all CPUs have seen the new clean list; the
7247c478bd9Sstevel@tonic-gate  * state of the dynamic variable space can be restored to CLEAN.
7257c478bd9Sstevel@tonic-gate  *
7267c478bd9Sstevel@tonic-gate  * There exist two final races that merit explanation.  The first is a simple
7277c478bd9Sstevel@tonic-gate  * allocation race:
7287c478bd9Sstevel@tonic-gate  *
7297c478bd9Sstevel@tonic-gate  *                 CPU A                                 CPU B
7307c478bd9Sstevel@tonic-gate  *  +---------------------------------+   +---------------------------------+
7317c478bd9Sstevel@tonic-gate  *  |                                 |   |                                 |
7327c478bd9Sstevel@tonic-gate  *  | allocates dynamic object a[123] |   | allocates dynamic object a[123] |
7337c478bd9Sstevel@tonic-gate  *  | by storing the value 345 to it  |   | by storing the value 567 to it  |
7347c478bd9Sstevel@tonic-gate  *  |                                 |   |                                 |
7357c478bd9Sstevel@tonic-gate  *  :                                 :   :                                 :
7367c478bd9Sstevel@tonic-gate  *  .                                 .   .                                 .
7377c478bd9Sstevel@tonic-gate  *
7387c478bd9Sstevel@tonic-gate  * Again, this is a race in the D program.  It can be resolved by having a[123]
7397c478bd9Sstevel@tonic-gate  * hold the value 345 or a[123] hold the value 567 -- but it must be true that
7407c478bd9Sstevel@tonic-gate  * a[123] have only _one_ of these values.  (That is, the racing CPUs may not
7417c478bd9Sstevel@tonic-gate  * put the same element twice on the same hash chain.)  This is resolved
7427c478bd9Sstevel@tonic-gate  * simply:  before the allocation is undertaken, the start of the new chunk's
7437c478bd9Sstevel@tonic-gate  * hash chain is noted.  Later, after the allocation is complete, the hash
7447c478bd9Sstevel@tonic-gate  * chain is atomically switched to point to the new element.  If this fails
7457c478bd9Sstevel@tonic-gate  * (because of either concurrent allocations or an allocation concurrent with a
7467c478bd9Sstevel@tonic-gate  * deletion), the newly allocated chunk is deallocated to the dirty list, and
7477c478bd9Sstevel@tonic-gate  * the whole process of looking up (and potentially allocating) the dynamic
7487c478bd9Sstevel@tonic-gate  * variable is reattempted.
7497c478bd9Sstevel@tonic-gate  *
7507c478bd9Sstevel@tonic-gate  * The final race is a simple deallocation race:
7517c478bd9Sstevel@tonic-gate  *
7527c478bd9Sstevel@tonic-gate  *                 CPU A                                 CPU B
7537c478bd9Sstevel@tonic-gate  *  +---------------------------------+   +---------------------------------+
7547c478bd9Sstevel@tonic-gate  *  |                                 |   |                                 |
7557c478bd9Sstevel@tonic-gate  *  | deallocates dynamic object      |   | deallocates dynamic object      |
7567c478bd9Sstevel@tonic-gate  *  | a[123] by storing the value 0   |   | a[123] by storing the value 0   |
7577c478bd9Sstevel@tonic-gate  *  | to it                           |   | to it                           |
7587c478bd9Sstevel@tonic-gate  *  |                                 |   |                                 |
7597c478bd9Sstevel@tonic-gate  *  :                                 :   :                                 :
7607c478bd9Sstevel@tonic-gate  *  .                                 .   .                                 .
7617c478bd9Sstevel@tonic-gate  *
7627c478bd9Sstevel@tonic-gate  * Once again, this is a race in the D program, but it is one that we must
7637c478bd9Sstevel@tonic-gate  * handle without corrupting the underlying data structures.  Because
7647c478bd9Sstevel@tonic-gate  * deallocations require the deletion of a chunk from the middle of a hash
7657c478bd9Sstevel@tonic-gate  * chain, we cannot use a single-word atomic operation to remove it.  For this,
7667c478bd9Sstevel@tonic-gate  * we add a spin lock to the hash buckets that is _only_ used for deallocations
7677c478bd9Sstevel@tonic-gate  * (allocation races are handled as above).  Further, this spin lock is _only_
7687c478bd9Sstevel@tonic-gate  * held for the duration of the delete; before control is returned to the DIF
7697c478bd9Sstevel@tonic-gate  * emulation code, the hash bucket is unlocked.
7707c478bd9Sstevel@tonic-gate  */
7717c478bd9Sstevel@tonic-gate typedef struct dtrace_key {
7727c478bd9Sstevel@tonic-gate 	uint64_t dttk_value;			/* data value or data pointer */
7737c478bd9Sstevel@tonic-gate 	uint64_t dttk_size;			/* 0 if by-val, >0 if by-ref */
7747c478bd9Sstevel@tonic-gate } dtrace_key_t;
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate typedef struct dtrace_tuple {
7777c478bd9Sstevel@tonic-gate 	uint32_t dtt_nkeys;			/* number of keys in tuple */
7787c478bd9Sstevel@tonic-gate 	uint32_t dtt_pad;			/* padding */
7797c478bd9Sstevel@tonic-gate 	dtrace_key_t dtt_key[1];		/* array of tuple keys */
7807c478bd9Sstevel@tonic-gate } dtrace_tuple_t;
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate typedef struct dtrace_dynvar {
7837c478bd9Sstevel@tonic-gate 	uint64_t dtdv_hashval;			/* hash value -- 0 if free */
7847c478bd9Sstevel@tonic-gate 	struct dtrace_dynvar *dtdv_next;	/* next on list or hash chain */
7857c478bd9Sstevel@tonic-gate 	void *dtdv_data;			/* pointer to data */
7867c478bd9Sstevel@tonic-gate 	dtrace_tuple_t dtdv_tuple;		/* tuple key */
7877c478bd9Sstevel@tonic-gate } dtrace_dynvar_t;
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate typedef enum dtrace_dynvar_op {
7907c478bd9Sstevel@tonic-gate 	DTRACE_DYNVAR_ALLOC,
7917c478bd9Sstevel@tonic-gate 	DTRACE_DYNVAR_NOALLOC,
7927c478bd9Sstevel@tonic-gate 	DTRACE_DYNVAR_DEALLOC
7937c478bd9Sstevel@tonic-gate } dtrace_dynvar_op_t;
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate typedef struct dtrace_dynhash {
7967c478bd9Sstevel@tonic-gate 	dtrace_dynvar_t *dtdh_chain;		/* hash chain for this bucket */
7977c478bd9Sstevel@tonic-gate 	uintptr_t dtdh_lock;			/* deallocation lock */
7987c478bd9Sstevel@tonic-gate #ifdef _LP64
7997c478bd9Sstevel@tonic-gate 	uintptr_t dtdh_pad[6];			/* pad to avoid false sharing */
8007c478bd9Sstevel@tonic-gate #else
8017c478bd9Sstevel@tonic-gate 	uintptr_t dtdh_pad[14];			/* pad to avoid false sharing */
8027c478bd9Sstevel@tonic-gate #endif
8037c478bd9Sstevel@tonic-gate } dtrace_dynhash_t;
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate typedef struct dtrace_dstate_percpu {
8067c478bd9Sstevel@tonic-gate 	dtrace_dynvar_t *dtdsc_free;		/* free list for this CPU */
8077c478bd9Sstevel@tonic-gate 	dtrace_dynvar_t *dtdsc_dirty;		/* dirty list for this CPU */
8087c478bd9Sstevel@tonic-gate 	dtrace_dynvar_t *dtdsc_rinsing;		/* rinsing list for this CPU */
8097c478bd9Sstevel@tonic-gate 	dtrace_dynvar_t *dtdsc_clean;		/* clean list for this CPU */
8107c478bd9Sstevel@tonic-gate 	uint64_t dtdsc_drops;			/* number of capacity drops */
8117c478bd9Sstevel@tonic-gate 	uint64_t dtdsc_dirty_drops;		/* number of dirty drops */
8127c478bd9Sstevel@tonic-gate 	uint64_t dtdsc_rinsing_drops;		/* number of rinsing drops */
8137c478bd9Sstevel@tonic-gate #ifdef _LP64
8147c478bd9Sstevel@tonic-gate 	uint64_t dtdsc_pad;			/* pad to avoid false sharing */
8157c478bd9Sstevel@tonic-gate #else
8167c478bd9Sstevel@tonic-gate 	uint64_t dtdsc_pad[2];			/* pad to avoid false sharing */
8177c478bd9Sstevel@tonic-gate #endif
8187c478bd9Sstevel@tonic-gate } dtrace_dstate_percpu_t;
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate typedef enum dtrace_dstate_state {
8217c478bd9Sstevel@tonic-gate 	DTRACE_DSTATE_CLEAN = 0,
8227c478bd9Sstevel@tonic-gate 	DTRACE_DSTATE_EMPTY,
8237c478bd9Sstevel@tonic-gate 	DTRACE_DSTATE_DIRTY,
8247c478bd9Sstevel@tonic-gate 	DTRACE_DSTATE_RINSING
8257c478bd9Sstevel@tonic-gate } dtrace_dstate_state_t;
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate typedef struct dtrace_dstate {
8287c478bd9Sstevel@tonic-gate 	void *dtds_base;			/* base of dynamic var. space */
8297c478bd9Sstevel@tonic-gate 	size_t dtds_size;			/* size of dynamic var. space */
8307c478bd9Sstevel@tonic-gate 	size_t dtds_hashsize;			/* number of buckets in hash */
8317c478bd9Sstevel@tonic-gate 	size_t dtds_chunksize;			/* size of each chunk */
8327c478bd9Sstevel@tonic-gate 	dtrace_dynhash_t *dtds_hash;		/* pointer to hash table */
8337c478bd9Sstevel@tonic-gate 	dtrace_dstate_state_t dtds_state;	/* current dynamic var. state */
8347c478bd9Sstevel@tonic-gate 	dtrace_dstate_percpu_t *dtds_percpu;	/* per-CPU dyn. var. state */
8357c478bd9Sstevel@tonic-gate } dtrace_dstate_t;
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate /*
8387c478bd9Sstevel@tonic-gate  * DTrace Variable State
8397c478bd9Sstevel@tonic-gate  *
8407c478bd9Sstevel@tonic-gate  * The DTrace variable state tracks user-defined variables in its dtrace_vstate
8417c478bd9Sstevel@tonic-gate  * structure.  Each DTrace consumer has exactly one dtrace_vstate structure,
8427c478bd9Sstevel@tonic-gate  * but some dtrace_vstate structures may exist without a corresponding DTrace
8437c478bd9Sstevel@tonic-gate  * consumer (see "DTrace Helpers", below).  As described in <sys/dtrace.h>,
8447c478bd9Sstevel@tonic-gate  * user-defined variables can have one of three scopes:
8457c478bd9Sstevel@tonic-gate  *
8467c478bd9Sstevel@tonic-gate  *  DIFV_SCOPE_GLOBAL  =>  global scope
8477c478bd9Sstevel@tonic-gate  *  DIFV_SCOPE_THREAD  =>  thread-local scope (i.e. "self->" variables)
8487c478bd9Sstevel@tonic-gate  *  DIFV_SCOPE_LOCAL   =>  clause-local scope (i.e. "this->" variables)
8497c478bd9Sstevel@tonic-gate  *
8507c478bd9Sstevel@tonic-gate  * The variable state tracks variables by both their scope and their allocation
8517c478bd9Sstevel@tonic-gate  * type:
8527c478bd9Sstevel@tonic-gate  *
8537c478bd9Sstevel@tonic-gate  *  - The dtvs_globals and dtvs_locals members each point to an array of
8547c478bd9Sstevel@tonic-gate  *    dtrace_statvar structures.  These structures contain both the variable
8557c478bd9Sstevel@tonic-gate  *    metadata (dtrace_difv structures) and the underlying storage for all
8567c478bd9Sstevel@tonic-gate  *    statically allocated variables, including statically allocated
8577c478bd9Sstevel@tonic-gate  *    DIFV_SCOPE_GLOBAL variables and all DIFV_SCOPE_LOCAL variables.
8587c478bd9Sstevel@tonic-gate  *
8597c478bd9Sstevel@tonic-gate  *  - The dtvs_tlocals member points to an array of dtrace_difv structures for
8607c478bd9Sstevel@tonic-gate  *    DIFV_SCOPE_THREAD variables.  As such, this array tracks _only_ the
8617c478bd9Sstevel@tonic-gate  *    variable metadata for DIFV_SCOPE_THREAD variables; the underlying storage
8627c478bd9Sstevel@tonic-gate  *    is allocated out of the dynamic variable space.
8637c478bd9Sstevel@tonic-gate  *
8647c478bd9Sstevel@tonic-gate  *  - The dtvs_dynvars member is the dynamic variable state associated with the
8657c478bd9Sstevel@tonic-gate  *    variable state.  The dynamic variable state (described in "DTrace Dynamic
8667c478bd9Sstevel@tonic-gate  *    Variables", above) tracks all DIFV_SCOPE_THREAD variables and all
8677c478bd9Sstevel@tonic-gate  *    dynamically-allocated DIFV_SCOPE_GLOBAL variables.
8687c478bd9Sstevel@tonic-gate  */
8697c478bd9Sstevel@tonic-gate typedef struct dtrace_statvar {
8707c478bd9Sstevel@tonic-gate 	uint64_t dtsv_data;			/* data or pointer to it */
8717c478bd9Sstevel@tonic-gate 	size_t dtsv_size;			/* size of pointed-to data */
8727c478bd9Sstevel@tonic-gate 	int dtsv_refcnt;			/* reference count */
8737c478bd9Sstevel@tonic-gate 	dtrace_difv_t dtsv_var;			/* variable metadata */
8747c478bd9Sstevel@tonic-gate } dtrace_statvar_t;
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate typedef struct dtrace_vstate {
8777c478bd9Sstevel@tonic-gate 	dtrace_state_t *dtvs_state;		/* back pointer to state */
8787c478bd9Sstevel@tonic-gate 	dtrace_statvar_t **dtvs_globals;	/* statically-allocated glbls */
8797c478bd9Sstevel@tonic-gate 	int dtvs_nglobals;			/* number of globals */
8807c478bd9Sstevel@tonic-gate 	dtrace_difv_t *dtvs_tlocals;		/* thread-local metadata */
8817c478bd9Sstevel@tonic-gate 	int dtvs_ntlocals;			/* number of thread-locals */
8827c478bd9Sstevel@tonic-gate 	dtrace_statvar_t **dtvs_locals;		/* clause-local data */
8837c478bd9Sstevel@tonic-gate 	int dtvs_nlocals;			/* number of clause-locals */
8847c478bd9Sstevel@tonic-gate 	dtrace_dstate_t dtvs_dynvars;		/* dynamic variable state */
8857c478bd9Sstevel@tonic-gate } dtrace_vstate_t;
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate /*
8887c478bd9Sstevel@tonic-gate  * DTrace Machine State
8897c478bd9Sstevel@tonic-gate  *
8907c478bd9Sstevel@tonic-gate  * In the process of processing a fired probe, DTrace needs to track and/or
8917c478bd9Sstevel@tonic-gate  * cache some per-CPU state associated with that particular firing.  This is
8927c478bd9Sstevel@tonic-gate  * state that is always discarded after the probe firing has completed, and
8937c478bd9Sstevel@tonic-gate  * much of it is not specific to any DTrace consumer, remaining valid across
8947c478bd9Sstevel@tonic-gate  * all ECBs.  This state is tracked in the dtrace_mstate structure.
8957c478bd9Sstevel@tonic-gate  */
8967c478bd9Sstevel@tonic-gate #define	DTRACE_MSTATE_ARGS		0x00000001
8977c478bd9Sstevel@tonic-gate #define	DTRACE_MSTATE_PROBE		0x00000002
8987c478bd9Sstevel@tonic-gate #define	DTRACE_MSTATE_EPID		0x00000004
8997c478bd9Sstevel@tonic-gate #define	DTRACE_MSTATE_TIMESTAMP		0x00000008
9007c478bd9Sstevel@tonic-gate #define	DTRACE_MSTATE_STACKDEPTH	0x00000010
9017c478bd9Sstevel@tonic-gate #define	DTRACE_MSTATE_CALLER		0x00000020
9027c478bd9Sstevel@tonic-gate #define	DTRACE_MSTATE_IPL		0x00000040
9037c478bd9Sstevel@tonic-gate #define	DTRACE_MSTATE_FLTOFFS		0x00000080
9047c478bd9Sstevel@tonic-gate #define	DTRACE_MSTATE_WALLTIMESTAMP	0x00000100
9050b38a8bdSahl #define	DTRACE_MSTATE_USTACKDEPTH	0x00000200
906a1b5e537Sbmc #define	DTRACE_MSTATE_UCALLER		0x00000400
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate typedef struct dtrace_mstate {
9097c478bd9Sstevel@tonic-gate 	uintptr_t dtms_scratch_base;		/* base of scratch space */
9107c478bd9Sstevel@tonic-gate 	uintptr_t dtms_scratch_ptr;		/* current scratch pointer */
9117c478bd9Sstevel@tonic-gate 	size_t dtms_scratch_size;		/* scratch size */
9127c478bd9Sstevel@tonic-gate 	uint32_t dtms_present;			/* variables that are present */
9137c478bd9Sstevel@tonic-gate 	uint64_t dtms_arg[5];			/* cached arguments */
9147c478bd9Sstevel@tonic-gate 	dtrace_epid_t dtms_epid;		/* current EPID */
9157c478bd9Sstevel@tonic-gate 	uint64_t dtms_timestamp;		/* cached timestamp */
9167c478bd9Sstevel@tonic-gate 	hrtime_t dtms_walltimestamp;		/* cached wall timestamp */
9177c478bd9Sstevel@tonic-gate 	int dtms_stackdepth;			/* cached stackdepth */
9180b38a8bdSahl 	int dtms_ustackdepth;			/* cached ustackdepth */
9197c478bd9Sstevel@tonic-gate 	struct dtrace_probe *dtms_probe;	/* current probe */
9207c478bd9Sstevel@tonic-gate 	uintptr_t dtms_caller;			/* cached caller */
921a1b5e537Sbmc 	uint64_t dtms_ucaller;			/* cached user-level caller */
9227c478bd9Sstevel@tonic-gate 	int dtms_ipl;				/* cached interrupt pri lev */
9237c478bd9Sstevel@tonic-gate 	int dtms_fltoffs;			/* faulting DIFO offset */
9247c478bd9Sstevel@tonic-gate 	uintptr_t dtms_strtok;			/* saved strtok() pointer */
925771e39c3SPatrick Mooney 	uintptr_t dtms_strtok_limit;		/* upper bound of strtok ptr */
926e0aad1e0Sdp 	uint32_t dtms_access;			/* memory access rights */
927e0aad1e0Sdp 	dtrace_difo_t *dtms_difo;		/* current dif object */
928b0f673c4SBryan Cantrill 	file_t *dtms_getf;			/* cached rval of getf() */
9297c478bd9Sstevel@tonic-gate } dtrace_mstate_t;
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate #define	DTRACE_COND_OWNER	0x1
9327c478bd9Sstevel@tonic-gate #define	DTRACE_COND_USERMODE	0x2
933ad4023c4Sdp #define	DTRACE_COND_ZONEOWNER	0x4
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate #define	DTRACE_PROBEKEY_MAXDEPTH	8	/* max glob recursion depth */
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate /*
938e0aad1e0Sdp  * Access flag used by dtrace_mstate.dtms_access.
939e0aad1e0Sdp  */
940e0aad1e0Sdp #define	DTRACE_ACCESS_KERNEL	0x1		/* the priv to read kmem */
9417d5c9b5fSBryan Cantrill #define	DTRACE_ACCESS_PROC	0x2		/* the priv for proc state */
9427d5c9b5fSBryan Cantrill #define	DTRACE_ACCESS_ARGS	0x4		/* the priv to examine args */
943e0aad1e0Sdp 
944e0aad1e0Sdp /*
9457c478bd9Sstevel@tonic-gate  * DTrace Activity
9467c478bd9Sstevel@tonic-gate  *
9477c478bd9Sstevel@tonic-gate  * Each DTrace consumer is in one of several states, which (for purposes of
9487c478bd9Sstevel@tonic-gate  * avoiding yet-another overloading of the noun "state") we call the current
9497c478bd9Sstevel@tonic-gate  * _activity_.  The activity transitions on dtrace_go() (from DTRACIOCGO), on
9507c478bd9Sstevel@tonic-gate  * dtrace_stop() (from DTRACIOCSTOP) and on the exit() action.  Activities may
9517c478bd9Sstevel@tonic-gate  * only transition in one direction; the activity transition diagram is a
9527c478bd9Sstevel@tonic-gate  * directed acyclic graph.  The activity transition diagram is as follows:
9537c478bd9Sstevel@tonic-gate  *
9547c478bd9Sstevel@tonic-gate  *
9557c478bd9Sstevel@tonic-gate  * +----------+                   +--------+                   +--------+
9567c478bd9Sstevel@tonic-gate  * | INACTIVE |------------------>| WARMUP |------------------>| ACTIVE |
9577c478bd9Sstevel@tonic-gate  * +----------+   dtrace_go(),    +--------+   dtrace_go(),    +--------+
9587c478bd9Sstevel@tonic-gate  *                before BEGIN        |        after BEGIN       |  |  |
9597c478bd9Sstevel@tonic-gate  *                                    |                          |  |  |
9607c478bd9Sstevel@tonic-gate  *                      exit() action |                          |  |  |
9617c478bd9Sstevel@tonic-gate  *                     from BEGIN ECB |                          |  |  |
9627c478bd9Sstevel@tonic-gate  *                                    |                          |  |  |
9637c478bd9Sstevel@tonic-gate  *                                    v                          |  |  |
9647c478bd9Sstevel@tonic-gate  *                               +----------+     exit() action  |  |  |
965586d07d0Sbmc  * +-----------------------------| DRAINING |<-------------------+  |  |
966586d07d0Sbmc  * |                             +----------+                       |  |
967586d07d0Sbmc  * |                                  |                             |  |
968586d07d0Sbmc  * |                   dtrace_stop(), |                             |  |
969586d07d0Sbmc  * |                     before END   |                             |  |
970586d07d0Sbmc  * |                                  |                             |  |
971586d07d0Sbmc  * |                                  v                             |  |
972586d07d0Sbmc  * | +---------+                 +----------+                       |  |
973586d07d0Sbmc  * | | STOPPED |<----------------| COOLDOWN |<----------------------+  |
974586d07d0Sbmc  * | +---------+  dtrace_stop(), +----------+     dtrace_stop(),       |
975586d07d0Sbmc  * |                after END                       before END         |
976586d07d0Sbmc  * |                                                                   |
977586d07d0Sbmc  * |                              +--------+                           |
978586d07d0Sbmc  * +----------------------------->| KILLED |<--------------------------+
979586d07d0Sbmc  *       deadman timeout or       +--------+     deadman timeout or
980586d07d0Sbmc  *        killed consumer                         killed consumer
9817c478bd9Sstevel@tonic-gate  *
9827c478bd9Sstevel@tonic-gate  * Note that once a DTrace consumer has stopped tracing, there is no way to
9837c478bd9Sstevel@tonic-gate  * restart it; if a DTrace consumer wishes to restart tracing, it must reopen
9847c478bd9Sstevel@tonic-gate  * the DTrace pseudodevice.
9857c478bd9Sstevel@tonic-gate  */
9867c478bd9Sstevel@tonic-gate typedef enum dtrace_activity {
9877c478bd9Sstevel@tonic-gate 	DTRACE_ACTIVITY_INACTIVE = 0,		/* not yet running */
9887c478bd9Sstevel@tonic-gate 	DTRACE_ACTIVITY_WARMUP,			/* while starting */
9897c478bd9Sstevel@tonic-gate 	DTRACE_ACTIVITY_ACTIVE,			/* running */
9907c478bd9Sstevel@tonic-gate 	DTRACE_ACTIVITY_DRAINING,		/* before stopping */
9917c478bd9Sstevel@tonic-gate 	DTRACE_ACTIVITY_COOLDOWN,		/* while stopping */
9927c478bd9Sstevel@tonic-gate 	DTRACE_ACTIVITY_STOPPED,		/* after stopping */
993586d07d0Sbmc 	DTRACE_ACTIVITY_KILLED			/* killed */
9947c478bd9Sstevel@tonic-gate } dtrace_activity_t;
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate /*
9977c478bd9Sstevel@tonic-gate  * DTrace Helper Implementation
9987c478bd9Sstevel@tonic-gate  *
9997c478bd9Sstevel@tonic-gate  * A description of the helper architecture may be found in <sys/dtrace.h>.
10007c478bd9Sstevel@tonic-gate  * Each process contains a pointer to its helpers in its p_dtrace_helpers
10017c478bd9Sstevel@tonic-gate  * member.  This is a pointer to a dtrace_helpers structure, which contains an
10027c478bd9Sstevel@tonic-gate  * array of pointers to dtrace_helper structures, helper variable state (shared
10037c478bd9Sstevel@tonic-gate  * among a process's helpers) and a generation count.  (The generation count is
10047c478bd9Sstevel@tonic-gate  * used to provide an identifier when a helper is added so that it may be
10057c478bd9Sstevel@tonic-gate  * subsequently removed.)  The dtrace_helper structure is self-explanatory,
10067c478bd9Sstevel@tonic-gate  * containing pointers to the objects needed to execute the helper.  Note that
10077c478bd9Sstevel@tonic-gate  * helpers are _duplicated_ across fork(2), and destroyed on exec(2).  No more
10087c478bd9Sstevel@tonic-gate  * than dtrace_helpers_max are allowed per-process.
10097c478bd9Sstevel@tonic-gate  */
10107c478bd9Sstevel@tonic-gate #define	DTRACE_HELPER_ACTION_USTACK	0
10117c478bd9Sstevel@tonic-gate #define	DTRACE_NHELPER_ACTIONS		1
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate typedef struct dtrace_helper_action {
101499fd1a49Sahl 	int dtha_generation;			/* helper action generation */
101599fd1a49Sahl 	int dtha_nactions;			/* number of actions */
101699fd1a49Sahl 	dtrace_difo_t *dtha_predicate;		/* helper action predicate */
101799fd1a49Sahl 	dtrace_difo_t **dtha_actions;		/* array of actions */
101899fd1a49Sahl 	struct dtrace_helper_action *dtha_next;	/* next helper action */
10197c478bd9Sstevel@tonic-gate } dtrace_helper_action_t;
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate typedef struct dtrace_helper_provider {
102299fd1a49Sahl 	int dthp_generation;			/* helper provider generation */
10237c478bd9Sstevel@tonic-gate 	uint32_t dthp_ref;			/* reference count */
102499fd1a49Sahl 	dof_helper_t dthp_prov;			/* DOF w/ provider and probes */
10257c478bd9Sstevel@tonic-gate } dtrace_helper_provider_t;
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate typedef struct dtrace_helpers {
10287c478bd9Sstevel@tonic-gate 	dtrace_helper_action_t **dthps_actions;	/* array of helper actions */
10297c478bd9Sstevel@tonic-gate 	dtrace_vstate_t dthps_vstate;		/* helper action var. state */
10307c478bd9Sstevel@tonic-gate 	dtrace_helper_provider_t **dthps_provs;	/* array of providers */
10317c478bd9Sstevel@tonic-gate 	uint_t dthps_nprovs;			/* count of providers */
103299fd1a49Sahl 	uint_t dthps_maxprovs;			/* provider array size */
10337c478bd9Sstevel@tonic-gate 	int dthps_generation;			/* current generation */
10347c478bd9Sstevel@tonic-gate 	pid_t dthps_pid;			/* pid of associated proc */
1035dafb5540Sahl 	int dthps_deferred;			/* helper in deferred list */
10367c478bd9Sstevel@tonic-gate 	struct dtrace_helpers *dthps_next;	/* next pointer */
10377c478bd9Sstevel@tonic-gate 	struct dtrace_helpers *dthps_prev;	/* prev pointer */
10387c478bd9Sstevel@tonic-gate } dtrace_helpers_t;
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate /*
10417c478bd9Sstevel@tonic-gate  * DTrace Helper Action Tracing
10427c478bd9Sstevel@tonic-gate  *
10437c478bd9Sstevel@tonic-gate  * Debugging helper actions can be arduous.  To ease the development and
10447c478bd9Sstevel@tonic-gate  * debugging of helpers, DTrace contains a tracing-framework-within-a-tracing-
10457c478bd9Sstevel@tonic-gate  * framework: helper tracing.  If dtrace_helptrace_enabled is non-zero (which
10467c478bd9Sstevel@tonic-gate  * it is by default on DEBUG kernels), all helper activity will be traced to a
10477c478bd9Sstevel@tonic-gate  * global, in-kernel ring buffer.  Each entry includes a pointer to the specific
10487c478bd9Sstevel@tonic-gate  * helper, the location within the helper, and a trace of all local variables.
10497c478bd9Sstevel@tonic-gate  * The ring buffer may be displayed in a human-readable format with the
10507c478bd9Sstevel@tonic-gate  * ::dtrace_helptrace mdb(1) dcmd.
10517c478bd9Sstevel@tonic-gate  */
10527c478bd9Sstevel@tonic-gate #define	DTRACE_HELPTRACE_NEXT	(-1)
10537c478bd9Sstevel@tonic-gate #define	DTRACE_HELPTRACE_DONE	(-2)
10547c478bd9Sstevel@tonic-gate #define	DTRACE_HELPTRACE_ERR	(-3)
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate typedef struct dtrace_helptrace {
10577c478bd9Sstevel@tonic-gate 	dtrace_helper_action_t	*dtht_helper;	/* helper action */
10587c478bd9Sstevel@tonic-gate 	int dtht_where;				/* where in helper action */
10597c478bd9Sstevel@tonic-gate 	int dtht_nlocals;			/* number of locals */
1060187eccf8Sbmc 	int dtht_fault;				/* type of fault (if any) */
1061187eccf8Sbmc 	int dtht_fltoffs;			/* DIF offset */
1062187eccf8Sbmc 	uint64_t dtht_illval;			/* faulting value */
10637c478bd9Sstevel@tonic-gate 	uint64_t dtht_locals[1];		/* local variables */
10647c478bd9Sstevel@tonic-gate } dtrace_helptrace_t;
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate /*
10677c478bd9Sstevel@tonic-gate  * DTrace Credentials
10687c478bd9Sstevel@tonic-gate  *
1069ad4023c4Sdp  * In probe context, we have limited flexibility to examine the credentials
1070ad4023c4Sdp  * of the DTrace consumer that created a particular enabling.  We use
1071ad4023c4Sdp  * the Least Privilege interfaces to cache the consumer's cred pointer and
1072ad4023c4Sdp  * some facts about that credential in a dtrace_cred_t structure. These
1073ad4023c4Sdp  * can limit the consumer's breadth of visibility and what actions the
1074ad4023c4Sdp  * consumer may take.
10757c478bd9Sstevel@tonic-gate  */
10767c478bd9Sstevel@tonic-gate #define	DTRACE_CRV_ALLPROC		0x01
10777c478bd9Sstevel@tonic-gate #define	DTRACE_CRV_KERNEL		0x02
1078ad4023c4Sdp #define	DTRACE_CRV_ALLZONE		0x04
10797c478bd9Sstevel@tonic-gate 
1080ad4023c4Sdp #define	DTRACE_CRV_ALL		(DTRACE_CRV_ALLPROC | DTRACE_CRV_KERNEL | \
1081ad4023c4Sdp 	DTRACE_CRV_ALLZONE)
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate #define	DTRACE_CRA_PROC				0x0001
1084ad4023c4Sdp #define	DTRACE_CRA_PROC_CONTROL			0x0002
1085ad4023c4Sdp #define	DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER	0x0004
1086ad4023c4Sdp #define	DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE	0x0008
1087ad4023c4Sdp #define	DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG	0x0010
1088ad4023c4Sdp #define	DTRACE_CRA_KERNEL			0x0020
1089ad4023c4Sdp #define	DTRACE_CRA_KERNEL_DESTRUCTIVE		0x0040
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate #define	DTRACE_CRA_ALL		(DTRACE_CRA_PROC | \
1092ad4023c4Sdp 	DTRACE_CRA_PROC_CONTROL | \
1093ad4023c4Sdp 	DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER | \
1094ad4023c4Sdp 	DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE | \
1095ad4023c4Sdp 	DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG | \
1096ad4023c4Sdp 	DTRACE_CRA_KERNEL | \
1097ad4023c4Sdp 	DTRACE_CRA_KERNEL_DESTRUCTIVE)
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate typedef struct dtrace_cred {
1100ad4023c4Sdp 	cred_t			*dcr_cred;
11017c478bd9Sstevel@tonic-gate 	uint8_t			dcr_destructive;
11027c478bd9Sstevel@tonic-gate 	uint8_t			dcr_visible;
11037c478bd9Sstevel@tonic-gate 	uint16_t		dcr_action;
11047c478bd9Sstevel@tonic-gate } dtrace_cred_t;
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate /*
11077c478bd9Sstevel@tonic-gate  * DTrace Consumer State
11087c478bd9Sstevel@tonic-gate  *
11097c478bd9Sstevel@tonic-gate  * Each DTrace consumer has an associated dtrace_state structure that contains
11107c478bd9Sstevel@tonic-gate  * its in-kernel DTrace state -- including options, credentials, statistics and
11117c478bd9Sstevel@tonic-gate  * pointers to ECBs, buffers, speculations and formats.  A dtrace_state
11127c478bd9Sstevel@tonic-gate  * structure is also allocated for anonymous enablings.  When anonymous state
11137c478bd9Sstevel@tonic-gate  * is grabbed, the grabbing consumers dts_anon pointer is set to the grabbed
11147c478bd9Sstevel@tonic-gate  * dtrace_state structure.
11157c478bd9Sstevel@tonic-gate  */
11167c478bd9Sstevel@tonic-gate struct dtrace_state {
11177c478bd9Sstevel@tonic-gate 	dev_t dts_dev;				/* device */
11187c478bd9Sstevel@tonic-gate 	int dts_necbs;				/* total number of ECBs */
11197c478bd9Sstevel@tonic-gate 	dtrace_ecb_t **dts_ecbs;		/* array of ECBs */
11207c478bd9Sstevel@tonic-gate 	dtrace_epid_t dts_epid;			/* next EPID to allocate */
11217c478bd9Sstevel@tonic-gate 	size_t dts_needed;			/* greatest needed space */
11227c478bd9Sstevel@tonic-gate 	struct dtrace_state *dts_anon;		/* anon. state, if grabbed */
11237c478bd9Sstevel@tonic-gate 	dtrace_activity_t dts_activity;		/* current activity */
11247c478bd9Sstevel@tonic-gate 	dtrace_vstate_t dts_vstate;		/* variable state */
11257c478bd9Sstevel@tonic-gate 	dtrace_buffer_t *dts_buffer;		/* principal buffer */
11267c478bd9Sstevel@tonic-gate 	dtrace_buffer_t *dts_aggbuffer;		/* aggregation buffer */
11277c478bd9Sstevel@tonic-gate 	dtrace_speculation_t *dts_speculations;	/* speculation array */
11287c478bd9Sstevel@tonic-gate 	int dts_nspeculations;			/* number of speculations */
11297c478bd9Sstevel@tonic-gate 	int dts_naggregations;			/* number of aggregations */
11307c478bd9Sstevel@tonic-gate 	dtrace_aggregation_t **dts_aggregations; /* aggregation array */
11317c478bd9Sstevel@tonic-gate 	vmem_t *dts_aggid_arena;		/* arena for aggregation IDs */
1132a1b5e537Sbmc 	uint64_t dts_errors;			/* total number of errors */
11337c478bd9Sstevel@tonic-gate 	uint32_t dts_speculations_busy;		/* number of spec. busy */
11347c478bd9Sstevel@tonic-gate 	uint32_t dts_speculations_unavail;	/* number of spec unavail */
1135a1b5e537Sbmc 	uint32_t dts_stkstroverflows;		/* stack string tab overflows */
1136a1b5e537Sbmc 	uint32_t dts_dblerrors;			/* errors in ERROR probes */
11377c478bd9Sstevel@tonic-gate 	uint32_t dts_reserve;			/* space reserved for END */
11387c478bd9Sstevel@tonic-gate 	hrtime_t dts_laststatus;		/* time of last status */
11397c478bd9Sstevel@tonic-gate 	cyclic_id_t dts_cleaner;		/* cleaning cyclic */
11407c478bd9Sstevel@tonic-gate 	cyclic_id_t dts_deadman;		/* deadman cyclic */
11417c478bd9Sstevel@tonic-gate 	hrtime_t dts_alive;			/* time last alive */
11427c478bd9Sstevel@tonic-gate 	char dts_speculates;			/* boolean: has speculations */
11437c478bd9Sstevel@tonic-gate 	char dts_destructive;			/* boolean: has dest. actions */
11447c478bd9Sstevel@tonic-gate 	int dts_nformats;			/* number of formats */
11457c478bd9Sstevel@tonic-gate 	char **dts_formats;			/* format string array */
11467c478bd9Sstevel@tonic-gate 	dtrace_optval_t dts_options[DTRACEOPT_MAX]; /* options */
11477c478bd9Sstevel@tonic-gate 	dtrace_cred_t dts_cred;			/* credentials */
11487c478bd9Sstevel@tonic-gate 	size_t dts_nretained;			/* number of retained enabs */
1149b0f673c4SBryan Cantrill 	int dts_getf;				/* number of getf() calls */
11507c478bd9Sstevel@tonic-gate };
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate struct dtrace_provider {
11537c478bd9Sstevel@tonic-gate 	dtrace_pattr_t dtpv_attr;		/* provider attributes */
11547c478bd9Sstevel@tonic-gate 	dtrace_ppriv_t dtpv_priv;		/* provider privileges */
11557c478bd9Sstevel@tonic-gate 	dtrace_pops_t dtpv_pops;		/* provider operations */
11567c478bd9Sstevel@tonic-gate 	char *dtpv_name;			/* provider name */
11577c478bd9Sstevel@tonic-gate 	void *dtpv_arg;				/* provider argument */
1158f484800dSBryan Cantrill 	hrtime_t dtpv_defunct;			/* when made defunct */
11597c478bd9Sstevel@tonic-gate 	struct dtrace_provider *dtpv_next;	/* next provider */
11607c478bd9Sstevel@tonic-gate };
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate struct dtrace_meta {
11637c478bd9Sstevel@tonic-gate 	dtrace_mops_t dtm_mops;			/* meta provider operations */
11647c478bd9Sstevel@tonic-gate 	char *dtm_name;				/* meta provider name */
11657c478bd9Sstevel@tonic-gate 	void *dtm_arg;				/* meta provider user arg */
11667c478bd9Sstevel@tonic-gate 	uint64_t dtm_count;			/* no. of associated provs. */
11677c478bd9Sstevel@tonic-gate };
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate /*
11707c478bd9Sstevel@tonic-gate  * DTrace Enablings
11717c478bd9Sstevel@tonic-gate  *
11727c478bd9Sstevel@tonic-gate  * A dtrace_enabling structure is used to track a collection of ECB
11737c478bd9Sstevel@tonic-gate  * descriptions -- before they have been turned into actual ECBs.  This is
11747c478bd9Sstevel@tonic-gate  * created as a result of DOF processing, and is generally used to generate
11757c478bd9Sstevel@tonic-gate  * ECBs immediately thereafter.  However, enablings are also generally
11767c478bd9Sstevel@tonic-gate  * retained should the probes they describe be created at a later time; as
11777c478bd9Sstevel@tonic-gate  * each new module or provider registers with the framework, the retained
11787c478bd9Sstevel@tonic-gate  * enablings are reevaluated, with any new match resulting in new ECBs.  To
11797c478bd9Sstevel@tonic-gate  * prevent probes from being matched more than once, the enabling tracks the
11807c478bd9Sstevel@tonic-gate  * last probe generation matched, and only matches probes from subsequent
11817c478bd9Sstevel@tonic-gate  * generations.
11827c478bd9Sstevel@tonic-gate  */
11837c478bd9Sstevel@tonic-gate typedef struct dtrace_enabling {
11847c478bd9Sstevel@tonic-gate 	dtrace_ecbdesc_t **dten_desc;		/* all ECB descriptions */
11857c478bd9Sstevel@tonic-gate 	int dten_ndesc;				/* number of ECB descriptions */
11867c478bd9Sstevel@tonic-gate 	int dten_maxdesc;			/* size of ECB array */
11877c478bd9Sstevel@tonic-gate 	dtrace_vstate_t *dten_vstate;		/* associated variable state */
11887c478bd9Sstevel@tonic-gate 	dtrace_genid_t dten_probegen;		/* matched probe generation */
11897c478bd9Sstevel@tonic-gate 	dtrace_ecbdesc_t *dten_current;		/* current ECB description */
11907c478bd9Sstevel@tonic-gate 	int dten_error;				/* current error value */
11917c478bd9Sstevel@tonic-gate 	int dten_primed;			/* boolean: set if primed */
11927c478bd9Sstevel@tonic-gate 	struct dtrace_enabling *dten_prev;	/* previous enabling */
11937c478bd9Sstevel@tonic-gate 	struct dtrace_enabling *dten_next;	/* next enabling */
11947c478bd9Sstevel@tonic-gate } dtrace_enabling_t;
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate /*
11977c478bd9Sstevel@tonic-gate  * DTrace Anonymous Enablings
11987c478bd9Sstevel@tonic-gate  *
11997c478bd9Sstevel@tonic-gate  * Anonymous enablings are DTrace enablings that are not associated with a
12007c478bd9Sstevel@tonic-gate  * controlling process, but rather derive their enabling from DOF stored as
12017c478bd9Sstevel@tonic-gate  * properties in the dtrace.conf file.  If there is an anonymous enabling, a
12027c478bd9Sstevel@tonic-gate  * DTrace consumer state and enabling are created on attach.  The state may be
12037c478bd9Sstevel@tonic-gate  * subsequently grabbed by the first consumer specifying the "grabanon"
1204*bbf21555SRichard Lowe  * option.  As long as an anonymous DTrace enabling exists, dtrace(4D) will
12057c478bd9Sstevel@tonic-gate  * refuse to unload.
12067c478bd9Sstevel@tonic-gate  */
12077c478bd9Sstevel@tonic-gate typedef struct dtrace_anon {
12087c478bd9Sstevel@tonic-gate 	dtrace_state_t *dta_state;		/* DTrace consumer state */
12097c478bd9Sstevel@tonic-gate 	dtrace_enabling_t *dta_enabling;	/* pointer to enabling */
12107c478bd9Sstevel@tonic-gate 	processorid_t dta_beganon;		/* which CPU BEGIN ran on */
12117c478bd9Sstevel@tonic-gate } dtrace_anon_t;
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate /*
12147c478bd9Sstevel@tonic-gate  * DTrace Error Debugging
12157c478bd9Sstevel@tonic-gate  */
12167c478bd9Sstevel@tonic-gate #ifdef DEBUG
12177c478bd9Sstevel@tonic-gate #define	DTRACE_ERRDEBUG
12187c478bd9Sstevel@tonic-gate #endif
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate #ifdef DTRACE_ERRDEBUG
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate typedef struct dtrace_errhash {
12237c478bd9Sstevel@tonic-gate 	const char	*dter_msg;	/* error message */
12247c478bd9Sstevel@tonic-gate 	int		dter_count;	/* number of times seen */
12257c478bd9Sstevel@tonic-gate } dtrace_errhash_t;
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate #define	DTRACE_ERRHASHSZ	256	/* must be > number of err msgs */
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate #endif	/* DTRACE_ERRDEBUG */
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate /*
12327c478bd9Sstevel@tonic-gate  * DTrace Toxic Ranges
12337c478bd9Sstevel@tonic-gate  *
12347c478bd9Sstevel@tonic-gate  * DTrace supports safe loads from probe context; if the address turns out to
12357c478bd9Sstevel@tonic-gate  * be invalid, a bit will be set by the kernel indicating that DTrace
12367c478bd9Sstevel@tonic-gate  * encountered a memory error, and DTrace will propagate the error to the user
12377c478bd9Sstevel@tonic-gate  * accordingly.  However, there may exist some regions of memory in which an
12387c478bd9Sstevel@tonic-gate  * arbitrary load can change system state, and from which it is impossible to
12397c478bd9Sstevel@tonic-gate  * recover from such a load after it has been attempted.  Examples of this may
12407c478bd9Sstevel@tonic-gate  * include memory in which programmable I/O registers are mapped (for which a
12417c478bd9Sstevel@tonic-gate  * read may have some implications for the device) or (in the specific case of
12427c478bd9Sstevel@tonic-gate  * UltraSPARC-I and -II) the virtual address hole.  The platform is required
12437c478bd9Sstevel@tonic-gate  * to make DTrace aware of these toxic ranges; DTrace will then check that
12447c478bd9Sstevel@tonic-gate  * target addresses are not in a toxic range before attempting to issue a
12457c478bd9Sstevel@tonic-gate  * safe load.
12467c478bd9Sstevel@tonic-gate  */
12477c478bd9Sstevel@tonic-gate typedef struct dtrace_toxrange {
12487c478bd9Sstevel@tonic-gate 	uintptr_t	dtt_base;		/* base of toxic range */
12497c478bd9Sstevel@tonic-gate 	uintptr_t	dtt_limit;		/* limit of toxic range */
12507c478bd9Sstevel@tonic-gate } dtrace_toxrange_t;
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate extern uint64_t dtrace_getarg(int, int);
12537c478bd9Sstevel@tonic-gate extern greg_t dtrace_getfp(void);
12547c478bd9Sstevel@tonic-gate extern int dtrace_getipl(void);
12557c478bd9Sstevel@tonic-gate extern uintptr_t dtrace_caller(int);
12567c478bd9Sstevel@tonic-gate extern uint32_t dtrace_cas32(uint32_t *, uint32_t, uint32_t);
12577c478bd9Sstevel@tonic-gate extern void *dtrace_casptr(void *, void *, void *);
125828406508Ssudheer extern void dtrace_copyin(uintptr_t, uintptr_t, size_t, volatile uint16_t *);
125928406508Ssudheer extern void dtrace_copyinstr(uintptr_t, uintptr_t, size_t, volatile uint16_t *);
126028406508Ssudheer extern void dtrace_copyout(uintptr_t, uintptr_t, size_t, volatile uint16_t *);
126128406508Ssudheer extern void dtrace_copyoutstr(uintptr_t, uintptr_t, size_t,
126228406508Ssudheer     volatile uint16_t *);
12637c478bd9Sstevel@tonic-gate extern void dtrace_getpcstack(pc_t *, int, int, uint32_t *);
12647c478bd9Sstevel@tonic-gate extern ulong_t dtrace_getreg(struct regs *, uint_t);
126556a20711SPatrick Mooney extern void dtrace_setreg(struct regs *, uint_t, ulong_t);
12667aa76ffcSBryan Cantrill extern uint64_t dtrace_getvmreg(uint_t, volatile uint16_t *);
12677c478bd9Sstevel@tonic-gate extern int dtrace_getstackdepth(int);
12687c478bd9Sstevel@tonic-gate extern void dtrace_getupcstack(uint64_t *, int);
12697c478bd9Sstevel@tonic-gate extern void dtrace_getufpstack(uint64_t *, uint64_t *, int);
12700b38a8bdSahl extern int dtrace_getustackdepth(void);
12717c478bd9Sstevel@tonic-gate extern uintptr_t dtrace_fulword(void *);
12727c478bd9Sstevel@tonic-gate extern uint8_t dtrace_fuword8(void *);
12737c478bd9Sstevel@tonic-gate extern uint16_t dtrace_fuword16(void *);
12747c478bd9Sstevel@tonic-gate extern uint32_t dtrace_fuword32(void *);
12757c478bd9Sstevel@tonic-gate extern uint64_t dtrace_fuword64(void *);
12767c478bd9Sstevel@tonic-gate extern void dtrace_probe_error(dtrace_state_t *, dtrace_epid_t, int, int,
12777c478bd9Sstevel@tonic-gate     int, uintptr_t);
12787c478bd9Sstevel@tonic-gate extern int dtrace_assfail(const char *, const char *, int);
12797c478bd9Sstevel@tonic-gate extern int dtrace_attached(void);
12807c478bd9Sstevel@tonic-gate extern hrtime_t dtrace_gethrestime();
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate #ifdef __sparc
12837c478bd9Sstevel@tonic-gate extern void dtrace_flush_windows(void);
12847c478bd9Sstevel@tonic-gate extern void dtrace_flush_user_windows(void);
12857c478bd9Sstevel@tonic-gate extern uint_t dtrace_getotherwin(void);
12867c478bd9Sstevel@tonic-gate extern uint_t dtrace_getfprs(void);
12877c478bd9Sstevel@tonic-gate #else
12887c478bd9Sstevel@tonic-gate extern void dtrace_copy(uintptr_t, uintptr_t, size_t);
128928406508Ssudheer extern void dtrace_copystr(uintptr_t, uintptr_t, size_t, volatile uint16_t *);
12907c478bd9Sstevel@tonic-gate #endif
12917c478bd9Sstevel@tonic-gate 
12927c478bd9Sstevel@tonic-gate /*
12937c478bd9Sstevel@tonic-gate  * DTrace Assertions
12947c478bd9Sstevel@tonic-gate  *
1295395c7a3dSBryan Cantrill  * DTrace calls ASSERT and VERIFY from probe context.  To assure that a failed
1296395c7a3dSBryan Cantrill  * ASSERT or VERIFY does not induce a markedly more catastrophic failure (e.g.,
1297395c7a3dSBryan Cantrill  * one from which a dump cannot be gleaned), DTrace must define its own ASSERT
1298395c7a3dSBryan Cantrill  * and VERIFY macros to be ones that may safely be called from probe context.
1299395c7a3dSBryan Cantrill  * This header file must thus be included by any DTrace component that calls
1300395c7a3dSBryan Cantrill  * ASSERT and/or VERIFY from probe context, and _only_ by those components.
1301395c7a3dSBryan Cantrill  * (The only exception to this is kernel debugging infrastructure at user-level
1302395c7a3dSBryan Cantrill  * that doesn't depend on calling ASSERT.)
13037c478bd9Sstevel@tonic-gate  */
13047c478bd9Sstevel@tonic-gate #undef ASSERT
1305395c7a3dSBryan Cantrill #undef VERIFY
1306395c7a3dSBryan Cantrill #define	VERIFY(EX)	((void)((EX) || \
1307395c7a3dSBryan Cantrill 			dtrace_assfail(#EX, __FILE__, __LINE__)))
13087c478bd9Sstevel@tonic-gate #ifdef DEBUG
13097c478bd9Sstevel@tonic-gate #define	ASSERT(EX)	((void)((EX) || \
13107c478bd9Sstevel@tonic-gate 			dtrace_assfail(#EX, __FILE__, __LINE__)))
13117c478bd9Sstevel@tonic-gate #else
13127c478bd9Sstevel@tonic-gate #define	ASSERT(X)	((void)0)
13137c478bd9Sstevel@tonic-gate #endif
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
13167c478bd9Sstevel@tonic-gate }
13177c478bd9Sstevel@tonic-gate #endif
13187c478bd9Sstevel@tonic-gate 
13197c478bd9Sstevel@tonic-gate #endif /* _SYS_DTRACE_IMPL_H */
1320