xref: /onnv-gate/usr/src/uts/common/sys/dtrace_impl.h (revision 3677:b34b45f03271)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51677Sdp  * Common Development and Distribution License (the "License").
61677Sdp  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
212021Sahl 
220Sstevel@tonic-gate /*
23*3677Ssudheer  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #ifndef _SYS_DTRACE_IMPL_H
280Sstevel@tonic-gate #define	_SYS_DTRACE_IMPL_H
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #ifdef	__cplusplus
330Sstevel@tonic-gate extern "C" {
340Sstevel@tonic-gate #endif
350Sstevel@tonic-gate 
360Sstevel@tonic-gate /*
370Sstevel@tonic-gate  * DTrace Dynamic Tracing Software: Kernel Implementation Interfaces
380Sstevel@tonic-gate  *
390Sstevel@tonic-gate  * Note: The contents of this file are private to the implementation of the
400Sstevel@tonic-gate  * Solaris system and DTrace subsystem and are subject to change at any time
410Sstevel@tonic-gate  * without notice.  Applications and drivers using these interfaces will fail
420Sstevel@tonic-gate  * to run on future releases.  These interfaces should not be used for any
430Sstevel@tonic-gate  * purpose except those expressly outlined in dtrace(7D) and libdtrace(3LIB).
440Sstevel@tonic-gate  * Please refer to the "Solaris Dynamic Tracing Guide" for more information.
450Sstevel@tonic-gate  */
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #include <sys/dtrace.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate  * DTrace Implementation Constants and Typedefs
510Sstevel@tonic-gate  */
520Sstevel@tonic-gate #define	DTRACE_MAXPROPLEN		128
530Sstevel@tonic-gate #define	DTRACE_DYNVAR_CHUNKSIZE		256
540Sstevel@tonic-gate 
550Sstevel@tonic-gate struct dtrace_probe;
560Sstevel@tonic-gate struct dtrace_ecb;
570Sstevel@tonic-gate struct dtrace_predicate;
580Sstevel@tonic-gate struct dtrace_action;
590Sstevel@tonic-gate struct dtrace_provider;
600Sstevel@tonic-gate struct dtrace_state;
610Sstevel@tonic-gate 
620Sstevel@tonic-gate typedef struct dtrace_probe dtrace_probe_t;
630Sstevel@tonic-gate typedef struct dtrace_ecb dtrace_ecb_t;
640Sstevel@tonic-gate typedef struct dtrace_predicate dtrace_predicate_t;
650Sstevel@tonic-gate typedef struct dtrace_action dtrace_action_t;
660Sstevel@tonic-gate typedef struct dtrace_provider dtrace_provider_t;
670Sstevel@tonic-gate typedef struct dtrace_meta dtrace_meta_t;
680Sstevel@tonic-gate typedef struct dtrace_state dtrace_state_t;
690Sstevel@tonic-gate typedef uint32_t dtrace_optid_t;
700Sstevel@tonic-gate typedef uint32_t dtrace_specid_t;
710Sstevel@tonic-gate typedef uint64_t dtrace_genid_t;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate  * DTrace Probes
750Sstevel@tonic-gate  *
760Sstevel@tonic-gate  * The probe is the fundamental unit of the DTrace architecture.  Probes are
770Sstevel@tonic-gate  * created by DTrace providers, and managed by the DTrace framework.  A probe
780Sstevel@tonic-gate  * is identified by a unique <provider, module, function, name> tuple, and has
790Sstevel@tonic-gate  * a unique probe identifier assigned to it.  (Some probes are not associated
800Sstevel@tonic-gate  * with a specific point in text; these are called _unanchored probes_ and have
810Sstevel@tonic-gate  * no module or function associated with them.)  Probes are represented as a
820Sstevel@tonic-gate  * dtrace_probe structure.  To allow quick lookups based on each element of the
830Sstevel@tonic-gate  * probe tuple, probes are hashed by each of provider, module, function and
840Sstevel@tonic-gate  * name.  (If a lookup is performed based on a regular expression, a
850Sstevel@tonic-gate  * dtrace_probekey is prepared, and a linear search is performed.) Each probe
860Sstevel@tonic-gate  * is additionally pointed to by a linear array indexed by its identifier.  The
870Sstevel@tonic-gate  * identifier is the provider's mechanism for indicating to the DTrace
880Sstevel@tonic-gate  * framework that a probe has fired:  the identifier is passed as the first
890Sstevel@tonic-gate  * argument to dtrace_probe(), where it is then mapped into the corresponding
900Sstevel@tonic-gate  * dtrace_probe structure.  From the dtrace_probe structure, dtrace_probe() can
910Sstevel@tonic-gate  * iterate over the probe's list of enabling control blocks; see "DTrace
920Sstevel@tonic-gate  * Enabling Control Blocks", below.)
930Sstevel@tonic-gate  */
940Sstevel@tonic-gate struct dtrace_probe {
950Sstevel@tonic-gate 	dtrace_id_t dtpr_id;			/* probe identifier */
960Sstevel@tonic-gate 	dtrace_ecb_t *dtpr_ecb;			/* ECB list; see below */
970Sstevel@tonic-gate 	dtrace_ecb_t *dtpr_ecb_last;		/* last ECB in list */
980Sstevel@tonic-gate 	void *dtpr_arg;				/* provider argument */
990Sstevel@tonic-gate 	dtrace_cacheid_t dtpr_predcache;	/* predicate cache ID */
1000Sstevel@tonic-gate 	int dtpr_aframes;			/* artificial frames */
1010Sstevel@tonic-gate 	dtrace_provider_t *dtpr_provider;	/* pointer to provider */
1020Sstevel@tonic-gate 	char *dtpr_mod;				/* probe's module name */
1030Sstevel@tonic-gate 	char *dtpr_func;			/* probe's function name */
1040Sstevel@tonic-gate 	char *dtpr_name;			/* probe's name */
1050Sstevel@tonic-gate 	dtrace_probe_t *dtpr_nextmod;		/* next in module hash */
1060Sstevel@tonic-gate 	dtrace_probe_t *dtpr_prevmod;		/* previous in module hash */
1070Sstevel@tonic-gate 	dtrace_probe_t *dtpr_nextfunc;		/* next in function hash */
1080Sstevel@tonic-gate 	dtrace_probe_t *dtpr_prevfunc;		/* previous in function hash */
1090Sstevel@tonic-gate 	dtrace_probe_t *dtpr_nextname;		/* next in name hash */
1100Sstevel@tonic-gate 	dtrace_probe_t *dtpr_prevname;		/* previous in name hash */
1110Sstevel@tonic-gate 	dtrace_genid_t dtpr_gen;		/* probe generation ID */
1120Sstevel@tonic-gate };
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate typedef int dtrace_probekey_f(const char *, const char *, int);
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate typedef struct dtrace_probekey {
1170Sstevel@tonic-gate 	const char *dtpk_prov;			/* provider name to match */
1180Sstevel@tonic-gate 	dtrace_probekey_f *dtpk_pmatch;		/* provider matching function */
1190Sstevel@tonic-gate 	const char *dtpk_mod;			/* module name to match */
1200Sstevel@tonic-gate 	dtrace_probekey_f *dtpk_mmatch;		/* module matching function */
1210Sstevel@tonic-gate 	const char *dtpk_func;			/* func name to match */
1220Sstevel@tonic-gate 	dtrace_probekey_f *dtpk_fmatch;		/* func matching function */
1230Sstevel@tonic-gate 	const char *dtpk_name;			/* name to match */
1240Sstevel@tonic-gate 	dtrace_probekey_f *dtpk_nmatch;		/* name matching function */
1250Sstevel@tonic-gate 	dtrace_id_t dtpk_id;			/* identifier to match */
1260Sstevel@tonic-gate } dtrace_probekey_t;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate typedef struct dtrace_hashbucket {
1290Sstevel@tonic-gate 	struct dtrace_hashbucket *dthb_next;	/* next on hash chain */
1300Sstevel@tonic-gate 	dtrace_probe_t *dthb_chain;		/* chain of probes */
1310Sstevel@tonic-gate 	int dthb_len;				/* number of probes here */
1320Sstevel@tonic-gate } dtrace_hashbucket_t;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate typedef struct dtrace_hash {
1350Sstevel@tonic-gate 	dtrace_hashbucket_t **dth_tab;		/* hash table */
1360Sstevel@tonic-gate 	int dth_size;				/* size of hash table */
1370Sstevel@tonic-gate 	int dth_mask;				/* mask to index into table */
1380Sstevel@tonic-gate 	int dth_nbuckets;			/* total number of buckets */
1390Sstevel@tonic-gate 	uintptr_t dth_nextoffs;			/* offset of next in probe */
1400Sstevel@tonic-gate 	uintptr_t dth_prevoffs;			/* offset of prev in probe */
1410Sstevel@tonic-gate 	uintptr_t dth_stroffs;			/* offset of str in probe */
1420Sstevel@tonic-gate } dtrace_hash_t;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate /*
1450Sstevel@tonic-gate  * DTrace Enabling Control Blocks
1460Sstevel@tonic-gate  *
1470Sstevel@tonic-gate  * When a provider wishes to fire a probe, it calls into dtrace_probe(),
1480Sstevel@tonic-gate  * passing the probe identifier as the first argument.  As described above,
1490Sstevel@tonic-gate  * dtrace_probe() maps the identifier into a pointer to a dtrace_probe_t
1500Sstevel@tonic-gate  * structure.  This structure contains information about the probe, and a
1510Sstevel@tonic-gate  * pointer to the list of Enabling Control Blocks (ECBs).  Each ECB points to
1520Sstevel@tonic-gate  * DTrace consumer state, and contains an optional predicate, and a list of
1530Sstevel@tonic-gate  * actions.  (Shown schematically below.)  The ECB abstraction allows a single
1540Sstevel@tonic-gate  * probe to be multiplexed across disjoint consumers, or across disjoint
1550Sstevel@tonic-gate  * enablings of a single probe within one consumer.
1560Sstevel@tonic-gate  *
1570Sstevel@tonic-gate  *   Enabling Control Block
1580Sstevel@tonic-gate  *        dtrace_ecb_t
1590Sstevel@tonic-gate  * +------------------------+
1600Sstevel@tonic-gate  * | dtrace_epid_t ---------+--------------> Enabled Probe ID (EPID)
1610Sstevel@tonic-gate  * | dtrace_state_t * ------+--------------> State associated with this ECB
1620Sstevel@tonic-gate  * | dtrace_predicate_t * --+---------+
1630Sstevel@tonic-gate  * | dtrace_action_t * -----+----+    |
1640Sstevel@tonic-gate  * | dtrace_ecb_t * ---+    |    |    |       Predicate (if any)
1650Sstevel@tonic-gate  * +-------------------+----+    |    |       dtrace_predicate_t
1660Sstevel@tonic-gate  *                     |         |    +---> +--------------------+
1670Sstevel@tonic-gate  *                     |         |          | dtrace_difo_t * ---+----> DIFO
1680Sstevel@tonic-gate  *                     |         |          +--------------------+
1690Sstevel@tonic-gate  *                     |         |
1700Sstevel@tonic-gate  *            Next ECB |         |           Action
1710Sstevel@tonic-gate  *            (if any) |         |       dtrace_action_t
1720Sstevel@tonic-gate  *                     :         +--> +-------------------+
1730Sstevel@tonic-gate  *                     :              | dtrace_actkind_t -+------> kind
1740Sstevel@tonic-gate  *                     v              | dtrace_difo_t * --+------> DIFO (if any)
1750Sstevel@tonic-gate  *                                    | dtrace_recdesc_t -+------> record descr.
1760Sstevel@tonic-gate  *                                    | dtrace_action_t * +------+
1770Sstevel@tonic-gate  *                                    +-------------------+      |
1780Sstevel@tonic-gate  *                                                               | Next action
1790Sstevel@tonic-gate  *                               +-------------------------------+  (if any)
1800Sstevel@tonic-gate  *                               |
1810Sstevel@tonic-gate  *                               |           Action
1820Sstevel@tonic-gate  *                               |       dtrace_action_t
1830Sstevel@tonic-gate  *                               +--> +-------------------+
1840Sstevel@tonic-gate  *                                    | dtrace_actkind_t -+------> kind
1850Sstevel@tonic-gate  *                                    | dtrace_difo_t * --+------> DIFO (if any)
1860Sstevel@tonic-gate  *                                    | dtrace_action_t * +------+
1870Sstevel@tonic-gate  *                                    +-------------------+      |
1880Sstevel@tonic-gate  *                                                               | Next action
1890Sstevel@tonic-gate  *                               +-------------------------------+  (if any)
1900Sstevel@tonic-gate  *                               |
1910Sstevel@tonic-gate  *                               :
1920Sstevel@tonic-gate  *                               v
1930Sstevel@tonic-gate  *
1940Sstevel@tonic-gate  *
1950Sstevel@tonic-gate  * dtrace_probe() iterates over the ECB list.  If the ECB needs less space
1960Sstevel@tonic-gate  * than is available in the principal buffer, the ECB is processed:  if the
1970Sstevel@tonic-gate  * predicate is non-NULL, the DIF object is executed.  If the result is
1980Sstevel@tonic-gate  * non-zero, the action list is processed, with each action being executed
1990Sstevel@tonic-gate  * accordingly.  When the action list has been completely executed, processing
2000Sstevel@tonic-gate  * advances to the next ECB.  processing advances to the next ECB.  If the
2010Sstevel@tonic-gate  * result is non-zero; For each ECB, it first determines the The ECB
2020Sstevel@tonic-gate  * abstraction allows disjoint consumers to multiplex on single probes.
2030Sstevel@tonic-gate  */
2040Sstevel@tonic-gate struct dtrace_ecb {
2050Sstevel@tonic-gate 	dtrace_epid_t dte_epid;			/* enabled probe ID */
2060Sstevel@tonic-gate 	uint32_t dte_alignment;			/* required alignment */
2070Sstevel@tonic-gate 	size_t dte_needed;			/* bytes needed */
2080Sstevel@tonic-gate 	size_t dte_size;			/* total size of payload */
2090Sstevel@tonic-gate 	dtrace_predicate_t *dte_predicate;	/* predicate, if any */
2100Sstevel@tonic-gate 	dtrace_action_t *dte_action;		/* actions, if any */
2110Sstevel@tonic-gate 	dtrace_ecb_t *dte_next;			/* next ECB on probe */
2120Sstevel@tonic-gate 	dtrace_state_t *dte_state;		/* pointer to state */
2130Sstevel@tonic-gate 	uint32_t dte_cond;			/* security condition */
2140Sstevel@tonic-gate 	dtrace_probe_t *dte_probe;		/* pointer to probe */
2150Sstevel@tonic-gate 	dtrace_action_t *dte_action_last;	/* last action on ECB */
2160Sstevel@tonic-gate 	uint64_t dte_uarg;			/* library argument */
2170Sstevel@tonic-gate };
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate struct dtrace_predicate {
2200Sstevel@tonic-gate 	dtrace_difo_t *dtp_difo;		/* DIF object */
2210Sstevel@tonic-gate 	dtrace_cacheid_t dtp_cacheid;		/* cache identifier */
2220Sstevel@tonic-gate 	int dtp_refcnt;				/* reference count */
2230Sstevel@tonic-gate };
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate struct dtrace_action {
2260Sstevel@tonic-gate 	dtrace_actkind_t dta_kind;		/* kind of action */
2270Sstevel@tonic-gate 	uint16_t dta_intuple;			/* boolean:  in aggregation */
2280Sstevel@tonic-gate 	uint32_t dta_refcnt;			/* reference count */
2290Sstevel@tonic-gate 	dtrace_difo_t *dta_difo;		/* pointer to DIFO */
2300Sstevel@tonic-gate 	dtrace_recdesc_t dta_rec;		/* record description */
2310Sstevel@tonic-gate 	dtrace_action_t *dta_prev;		/* previous action */
2320Sstevel@tonic-gate 	dtrace_action_t *dta_next;		/* next action */
2330Sstevel@tonic-gate };
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate typedef struct dtrace_aggregation {
2360Sstevel@tonic-gate 	dtrace_action_t dtag_action;		/* action; must be first */
2370Sstevel@tonic-gate 	dtrace_aggid_t dtag_id;			/* identifier */
2380Sstevel@tonic-gate 	dtrace_ecb_t *dtag_ecb;			/* corresponding ECB */
2390Sstevel@tonic-gate 	dtrace_action_t *dtag_first;		/* first action in tuple */
2400Sstevel@tonic-gate 	uint32_t dtag_base;			/* base of aggregation */
241457Sbmc 	uint8_t dtag_hasarg;			/* boolean:  has argument */
2420Sstevel@tonic-gate 	uint64_t dtag_initial;			/* initial value */
243457Sbmc 	void (*dtag_aggregate)(uint64_t *, uint64_t, uint64_t);
2440Sstevel@tonic-gate } dtrace_aggregation_t;
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate /*
2470Sstevel@tonic-gate  * DTrace Buffers
2480Sstevel@tonic-gate  *
2490Sstevel@tonic-gate  * Principal buffers, aggregation buffers, and speculative buffers are all
2500Sstevel@tonic-gate  * managed with the dtrace_buffer structure.  By default, this structure
2510Sstevel@tonic-gate  * includes twin data buffers -- dtb_tomax and dtb_xamot -- that serve as the
2520Sstevel@tonic-gate  * active and passive buffers, respectively.  For speculative buffers,
2530Sstevel@tonic-gate  * dtb_xamot will be NULL; for "ring" and "fill" buffers, dtb_xamot will point
2540Sstevel@tonic-gate  * to a scratch buffer.  For all buffer types, the dtrace_buffer structure is
2550Sstevel@tonic-gate  * always allocated on a per-CPU basis; a single dtrace_buffer structure is
2560Sstevel@tonic-gate  * never shared among CPUs.  (That is, there is never true sharing of the
2570Sstevel@tonic-gate  * dtrace_buffer structure; to prevent false sharing of the structure, it must
2580Sstevel@tonic-gate  * always be aligned to the coherence granularity -- generally 64 bytes.)
2590Sstevel@tonic-gate  *
2600Sstevel@tonic-gate  * One of the critical design decisions of DTrace is that a given ECB always
2610Sstevel@tonic-gate  * stores the same quantity and type of data.  This is done to assure that the
2620Sstevel@tonic-gate  * only metadata required for an ECB's traced data is the EPID.  That is, from
2630Sstevel@tonic-gate  * the EPID, the consumer can determine the data layout.  (The data buffer
2640Sstevel@tonic-gate  * layout is shown schematically below.)  By assuring that one can determine
2650Sstevel@tonic-gate  * data layout from the EPID, the metadata stream can be separated from the
2660Sstevel@tonic-gate  * data stream -- simplifying the data stream enormously.
2670Sstevel@tonic-gate  *
2680Sstevel@tonic-gate  *      base of data buffer --->  +------+--------------------+------+
2690Sstevel@tonic-gate  *                                | EPID | data               | EPID |
2700Sstevel@tonic-gate  *                                +------+--------+------+----+------+
2710Sstevel@tonic-gate  *                                | data          | EPID | data      |
2720Sstevel@tonic-gate  *                                +---------------+------+-----------+
2730Sstevel@tonic-gate  *                                | data, cont.                      |
2740Sstevel@tonic-gate  *                                +------+--------------------+------+
2750Sstevel@tonic-gate  *                                | EPID | data               |      |
2760Sstevel@tonic-gate  *                                +------+--------------------+      |
2770Sstevel@tonic-gate  *                                |                ||                |
2780Sstevel@tonic-gate  *                                |                ||                |
2790Sstevel@tonic-gate  *                                |                \/                |
2800Sstevel@tonic-gate  *                                :                                  :
2810Sstevel@tonic-gate  *                                .                                  .
2820Sstevel@tonic-gate  *                                .                                  .
2830Sstevel@tonic-gate  *                                .                                  .
2840Sstevel@tonic-gate  *                                :                                  :
2850Sstevel@tonic-gate  *                                |                                  |
2860Sstevel@tonic-gate  *     limit of data buffer --->  +----------------------------------+
2870Sstevel@tonic-gate  *
2880Sstevel@tonic-gate  * When evaluating an ECB, dtrace_probe() determines if the ECB's needs of the
2890Sstevel@tonic-gate  * principal buffer (both scratch and payload) exceed the available space.  If
2900Sstevel@tonic-gate  * the ECB's needs exceed available space (and if the principal buffer policy
2910Sstevel@tonic-gate  * is the default "switch" policy), the ECB is dropped, the buffer's drop count
2920Sstevel@tonic-gate  * is incremented, and processing advances to the next ECB.  If the ECB's needs
2930Sstevel@tonic-gate  * can be met with the available space, the ECB is processed, but the offset in
2940Sstevel@tonic-gate  * the principal buffer is only advanced if the ECB completes processing
2950Sstevel@tonic-gate  * without error.
2960Sstevel@tonic-gate  *
2970Sstevel@tonic-gate  * When a buffer is to be switched (either because the buffer is the principal
2980Sstevel@tonic-gate  * buffer with a "switch" policy or because it is an aggregation buffer), a
2990Sstevel@tonic-gate  * cross call is issued to the CPU associated with the buffer.  In the cross
3000Sstevel@tonic-gate  * call context, interrupts are disabled, and the active and the inactive
3010Sstevel@tonic-gate  * buffers are atomically switched.  This involves switching the data pointers,
3020Sstevel@tonic-gate  * copying the various state fields (offset, drops, errors, etc.) into their
3030Sstevel@tonic-gate  * inactive equivalents, and clearing the state fields.  Because interrupts are
3040Sstevel@tonic-gate  * disabled during this procedure, the switch is guaranteed to appear atomic to
3050Sstevel@tonic-gate  * dtrace_probe().
3060Sstevel@tonic-gate  *
3070Sstevel@tonic-gate  * DTrace Ring Buffering
3080Sstevel@tonic-gate  *
3090Sstevel@tonic-gate  * To process a ring buffer correctly, one must know the oldest valid record.
3100Sstevel@tonic-gate  * Processing starts at the oldest record in the buffer and continues until
3110Sstevel@tonic-gate  * the end of the buffer is reached.  Processing then resumes starting with
3120Sstevel@tonic-gate  * the record stored at offset 0 in the buffer, and continues until the
3130Sstevel@tonic-gate  * youngest record is processed.  If trace records are of a fixed-length,
3140Sstevel@tonic-gate  * determining the oldest record is trivial:
3150Sstevel@tonic-gate  *
3160Sstevel@tonic-gate  *   - If the ring buffer has not wrapped, the oldest record is the record
3170Sstevel@tonic-gate  *     stored at offset 0.
3180Sstevel@tonic-gate  *
3190Sstevel@tonic-gate  *   - If the ring buffer has wrapped, the oldest record is the record stored
3200Sstevel@tonic-gate  *     at the current offset.
3210Sstevel@tonic-gate  *
3220Sstevel@tonic-gate  * With variable length records, however, just knowing the current offset
3230Sstevel@tonic-gate  * doesn't suffice for determining the oldest valid record:  assuming that one
3240Sstevel@tonic-gate  * allows for arbitrary data, one has no way of searching forward from the
3250Sstevel@tonic-gate  * current offset to find the oldest valid record.  (That is, one has no way
3260Sstevel@tonic-gate  * of separating data from metadata.) It would be possible to simply refuse to
3270Sstevel@tonic-gate  * process any data in the ring buffer between the current offset and the
3280Sstevel@tonic-gate  * limit, but this leaves (potentially) an enormous amount of otherwise valid
3290Sstevel@tonic-gate  * data unprocessed.
3300Sstevel@tonic-gate  *
3310Sstevel@tonic-gate  * To effect ring buffering, we track two offsets in the buffer:  the current
3320Sstevel@tonic-gate  * offset and the _wrapped_ offset.  If a request is made to reserve some
3330Sstevel@tonic-gate  * amount of data, and the buffer has wrapped, the wrapped offset is
3340Sstevel@tonic-gate  * incremented until the wrapped offset minus the current offset is greater
3350Sstevel@tonic-gate  * than or equal to the reserve request.  This is done by repeatedly looking
3360Sstevel@tonic-gate  * up the ECB corresponding to the EPID at the current wrapped offset, and
3370Sstevel@tonic-gate  * incrementing the wrapped offset by the size of the data payload
3380Sstevel@tonic-gate  * corresponding to that ECB.  If this offset is greater than or equal to the
3390Sstevel@tonic-gate  * limit of the data buffer, the wrapped offset is set to 0.  Thus, the
3400Sstevel@tonic-gate  * current offset effectively "chases" the wrapped offset around the buffer.
3410Sstevel@tonic-gate  * Schematically:
3420Sstevel@tonic-gate  *
3430Sstevel@tonic-gate  *      base of data buffer --->  +------+--------------------+------+
3440Sstevel@tonic-gate  *                                | EPID | data               | EPID |
3450Sstevel@tonic-gate  *                                +------+--------+------+----+------+
3460Sstevel@tonic-gate  *                                | data          | EPID | data      |
3470Sstevel@tonic-gate  *                                +---------------+------+-----------+
3480Sstevel@tonic-gate  *                                | data, cont.                      |
3490Sstevel@tonic-gate  *                                +------+---------------------------+
3500Sstevel@tonic-gate  *                                | EPID | data                      |
3510Sstevel@tonic-gate  *           current offset --->  +------+---------------------------+
3520Sstevel@tonic-gate  *                                | invalid data                     |
3530Sstevel@tonic-gate  *           wrapped offset --->  +------+--------------------+------+
3540Sstevel@tonic-gate  *                                | EPID | data               | EPID |
3550Sstevel@tonic-gate  *                                +------+--------+------+----+------+
3560Sstevel@tonic-gate  *                                | data          | EPID | data      |
3570Sstevel@tonic-gate  *                                +---------------+------+-----------+
3580Sstevel@tonic-gate  *                                :                                  :
3590Sstevel@tonic-gate  *                                .                                  .
3600Sstevel@tonic-gate  *                                .        ... valid data ...        .
3610Sstevel@tonic-gate  *                                .                                  .
3620Sstevel@tonic-gate  *                                :                                  :
3630Sstevel@tonic-gate  *                                +------+-------------+------+------+
3640Sstevel@tonic-gate  *                                | EPID | data        | EPID | data |
3650Sstevel@tonic-gate  *                                +------+------------++------+------+
3660Sstevel@tonic-gate  *                                | data, cont.       | leftover     |
3670Sstevel@tonic-gate  *     limit of data buffer --->  +-------------------+--------------+
3680Sstevel@tonic-gate  *
3690Sstevel@tonic-gate  * If the amount of requested buffer space exceeds the amount of space
3700Sstevel@tonic-gate  * available between the current offset and the end of the buffer:
3710Sstevel@tonic-gate  *
3720Sstevel@tonic-gate  *  (1)  all words in the data buffer between the current offset and the limit
3730Sstevel@tonic-gate  *       of the data buffer (marked "leftover", above) are set to
3740Sstevel@tonic-gate  *       DTRACE_EPIDNONE
3750Sstevel@tonic-gate  *
3760Sstevel@tonic-gate  *  (2)  the wrapped offset is set to zero
3770Sstevel@tonic-gate  *
3780Sstevel@tonic-gate  *  (3)  the iteration process described above occurs until the wrapped offset
3790Sstevel@tonic-gate  *       is greater than the amount of desired space.
3800Sstevel@tonic-gate  *
3810Sstevel@tonic-gate  * The wrapped offset is implemented by (re-)using the inactive offset.
3820Sstevel@tonic-gate  * In a "switch" buffer policy, the inactive offset stores the offset in
3830Sstevel@tonic-gate  * the inactive buffer; in a "ring" buffer policy, it stores the wrapped
3840Sstevel@tonic-gate  * offset.
3850Sstevel@tonic-gate  *
3860Sstevel@tonic-gate  * DTrace Scratch Buffering
3870Sstevel@tonic-gate  *
3880Sstevel@tonic-gate  * Some ECBs may wish to allocate dynamically-sized temporary scratch memory.
3890Sstevel@tonic-gate  * To accommodate such requests easily, scratch memory may be allocated in
3900Sstevel@tonic-gate  * the buffer beyond the current offset plus the needed memory of the current
3910Sstevel@tonic-gate  * ECB.  If there isn't sufficient room in the buffer for the requested amount
3920Sstevel@tonic-gate  * of scratch space, the allocation fails and an error is generated.  Scratch
3930Sstevel@tonic-gate  * memory is tracked in the dtrace_mstate_t and is automatically freed when
3940Sstevel@tonic-gate  * the ECB ceases processing.  Note that ring buffers cannot allocate their
3950Sstevel@tonic-gate  * scratch from the principal buffer -- lest they needlessly overwrite older,
3960Sstevel@tonic-gate  * valid data.  Ring buffers therefore have their own dedicated scratch buffer
3970Sstevel@tonic-gate  * from which scratch is allocated.
3980Sstevel@tonic-gate  */
3990Sstevel@tonic-gate #define	DTRACEBUF_RING		0x0001		/* bufpolicy set to "ring" */
4000Sstevel@tonic-gate #define	DTRACEBUF_FILL		0x0002		/* bufpolicy set to "fill" */
4010Sstevel@tonic-gate #define	DTRACEBUF_NOSWITCH	0x0004		/* do not switch buffer */
4020Sstevel@tonic-gate #define	DTRACEBUF_WRAPPED	0x0008		/* ring buffer has wrapped */
4030Sstevel@tonic-gate #define	DTRACEBUF_DROPPED	0x0010		/* drops occurred */
4040Sstevel@tonic-gate #define	DTRACEBUF_ERROR		0x0020		/* errors occurred */
4050Sstevel@tonic-gate #define	DTRACEBUF_FULL		0x0040		/* "fill" buffer is full */
4060Sstevel@tonic-gate #define	DTRACEBUF_CONSUMED	0x0080		/* buffer has been consumed */
4070Sstevel@tonic-gate #define	DTRACEBUF_INACTIVE	0x0100		/* buffer is not yet active */
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate typedef struct dtrace_buffer {
4100Sstevel@tonic-gate 	uint64_t dtb_offset;			/* current offset in buffer */
4110Sstevel@tonic-gate 	uint64_t dtb_size;			/* size of buffer */
4120Sstevel@tonic-gate 	uint32_t dtb_flags;			/* flags */
4130Sstevel@tonic-gate 	uint32_t dtb_drops;			/* number of drops */
4140Sstevel@tonic-gate 	caddr_t dtb_tomax;			/* active buffer */
4150Sstevel@tonic-gate 	caddr_t dtb_xamot;			/* inactive buffer */
4160Sstevel@tonic-gate 	uint32_t dtb_xamot_flags;		/* inactive flags */
4170Sstevel@tonic-gate 	uint32_t dtb_xamot_drops;		/* drops in inactive buffer */
4180Sstevel@tonic-gate 	uint64_t dtb_xamot_offset;		/* offset in inactive buffer */
4190Sstevel@tonic-gate 	uint32_t dtb_errors;			/* number of errors */
4200Sstevel@tonic-gate 	uint32_t dtb_xamot_errors;		/* errors in inactive buffer */
4210Sstevel@tonic-gate #ifndef _LP64
4220Sstevel@tonic-gate 	uint64_t dtb_pad1;
4230Sstevel@tonic-gate #endif
4240Sstevel@tonic-gate } dtrace_buffer_t;
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate /*
4270Sstevel@tonic-gate  * DTrace Aggregation Buffers
4280Sstevel@tonic-gate  *
4290Sstevel@tonic-gate  * Aggregation buffers use much of the same mechanism as described above
4300Sstevel@tonic-gate  * ("DTrace Buffers").  However, because an aggregation is fundamentally a
4310Sstevel@tonic-gate  * hash, there exists dynamic metadata associated with an aggregation buffer
4320Sstevel@tonic-gate  * that is not associated with other kinds of buffers.  This aggregation
4330Sstevel@tonic-gate  * metadata is _only_ relevant for the in-kernel implementation of
4340Sstevel@tonic-gate  * aggregations; it is not actually relevant to user-level consumers.  To do
4350Sstevel@tonic-gate  * this, we allocate dynamic aggregation data (hash keys and hash buckets)
4360Sstevel@tonic-gate  * starting below the _limit_ of the buffer, and we allocate data from the
4370Sstevel@tonic-gate  * _base_ of the buffer.  When the aggregation buffer is copied out, _only_ the
4380Sstevel@tonic-gate  * data is copied out; the metadata is simply discarded.  Schematically,
4390Sstevel@tonic-gate  * aggregation buffers look like:
4400Sstevel@tonic-gate  *
4410Sstevel@tonic-gate  *      base of data buffer --->  +-------+------+-----------+-------+
4420Sstevel@tonic-gate  *                                | aggid | key  | value     | aggid |
4430Sstevel@tonic-gate  *                                +-------+------+-----------+-------+
4440Sstevel@tonic-gate  *                                | key                              |
4450Sstevel@tonic-gate  *                                +-------+-------+-----+------------+
4460Sstevel@tonic-gate  *                                | value | aggid | key | value      |
4470Sstevel@tonic-gate  *                                +-------+------++-----+------+-----+
4480Sstevel@tonic-gate  *                                | aggid | key  | value       |     |
4490Sstevel@tonic-gate  *                                +-------+------+-------------+     |
4500Sstevel@tonic-gate  *                                |                ||                |
4510Sstevel@tonic-gate  *                                |                ||                |
4520Sstevel@tonic-gate  *                                |                \/                |
4530Sstevel@tonic-gate  *                                :                                  :
4540Sstevel@tonic-gate  *                                .                                  .
4550Sstevel@tonic-gate  *                                .                                  .
4560Sstevel@tonic-gate  *                                .                                  .
4570Sstevel@tonic-gate  *                                :                                  :
4580Sstevel@tonic-gate  *                                |                /\                |
4590Sstevel@tonic-gate  *                                |                ||   +------------+
4600Sstevel@tonic-gate  *                                |                ||   |            |
4610Sstevel@tonic-gate  *                                +---------------------+            |
4620Sstevel@tonic-gate  *                                | hash keys                        |
4630Sstevel@tonic-gate  *                                | (dtrace_aggkey structures)       |
4640Sstevel@tonic-gate  *                                |                                  |
4650Sstevel@tonic-gate  *                                +----------------------------------+
4660Sstevel@tonic-gate  *                                | hash buckets                     |
4670Sstevel@tonic-gate  *                                | (dtrace_aggbuffer structure)     |
4680Sstevel@tonic-gate  *                                |                                  |
4690Sstevel@tonic-gate  *     limit of data buffer --->  +----------------------------------+
4700Sstevel@tonic-gate  *
4710Sstevel@tonic-gate  *
4720Sstevel@tonic-gate  * As implied above, just as we assure that ECBs always store a constant
4730Sstevel@tonic-gate  * amount of data, we assure that a given aggregation -- identified by its
4740Sstevel@tonic-gate  * aggregation ID -- always stores data of a constant quantity and type.
4750Sstevel@tonic-gate  * As with EPIDs, this allows the aggregation ID to serve as the metadata for a
4760Sstevel@tonic-gate  * given record.
4770Sstevel@tonic-gate  *
4780Sstevel@tonic-gate  * Note that the size of the dtrace_aggkey structure must be sizeof (uintptr_t)
4790Sstevel@tonic-gate  * aligned.  (If this the structure changes such that this becomes false, an
4800Sstevel@tonic-gate  * assertion will fail in dtrace_aggregate().)
4810Sstevel@tonic-gate  */
4820Sstevel@tonic-gate typedef struct dtrace_aggkey {
4830Sstevel@tonic-gate 	uint32_t dtak_hashval;			/* hash value */
4840Sstevel@tonic-gate 	uint32_t dtak_action:4;			/* action -- 4 bits */
4850Sstevel@tonic-gate 	uint32_t dtak_size:28;			/* size -- 28 bits */
4860Sstevel@tonic-gate 	caddr_t dtak_data;			/* data pointer */
4870Sstevel@tonic-gate 	struct dtrace_aggkey *dtak_next;	/* next in hash chain */
4880Sstevel@tonic-gate } dtrace_aggkey_t;
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate typedef struct dtrace_aggbuffer {
4910Sstevel@tonic-gate 	uintptr_t dtagb_hashsize;		/* number of buckets */
4920Sstevel@tonic-gate 	uintptr_t dtagb_free;			/* free list of keys */
4930Sstevel@tonic-gate 	dtrace_aggkey_t **dtagb_hash;		/* hash table */
4940Sstevel@tonic-gate } dtrace_aggbuffer_t;
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate /*
4970Sstevel@tonic-gate  * DTrace Speculations
4980Sstevel@tonic-gate  *
4990Sstevel@tonic-gate  * Speculations have a per-CPU buffer and a global state.  Once a speculation
5000Sstevel@tonic-gate  * buffer has been comitted or discarded, it cannot be reused until all CPUs
5010Sstevel@tonic-gate  * have taken the same action (commit or discard) on their respective
5020Sstevel@tonic-gate  * speculative buffer.  However, because DTrace probes may execute in arbitrary
5030Sstevel@tonic-gate  * context, other CPUs cannot simply be cross-called at probe firing time to
5040Sstevel@tonic-gate  * perform the necessary commit or discard.  The speculation states thus
5050Sstevel@tonic-gate  * optimize for the case that a speculative buffer is only active on one CPU at
5060Sstevel@tonic-gate  * the time of a commit() or discard() -- for if this is the case, other CPUs
5070Sstevel@tonic-gate  * need not take action, and the speculation is immediately available for
5080Sstevel@tonic-gate  * reuse.  If the speculation is active on multiple CPUs, it must be
5090Sstevel@tonic-gate  * asynchronously cleaned -- potentially leading to a higher rate of dirty
5100Sstevel@tonic-gate  * speculative drops.  The speculation states are as follows:
5110Sstevel@tonic-gate  *
5120Sstevel@tonic-gate  *  DTRACESPEC_INACTIVE       <= Initial state; inactive speculation
5130Sstevel@tonic-gate  *  DTRACESPEC_ACTIVE         <= Allocated, but not yet speculatively traced to
5140Sstevel@tonic-gate  *  DTRACESPEC_ACTIVEONE      <= Speculatively traced to on one CPU
5150Sstevel@tonic-gate  *  DTRACESPEC_ACTIVEMANY     <= Speculatively traced to on more than one CPU
5160Sstevel@tonic-gate  *  DTRACESPEC_COMMITTING     <= Currently being commited on one CPU
5170Sstevel@tonic-gate  *  DTRACESPEC_COMMITTINGMANY <= Currently being commited on many CPUs
5180Sstevel@tonic-gate  *  DTRACESPEC_DISCARDING     <= Currently being discarded on many CPUs
5190Sstevel@tonic-gate  *
5200Sstevel@tonic-gate  * The state transition diagram is as follows:
5210Sstevel@tonic-gate  *
5220Sstevel@tonic-gate  *     +----------------------------------------------------------+
5230Sstevel@tonic-gate  *     |                                                          |
5240Sstevel@tonic-gate  *     |                      +------------+                      |
5250Sstevel@tonic-gate  *     |  +-------------------| COMMITTING |<-----------------+   |
5260Sstevel@tonic-gate  *     |  |                   +------------+                  |   |
5270Sstevel@tonic-gate  *     |  | copied spec.            ^             commit() on |   | discard() on
5280Sstevel@tonic-gate  *     |  | into principal          |              active CPU |   | active CPU
5290Sstevel@tonic-gate  *     |  |                         | commit()                |   |
5300Sstevel@tonic-gate  *     V  V                         |                         |   |
5310Sstevel@tonic-gate  * +----------+                 +--------+                +-----------+
5320Sstevel@tonic-gate  * | INACTIVE |---------------->| ACTIVE |--------------->| ACTIVEONE |
5330Sstevel@tonic-gate  * +----------+  speculation()  +--------+  speculate()   +-----------+
5340Sstevel@tonic-gate  *     ^  ^                         |                         |   |
5350Sstevel@tonic-gate  *     |  |                         | discard()               |   |
5360Sstevel@tonic-gate  *     |  | asynchronously          |            discard() on |   | speculate()
5370Sstevel@tonic-gate  *     |  | cleaned                 V            inactive CPU |   | on inactive
5380Sstevel@tonic-gate  *     |  |                   +------------+                  |   | CPU
5390Sstevel@tonic-gate  *     |  +-------------------| DISCARDING |<-----------------+   |
5400Sstevel@tonic-gate  *     |                      +------------+                      |
5410Sstevel@tonic-gate  *     | asynchronously             ^                             |
5420Sstevel@tonic-gate  *     | copied spec.               |       discard()             |
5430Sstevel@tonic-gate  *     | into principal             +------------------------+    |
5440Sstevel@tonic-gate  *     |                                                     |    V
5450Sstevel@tonic-gate  *  +----------------+             commit()              +------------+
5460Sstevel@tonic-gate  *  | COMMITTINGMANY |<----------------------------------| ACTIVEMANY |
5470Sstevel@tonic-gate  *  +----------------+                                   +------------+
5480Sstevel@tonic-gate  */
5490Sstevel@tonic-gate typedef enum dtrace_speculation_state {
5500Sstevel@tonic-gate 	DTRACESPEC_INACTIVE = 0,
5510Sstevel@tonic-gate 	DTRACESPEC_ACTIVE,
5520Sstevel@tonic-gate 	DTRACESPEC_ACTIVEONE,
5530Sstevel@tonic-gate 	DTRACESPEC_ACTIVEMANY,
5540Sstevel@tonic-gate 	DTRACESPEC_COMMITTING,
5550Sstevel@tonic-gate 	DTRACESPEC_COMMITTINGMANY,
5560Sstevel@tonic-gate 	DTRACESPEC_DISCARDING
5570Sstevel@tonic-gate } dtrace_speculation_state_t;
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate typedef struct dtrace_speculation {
5600Sstevel@tonic-gate 	dtrace_speculation_state_t dtsp_state;	/* current speculation state */
5610Sstevel@tonic-gate 	int dtsp_cleaning;			/* non-zero if being cleaned */
5620Sstevel@tonic-gate 	dtrace_buffer_t *dtsp_buffer;		/* speculative buffer */
5630Sstevel@tonic-gate } dtrace_speculation_t;
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate /*
5660Sstevel@tonic-gate  * DTrace Dynamic Variables
5670Sstevel@tonic-gate  *
5680Sstevel@tonic-gate  * The dynamic variable problem is obviously decomposed into two subproblems:
5690Sstevel@tonic-gate  * allocating new dynamic storage, and freeing old dynamic storage.  The
5700Sstevel@tonic-gate  * presence of the second problem makes the first much more complicated -- or
5710Sstevel@tonic-gate  * rather, the absence of the second renders the first trivial.  This is the
5720Sstevel@tonic-gate  * case with aggregations, for which there is effectively no deallocation of
5730Sstevel@tonic-gate  * dynamic storage.  (Or more accurately, all dynamic storage is deallocated
5740Sstevel@tonic-gate  * when a snapshot is taken of the aggregation.)  As DTrace dynamic variables
5750Sstevel@tonic-gate  * allow for both dynamic allocation and dynamic deallocation, the
5760Sstevel@tonic-gate  * implementation of dynamic variables is quite a bit more complicated than
5770Sstevel@tonic-gate  * that of their aggregation kin.
5780Sstevel@tonic-gate  *
5790Sstevel@tonic-gate  * We observe that allocating new dynamic storage is tricky only because the
5800Sstevel@tonic-gate  * size can vary -- the allocation problem is much easier if allocation sizes
5810Sstevel@tonic-gate  * are uniform.  We further observe that in D, the size of dynamic variables is
5820Sstevel@tonic-gate  * actually _not_ dynamic -- dynamic variable sizes may be determined by static
5830Sstevel@tonic-gate  * analysis of DIF text.  (This is true even of putatively dynamically-sized
5840Sstevel@tonic-gate  * objects like strings and stacks, the sizes of which are dictated by the
5850Sstevel@tonic-gate  * "stringsize" and "stackframes" variables, respectively.)  We exploit this by
5860Sstevel@tonic-gate  * performing this analysis on all DIF before enabling any probes.  For each
5870Sstevel@tonic-gate  * dynamic load or store, we calculate the dynamically-allocated size plus the
5880Sstevel@tonic-gate  * size of the dtrace_dynvar structure plus the storage required to key the
5890Sstevel@tonic-gate  * data.  For all DIF, we take the largest value and dub it the _chunksize_.
5900Sstevel@tonic-gate  * We then divide dynamic memory into two parts:  a hash table that is wide
5910Sstevel@tonic-gate  * enough to have every chunk in its own bucket, and a larger region of equal
5920Sstevel@tonic-gate  * chunksize units.  Whenever we wish to dynamically allocate a variable, we
5930Sstevel@tonic-gate  * always allocate a single chunk of memory.  Depending on the uniformity of
5940Sstevel@tonic-gate  * allocation, this will waste some amount of memory -- but it eliminates the
5950Sstevel@tonic-gate  * non-determinism inherent in traditional heap fragmentation.
5960Sstevel@tonic-gate  *
5970Sstevel@tonic-gate  * Dynamic objects are allocated by storing a non-zero value to them; they are
5980Sstevel@tonic-gate  * deallocated by storing a zero value to them.  Dynamic variables are
5990Sstevel@tonic-gate  * complicated enormously by being shared between CPUs.  In particular,
6000Sstevel@tonic-gate  * consider the following scenario:
6010Sstevel@tonic-gate  *
6020Sstevel@tonic-gate  *                 CPU A                                 CPU B
6030Sstevel@tonic-gate  *  +---------------------------------+   +---------------------------------+
6040Sstevel@tonic-gate  *  |                                 |   |                                 |
6050Sstevel@tonic-gate  *  | allocates dynamic object a[123] |   |                                 |
6060Sstevel@tonic-gate  *  | by storing the value 345 to it  |   |                                 |
6070Sstevel@tonic-gate  *  |                               --------->                              |
6080Sstevel@tonic-gate  *  |                                 |   | wishing to load from object     |
6090Sstevel@tonic-gate  *  |                                 |   | a[123], performs lookup in      |
6100Sstevel@tonic-gate  *  |                                 |   | dynamic variable space          |
6110Sstevel@tonic-gate  *  |                               <---------                              |
6120Sstevel@tonic-gate  *  | deallocates object a[123] by    |   |                                 |
6130Sstevel@tonic-gate  *  | storing 0 to it                 |   |                                 |
6140Sstevel@tonic-gate  *  |                                 |   |                                 |
6150Sstevel@tonic-gate  *  | allocates dynamic object b[567] |   | performs load from a[123]       |
6160Sstevel@tonic-gate  *  | by storing the value 789 to it  |   |                                 |
6170Sstevel@tonic-gate  *  :                                 :   :                                 :
6180Sstevel@tonic-gate  *  .                                 .   .                                 .
6190Sstevel@tonic-gate  *
6200Sstevel@tonic-gate  * This is obviously a race in the D program, but there are nonetheless only
6210Sstevel@tonic-gate  * two valid values for CPU B's load from a[123]:  345 or 0.  Most importantly,
6220Sstevel@tonic-gate  * CPU B may _not_ see the value 789 for a[123].
6230Sstevel@tonic-gate  *
6240Sstevel@tonic-gate  * There are essentially two ways to deal with this:
6250Sstevel@tonic-gate  *
6260Sstevel@tonic-gate  *  (1)  Explicitly spin-lock variables.  That is, if CPU B wishes to load
6270Sstevel@tonic-gate  *       from a[123], it needs to lock a[123] and hold the lock for the
6280Sstevel@tonic-gate  *       duration that it wishes to manipulate it.
6290Sstevel@tonic-gate  *
6300Sstevel@tonic-gate  *  (2)  Avoid reusing freed chunks until it is known that no CPU is referring
6310Sstevel@tonic-gate  *       to them.
6320Sstevel@tonic-gate  *
6330Sstevel@tonic-gate  * The implementation of (1) is rife with complexity, because it requires the
6340Sstevel@tonic-gate  * user of a dynamic variable to explicitly decree when they are done using it.
6350Sstevel@tonic-gate  * Were all variables by value, this perhaps wouldn't be debilitating -- but
6360Sstevel@tonic-gate  * dynamic variables of non-scalar types are tracked by reference.  That is, if
6370Sstevel@tonic-gate  * a dynamic variable is, say, a string, and that variable is to be traced to,
6380Sstevel@tonic-gate  * say, the principal buffer, the DIF emulation code returns to the main
6390Sstevel@tonic-gate  * dtrace_probe() loop a pointer to the underlying storage, not the contents of
6400Sstevel@tonic-gate  * the storage.  Further, code calling on DIF emulation would have to be aware
6410Sstevel@tonic-gate  * that the DIF emulation has returned a reference to a dynamic variable that
6420Sstevel@tonic-gate  * has been potentially locked.  The variable would have to be unlocked after
6430Sstevel@tonic-gate  * the main dtrace_probe() loop is finished with the variable, and the main
6440Sstevel@tonic-gate  * dtrace_probe() loop would have to be careful to not call any further DIF
6450Sstevel@tonic-gate  * emulation while the variable is locked to avoid deadlock.  More generally,
6460Sstevel@tonic-gate  * if one were to implement (1), DIF emulation code dealing with dynamic
6470Sstevel@tonic-gate  * variables could only deal with one dynamic variable at a time (lest deadlock
6480Sstevel@tonic-gate  * result).  To sum, (1) exports too much subtlety to the users of dynamic
6490Sstevel@tonic-gate  * variables -- increasing maintenance burden and imposing serious constraints
6500Sstevel@tonic-gate  * on future DTrace development.
6510Sstevel@tonic-gate  *
6520Sstevel@tonic-gate  * The implementation of (2) is also complex, but the complexity is more
6530Sstevel@tonic-gate  * manageable.  We need to be sure that when a variable is deallocated, it is
6540Sstevel@tonic-gate  * not placed on a traditional free list, but rather on a _dirty_ list.  Once a
6550Sstevel@tonic-gate  * variable is on a dirty list, it cannot be found by CPUs performing a
6560Sstevel@tonic-gate  * subsequent lookup of the variable -- but it may still be in use by other
6570Sstevel@tonic-gate  * CPUs.  To assure that all CPUs that may be seeing the old variable have
6580Sstevel@tonic-gate  * cleared out of probe context, a dtrace_sync() can be issued.  Once the
6590Sstevel@tonic-gate  * dtrace_sync() has completed, it can be known that all CPUs are done
6600Sstevel@tonic-gate  * manipulating the dynamic variable -- the dirty list can be atomically
6610Sstevel@tonic-gate  * appended to the free list.  Unfortunately, there's a slight hiccup in this
6620Sstevel@tonic-gate  * mechanism:  dtrace_sync() may not be issued from probe context.  The
6630Sstevel@tonic-gate  * dtrace_sync() must be therefore issued asynchronously from non-probe
6640Sstevel@tonic-gate  * context.  For this we rely on the DTrace cleaner, a cyclic that runs at the
6650Sstevel@tonic-gate  * "cleanrate" frequency.  To ease this implementation, we define several chunk
6660Sstevel@tonic-gate  * lists:
6670Sstevel@tonic-gate  *
6680Sstevel@tonic-gate  *   - Dirty.  Deallocated chunks, not yet cleaned.  Not available.
6690Sstevel@tonic-gate  *
6700Sstevel@tonic-gate  *   - Rinsing.  Formerly dirty chunks that are currently being asynchronously
6710Sstevel@tonic-gate  *     cleaned.  Not available, but will be shortly.  Dynamic variable
6720Sstevel@tonic-gate  *     allocation may not spin or block for availability, however.
6730Sstevel@tonic-gate  *
6740Sstevel@tonic-gate  *   - Clean.  Clean chunks, ready for allocation -- but not on the free list.
6750Sstevel@tonic-gate  *
6760Sstevel@tonic-gate  *   - Free.  Available for allocation.
6770Sstevel@tonic-gate  *
6780Sstevel@tonic-gate  * Moreover, to avoid absurd contention, _each_ of these lists is implemented
6790Sstevel@tonic-gate  * on a per-CPU basis.  This is only for performance, not correctness; chunks
6800Sstevel@tonic-gate  * may be allocated from another CPU's free list.  The algorithm for allocation
6810Sstevel@tonic-gate  * then is this:
6820Sstevel@tonic-gate  *
6830Sstevel@tonic-gate  *   (1)  Attempt to atomically allocate from current CPU's free list.  If list
6840Sstevel@tonic-gate  *        is non-empty and allocation is successful, allocation is complete.
6850Sstevel@tonic-gate  *
6860Sstevel@tonic-gate  *   (2)  If the clean list is non-empty, atomically move it to the free list,
6870Sstevel@tonic-gate  *        and reattempt (1).
6880Sstevel@tonic-gate  *
6890Sstevel@tonic-gate  *   (3)  If the dynamic variable space is in the CLEAN state, look for free
6900Sstevel@tonic-gate  *        and clean lists on other CPUs by setting the current CPU to the next
6910Sstevel@tonic-gate  *        CPU, and reattempting (1).  If the next CPU is the current CPU (that
6920Sstevel@tonic-gate  *        is, if all CPUs have been checked), atomically switch the state of
6930Sstevel@tonic-gate  *        the dynamic variable space based on the following:
6940Sstevel@tonic-gate  *
6950Sstevel@tonic-gate  *        - If no free chunks were found and no dirty chunks were found,
6960Sstevel@tonic-gate  *          atomically set the state to EMPTY.
6970Sstevel@tonic-gate  *
6980Sstevel@tonic-gate  *        - If dirty chunks were found, atomically set the state to DIRTY.
6990Sstevel@tonic-gate  *
7000Sstevel@tonic-gate  *        - If rinsing chunks were found, atomically set the state to RINSING.
7010Sstevel@tonic-gate  *
7020Sstevel@tonic-gate  *   (4)  Based on state of dynamic variable space state, increment appropriate
7030Sstevel@tonic-gate  *        counter to indicate dynamic drops (if in EMPTY state) vs. dynamic
7040Sstevel@tonic-gate  *        dirty drops (if in DIRTY state) vs. dynamic rinsing drops (if in
7050Sstevel@tonic-gate  *        RINSING state).  Fail the allocation.
7060Sstevel@tonic-gate  *
7070Sstevel@tonic-gate  * The cleaning cyclic operates with the following algorithm:  for all CPUs
7080Sstevel@tonic-gate  * with a non-empty dirty list, atomically move the dirty list to the rinsing
7090Sstevel@tonic-gate  * list.  Perform a dtrace_sync().  For all CPUs with a non-empty rinsing list,
7100Sstevel@tonic-gate  * atomically move the rinsing list to the clean list.  Perform another
7110Sstevel@tonic-gate  * dtrace_sync().  By this point, all CPUs have seen the new clean list; the
7120Sstevel@tonic-gate  * state of the dynamic variable space can be restored to CLEAN.
7130Sstevel@tonic-gate  *
7140Sstevel@tonic-gate  * There exist two final races that merit explanation.  The first is a simple
7150Sstevel@tonic-gate  * allocation race:
7160Sstevel@tonic-gate  *
7170Sstevel@tonic-gate  *                 CPU A                                 CPU B
7180Sstevel@tonic-gate  *  +---------------------------------+   +---------------------------------+
7190Sstevel@tonic-gate  *  |                                 |   |                                 |
7200Sstevel@tonic-gate  *  | allocates dynamic object a[123] |   | allocates dynamic object a[123] |
7210Sstevel@tonic-gate  *  | by storing the value 345 to it  |   | by storing the value 567 to it  |
7220Sstevel@tonic-gate  *  |                                 |   |                                 |
7230Sstevel@tonic-gate  *  :                                 :   :                                 :
7240Sstevel@tonic-gate  *  .                                 .   .                                 .
7250Sstevel@tonic-gate  *
7260Sstevel@tonic-gate  * Again, this is a race in the D program.  It can be resolved by having a[123]
7270Sstevel@tonic-gate  * hold the value 345 or a[123] hold the value 567 -- but it must be true that
7280Sstevel@tonic-gate  * a[123] have only _one_ of these values.  (That is, the racing CPUs may not
7290Sstevel@tonic-gate  * put the same element twice on the same hash chain.)  This is resolved
7300Sstevel@tonic-gate  * simply:  before the allocation is undertaken, the start of the new chunk's
7310Sstevel@tonic-gate  * hash chain is noted.  Later, after the allocation is complete, the hash
7320Sstevel@tonic-gate  * chain is atomically switched to point to the new element.  If this fails
7330Sstevel@tonic-gate  * (because of either concurrent allocations or an allocation concurrent with a
7340Sstevel@tonic-gate  * deletion), the newly allocated chunk is deallocated to the dirty list, and
7350Sstevel@tonic-gate  * the whole process of looking up (and potentially allocating) the dynamic
7360Sstevel@tonic-gate  * variable is reattempted.
7370Sstevel@tonic-gate  *
7380Sstevel@tonic-gate  * The final race is a simple deallocation race:
7390Sstevel@tonic-gate  *
7400Sstevel@tonic-gate  *                 CPU A                                 CPU B
7410Sstevel@tonic-gate  *  +---------------------------------+   +---------------------------------+
7420Sstevel@tonic-gate  *  |                                 |   |                                 |
7430Sstevel@tonic-gate  *  | deallocates dynamic object      |   | deallocates dynamic object      |
7440Sstevel@tonic-gate  *  | a[123] by storing the value 0   |   | a[123] by storing the value 0   |
7450Sstevel@tonic-gate  *  | to it                           |   | to it                           |
7460Sstevel@tonic-gate  *  |                                 |   |                                 |
7470Sstevel@tonic-gate  *  :                                 :   :                                 :
7480Sstevel@tonic-gate  *  .                                 .   .                                 .
7490Sstevel@tonic-gate  *
7500Sstevel@tonic-gate  * Once again, this is a race in the D program, but it is one that we must
7510Sstevel@tonic-gate  * handle without corrupting the underlying data structures.  Because
7520Sstevel@tonic-gate  * deallocations require the deletion of a chunk from the middle of a hash
7530Sstevel@tonic-gate  * chain, we cannot use a single-word atomic operation to remove it.  For this,
7540Sstevel@tonic-gate  * we add a spin lock to the hash buckets that is _only_ used for deallocations
7550Sstevel@tonic-gate  * (allocation races are handled as above).  Further, this spin lock is _only_
7560Sstevel@tonic-gate  * held for the duration of the delete; before control is returned to the DIF
7570Sstevel@tonic-gate  * emulation code, the hash bucket is unlocked.
7580Sstevel@tonic-gate  */
7590Sstevel@tonic-gate typedef struct dtrace_key {
7600Sstevel@tonic-gate 	uint64_t dttk_value;			/* data value or data pointer */
7610Sstevel@tonic-gate 	uint64_t dttk_size;			/* 0 if by-val, >0 if by-ref */
7620Sstevel@tonic-gate } dtrace_key_t;
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate typedef struct dtrace_tuple {
7650Sstevel@tonic-gate 	uint32_t dtt_nkeys;			/* number of keys in tuple */
7660Sstevel@tonic-gate 	uint32_t dtt_pad;			/* padding */
7670Sstevel@tonic-gate 	dtrace_key_t dtt_key[1];		/* array of tuple keys */
7680Sstevel@tonic-gate } dtrace_tuple_t;
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate typedef struct dtrace_dynvar {
7710Sstevel@tonic-gate 	uint64_t dtdv_hashval;			/* hash value -- 0 if free */
7720Sstevel@tonic-gate 	struct dtrace_dynvar *dtdv_next;	/* next on list or hash chain */
7730Sstevel@tonic-gate 	void *dtdv_data;			/* pointer to data */
7740Sstevel@tonic-gate 	dtrace_tuple_t dtdv_tuple;		/* tuple key */
7750Sstevel@tonic-gate } dtrace_dynvar_t;
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate typedef enum dtrace_dynvar_op {
7780Sstevel@tonic-gate 	DTRACE_DYNVAR_ALLOC,
7790Sstevel@tonic-gate 	DTRACE_DYNVAR_NOALLOC,
7800Sstevel@tonic-gate 	DTRACE_DYNVAR_DEALLOC
7810Sstevel@tonic-gate } dtrace_dynvar_op_t;
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate typedef struct dtrace_dynhash {
7840Sstevel@tonic-gate 	dtrace_dynvar_t *dtdh_chain;		/* hash chain for this bucket */
7850Sstevel@tonic-gate 	uintptr_t dtdh_lock;			/* deallocation lock */
7860Sstevel@tonic-gate #ifdef _LP64
7870Sstevel@tonic-gate 	uintptr_t dtdh_pad[6];			/* pad to avoid false sharing */
7880Sstevel@tonic-gate #else
7890Sstevel@tonic-gate 	uintptr_t dtdh_pad[14];			/* pad to avoid false sharing */
7900Sstevel@tonic-gate #endif
7910Sstevel@tonic-gate } dtrace_dynhash_t;
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate typedef struct dtrace_dstate_percpu {
7940Sstevel@tonic-gate 	dtrace_dynvar_t *dtdsc_free;		/* free list for this CPU */
7950Sstevel@tonic-gate 	dtrace_dynvar_t *dtdsc_dirty;		/* dirty list for this CPU */
7960Sstevel@tonic-gate 	dtrace_dynvar_t *dtdsc_rinsing;		/* rinsing list for this CPU */
7970Sstevel@tonic-gate 	dtrace_dynvar_t *dtdsc_clean;		/* clean list for this CPU */
7980Sstevel@tonic-gate 	uint64_t dtdsc_drops;			/* number of capacity drops */
7990Sstevel@tonic-gate 	uint64_t dtdsc_dirty_drops;		/* number of dirty drops */
8000Sstevel@tonic-gate 	uint64_t dtdsc_rinsing_drops;		/* number of rinsing drops */
8010Sstevel@tonic-gate #ifdef _LP64
8020Sstevel@tonic-gate 	uint64_t dtdsc_pad;			/* pad to avoid false sharing */
8030Sstevel@tonic-gate #else
8040Sstevel@tonic-gate 	uint64_t dtdsc_pad[2];			/* pad to avoid false sharing */
8050Sstevel@tonic-gate #endif
8060Sstevel@tonic-gate } dtrace_dstate_percpu_t;
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate typedef enum dtrace_dstate_state {
8090Sstevel@tonic-gate 	DTRACE_DSTATE_CLEAN = 0,
8100Sstevel@tonic-gate 	DTRACE_DSTATE_EMPTY,
8110Sstevel@tonic-gate 	DTRACE_DSTATE_DIRTY,
8120Sstevel@tonic-gate 	DTRACE_DSTATE_RINSING
8130Sstevel@tonic-gate } dtrace_dstate_state_t;
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate typedef struct dtrace_dstate {
8160Sstevel@tonic-gate 	void *dtds_base;			/* base of dynamic var. space */
8170Sstevel@tonic-gate 	size_t dtds_size;			/* size of dynamic var. space */
8180Sstevel@tonic-gate 	size_t dtds_hashsize;			/* number of buckets in hash */
8190Sstevel@tonic-gate 	size_t dtds_chunksize;			/* size of each chunk */
8200Sstevel@tonic-gate 	dtrace_dynhash_t *dtds_hash;		/* pointer to hash table */
8210Sstevel@tonic-gate 	dtrace_dstate_state_t dtds_state;	/* current dynamic var. state */
8220Sstevel@tonic-gate 	dtrace_dstate_percpu_t *dtds_percpu;	/* per-CPU dyn. var. state */
8230Sstevel@tonic-gate } dtrace_dstate_t;
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate /*
8260Sstevel@tonic-gate  * DTrace Variable State
8270Sstevel@tonic-gate  *
8280Sstevel@tonic-gate  * The DTrace variable state tracks user-defined variables in its dtrace_vstate
8290Sstevel@tonic-gate  * structure.  Each DTrace consumer has exactly one dtrace_vstate structure,
8300Sstevel@tonic-gate  * but some dtrace_vstate structures may exist without a corresponding DTrace
8310Sstevel@tonic-gate  * consumer (see "DTrace Helpers", below).  As described in <sys/dtrace.h>,
8320Sstevel@tonic-gate  * user-defined variables can have one of three scopes:
8330Sstevel@tonic-gate  *
8340Sstevel@tonic-gate  *  DIFV_SCOPE_GLOBAL  =>  global scope
8350Sstevel@tonic-gate  *  DIFV_SCOPE_THREAD  =>  thread-local scope (i.e. "self->" variables)
8360Sstevel@tonic-gate  *  DIFV_SCOPE_LOCAL   =>  clause-local scope (i.e. "this->" variables)
8370Sstevel@tonic-gate  *
8380Sstevel@tonic-gate  * The variable state tracks variables by both their scope and their allocation
8390Sstevel@tonic-gate  * type:
8400Sstevel@tonic-gate  *
8410Sstevel@tonic-gate  *  - The dtvs_globals and dtvs_locals members each point to an array of
8420Sstevel@tonic-gate  *    dtrace_statvar structures.  These structures contain both the variable
8430Sstevel@tonic-gate  *    metadata (dtrace_difv structures) and the underlying storage for all
8440Sstevel@tonic-gate  *    statically allocated variables, including statically allocated
8450Sstevel@tonic-gate  *    DIFV_SCOPE_GLOBAL variables and all DIFV_SCOPE_LOCAL variables.
8460Sstevel@tonic-gate  *
8470Sstevel@tonic-gate  *  - The dtvs_tlocals member points to an array of dtrace_difv structures for
8480Sstevel@tonic-gate  *    DIFV_SCOPE_THREAD variables.  As such, this array tracks _only_ the
8490Sstevel@tonic-gate  *    variable metadata for DIFV_SCOPE_THREAD variables; the underlying storage
8500Sstevel@tonic-gate  *    is allocated out of the dynamic variable space.
8510Sstevel@tonic-gate  *
8520Sstevel@tonic-gate  *  - The dtvs_dynvars member is the dynamic variable state associated with the
8530Sstevel@tonic-gate  *    variable state.  The dynamic variable state (described in "DTrace Dynamic
8540Sstevel@tonic-gate  *    Variables", above) tracks all DIFV_SCOPE_THREAD variables and all
8550Sstevel@tonic-gate  *    dynamically-allocated DIFV_SCOPE_GLOBAL variables.
8560Sstevel@tonic-gate  */
8570Sstevel@tonic-gate typedef struct dtrace_statvar {
8580Sstevel@tonic-gate 	uint64_t dtsv_data;			/* data or pointer to it */
8590Sstevel@tonic-gate 	size_t dtsv_size;			/* size of pointed-to data */
8600Sstevel@tonic-gate 	int dtsv_refcnt;			/* reference count */
8610Sstevel@tonic-gate 	dtrace_difv_t dtsv_var;			/* variable metadata */
8620Sstevel@tonic-gate } dtrace_statvar_t;
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate typedef struct dtrace_vstate {
8650Sstevel@tonic-gate 	dtrace_state_t *dtvs_state;		/* back pointer to state */
8660Sstevel@tonic-gate 	dtrace_statvar_t **dtvs_globals;	/* statically-allocated glbls */
8670Sstevel@tonic-gate 	int dtvs_nglobals;			/* number of globals */
8680Sstevel@tonic-gate 	dtrace_difv_t *dtvs_tlocals;		/* thread-local metadata */
8690Sstevel@tonic-gate 	int dtvs_ntlocals;			/* number of thread-locals */
8700Sstevel@tonic-gate 	dtrace_statvar_t **dtvs_locals;		/* clause-local data */
8710Sstevel@tonic-gate 	int dtvs_nlocals;			/* number of clause-locals */
8720Sstevel@tonic-gate 	dtrace_dstate_t dtvs_dynvars;		/* dynamic variable state */
8730Sstevel@tonic-gate } dtrace_vstate_t;
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate /*
8760Sstevel@tonic-gate  * DTrace Machine State
8770Sstevel@tonic-gate  *
8780Sstevel@tonic-gate  * In the process of processing a fired probe, DTrace needs to track and/or
8790Sstevel@tonic-gate  * cache some per-CPU state associated with that particular firing.  This is
8800Sstevel@tonic-gate  * state that is always discarded after the probe firing has completed, and
8810Sstevel@tonic-gate  * much of it is not specific to any DTrace consumer, remaining valid across
8820Sstevel@tonic-gate  * all ECBs.  This state is tracked in the dtrace_mstate structure.
8830Sstevel@tonic-gate  */
8840Sstevel@tonic-gate #define	DTRACE_MSTATE_ARGS		0x00000001
8850Sstevel@tonic-gate #define	DTRACE_MSTATE_PROBE		0x00000002
8860Sstevel@tonic-gate #define	DTRACE_MSTATE_EPID		0x00000004
8870Sstevel@tonic-gate #define	DTRACE_MSTATE_TIMESTAMP		0x00000008
8880Sstevel@tonic-gate #define	DTRACE_MSTATE_STACKDEPTH	0x00000010
8890Sstevel@tonic-gate #define	DTRACE_MSTATE_CALLER		0x00000020
8900Sstevel@tonic-gate #define	DTRACE_MSTATE_IPL		0x00000040
8910Sstevel@tonic-gate #define	DTRACE_MSTATE_FLTOFFS		0x00000080
8920Sstevel@tonic-gate #define	DTRACE_MSTATE_WALLTIMESTAMP	0x00000100
893191Sahl #define	DTRACE_MSTATE_USTACKDEPTH	0x00000200
894457Sbmc #define	DTRACE_MSTATE_UCALLER		0x00000400
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate typedef struct dtrace_mstate {
8970Sstevel@tonic-gate 	uintptr_t dtms_scratch_base;		/* base of scratch space */
8980Sstevel@tonic-gate 	uintptr_t dtms_scratch_ptr;		/* current scratch pointer */
8990Sstevel@tonic-gate 	size_t dtms_scratch_size;		/* scratch size */
9000Sstevel@tonic-gate 	uint32_t dtms_present;			/* variables that are present */
9010Sstevel@tonic-gate 	uint64_t dtms_arg[5];			/* cached arguments */
9020Sstevel@tonic-gate 	dtrace_epid_t dtms_epid;		/* current EPID */
9030Sstevel@tonic-gate 	uint64_t dtms_timestamp;		/* cached timestamp */
9040Sstevel@tonic-gate 	hrtime_t dtms_walltimestamp;		/* cached wall timestamp */
9050Sstevel@tonic-gate 	int dtms_stackdepth;			/* cached stackdepth */
906191Sahl 	int dtms_ustackdepth;			/* cached ustackdepth */
9070Sstevel@tonic-gate 	struct dtrace_probe *dtms_probe;	/* current probe */
9080Sstevel@tonic-gate 	uintptr_t dtms_caller;			/* cached caller */
909457Sbmc 	uint64_t dtms_ucaller;			/* cached user-level caller */
9100Sstevel@tonic-gate 	int dtms_ipl;				/* cached interrupt pri lev */
9110Sstevel@tonic-gate 	int dtms_fltoffs;			/* faulting DIFO offset */
9120Sstevel@tonic-gate 	uintptr_t dtms_strtok;			/* saved strtok() pointer */
9132870Sdp 	uint32_t dtms_access;			/* memory access rights */
9142870Sdp 	dtrace_difo_t *dtms_difo;		/* current dif object */
9150Sstevel@tonic-gate } dtrace_mstate_t;
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate #define	DTRACE_COND_OWNER	0x1
9180Sstevel@tonic-gate #define	DTRACE_COND_USERMODE	0x2
9191677Sdp #define	DTRACE_COND_ZONEOWNER	0x4
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate #define	DTRACE_PROBEKEY_MAXDEPTH	8	/* max glob recursion depth */
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate /*
9242870Sdp  * Access flag used by dtrace_mstate.dtms_access.
9252870Sdp  */
9262870Sdp #define	DTRACE_ACCESS_KERNEL	0x1		/* the priv to read kmem */
9272870Sdp 
9282870Sdp 
9292870Sdp /*
9300Sstevel@tonic-gate  * DTrace Activity
9310Sstevel@tonic-gate  *
9320Sstevel@tonic-gate  * Each DTrace consumer is in one of several states, which (for purposes of
9330Sstevel@tonic-gate  * avoiding yet-another overloading of the noun "state") we call the current
9340Sstevel@tonic-gate  * _activity_.  The activity transitions on dtrace_go() (from DTRACIOCGO), on
9350Sstevel@tonic-gate  * dtrace_stop() (from DTRACIOCSTOP) and on the exit() action.  Activities may
9360Sstevel@tonic-gate  * only transition in one direction; the activity transition diagram is a
9370Sstevel@tonic-gate  * directed acyclic graph.  The activity transition diagram is as follows:
9380Sstevel@tonic-gate  *
9390Sstevel@tonic-gate  *
9400Sstevel@tonic-gate  * +----------+                   +--------+                   +--------+
9410Sstevel@tonic-gate  * | INACTIVE |------------------>| WARMUP |------------------>| ACTIVE |
9420Sstevel@tonic-gate  * +----------+   dtrace_go(),    +--------+   dtrace_go(),    +--------+
9430Sstevel@tonic-gate  *                before BEGIN        |        after BEGIN       |  |  |
9440Sstevel@tonic-gate  *                                    |                          |  |  |
9450Sstevel@tonic-gate  *                      exit() action |                          |  |  |
9460Sstevel@tonic-gate  *                     from BEGIN ECB |                          |  |  |
9470Sstevel@tonic-gate  *                                    |                          |  |  |
9480Sstevel@tonic-gate  *                                    v                          |  |  |
9490Sstevel@tonic-gate  *                               +----------+     exit() action  |  |  |
9501739Sbmc  * +-----------------------------| DRAINING |<-------------------+  |  |
9511739Sbmc  * |                             +----------+                       |  |
9521739Sbmc  * |                                  |                             |  |
9531739Sbmc  * |                   dtrace_stop(), |                             |  |
9541739Sbmc  * |                     before END   |                             |  |
9551739Sbmc  * |                                  |                             |  |
9561739Sbmc  * |                                  v                             |  |
9571739Sbmc  * | +---------+                 +----------+                       |  |
9581739Sbmc  * | | STOPPED |<----------------| COOLDOWN |<----------------------+  |
9591739Sbmc  * | +---------+  dtrace_stop(), +----------+     dtrace_stop(),       |
9601739Sbmc  * |                after END                       before END         |
9611739Sbmc  * |                                                                   |
9621739Sbmc  * |                              +--------+                           |
9631739Sbmc  * +----------------------------->| KILLED |<--------------------------+
9641739Sbmc  *       deadman timeout or       +--------+     deadman timeout or
9651739Sbmc  *        killed consumer                         killed consumer
9660Sstevel@tonic-gate  *
9670Sstevel@tonic-gate  * Note that once a DTrace consumer has stopped tracing, there is no way to
9680Sstevel@tonic-gate  * restart it; if a DTrace consumer wishes to restart tracing, it must reopen
9690Sstevel@tonic-gate  * the DTrace pseudodevice.
9700Sstevel@tonic-gate  */
9710Sstevel@tonic-gate typedef enum dtrace_activity {
9720Sstevel@tonic-gate 	DTRACE_ACTIVITY_INACTIVE = 0,		/* not yet running */
9730Sstevel@tonic-gate 	DTRACE_ACTIVITY_WARMUP,			/* while starting */
9740Sstevel@tonic-gate 	DTRACE_ACTIVITY_ACTIVE,			/* running */
9750Sstevel@tonic-gate 	DTRACE_ACTIVITY_DRAINING,		/* before stopping */
9760Sstevel@tonic-gate 	DTRACE_ACTIVITY_COOLDOWN,		/* while stopping */
9770Sstevel@tonic-gate 	DTRACE_ACTIVITY_STOPPED,		/* after stopping */
9781739Sbmc 	DTRACE_ACTIVITY_KILLED			/* killed */
9790Sstevel@tonic-gate } dtrace_activity_t;
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate /*
9820Sstevel@tonic-gate  * DTrace Helper Implementation
9830Sstevel@tonic-gate  *
9840Sstevel@tonic-gate  * A description of the helper architecture may be found in <sys/dtrace.h>.
9850Sstevel@tonic-gate  * Each process contains a pointer to its helpers in its p_dtrace_helpers
9860Sstevel@tonic-gate  * member.  This is a pointer to a dtrace_helpers structure, which contains an
9870Sstevel@tonic-gate  * array of pointers to dtrace_helper structures, helper variable state (shared
9880Sstevel@tonic-gate  * among a process's helpers) and a generation count.  (The generation count is
9890Sstevel@tonic-gate  * used to provide an identifier when a helper is added so that it may be
9900Sstevel@tonic-gate  * subsequently removed.)  The dtrace_helper structure is self-explanatory,
9910Sstevel@tonic-gate  * containing pointers to the objects needed to execute the helper.  Note that
9920Sstevel@tonic-gate  * helpers are _duplicated_ across fork(2), and destroyed on exec(2).  No more
9930Sstevel@tonic-gate  * than dtrace_helpers_max are allowed per-process.
9940Sstevel@tonic-gate  */
9950Sstevel@tonic-gate #define	DTRACE_HELPER_ACTION_USTACK	0
9960Sstevel@tonic-gate #define	DTRACE_NHELPER_ACTIONS		1
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate typedef struct dtrace_helper_action {
9992021Sahl 	int dtha_generation;			/* helper action generation */
10002021Sahl 	int dtha_nactions;			/* number of actions */
10012021Sahl 	dtrace_difo_t *dtha_predicate;		/* helper action predicate */
10022021Sahl 	dtrace_difo_t **dtha_actions;		/* array of actions */
10032021Sahl 	struct dtrace_helper_action *dtha_next;	/* next helper action */
10040Sstevel@tonic-gate } dtrace_helper_action_t;
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate typedef struct dtrace_helper_provider {
10072021Sahl 	int dthp_generation;			/* helper provider generation */
10082021Sahl 	uint32_t dthp_ref;			/* reference count */
10090Sstevel@tonic-gate 	dof_helper_t dthp_prov;			/* DOF w/ provider and probes */
10100Sstevel@tonic-gate } dtrace_helper_provider_t;
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate typedef struct dtrace_helpers {
10130Sstevel@tonic-gate 	dtrace_helper_action_t **dthps_actions;	/* array of helper actions */
10140Sstevel@tonic-gate 	dtrace_vstate_t dthps_vstate;		/* helper action var. state */
10150Sstevel@tonic-gate 	dtrace_helper_provider_t **dthps_provs;	/* array of providers */
10160Sstevel@tonic-gate 	uint_t dthps_nprovs;			/* count of providers */
10172021Sahl 	uint_t dthps_maxprovs;			/* provider array size */
10180Sstevel@tonic-gate 	int dthps_generation;			/* current generation */
10190Sstevel@tonic-gate 	pid_t dthps_pid;			/* pid of associated proc */
1020935Sahl 	int dthps_deferred;			/* helper in deferred list */
10210Sstevel@tonic-gate 	struct dtrace_helpers *dthps_next;	/* next pointer */
10220Sstevel@tonic-gate 	struct dtrace_helpers *dthps_prev;	/* prev pointer */
10230Sstevel@tonic-gate } dtrace_helpers_t;
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate /*
10260Sstevel@tonic-gate  * DTrace Helper Action Tracing
10270Sstevel@tonic-gate  *
10280Sstevel@tonic-gate  * Debugging helper actions can be arduous.  To ease the development and
10290Sstevel@tonic-gate  * debugging of helpers, DTrace contains a tracing-framework-within-a-tracing-
10300Sstevel@tonic-gate  * framework: helper tracing.  If dtrace_helptrace_enabled is non-zero (which
10310Sstevel@tonic-gate  * it is by default on DEBUG kernels), all helper activity will be traced to a
10320Sstevel@tonic-gate  * global, in-kernel ring buffer.  Each entry includes a pointer to the specific
10330Sstevel@tonic-gate  * helper, the location within the helper, and a trace of all local variables.
10340Sstevel@tonic-gate  * The ring buffer may be displayed in a human-readable format with the
10350Sstevel@tonic-gate  * ::dtrace_helptrace mdb(1) dcmd.
10360Sstevel@tonic-gate  */
10370Sstevel@tonic-gate #define	DTRACE_HELPTRACE_NEXT	(-1)
10380Sstevel@tonic-gate #define	DTRACE_HELPTRACE_DONE	(-2)
10390Sstevel@tonic-gate #define	DTRACE_HELPTRACE_ERR	(-3)
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate typedef struct dtrace_helptrace {
10420Sstevel@tonic-gate 	dtrace_helper_action_t	*dtht_helper;	/* helper action */
10430Sstevel@tonic-gate 	int dtht_where;				/* where in helper action */
10440Sstevel@tonic-gate 	int dtht_nlocals;			/* number of locals */
1045491Sbmc 	int dtht_fault;				/* type of fault (if any) */
1046491Sbmc 	int dtht_fltoffs;			/* DIF offset */
1047491Sbmc 	uint64_t dtht_illval;			/* faulting value */
10480Sstevel@tonic-gate 	uint64_t dtht_locals[1];		/* local variables */
10490Sstevel@tonic-gate } dtrace_helptrace_t;
10500Sstevel@tonic-gate 
10510Sstevel@tonic-gate /*
10520Sstevel@tonic-gate  * DTrace Credentials
10530Sstevel@tonic-gate  *
10541677Sdp  * In probe context, we have limited flexibility to examine the credentials
10551677Sdp  * of the DTrace consumer that created a particular enabling.  We use
10561677Sdp  * the Least Privilege interfaces to cache the consumer's cred pointer and
10571677Sdp  * some facts about that credential in a dtrace_cred_t structure. These
10581677Sdp  * can limit the consumer's breadth of visibility and what actions the
10591677Sdp  * consumer may take.
10600Sstevel@tonic-gate  */
10610Sstevel@tonic-gate #define	DTRACE_CRV_ALLPROC		0x01
10620Sstevel@tonic-gate #define	DTRACE_CRV_KERNEL		0x02
10631677Sdp #define	DTRACE_CRV_ALLZONE		0x04
10640Sstevel@tonic-gate 
10651677Sdp #define	DTRACE_CRV_ALL		(DTRACE_CRV_ALLPROC | DTRACE_CRV_KERNEL | \
10661677Sdp 	DTRACE_CRV_ALLZONE)
10670Sstevel@tonic-gate 
10681677Sdp #define	DTRACE_CRA_PROC				0x0001
10691677Sdp #define	DTRACE_CRA_PROC_CONTROL			0x0002
10701677Sdp #define	DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER	0x0004
10711677Sdp #define	DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE	0x0008
10721677Sdp #define	DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG	0x0010
10731677Sdp #define	DTRACE_CRA_KERNEL			0x0020
10741677Sdp #define	DTRACE_CRA_KERNEL_DESTRUCTIVE		0x0040
10750Sstevel@tonic-gate 
10760Sstevel@tonic-gate #define	DTRACE_CRA_ALL		(DTRACE_CRA_PROC | \
10771677Sdp 	DTRACE_CRA_PROC_CONTROL | \
10781677Sdp 	DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER | \
10791677Sdp 	DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE | \
10801677Sdp 	DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG | \
10811677Sdp 	DTRACE_CRA_KERNEL | \
10821677Sdp 	DTRACE_CRA_KERNEL_DESTRUCTIVE)
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate typedef struct dtrace_cred {
10851677Sdp 	cred_t			*dcr_cred;
10860Sstevel@tonic-gate 	uint8_t			dcr_destructive;
10870Sstevel@tonic-gate 	uint8_t			dcr_visible;
10880Sstevel@tonic-gate 	uint16_t		dcr_action;
10890Sstevel@tonic-gate } dtrace_cred_t;
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate /*
10920Sstevel@tonic-gate  * DTrace Consumer State
10930Sstevel@tonic-gate  *
10940Sstevel@tonic-gate  * Each DTrace consumer has an associated dtrace_state structure that contains
10950Sstevel@tonic-gate  * its in-kernel DTrace state -- including options, credentials, statistics and
10960Sstevel@tonic-gate  * pointers to ECBs, buffers, speculations and formats.  A dtrace_state
10970Sstevel@tonic-gate  * structure is also allocated for anonymous enablings.  When anonymous state
10980Sstevel@tonic-gate  * is grabbed, the grabbing consumers dts_anon pointer is set to the grabbed
10990Sstevel@tonic-gate  * dtrace_state structure.
11000Sstevel@tonic-gate  */
11010Sstevel@tonic-gate struct dtrace_state {
11020Sstevel@tonic-gate 	dev_t dts_dev;				/* device */
11030Sstevel@tonic-gate 	int dts_necbs;				/* total number of ECBs */
11040Sstevel@tonic-gate 	dtrace_ecb_t **dts_ecbs;		/* array of ECBs */
11050Sstevel@tonic-gate 	dtrace_epid_t dts_epid;			/* next EPID to allocate */
11060Sstevel@tonic-gate 	size_t dts_needed;			/* greatest needed space */
11070Sstevel@tonic-gate 	struct dtrace_state *dts_anon;		/* anon. state, if grabbed */
11080Sstevel@tonic-gate 	dtrace_activity_t dts_activity;		/* current activity */
11090Sstevel@tonic-gate 	dtrace_vstate_t dts_vstate;		/* variable state */
11100Sstevel@tonic-gate 	dtrace_buffer_t *dts_buffer;		/* principal buffer */
11110Sstevel@tonic-gate 	dtrace_buffer_t *dts_aggbuffer;		/* aggregation buffer */
11120Sstevel@tonic-gate 	dtrace_speculation_t *dts_speculations;	/* speculation array */
11130Sstevel@tonic-gate 	int dts_nspeculations;			/* number of speculations */
11140Sstevel@tonic-gate 	int dts_naggregations;			/* number of aggregations */
11150Sstevel@tonic-gate 	dtrace_aggregation_t **dts_aggregations; /* aggregation array */
11160Sstevel@tonic-gate 	vmem_t *dts_aggid_arena;		/* arena for aggregation IDs */
1117457Sbmc 	uint64_t dts_errors;			/* total number of errors */
11180Sstevel@tonic-gate 	uint32_t dts_speculations_busy;		/* number of spec. busy */
11190Sstevel@tonic-gate 	uint32_t dts_speculations_unavail;	/* number of spec unavail */
1120457Sbmc 	uint32_t dts_stkstroverflows;		/* stack string tab overflows */
1121457Sbmc 	uint32_t dts_dblerrors;			/* errors in ERROR probes */
11220Sstevel@tonic-gate 	uint32_t dts_reserve;			/* space reserved for END */
11230Sstevel@tonic-gate 	hrtime_t dts_laststatus;		/* time of last status */
11240Sstevel@tonic-gate 	cyclic_id_t dts_cleaner;		/* cleaning cyclic */
11250Sstevel@tonic-gate 	cyclic_id_t dts_deadman;		/* deadman cyclic */
11260Sstevel@tonic-gate 	hrtime_t dts_alive;			/* time last alive */
11270Sstevel@tonic-gate 	char dts_speculates;			/* boolean: has speculations */
11280Sstevel@tonic-gate 	char dts_destructive;			/* boolean: has dest. actions */
11290Sstevel@tonic-gate 	int dts_nformats;			/* number of formats */
11300Sstevel@tonic-gate 	char **dts_formats;			/* format string array */
11310Sstevel@tonic-gate 	dtrace_optval_t dts_options[DTRACEOPT_MAX]; /* options */
11320Sstevel@tonic-gate 	dtrace_cred_t dts_cred;			/* credentials */
11330Sstevel@tonic-gate 	size_t dts_nretained;			/* number of retained enabs */
11340Sstevel@tonic-gate };
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate struct dtrace_provider {
11370Sstevel@tonic-gate 	dtrace_pattr_t dtpv_attr;		/* provider attributes */
11380Sstevel@tonic-gate 	dtrace_ppriv_t dtpv_priv;		/* provider privileges */
11390Sstevel@tonic-gate 	dtrace_pops_t dtpv_pops;		/* provider operations */
11400Sstevel@tonic-gate 	char *dtpv_name;			/* provider name */
11410Sstevel@tonic-gate 	void *dtpv_arg;				/* provider argument */
11420Sstevel@tonic-gate 	uint_t dtpv_defunct;			/* boolean: defunct provider */
11430Sstevel@tonic-gate 	struct dtrace_provider *dtpv_next;	/* next provider */
11440Sstevel@tonic-gate };
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate struct dtrace_meta {
11470Sstevel@tonic-gate 	dtrace_mops_t dtm_mops;			/* meta provider operations */
11480Sstevel@tonic-gate 	char *dtm_name;				/* meta provider name */
11490Sstevel@tonic-gate 	void *dtm_arg;				/* meta provider user arg */
11500Sstevel@tonic-gate 	uint64_t dtm_count;			/* no. of associated provs. */
11510Sstevel@tonic-gate };
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate /*
11540Sstevel@tonic-gate  * DTrace Enablings
11550Sstevel@tonic-gate  *
11560Sstevel@tonic-gate  * A dtrace_enabling structure is used to track a collection of ECB
11570Sstevel@tonic-gate  * descriptions -- before they have been turned into actual ECBs.  This is
11580Sstevel@tonic-gate  * created as a result of DOF processing, and is generally used to generate
11590Sstevel@tonic-gate  * ECBs immediately thereafter.  However, enablings are also generally
11600Sstevel@tonic-gate  * retained should the probes they describe be created at a later time; as
11610Sstevel@tonic-gate  * each new module or provider registers with the framework, the retained
11620Sstevel@tonic-gate  * enablings are reevaluated, with any new match resulting in new ECBs.  To
11630Sstevel@tonic-gate  * prevent probes from being matched more than once, the enabling tracks the
11640Sstevel@tonic-gate  * last probe generation matched, and only matches probes from subsequent
11650Sstevel@tonic-gate  * generations.
11660Sstevel@tonic-gate  */
11670Sstevel@tonic-gate typedef struct dtrace_enabling {
11680Sstevel@tonic-gate 	dtrace_ecbdesc_t **dten_desc;		/* all ECB descriptions */
11690Sstevel@tonic-gate 	int dten_ndesc;				/* number of ECB descriptions */
11700Sstevel@tonic-gate 	int dten_maxdesc;			/* size of ECB array */
11710Sstevel@tonic-gate 	dtrace_vstate_t *dten_vstate;		/* associated variable state */
11720Sstevel@tonic-gate 	dtrace_genid_t dten_probegen;		/* matched probe generation */
11730Sstevel@tonic-gate 	dtrace_ecbdesc_t *dten_current;		/* current ECB description */
11740Sstevel@tonic-gate 	int dten_error;				/* current error value */
11750Sstevel@tonic-gate 	int dten_primed;			/* boolean: set if primed */
11760Sstevel@tonic-gate 	struct dtrace_enabling *dten_prev;	/* previous enabling */
11770Sstevel@tonic-gate 	struct dtrace_enabling *dten_next;	/* next enabling */
11780Sstevel@tonic-gate } dtrace_enabling_t;
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate /*
11810Sstevel@tonic-gate  * DTrace Anonymous Enablings
11820Sstevel@tonic-gate  *
11830Sstevel@tonic-gate  * Anonymous enablings are DTrace enablings that are not associated with a
11840Sstevel@tonic-gate  * controlling process, but rather derive their enabling from DOF stored as
11850Sstevel@tonic-gate  * properties in the dtrace.conf file.  If there is an anonymous enabling, a
11860Sstevel@tonic-gate  * DTrace consumer state and enabling are created on attach.  The state may be
11870Sstevel@tonic-gate  * subsequently grabbed by the first consumer specifying the "grabanon"
11880Sstevel@tonic-gate  * option.  As long as an anonymous DTrace enabling exists, dtrace(7D) will
11890Sstevel@tonic-gate  * refuse to unload.
11900Sstevel@tonic-gate  */
11910Sstevel@tonic-gate typedef struct dtrace_anon {
11920Sstevel@tonic-gate 	dtrace_state_t *dta_state;		/* DTrace consumer state */
11930Sstevel@tonic-gate 	dtrace_enabling_t *dta_enabling;	/* pointer to enabling */
11940Sstevel@tonic-gate 	processorid_t dta_beganon;		/* which CPU BEGIN ran on */
11950Sstevel@tonic-gate } dtrace_anon_t;
11960Sstevel@tonic-gate 
11970Sstevel@tonic-gate /*
11980Sstevel@tonic-gate  * DTrace Error Debugging
11990Sstevel@tonic-gate  */
12000Sstevel@tonic-gate #ifdef DEBUG
12010Sstevel@tonic-gate #define	DTRACE_ERRDEBUG
12020Sstevel@tonic-gate #endif
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate #ifdef DTRACE_ERRDEBUG
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate typedef struct dtrace_errhash {
12070Sstevel@tonic-gate 	const char	*dter_msg;	/* error message */
12080Sstevel@tonic-gate 	int		dter_count;	/* number of times seen */
12090Sstevel@tonic-gate } dtrace_errhash_t;
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate #define	DTRACE_ERRHASHSZ	256	/* must be > number of err msgs */
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate #endif	/* DTRACE_ERRDEBUG */
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate /*
12160Sstevel@tonic-gate  * DTrace Toxic Ranges
12170Sstevel@tonic-gate  *
12180Sstevel@tonic-gate  * DTrace supports safe loads from probe context; if the address turns out to
12190Sstevel@tonic-gate  * be invalid, a bit will be set by the kernel indicating that DTrace
12200Sstevel@tonic-gate  * encountered a memory error, and DTrace will propagate the error to the user
12210Sstevel@tonic-gate  * accordingly.  However, there may exist some regions of memory in which an
12220Sstevel@tonic-gate  * arbitrary load can change system state, and from which it is impossible to
12230Sstevel@tonic-gate  * recover from such a load after it has been attempted.  Examples of this may
12240Sstevel@tonic-gate  * include memory in which programmable I/O registers are mapped (for which a
12250Sstevel@tonic-gate  * read may have some implications for the device) or (in the specific case of
12260Sstevel@tonic-gate  * UltraSPARC-I and -II) the virtual address hole.  The platform is required
12270Sstevel@tonic-gate  * to make DTrace aware of these toxic ranges; DTrace will then check that
12280Sstevel@tonic-gate  * target addresses are not in a toxic range before attempting to issue a
12290Sstevel@tonic-gate  * safe load.
12300Sstevel@tonic-gate  */
12310Sstevel@tonic-gate typedef struct dtrace_toxrange {
12320Sstevel@tonic-gate 	uintptr_t	dtt_base;		/* base of toxic range */
12330Sstevel@tonic-gate 	uintptr_t	dtt_limit;		/* limit of toxic range */
12340Sstevel@tonic-gate } dtrace_toxrange_t;
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate extern uint64_t dtrace_getarg(int, int);
12370Sstevel@tonic-gate extern greg_t dtrace_getfp(void);
12380Sstevel@tonic-gate extern int dtrace_getipl(void);
12390Sstevel@tonic-gate extern uintptr_t dtrace_caller(int);
12400Sstevel@tonic-gate extern uint32_t dtrace_cas32(uint32_t *, uint32_t, uint32_t);
12410Sstevel@tonic-gate extern void *dtrace_casptr(void *, void *, void *);
1242*3677Ssudheer extern void dtrace_copyin(uintptr_t, uintptr_t, size_t, volatile uint16_t *);
1243*3677Ssudheer extern void dtrace_copyinstr(uintptr_t, uintptr_t, size_t, volatile uint16_t *);
1244*3677Ssudheer extern void dtrace_copyout(uintptr_t, uintptr_t, size_t, volatile uint16_t *);
1245*3677Ssudheer extern void dtrace_copyoutstr(uintptr_t, uintptr_t, size_t,
1246*3677Ssudheer     volatile uint16_t *);
12470Sstevel@tonic-gate extern void dtrace_getpcstack(pc_t *, int, int, uint32_t *);
12480Sstevel@tonic-gate extern ulong_t dtrace_getreg(struct regs *, uint_t);
12490Sstevel@tonic-gate extern int dtrace_getstackdepth(int);
12500Sstevel@tonic-gate extern void dtrace_getupcstack(uint64_t *, int);
12510Sstevel@tonic-gate extern void dtrace_getufpstack(uint64_t *, uint64_t *, int);
1252191Sahl extern int dtrace_getustackdepth(void);
12530Sstevel@tonic-gate extern uintptr_t dtrace_fulword(void *);
12540Sstevel@tonic-gate extern uint8_t dtrace_fuword8(void *);
12550Sstevel@tonic-gate extern uint16_t dtrace_fuword16(void *);
12560Sstevel@tonic-gate extern uint32_t dtrace_fuword32(void *);
12570Sstevel@tonic-gate extern uint64_t dtrace_fuword64(void *);
12580Sstevel@tonic-gate extern void dtrace_probe_error(dtrace_state_t *, dtrace_epid_t, int, int,
12590Sstevel@tonic-gate     int, uintptr_t);
12600Sstevel@tonic-gate extern int dtrace_assfail(const char *, const char *, int);
12610Sstevel@tonic-gate extern int dtrace_attached(void);
12620Sstevel@tonic-gate extern hrtime_t dtrace_gethrestime();
12630Sstevel@tonic-gate 
12640Sstevel@tonic-gate #ifdef __sparc
12650Sstevel@tonic-gate extern void dtrace_flush_windows(void);
12660Sstevel@tonic-gate extern void dtrace_flush_user_windows(void);
12670Sstevel@tonic-gate extern uint_t dtrace_getotherwin(void);
12680Sstevel@tonic-gate extern uint_t dtrace_getfprs(void);
12690Sstevel@tonic-gate #else
12700Sstevel@tonic-gate extern void dtrace_copy(uintptr_t, uintptr_t, size_t);
1271*3677Ssudheer extern void dtrace_copystr(uintptr_t, uintptr_t, size_t, volatile uint16_t *);
12720Sstevel@tonic-gate #endif
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate /*
12750Sstevel@tonic-gate  * DTrace Assertions
12760Sstevel@tonic-gate  *
12770Sstevel@tonic-gate  * DTrace calls ASSERT from probe context.  To assure that a failed ASSERT
12780Sstevel@tonic-gate  * does not induce a markedly more catastrophic failure (e.g., one from which
12790Sstevel@tonic-gate  * a dump cannot be gleaned), DTrace must define its own ASSERT to be one that
12800Sstevel@tonic-gate  * may safely be called from probe context.  This header file must thus be
12810Sstevel@tonic-gate  * included by any DTrace component that calls ASSERT from probe context, and
12820Sstevel@tonic-gate  * _only_ by those components.  (The only exception to this is kernel
12830Sstevel@tonic-gate  * debugging infrastructure at user-level that doesn't depend on calling
12840Sstevel@tonic-gate  * ASSERT.)
12850Sstevel@tonic-gate  */
12860Sstevel@tonic-gate #undef ASSERT
12870Sstevel@tonic-gate #ifdef DEBUG
12880Sstevel@tonic-gate #define	ASSERT(EX)	((void)((EX) || \
12890Sstevel@tonic-gate 			dtrace_assfail(#EX, __FILE__, __LINE__)))
12900Sstevel@tonic-gate #else
12910Sstevel@tonic-gate #define	ASSERT(X)	((void)0)
12920Sstevel@tonic-gate #endif
12930Sstevel@tonic-gate 
12940Sstevel@tonic-gate #ifdef	__cplusplus
12950Sstevel@tonic-gate }
12960Sstevel@tonic-gate #endif
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate #endif /* _SYS_DTRACE_IMPL_H */
1299