xref: /onnv-gate/usr/src/uts/common/sys/dtrace_impl.h (revision 191:b6960a1438df)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 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 */
2410Sstevel@tonic-gate 	uint64_t dtag_initial;			/* initial value */
2420Sstevel@tonic-gate 	void (*dtag_aggregate)(uint64_t *, uint64_t);
2430Sstevel@tonic-gate } dtrace_aggregation_t;
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate /*
2460Sstevel@tonic-gate  * DTrace Buffers
2470Sstevel@tonic-gate  *
2480Sstevel@tonic-gate  * Principal buffers, aggregation buffers, and speculative buffers are all
2490Sstevel@tonic-gate  * managed with the dtrace_buffer structure.  By default, this structure
2500Sstevel@tonic-gate  * includes twin data buffers -- dtb_tomax and dtb_xamot -- that serve as the
2510Sstevel@tonic-gate  * active and passive buffers, respectively.  For speculative buffers,
2520Sstevel@tonic-gate  * dtb_xamot will be NULL; for "ring" and "fill" buffers, dtb_xamot will point
2530Sstevel@tonic-gate  * to a scratch buffer.  For all buffer types, the dtrace_buffer structure is
2540Sstevel@tonic-gate  * always allocated on a per-CPU basis; a single dtrace_buffer structure is
2550Sstevel@tonic-gate  * never shared among CPUs.  (That is, there is never true sharing of the
2560Sstevel@tonic-gate  * dtrace_buffer structure; to prevent false sharing of the structure, it must
2570Sstevel@tonic-gate  * always be aligned to the coherence granularity -- generally 64 bytes.)
2580Sstevel@tonic-gate  *
2590Sstevel@tonic-gate  * One of the critical design decisions of DTrace is that a given ECB always
2600Sstevel@tonic-gate  * stores the same quantity and type of data.  This is done to assure that the
2610Sstevel@tonic-gate  * only metadata required for an ECB's traced data is the EPID.  That is, from
2620Sstevel@tonic-gate  * the EPID, the consumer can determine the data layout.  (The data buffer
2630Sstevel@tonic-gate  * layout is shown schematically below.)  By assuring that one can determine
2640Sstevel@tonic-gate  * data layout from the EPID, the metadata stream can be separated from the
2650Sstevel@tonic-gate  * data stream -- simplifying the data stream enormously.
2660Sstevel@tonic-gate  *
2670Sstevel@tonic-gate  *      base of data buffer --->  +------+--------------------+------+
2680Sstevel@tonic-gate  *                                | EPID | data               | EPID |
2690Sstevel@tonic-gate  *                                +------+--------+------+----+------+
2700Sstevel@tonic-gate  *                                | data          | EPID | data      |
2710Sstevel@tonic-gate  *                                +---------------+------+-----------+
2720Sstevel@tonic-gate  *                                | data, cont.                      |
2730Sstevel@tonic-gate  *                                +------+--------------------+------+
2740Sstevel@tonic-gate  *                                | EPID | data               |      |
2750Sstevel@tonic-gate  *                                +------+--------------------+      |
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  *     limit of data buffer --->  +----------------------------------+
2860Sstevel@tonic-gate  *
2870Sstevel@tonic-gate  * When evaluating an ECB, dtrace_probe() determines if the ECB's needs of the
2880Sstevel@tonic-gate  * principal buffer (both scratch and payload) exceed the available space.  If
2890Sstevel@tonic-gate  * the ECB's needs exceed available space (and if the principal buffer policy
2900Sstevel@tonic-gate  * is the default "switch" policy), the ECB is dropped, the buffer's drop count
2910Sstevel@tonic-gate  * is incremented, and processing advances to the next ECB.  If the ECB's needs
2920Sstevel@tonic-gate  * can be met with the available space, the ECB is processed, but the offset in
2930Sstevel@tonic-gate  * the principal buffer is only advanced if the ECB completes processing
2940Sstevel@tonic-gate  * without error.
2950Sstevel@tonic-gate  *
2960Sstevel@tonic-gate  * When a buffer is to be switched (either because the buffer is the principal
2970Sstevel@tonic-gate  * buffer with a "switch" policy or because it is an aggregation buffer), a
2980Sstevel@tonic-gate  * cross call is issued to the CPU associated with the buffer.  In the cross
2990Sstevel@tonic-gate  * call context, interrupts are disabled, and the active and the inactive
3000Sstevel@tonic-gate  * buffers are atomically switched.  This involves switching the data pointers,
3010Sstevel@tonic-gate  * copying the various state fields (offset, drops, errors, etc.) into their
3020Sstevel@tonic-gate  * inactive equivalents, and clearing the state fields.  Because interrupts are
3030Sstevel@tonic-gate  * disabled during this procedure, the switch is guaranteed to appear atomic to
3040Sstevel@tonic-gate  * dtrace_probe().
3050Sstevel@tonic-gate  *
3060Sstevel@tonic-gate  * DTrace Ring Buffering
3070Sstevel@tonic-gate  *
3080Sstevel@tonic-gate  * To process a ring buffer correctly, one must know the oldest valid record.
3090Sstevel@tonic-gate  * Processing starts at the oldest record in the buffer and continues until
3100Sstevel@tonic-gate  * the end of the buffer is reached.  Processing then resumes starting with
3110Sstevel@tonic-gate  * the record stored at offset 0 in the buffer, and continues until the
3120Sstevel@tonic-gate  * youngest record is processed.  If trace records are of a fixed-length,
3130Sstevel@tonic-gate  * determining the oldest record is trivial:
3140Sstevel@tonic-gate  *
3150Sstevel@tonic-gate  *   - If the ring buffer has not wrapped, the oldest record is the record
3160Sstevel@tonic-gate  *     stored at offset 0.
3170Sstevel@tonic-gate  *
3180Sstevel@tonic-gate  *   - If the ring buffer has wrapped, the oldest record is the record stored
3190Sstevel@tonic-gate  *     at the current offset.
3200Sstevel@tonic-gate  *
3210Sstevel@tonic-gate  * With variable length records, however, just knowing the current offset
3220Sstevel@tonic-gate  * doesn't suffice for determining the oldest valid record:  assuming that one
3230Sstevel@tonic-gate  * allows for arbitrary data, one has no way of searching forward from the
3240Sstevel@tonic-gate  * current offset to find the oldest valid record.  (That is, one has no way
3250Sstevel@tonic-gate  * of separating data from metadata.) It would be possible to simply refuse to
3260Sstevel@tonic-gate  * process any data in the ring buffer between the current offset and the
3270Sstevel@tonic-gate  * limit, but this leaves (potentially) an enormous amount of otherwise valid
3280Sstevel@tonic-gate  * data unprocessed.
3290Sstevel@tonic-gate  *
3300Sstevel@tonic-gate  * To effect ring buffering, we track two offsets in the buffer:  the current
3310Sstevel@tonic-gate  * offset and the _wrapped_ offset.  If a request is made to reserve some
3320Sstevel@tonic-gate  * amount of data, and the buffer has wrapped, the wrapped offset is
3330Sstevel@tonic-gate  * incremented until the wrapped offset minus the current offset is greater
3340Sstevel@tonic-gate  * than or equal to the reserve request.  This is done by repeatedly looking
3350Sstevel@tonic-gate  * up the ECB corresponding to the EPID at the current wrapped offset, and
3360Sstevel@tonic-gate  * incrementing the wrapped offset by the size of the data payload
3370Sstevel@tonic-gate  * corresponding to that ECB.  If this offset is greater than or equal to the
3380Sstevel@tonic-gate  * limit of the data buffer, the wrapped offset is set to 0.  Thus, the
3390Sstevel@tonic-gate  * current offset effectively "chases" the wrapped offset around the buffer.
3400Sstevel@tonic-gate  * Schematically:
3410Sstevel@tonic-gate  *
3420Sstevel@tonic-gate  *      base of data buffer --->  +------+--------------------+------+
3430Sstevel@tonic-gate  *                                | EPID | data               | EPID |
3440Sstevel@tonic-gate  *                                +------+--------+------+----+------+
3450Sstevel@tonic-gate  *                                | data          | EPID | data      |
3460Sstevel@tonic-gate  *                                +---------------+------+-----------+
3470Sstevel@tonic-gate  *                                | data, cont.                      |
3480Sstevel@tonic-gate  *                                +------+---------------------------+
3490Sstevel@tonic-gate  *                                | EPID | data                      |
3500Sstevel@tonic-gate  *           current offset --->  +------+---------------------------+
3510Sstevel@tonic-gate  *                                | invalid data                     |
3520Sstevel@tonic-gate  *           wrapped offset --->  +------+--------------------+------+
3530Sstevel@tonic-gate  *                                | EPID | data               | EPID |
3540Sstevel@tonic-gate  *                                +------+--------+------+----+------+
3550Sstevel@tonic-gate  *                                | data          | EPID | data      |
3560Sstevel@tonic-gate  *                                +---------------+------+-----------+
3570Sstevel@tonic-gate  *                                :                                  :
3580Sstevel@tonic-gate  *                                .                                  .
3590Sstevel@tonic-gate  *                                .        ... valid data ...        .
3600Sstevel@tonic-gate  *                                .                                  .
3610Sstevel@tonic-gate  *                                :                                  :
3620Sstevel@tonic-gate  *                                +------+-------------+------+------+
3630Sstevel@tonic-gate  *                                | EPID | data        | EPID | data |
3640Sstevel@tonic-gate  *                                +------+------------++------+------+
3650Sstevel@tonic-gate  *                                | data, cont.       | leftover     |
3660Sstevel@tonic-gate  *     limit of data buffer --->  +-------------------+--------------+
3670Sstevel@tonic-gate  *
3680Sstevel@tonic-gate  * If the amount of requested buffer space exceeds the amount of space
3690Sstevel@tonic-gate  * available between the current offset and the end of the buffer:
3700Sstevel@tonic-gate  *
3710Sstevel@tonic-gate  *  (1)  all words in the data buffer between the current offset and the limit
3720Sstevel@tonic-gate  *       of the data buffer (marked "leftover", above) are set to
3730Sstevel@tonic-gate  *       DTRACE_EPIDNONE
3740Sstevel@tonic-gate  *
3750Sstevel@tonic-gate  *  (2)  the wrapped offset is set to zero
3760Sstevel@tonic-gate  *
3770Sstevel@tonic-gate  *  (3)  the iteration process described above occurs until the wrapped offset
3780Sstevel@tonic-gate  *       is greater than the amount of desired space.
3790Sstevel@tonic-gate  *
3800Sstevel@tonic-gate  * The wrapped offset is implemented by (re-)using the inactive offset.
3810Sstevel@tonic-gate  * In a "switch" buffer policy, the inactive offset stores the offset in
3820Sstevel@tonic-gate  * the inactive buffer; in a "ring" buffer policy, it stores the wrapped
3830Sstevel@tonic-gate  * offset.
3840Sstevel@tonic-gate  *
3850Sstevel@tonic-gate  * DTrace Scratch Buffering
3860Sstevel@tonic-gate  *
3870Sstevel@tonic-gate  * Some ECBs may wish to allocate dynamically-sized temporary scratch memory.
3880Sstevel@tonic-gate  * To accommodate such requests easily, scratch memory may be allocated in
3890Sstevel@tonic-gate  * the buffer beyond the current offset plus the needed memory of the current
3900Sstevel@tonic-gate  * ECB.  If there isn't sufficient room in the buffer for the requested amount
3910Sstevel@tonic-gate  * of scratch space, the allocation fails and an error is generated.  Scratch
3920Sstevel@tonic-gate  * memory is tracked in the dtrace_mstate_t and is automatically freed when
3930Sstevel@tonic-gate  * the ECB ceases processing.  Note that ring buffers cannot allocate their
3940Sstevel@tonic-gate  * scratch from the principal buffer -- lest they needlessly overwrite older,
3950Sstevel@tonic-gate  * valid data.  Ring buffers therefore have their own dedicated scratch buffer
3960Sstevel@tonic-gate  * from which scratch is allocated.
3970Sstevel@tonic-gate  */
3980Sstevel@tonic-gate #define	DTRACEBUF_RING		0x0001		/* bufpolicy set to "ring" */
3990Sstevel@tonic-gate #define	DTRACEBUF_FILL		0x0002		/* bufpolicy set to "fill" */
4000Sstevel@tonic-gate #define	DTRACEBUF_NOSWITCH	0x0004		/* do not switch buffer */
4010Sstevel@tonic-gate #define	DTRACEBUF_WRAPPED	0x0008		/* ring buffer has wrapped */
4020Sstevel@tonic-gate #define	DTRACEBUF_DROPPED	0x0010		/* drops occurred */
4030Sstevel@tonic-gate #define	DTRACEBUF_ERROR		0x0020		/* errors occurred */
4040Sstevel@tonic-gate #define	DTRACEBUF_FULL		0x0040		/* "fill" buffer is full */
4050Sstevel@tonic-gate #define	DTRACEBUF_CONSUMED	0x0080		/* buffer has been consumed */
4060Sstevel@tonic-gate #define	DTRACEBUF_INACTIVE	0x0100		/* buffer is not yet active */
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate typedef struct dtrace_buffer {
4090Sstevel@tonic-gate 	uint64_t dtb_offset;			/* current offset in buffer */
4100Sstevel@tonic-gate 	uint64_t dtb_size;			/* size of buffer */
4110Sstevel@tonic-gate 	uint32_t dtb_flags;			/* flags */
4120Sstevel@tonic-gate 	uint32_t dtb_drops;			/* number of drops */
4130Sstevel@tonic-gate 	caddr_t dtb_tomax;			/* active buffer */
4140Sstevel@tonic-gate 	caddr_t dtb_xamot;			/* inactive buffer */
4150Sstevel@tonic-gate 	uint32_t dtb_xamot_flags;		/* inactive flags */
4160Sstevel@tonic-gate 	uint32_t dtb_xamot_drops;		/* drops in inactive buffer */
4170Sstevel@tonic-gate 	uint64_t dtb_xamot_offset;		/* offset in inactive buffer */
4180Sstevel@tonic-gate 	uint32_t dtb_errors;			/* number of errors */
4190Sstevel@tonic-gate 	uint32_t dtb_xamot_errors;		/* errors in inactive buffer */
4200Sstevel@tonic-gate #ifndef _LP64
4210Sstevel@tonic-gate 	uint64_t dtb_pad1;
4220Sstevel@tonic-gate #endif
4230Sstevel@tonic-gate } dtrace_buffer_t;
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate /*
4260Sstevel@tonic-gate  * DTrace Aggregation Buffers
4270Sstevel@tonic-gate  *
4280Sstevel@tonic-gate  * Aggregation buffers use much of the same mechanism as described above
4290Sstevel@tonic-gate  * ("DTrace Buffers").  However, because an aggregation is fundamentally a
4300Sstevel@tonic-gate  * hash, there exists dynamic metadata associated with an aggregation buffer
4310Sstevel@tonic-gate  * that is not associated with other kinds of buffers.  This aggregation
4320Sstevel@tonic-gate  * metadata is _only_ relevant for the in-kernel implementation of
4330Sstevel@tonic-gate  * aggregations; it is not actually relevant to user-level consumers.  To do
4340Sstevel@tonic-gate  * this, we allocate dynamic aggregation data (hash keys and hash buckets)
4350Sstevel@tonic-gate  * starting below the _limit_ of the buffer, and we allocate data from the
4360Sstevel@tonic-gate  * _base_ of the buffer.  When the aggregation buffer is copied out, _only_ the
4370Sstevel@tonic-gate  * data is copied out; the metadata is simply discarded.  Schematically,
4380Sstevel@tonic-gate  * aggregation buffers look like:
4390Sstevel@tonic-gate  *
4400Sstevel@tonic-gate  *      base of data buffer --->  +-------+------+-----------+-------+
4410Sstevel@tonic-gate  *                                | aggid | key  | value     | aggid |
4420Sstevel@tonic-gate  *                                +-------+------+-----------+-------+
4430Sstevel@tonic-gate  *                                | key                              |
4440Sstevel@tonic-gate  *                                +-------+-------+-----+------------+
4450Sstevel@tonic-gate  *                                | value | aggid | key | value      |
4460Sstevel@tonic-gate  *                                +-------+------++-----+------+-----+
4470Sstevel@tonic-gate  *                                | aggid | key  | value       |     |
4480Sstevel@tonic-gate  *                                +-------+------+-------------+     |
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  *                                | hash keys                        |
4620Sstevel@tonic-gate  *                                | (dtrace_aggkey structures)       |
4630Sstevel@tonic-gate  *                                |                                  |
4640Sstevel@tonic-gate  *                                +----------------------------------+
4650Sstevel@tonic-gate  *                                | hash buckets                     |
4660Sstevel@tonic-gate  *                                | (dtrace_aggbuffer structure)     |
4670Sstevel@tonic-gate  *                                |                                  |
4680Sstevel@tonic-gate  *     limit of data buffer --->  +----------------------------------+
4690Sstevel@tonic-gate  *
4700Sstevel@tonic-gate  *
4710Sstevel@tonic-gate  * As implied above, just as we assure that ECBs always store a constant
4720Sstevel@tonic-gate  * amount of data, we assure that a given aggregation -- identified by its
4730Sstevel@tonic-gate  * aggregation ID -- always stores data of a constant quantity and type.
4740Sstevel@tonic-gate  * As with EPIDs, this allows the aggregation ID to serve as the metadata for a
4750Sstevel@tonic-gate  * given record.
4760Sstevel@tonic-gate  *
4770Sstevel@tonic-gate  * Note that the size of the dtrace_aggkey structure must be sizeof (uintptr_t)
4780Sstevel@tonic-gate  * aligned.  (If this the structure changes such that this becomes false, an
4790Sstevel@tonic-gate  * assertion will fail in dtrace_aggregate().)
4800Sstevel@tonic-gate  */
4810Sstevel@tonic-gate typedef struct dtrace_aggkey {
4820Sstevel@tonic-gate 	uint32_t dtak_hashval;			/* hash value */
4830Sstevel@tonic-gate 	uint32_t dtak_action:4;			/* action -- 4 bits */
4840Sstevel@tonic-gate 	uint32_t dtak_size:28;			/* size -- 28 bits */
4850Sstevel@tonic-gate 	caddr_t dtak_data;			/* data pointer */
4860Sstevel@tonic-gate 	struct dtrace_aggkey *dtak_next;	/* next in hash chain */
4870Sstevel@tonic-gate } dtrace_aggkey_t;
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate typedef struct dtrace_aggbuffer {
4900Sstevel@tonic-gate 	uintptr_t dtagb_hashsize;		/* number of buckets */
4910Sstevel@tonic-gate 	uintptr_t dtagb_free;			/* free list of keys */
4920Sstevel@tonic-gate 	dtrace_aggkey_t **dtagb_hash;		/* hash table */
4930Sstevel@tonic-gate } dtrace_aggbuffer_t;
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate /*
4960Sstevel@tonic-gate  * DTrace Speculations
4970Sstevel@tonic-gate  *
4980Sstevel@tonic-gate  * Speculations have a per-CPU buffer and a global state.  Once a speculation
4990Sstevel@tonic-gate  * buffer has been comitted or discarded, it cannot be reused until all CPUs
5000Sstevel@tonic-gate  * have taken the same action (commit or discard) on their respective
5010Sstevel@tonic-gate  * speculative buffer.  However, because DTrace probes may execute in arbitrary
5020Sstevel@tonic-gate  * context, other CPUs cannot simply be cross-called at probe firing time to
5030Sstevel@tonic-gate  * perform the necessary commit or discard.  The speculation states thus
5040Sstevel@tonic-gate  * optimize for the case that a speculative buffer is only active on one CPU at
5050Sstevel@tonic-gate  * the time of a commit() or discard() -- for if this is the case, other CPUs
5060Sstevel@tonic-gate  * need not take action, and the speculation is immediately available for
5070Sstevel@tonic-gate  * reuse.  If the speculation is active on multiple CPUs, it must be
5080Sstevel@tonic-gate  * asynchronously cleaned -- potentially leading to a higher rate of dirty
5090Sstevel@tonic-gate  * speculative drops.  The speculation states are as follows:
5100Sstevel@tonic-gate  *
5110Sstevel@tonic-gate  *  DTRACESPEC_INACTIVE       <= Initial state; inactive speculation
5120Sstevel@tonic-gate  *  DTRACESPEC_ACTIVE         <= Allocated, but not yet speculatively traced to
5130Sstevel@tonic-gate  *  DTRACESPEC_ACTIVEONE      <= Speculatively traced to on one CPU
5140Sstevel@tonic-gate  *  DTRACESPEC_ACTIVEMANY     <= Speculatively traced to on more than one CPU
5150Sstevel@tonic-gate  *  DTRACESPEC_COMMITTING     <= Currently being commited on one CPU
5160Sstevel@tonic-gate  *  DTRACESPEC_COMMITTINGMANY <= Currently being commited on many CPUs
5170Sstevel@tonic-gate  *  DTRACESPEC_DISCARDING     <= Currently being discarded on many CPUs
5180Sstevel@tonic-gate  *
5190Sstevel@tonic-gate  * The state transition diagram is as follows:
5200Sstevel@tonic-gate  *
5210Sstevel@tonic-gate  *     +----------------------------------------------------------+
5220Sstevel@tonic-gate  *     |                                                          |
5230Sstevel@tonic-gate  *     |                      +------------+                      |
5240Sstevel@tonic-gate  *     |  +-------------------| COMMITTING |<-----------------+   |
5250Sstevel@tonic-gate  *     |  |                   +------------+                  |   |
5260Sstevel@tonic-gate  *     |  | copied spec.            ^             commit() on |   | discard() on
5270Sstevel@tonic-gate  *     |  | into principal          |              active CPU |   | active CPU
5280Sstevel@tonic-gate  *     |  |                         | commit()                |   |
5290Sstevel@tonic-gate  *     V  V                         |                         |   |
5300Sstevel@tonic-gate  * +----------+                 +--------+                +-----------+
5310Sstevel@tonic-gate  * | INACTIVE |---------------->| ACTIVE |--------------->| ACTIVEONE |
5320Sstevel@tonic-gate  * +----------+  speculation()  +--------+  speculate()   +-----------+
5330Sstevel@tonic-gate  *     ^  ^                         |                         |   |
5340Sstevel@tonic-gate  *     |  |                         | discard()               |   |
5350Sstevel@tonic-gate  *     |  | asynchronously          |            discard() on |   | speculate()
5360Sstevel@tonic-gate  *     |  | cleaned                 V            inactive CPU |   | on inactive
5370Sstevel@tonic-gate  *     |  |                   +------------+                  |   | CPU
5380Sstevel@tonic-gate  *     |  +-------------------| DISCARDING |<-----------------+   |
5390Sstevel@tonic-gate  *     |                      +------------+                      |
5400Sstevel@tonic-gate  *     | asynchronously             ^                             |
5410Sstevel@tonic-gate  *     | copied spec.               |       discard()             |
5420Sstevel@tonic-gate  *     | into principal             +------------------------+    |
5430Sstevel@tonic-gate  *     |                                                     |    V
5440Sstevel@tonic-gate  *  +----------------+             commit()              +------------+
5450Sstevel@tonic-gate  *  | COMMITTINGMANY |<----------------------------------| ACTIVEMANY |
5460Sstevel@tonic-gate  *  +----------------+                                   +------------+
5470Sstevel@tonic-gate  */
5480Sstevel@tonic-gate typedef enum dtrace_speculation_state {
5490Sstevel@tonic-gate 	DTRACESPEC_INACTIVE = 0,
5500Sstevel@tonic-gate 	DTRACESPEC_ACTIVE,
5510Sstevel@tonic-gate 	DTRACESPEC_ACTIVEONE,
5520Sstevel@tonic-gate 	DTRACESPEC_ACTIVEMANY,
5530Sstevel@tonic-gate 	DTRACESPEC_COMMITTING,
5540Sstevel@tonic-gate 	DTRACESPEC_COMMITTINGMANY,
5550Sstevel@tonic-gate 	DTRACESPEC_DISCARDING
5560Sstevel@tonic-gate } dtrace_speculation_state_t;
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate typedef struct dtrace_speculation {
5590Sstevel@tonic-gate 	dtrace_speculation_state_t dtsp_state;	/* current speculation state */
5600Sstevel@tonic-gate 	int dtsp_cleaning;			/* non-zero if being cleaned */
5610Sstevel@tonic-gate 	dtrace_buffer_t *dtsp_buffer;		/* speculative buffer */
5620Sstevel@tonic-gate } dtrace_speculation_t;
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate /*
5650Sstevel@tonic-gate  * DTrace Dynamic Variables
5660Sstevel@tonic-gate  *
5670Sstevel@tonic-gate  * The dynamic variable problem is obviously decomposed into two subproblems:
5680Sstevel@tonic-gate  * allocating new dynamic storage, and freeing old dynamic storage.  The
5690Sstevel@tonic-gate  * presence of the second problem makes the first much more complicated -- or
5700Sstevel@tonic-gate  * rather, the absence of the second renders the first trivial.  This is the
5710Sstevel@tonic-gate  * case with aggregations, for which there is effectively no deallocation of
5720Sstevel@tonic-gate  * dynamic storage.  (Or more accurately, all dynamic storage is deallocated
5730Sstevel@tonic-gate  * when a snapshot is taken of the aggregation.)  As DTrace dynamic variables
5740Sstevel@tonic-gate  * allow for both dynamic allocation and dynamic deallocation, the
5750Sstevel@tonic-gate  * implementation of dynamic variables is quite a bit more complicated than
5760Sstevel@tonic-gate  * that of their aggregation kin.
5770Sstevel@tonic-gate  *
5780Sstevel@tonic-gate  * We observe that allocating new dynamic storage is tricky only because the
5790Sstevel@tonic-gate  * size can vary -- the allocation problem is much easier if allocation sizes
5800Sstevel@tonic-gate  * are uniform.  We further observe that in D, the size of dynamic variables is
5810Sstevel@tonic-gate  * actually _not_ dynamic -- dynamic variable sizes may be determined by static
5820Sstevel@tonic-gate  * analysis of DIF text.  (This is true even of putatively dynamically-sized
5830Sstevel@tonic-gate  * objects like strings and stacks, the sizes of which are dictated by the
5840Sstevel@tonic-gate  * "stringsize" and "stackframes" variables, respectively.)  We exploit this by
5850Sstevel@tonic-gate  * performing this analysis on all DIF before enabling any probes.  For each
5860Sstevel@tonic-gate  * dynamic load or store, we calculate the dynamically-allocated size plus the
5870Sstevel@tonic-gate  * size of the dtrace_dynvar structure plus the storage required to key the
5880Sstevel@tonic-gate  * data.  For all DIF, we take the largest value and dub it the _chunksize_.
5890Sstevel@tonic-gate  * We then divide dynamic memory into two parts:  a hash table that is wide
5900Sstevel@tonic-gate  * enough to have every chunk in its own bucket, and a larger region of equal
5910Sstevel@tonic-gate  * chunksize units.  Whenever we wish to dynamically allocate a variable, we
5920Sstevel@tonic-gate  * always allocate a single chunk of memory.  Depending on the uniformity of
5930Sstevel@tonic-gate  * allocation, this will waste some amount of memory -- but it eliminates the
5940Sstevel@tonic-gate  * non-determinism inherent in traditional heap fragmentation.
5950Sstevel@tonic-gate  *
5960Sstevel@tonic-gate  * Dynamic objects are allocated by storing a non-zero value to them; they are
5970Sstevel@tonic-gate  * deallocated by storing a zero value to them.  Dynamic variables are
5980Sstevel@tonic-gate  * complicated enormously by being shared between CPUs.  In particular,
5990Sstevel@tonic-gate  * consider the following scenario:
6000Sstevel@tonic-gate  *
6010Sstevel@tonic-gate  *                 CPU A                                 CPU B
6020Sstevel@tonic-gate  *  +---------------------------------+   +---------------------------------+
6030Sstevel@tonic-gate  *  |                                 |   |                                 |
6040Sstevel@tonic-gate  *  | allocates dynamic object a[123] |   |                                 |
6050Sstevel@tonic-gate  *  | by storing the value 345 to it  |   |                                 |
6060Sstevel@tonic-gate  *  |                               --------->                              |
6070Sstevel@tonic-gate  *  |                                 |   | wishing to load from object     |
6080Sstevel@tonic-gate  *  |                                 |   | a[123], performs lookup in      |
6090Sstevel@tonic-gate  *  |                                 |   | dynamic variable space          |
6100Sstevel@tonic-gate  *  |                               <---------                              |
6110Sstevel@tonic-gate  *  | deallocates object a[123] by    |   |                                 |
6120Sstevel@tonic-gate  *  | storing 0 to it                 |   |                                 |
6130Sstevel@tonic-gate  *  |                                 |   |                                 |
6140Sstevel@tonic-gate  *  | allocates dynamic object b[567] |   | performs load from a[123]       |
6150Sstevel@tonic-gate  *  | by storing the value 789 to it  |   |                                 |
6160Sstevel@tonic-gate  *  :                                 :   :                                 :
6170Sstevel@tonic-gate  *  .                                 .   .                                 .
6180Sstevel@tonic-gate  *
6190Sstevel@tonic-gate  * This is obviously a race in the D program, but there are nonetheless only
6200Sstevel@tonic-gate  * two valid values for CPU B's load from a[123]:  345 or 0.  Most importantly,
6210Sstevel@tonic-gate  * CPU B may _not_ see the value 789 for a[123].
6220Sstevel@tonic-gate  *
6230Sstevel@tonic-gate  * There are essentially two ways to deal with this:
6240Sstevel@tonic-gate  *
6250Sstevel@tonic-gate  *  (1)  Explicitly spin-lock variables.  That is, if CPU B wishes to load
6260Sstevel@tonic-gate  *       from a[123], it needs to lock a[123] and hold the lock for the
6270Sstevel@tonic-gate  *       duration that it wishes to manipulate it.
6280Sstevel@tonic-gate  *
6290Sstevel@tonic-gate  *  (2)  Avoid reusing freed chunks until it is known that no CPU is referring
6300Sstevel@tonic-gate  *       to them.
6310Sstevel@tonic-gate  *
6320Sstevel@tonic-gate  * The implementation of (1) is rife with complexity, because it requires the
6330Sstevel@tonic-gate  * user of a dynamic variable to explicitly decree when they are done using it.
6340Sstevel@tonic-gate  * Were all variables by value, this perhaps wouldn't be debilitating -- but
6350Sstevel@tonic-gate  * dynamic variables of non-scalar types are tracked by reference.  That is, if
6360Sstevel@tonic-gate  * a dynamic variable is, say, a string, and that variable is to be traced to,
6370Sstevel@tonic-gate  * say, the principal buffer, the DIF emulation code returns to the main
6380Sstevel@tonic-gate  * dtrace_probe() loop a pointer to the underlying storage, not the contents of
6390Sstevel@tonic-gate  * the storage.  Further, code calling on DIF emulation would have to be aware
6400Sstevel@tonic-gate  * that the DIF emulation has returned a reference to a dynamic variable that
6410Sstevel@tonic-gate  * has been potentially locked.  The variable would have to be unlocked after
6420Sstevel@tonic-gate  * the main dtrace_probe() loop is finished with the variable, and the main
6430Sstevel@tonic-gate  * dtrace_probe() loop would have to be careful to not call any further DIF
6440Sstevel@tonic-gate  * emulation while the variable is locked to avoid deadlock.  More generally,
6450Sstevel@tonic-gate  * if one were to implement (1), DIF emulation code dealing with dynamic
6460Sstevel@tonic-gate  * variables could only deal with one dynamic variable at a time (lest deadlock
6470Sstevel@tonic-gate  * result).  To sum, (1) exports too much subtlety to the users of dynamic
6480Sstevel@tonic-gate  * variables -- increasing maintenance burden and imposing serious constraints
6490Sstevel@tonic-gate  * on future DTrace development.
6500Sstevel@tonic-gate  *
6510Sstevel@tonic-gate  * The implementation of (2) is also complex, but the complexity is more
6520Sstevel@tonic-gate  * manageable.  We need to be sure that when a variable is deallocated, it is
6530Sstevel@tonic-gate  * not placed on a traditional free list, but rather on a _dirty_ list.  Once a
6540Sstevel@tonic-gate  * variable is on a dirty list, it cannot be found by CPUs performing a
6550Sstevel@tonic-gate  * subsequent lookup of the variable -- but it may still be in use by other
6560Sstevel@tonic-gate  * CPUs.  To assure that all CPUs that may be seeing the old variable have
6570Sstevel@tonic-gate  * cleared out of probe context, a dtrace_sync() can be issued.  Once the
6580Sstevel@tonic-gate  * dtrace_sync() has completed, it can be known that all CPUs are done
6590Sstevel@tonic-gate  * manipulating the dynamic variable -- the dirty list can be atomically
6600Sstevel@tonic-gate  * appended to the free list.  Unfortunately, there's a slight hiccup in this
6610Sstevel@tonic-gate  * mechanism:  dtrace_sync() may not be issued from probe context.  The
6620Sstevel@tonic-gate  * dtrace_sync() must be therefore issued asynchronously from non-probe
6630Sstevel@tonic-gate  * context.  For this we rely on the DTrace cleaner, a cyclic that runs at the
6640Sstevel@tonic-gate  * "cleanrate" frequency.  To ease this implementation, we define several chunk
6650Sstevel@tonic-gate  * lists:
6660Sstevel@tonic-gate  *
6670Sstevel@tonic-gate  *   - Dirty.  Deallocated chunks, not yet cleaned.  Not available.
6680Sstevel@tonic-gate  *
6690Sstevel@tonic-gate  *   - Rinsing.  Formerly dirty chunks that are currently being asynchronously
6700Sstevel@tonic-gate  *     cleaned.  Not available, but will be shortly.  Dynamic variable
6710Sstevel@tonic-gate  *     allocation may not spin or block for availability, however.
6720Sstevel@tonic-gate  *
6730Sstevel@tonic-gate  *   - Clean.  Clean chunks, ready for allocation -- but not on the free list.
6740Sstevel@tonic-gate  *
6750Sstevel@tonic-gate  *   - Free.  Available for allocation.
6760Sstevel@tonic-gate  *
6770Sstevel@tonic-gate  * Moreover, to avoid absurd contention, _each_ of these lists is implemented
6780Sstevel@tonic-gate  * on a per-CPU basis.  This is only for performance, not correctness; chunks
6790Sstevel@tonic-gate  * may be allocated from another CPU's free list.  The algorithm for allocation
6800Sstevel@tonic-gate  * then is this:
6810Sstevel@tonic-gate  *
6820Sstevel@tonic-gate  *   (1)  Attempt to atomically allocate from current CPU's free list.  If list
6830Sstevel@tonic-gate  *        is non-empty and allocation is successful, allocation is complete.
6840Sstevel@tonic-gate  *
6850Sstevel@tonic-gate  *   (2)  If the clean list is non-empty, atomically move it to the free list,
6860Sstevel@tonic-gate  *        and reattempt (1).
6870Sstevel@tonic-gate  *
6880Sstevel@tonic-gate  *   (3)  If the dynamic variable space is in the CLEAN state, look for free
6890Sstevel@tonic-gate  *        and clean lists on other CPUs by setting the current CPU to the next
6900Sstevel@tonic-gate  *        CPU, and reattempting (1).  If the next CPU is the current CPU (that
6910Sstevel@tonic-gate  *        is, if all CPUs have been checked), atomically switch the state of
6920Sstevel@tonic-gate  *        the dynamic variable space based on the following:
6930Sstevel@tonic-gate  *
6940Sstevel@tonic-gate  *        - If no free chunks were found and no dirty chunks were found,
6950Sstevel@tonic-gate  *          atomically set the state to EMPTY.
6960Sstevel@tonic-gate  *
6970Sstevel@tonic-gate  *        - If dirty chunks were found, atomically set the state to DIRTY.
6980Sstevel@tonic-gate  *
6990Sstevel@tonic-gate  *        - If rinsing chunks were found, atomically set the state to RINSING.
7000Sstevel@tonic-gate  *
7010Sstevel@tonic-gate  *   (4)  Based on state of dynamic variable space state, increment appropriate
7020Sstevel@tonic-gate  *        counter to indicate dynamic drops (if in EMPTY state) vs. dynamic
7030Sstevel@tonic-gate  *        dirty drops (if in DIRTY state) vs. dynamic rinsing drops (if in
7040Sstevel@tonic-gate  *        RINSING state).  Fail the allocation.
7050Sstevel@tonic-gate  *
7060Sstevel@tonic-gate  * The cleaning cyclic operates with the following algorithm:  for all CPUs
7070Sstevel@tonic-gate  * with a non-empty dirty list, atomically move the dirty list to the rinsing
7080Sstevel@tonic-gate  * list.  Perform a dtrace_sync().  For all CPUs with a non-empty rinsing list,
7090Sstevel@tonic-gate  * atomically move the rinsing list to the clean list.  Perform another
7100Sstevel@tonic-gate  * dtrace_sync().  By this point, all CPUs have seen the new clean list; the
7110Sstevel@tonic-gate  * state of the dynamic variable space can be restored to CLEAN.
7120Sstevel@tonic-gate  *
7130Sstevel@tonic-gate  * There exist two final races that merit explanation.  The first is a simple
7140Sstevel@tonic-gate  * allocation race:
7150Sstevel@tonic-gate  *
7160Sstevel@tonic-gate  *                 CPU A                                 CPU B
7170Sstevel@tonic-gate  *  +---------------------------------+   +---------------------------------+
7180Sstevel@tonic-gate  *  |                                 |   |                                 |
7190Sstevel@tonic-gate  *  | allocates dynamic object a[123] |   | allocates dynamic object a[123] |
7200Sstevel@tonic-gate  *  | by storing the value 345 to it  |   | by storing the value 567 to it  |
7210Sstevel@tonic-gate  *  |                                 |   |                                 |
7220Sstevel@tonic-gate  *  :                                 :   :                                 :
7230Sstevel@tonic-gate  *  .                                 .   .                                 .
7240Sstevel@tonic-gate  *
7250Sstevel@tonic-gate  * Again, this is a race in the D program.  It can be resolved by having a[123]
7260Sstevel@tonic-gate  * hold the value 345 or a[123] hold the value 567 -- but it must be true that
7270Sstevel@tonic-gate  * a[123] have only _one_ of these values.  (That is, the racing CPUs may not
7280Sstevel@tonic-gate  * put the same element twice on the same hash chain.)  This is resolved
7290Sstevel@tonic-gate  * simply:  before the allocation is undertaken, the start of the new chunk's
7300Sstevel@tonic-gate  * hash chain is noted.  Later, after the allocation is complete, the hash
7310Sstevel@tonic-gate  * chain is atomically switched to point to the new element.  If this fails
7320Sstevel@tonic-gate  * (because of either concurrent allocations or an allocation concurrent with a
7330Sstevel@tonic-gate  * deletion), the newly allocated chunk is deallocated to the dirty list, and
7340Sstevel@tonic-gate  * the whole process of looking up (and potentially allocating) the dynamic
7350Sstevel@tonic-gate  * variable is reattempted.
7360Sstevel@tonic-gate  *
7370Sstevel@tonic-gate  * The final race is a simple deallocation race:
7380Sstevel@tonic-gate  *
7390Sstevel@tonic-gate  *                 CPU A                                 CPU B
7400Sstevel@tonic-gate  *  +---------------------------------+   +---------------------------------+
7410Sstevel@tonic-gate  *  |                                 |   |                                 |
7420Sstevel@tonic-gate  *  | deallocates dynamic object      |   | deallocates dynamic object      |
7430Sstevel@tonic-gate  *  | a[123] by storing the value 0   |   | a[123] by storing the value 0   |
7440Sstevel@tonic-gate  *  | to it                           |   | to it                           |
7450Sstevel@tonic-gate  *  |                                 |   |                                 |
7460Sstevel@tonic-gate  *  :                                 :   :                                 :
7470Sstevel@tonic-gate  *  .                                 .   .                                 .
7480Sstevel@tonic-gate  *
7490Sstevel@tonic-gate  * Once again, this is a race in the D program, but it is one that we must
7500Sstevel@tonic-gate  * handle without corrupting the underlying data structures.  Because
7510Sstevel@tonic-gate  * deallocations require the deletion of a chunk from the middle of a hash
7520Sstevel@tonic-gate  * chain, we cannot use a single-word atomic operation to remove it.  For this,
7530Sstevel@tonic-gate  * we add a spin lock to the hash buckets that is _only_ used for deallocations
7540Sstevel@tonic-gate  * (allocation races are handled as above).  Further, this spin lock is _only_
7550Sstevel@tonic-gate  * held for the duration of the delete; before control is returned to the DIF
7560Sstevel@tonic-gate  * emulation code, the hash bucket is unlocked.
7570Sstevel@tonic-gate  */
7580Sstevel@tonic-gate typedef struct dtrace_key {
7590Sstevel@tonic-gate 	uint64_t dttk_value;			/* data value or data pointer */
7600Sstevel@tonic-gate 	uint64_t dttk_size;			/* 0 if by-val, >0 if by-ref */
7610Sstevel@tonic-gate } dtrace_key_t;
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate typedef struct dtrace_tuple {
7640Sstevel@tonic-gate 	uint32_t dtt_nkeys;			/* number of keys in tuple */
7650Sstevel@tonic-gate 	uint32_t dtt_pad;			/* padding */
7660Sstevel@tonic-gate 	dtrace_key_t dtt_key[1];		/* array of tuple keys */
7670Sstevel@tonic-gate } dtrace_tuple_t;
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate typedef struct dtrace_dynvar {
7700Sstevel@tonic-gate 	uint64_t dtdv_hashval;			/* hash value -- 0 if free */
7710Sstevel@tonic-gate 	struct dtrace_dynvar *dtdv_next;	/* next on list or hash chain */
7720Sstevel@tonic-gate 	void *dtdv_data;			/* pointer to data */
7730Sstevel@tonic-gate 	dtrace_tuple_t dtdv_tuple;		/* tuple key */
7740Sstevel@tonic-gate } dtrace_dynvar_t;
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate typedef enum dtrace_dynvar_op {
7770Sstevel@tonic-gate 	DTRACE_DYNVAR_ALLOC,
7780Sstevel@tonic-gate 	DTRACE_DYNVAR_NOALLOC,
7790Sstevel@tonic-gate 	DTRACE_DYNVAR_DEALLOC
7800Sstevel@tonic-gate } dtrace_dynvar_op_t;
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate typedef struct dtrace_dynhash {
7830Sstevel@tonic-gate 	dtrace_dynvar_t *dtdh_chain;		/* hash chain for this bucket */
7840Sstevel@tonic-gate 	uintptr_t dtdh_lock;			/* deallocation lock */
7850Sstevel@tonic-gate #ifdef _LP64
7860Sstevel@tonic-gate 	uintptr_t dtdh_pad[6];			/* pad to avoid false sharing */
7870Sstevel@tonic-gate #else
7880Sstevel@tonic-gate 	uintptr_t dtdh_pad[14];			/* pad to avoid false sharing */
7890Sstevel@tonic-gate #endif
7900Sstevel@tonic-gate } dtrace_dynhash_t;
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate typedef struct dtrace_dstate_percpu {
7930Sstevel@tonic-gate 	dtrace_dynvar_t *dtdsc_free;		/* free list for this CPU */
7940Sstevel@tonic-gate 	dtrace_dynvar_t *dtdsc_dirty;		/* dirty list for this CPU */
7950Sstevel@tonic-gate 	dtrace_dynvar_t *dtdsc_rinsing;		/* rinsing list for this CPU */
7960Sstevel@tonic-gate 	dtrace_dynvar_t *dtdsc_clean;		/* clean list for this CPU */
7970Sstevel@tonic-gate 	uint64_t dtdsc_drops;			/* number of capacity drops */
7980Sstevel@tonic-gate 	uint64_t dtdsc_dirty_drops;		/* number of dirty drops */
7990Sstevel@tonic-gate 	uint64_t dtdsc_rinsing_drops;		/* number of rinsing drops */
8000Sstevel@tonic-gate #ifdef _LP64
8010Sstevel@tonic-gate 	uint64_t dtdsc_pad;			/* pad to avoid false sharing */
8020Sstevel@tonic-gate #else
8030Sstevel@tonic-gate 	uint64_t dtdsc_pad[2];			/* pad to avoid false sharing */
8040Sstevel@tonic-gate #endif
8050Sstevel@tonic-gate } dtrace_dstate_percpu_t;
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate typedef enum dtrace_dstate_state {
8080Sstevel@tonic-gate 	DTRACE_DSTATE_CLEAN = 0,
8090Sstevel@tonic-gate 	DTRACE_DSTATE_EMPTY,
8100Sstevel@tonic-gate 	DTRACE_DSTATE_DIRTY,
8110Sstevel@tonic-gate 	DTRACE_DSTATE_RINSING
8120Sstevel@tonic-gate } dtrace_dstate_state_t;
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate typedef struct dtrace_dstate {
8150Sstevel@tonic-gate 	void *dtds_base;			/* base of dynamic var. space */
8160Sstevel@tonic-gate 	size_t dtds_size;			/* size of dynamic var. space */
8170Sstevel@tonic-gate 	size_t dtds_hashsize;			/* number of buckets in hash */
8180Sstevel@tonic-gate 	size_t dtds_chunksize;			/* size of each chunk */
8190Sstevel@tonic-gate 	dtrace_dynhash_t *dtds_hash;		/* pointer to hash table */
8200Sstevel@tonic-gate 	dtrace_dstate_state_t dtds_state;	/* current dynamic var. state */
8210Sstevel@tonic-gate 	dtrace_dstate_percpu_t *dtds_percpu;	/* per-CPU dyn. var. state */
8220Sstevel@tonic-gate } dtrace_dstate_t;
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate /*
8250Sstevel@tonic-gate  * DTrace Variable State
8260Sstevel@tonic-gate  *
8270Sstevel@tonic-gate  * The DTrace variable state tracks user-defined variables in its dtrace_vstate
8280Sstevel@tonic-gate  * structure.  Each DTrace consumer has exactly one dtrace_vstate structure,
8290Sstevel@tonic-gate  * but some dtrace_vstate structures may exist without a corresponding DTrace
8300Sstevel@tonic-gate  * consumer (see "DTrace Helpers", below).  As described in <sys/dtrace.h>,
8310Sstevel@tonic-gate  * user-defined variables can have one of three scopes:
8320Sstevel@tonic-gate  *
8330Sstevel@tonic-gate  *  DIFV_SCOPE_GLOBAL  =>  global scope
8340Sstevel@tonic-gate  *  DIFV_SCOPE_THREAD  =>  thread-local scope (i.e. "self->" variables)
8350Sstevel@tonic-gate  *  DIFV_SCOPE_LOCAL   =>  clause-local scope (i.e. "this->" variables)
8360Sstevel@tonic-gate  *
8370Sstevel@tonic-gate  * The variable state tracks variables by both their scope and their allocation
8380Sstevel@tonic-gate  * type:
8390Sstevel@tonic-gate  *
8400Sstevel@tonic-gate  *  - The dtvs_globals and dtvs_locals members each point to an array of
8410Sstevel@tonic-gate  *    dtrace_statvar structures.  These structures contain both the variable
8420Sstevel@tonic-gate  *    metadata (dtrace_difv structures) and the underlying storage for all
8430Sstevel@tonic-gate  *    statically allocated variables, including statically allocated
8440Sstevel@tonic-gate  *    DIFV_SCOPE_GLOBAL variables and all DIFV_SCOPE_LOCAL variables.
8450Sstevel@tonic-gate  *
8460Sstevel@tonic-gate  *  - The dtvs_tlocals member points to an array of dtrace_difv structures for
8470Sstevel@tonic-gate  *    DIFV_SCOPE_THREAD variables.  As such, this array tracks _only_ the
8480Sstevel@tonic-gate  *    variable metadata for DIFV_SCOPE_THREAD variables; the underlying storage
8490Sstevel@tonic-gate  *    is allocated out of the dynamic variable space.
8500Sstevel@tonic-gate  *
8510Sstevel@tonic-gate  *  - The dtvs_dynvars member is the dynamic variable state associated with the
8520Sstevel@tonic-gate  *    variable state.  The dynamic variable state (described in "DTrace Dynamic
8530Sstevel@tonic-gate  *    Variables", above) tracks all DIFV_SCOPE_THREAD variables and all
8540Sstevel@tonic-gate  *    dynamically-allocated DIFV_SCOPE_GLOBAL variables.
8550Sstevel@tonic-gate  */
8560Sstevel@tonic-gate typedef struct dtrace_statvar {
8570Sstevel@tonic-gate 	uint64_t dtsv_data;			/* data or pointer to it */
8580Sstevel@tonic-gate 	size_t dtsv_size;			/* size of pointed-to data */
8590Sstevel@tonic-gate 	int dtsv_refcnt;			/* reference count */
8600Sstevel@tonic-gate 	dtrace_difv_t dtsv_var;			/* variable metadata */
8610Sstevel@tonic-gate } dtrace_statvar_t;
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate typedef struct dtrace_vstate {
8640Sstevel@tonic-gate 	dtrace_state_t *dtvs_state;		/* back pointer to state */
8650Sstevel@tonic-gate 	dtrace_statvar_t **dtvs_globals;	/* statically-allocated glbls */
8660Sstevel@tonic-gate 	int dtvs_nglobals;			/* number of globals */
8670Sstevel@tonic-gate 	dtrace_difv_t *dtvs_tlocals;		/* thread-local metadata */
8680Sstevel@tonic-gate 	int dtvs_ntlocals;			/* number of thread-locals */
8690Sstevel@tonic-gate 	dtrace_statvar_t **dtvs_locals;		/* clause-local data */
8700Sstevel@tonic-gate 	int dtvs_nlocals;			/* number of clause-locals */
8710Sstevel@tonic-gate 	dtrace_dstate_t dtvs_dynvars;		/* dynamic variable state */
8720Sstevel@tonic-gate } dtrace_vstate_t;
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate /*
8750Sstevel@tonic-gate  * DTrace Machine State
8760Sstevel@tonic-gate  *
8770Sstevel@tonic-gate  * In the process of processing a fired probe, DTrace needs to track and/or
8780Sstevel@tonic-gate  * cache some per-CPU state associated with that particular firing.  This is
8790Sstevel@tonic-gate  * state that is always discarded after the probe firing has completed, and
8800Sstevel@tonic-gate  * much of it is not specific to any DTrace consumer, remaining valid across
8810Sstevel@tonic-gate  * all ECBs.  This state is tracked in the dtrace_mstate structure.
8820Sstevel@tonic-gate  */
8830Sstevel@tonic-gate #define	DTRACE_MSTATE_ARGS		0x00000001
8840Sstevel@tonic-gate #define	DTRACE_MSTATE_PROBE		0x00000002
8850Sstevel@tonic-gate #define	DTRACE_MSTATE_EPID		0x00000004
8860Sstevel@tonic-gate #define	DTRACE_MSTATE_TIMESTAMP		0x00000008
8870Sstevel@tonic-gate #define	DTRACE_MSTATE_STACKDEPTH	0x00000010
8880Sstevel@tonic-gate #define	DTRACE_MSTATE_CALLER		0x00000020
8890Sstevel@tonic-gate #define	DTRACE_MSTATE_IPL		0x00000040
8900Sstevel@tonic-gate #define	DTRACE_MSTATE_FLTOFFS		0x00000080
8910Sstevel@tonic-gate #define	DTRACE_MSTATE_WALLTIMESTAMP	0x00000100
892*191Sahl #define	DTRACE_MSTATE_USTACKDEPTH	0x00000200
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate typedef struct dtrace_mstate {
8950Sstevel@tonic-gate 	uintptr_t dtms_scratch_base;		/* base of scratch space */
8960Sstevel@tonic-gate 	uintptr_t dtms_scratch_ptr;		/* current scratch pointer */
8970Sstevel@tonic-gate 	size_t dtms_scratch_size;		/* scratch size */
8980Sstevel@tonic-gate 	uint32_t dtms_present;			/* variables that are present */
8990Sstevel@tonic-gate 	uint64_t dtms_arg[5];			/* cached arguments */
9000Sstevel@tonic-gate 	dtrace_epid_t dtms_epid;		/* current EPID */
9010Sstevel@tonic-gate 	uint64_t dtms_timestamp;		/* cached timestamp */
9020Sstevel@tonic-gate 	hrtime_t dtms_walltimestamp;		/* cached wall timestamp */
9030Sstevel@tonic-gate 	int dtms_stackdepth;			/* cached stackdepth */
904*191Sahl 	int dtms_ustackdepth;			/* cached ustackdepth */
9050Sstevel@tonic-gate 	struct dtrace_probe *dtms_probe;	/* current probe */
9060Sstevel@tonic-gate 	uintptr_t dtms_caller;			/* cached caller */
9070Sstevel@tonic-gate 	int dtms_ipl;				/* cached interrupt pri lev */
9080Sstevel@tonic-gate 	int dtms_fltoffs;			/* faulting DIFO offset */
9090Sstevel@tonic-gate 	uintptr_t dtms_strtok;			/* saved strtok() pointer */
9100Sstevel@tonic-gate } dtrace_mstate_t;
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate #define	DTRACE_COND_OWNER	0x1
9130Sstevel@tonic-gate #define	DTRACE_COND_USERMODE	0x2
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate #define	DTRACE_PROBEKEY_MAXDEPTH	8	/* max glob recursion depth */
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate /*
9180Sstevel@tonic-gate  * DTrace Activity
9190Sstevel@tonic-gate  *
9200Sstevel@tonic-gate  * Each DTrace consumer is in one of several states, which (for purposes of
9210Sstevel@tonic-gate  * avoiding yet-another overloading of the noun "state") we call the current
9220Sstevel@tonic-gate  * _activity_.  The activity transitions on dtrace_go() (from DTRACIOCGO), on
9230Sstevel@tonic-gate  * dtrace_stop() (from DTRACIOCSTOP) and on the exit() action.  Activities may
9240Sstevel@tonic-gate  * only transition in one direction; the activity transition diagram is a
9250Sstevel@tonic-gate  * directed acyclic graph.  The activity transition diagram is as follows:
9260Sstevel@tonic-gate  *
9270Sstevel@tonic-gate  *
9280Sstevel@tonic-gate  * +----------+                   +--------+                   +--------+
9290Sstevel@tonic-gate  * | INACTIVE |------------------>| WARMUP |------------------>| ACTIVE |
9300Sstevel@tonic-gate  * +----------+   dtrace_go(),    +--------+   dtrace_go(),    +--------+
9310Sstevel@tonic-gate  *                before BEGIN        |        after BEGIN       |  |  |
9320Sstevel@tonic-gate  *                                    |                          |  |  |
9330Sstevel@tonic-gate  *                      exit() action |                          |  |  |
9340Sstevel@tonic-gate  *                     from BEGIN ECB |                          |  |  |
9350Sstevel@tonic-gate  *                                    |                          |  |  |
9360Sstevel@tonic-gate  *                                    v                          |  |  |
9370Sstevel@tonic-gate  *                               +----------+     exit() action  |  |  |
9380Sstevel@tonic-gate  *                               | DRAINING |<-------------------+  |  |
9390Sstevel@tonic-gate  *                               +----------+                       |  |
9400Sstevel@tonic-gate  *                                    |                             |  |
9410Sstevel@tonic-gate  *                     dtrace_stop(), |                             |  |
9420Sstevel@tonic-gate  *                       before END   |                             |  |
9430Sstevel@tonic-gate  *                                    |                             |  |
9440Sstevel@tonic-gate  *                                    v                             |  |
9450Sstevel@tonic-gate  * +---------+                   +----------+                       |  |
9460Sstevel@tonic-gate  * | STOPPED |<------------------| COOLDOWN |<----------------------+  |
9470Sstevel@tonic-gate  * +---------+   dtrace_stop(),  +----------+     dtrace_stop(),       |
9480Sstevel@tonic-gate  *                 after END                       before END          |
9490Sstevel@tonic-gate  *                                                                     |
9500Sstevel@tonic-gate  *                                +--------+                           |
9510Sstevel@tonic-gate  *                                | KILLED |<--------------------------+
9520Sstevel@tonic-gate  *                                +--------+     deadman timeout
9530Sstevel@tonic-gate  *
9540Sstevel@tonic-gate  * Note that once a DTrace consumer has stopped tracing, there is no way to
9550Sstevel@tonic-gate  * restart it; if a DTrace consumer wishes to restart tracing, it must reopen
9560Sstevel@tonic-gate  * the DTrace pseudodevice.
9570Sstevel@tonic-gate  */
9580Sstevel@tonic-gate typedef enum dtrace_activity {
9590Sstevel@tonic-gate 	DTRACE_ACTIVITY_INACTIVE = 0,		/* not yet running */
9600Sstevel@tonic-gate 	DTRACE_ACTIVITY_WARMUP,			/* while starting */
9610Sstevel@tonic-gate 	DTRACE_ACTIVITY_ACTIVE,			/* running */
9620Sstevel@tonic-gate 	DTRACE_ACTIVITY_DRAINING,		/* before stopping */
9630Sstevel@tonic-gate 	DTRACE_ACTIVITY_COOLDOWN,		/* while stopping */
9640Sstevel@tonic-gate 	DTRACE_ACTIVITY_STOPPED,		/* after stopping */
9650Sstevel@tonic-gate 	DTRACE_ACTIVITY_KILLED			/* killed due to deadman */
9660Sstevel@tonic-gate } dtrace_activity_t;
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate /*
9690Sstevel@tonic-gate  * DTrace Helper Implementation
9700Sstevel@tonic-gate  *
9710Sstevel@tonic-gate  * A description of the helper architecture may be found in <sys/dtrace.h>.
9720Sstevel@tonic-gate  * Each process contains a pointer to its helpers in its p_dtrace_helpers
9730Sstevel@tonic-gate  * member.  This is a pointer to a dtrace_helpers structure, which contains an
9740Sstevel@tonic-gate  * array of pointers to dtrace_helper structures, helper variable state (shared
9750Sstevel@tonic-gate  * among a process's helpers) and a generation count.  (The generation count is
9760Sstevel@tonic-gate  * used to provide an identifier when a helper is added so that it may be
9770Sstevel@tonic-gate  * subsequently removed.)  The dtrace_helper structure is self-explanatory,
9780Sstevel@tonic-gate  * containing pointers to the objects needed to execute the helper.  Note that
9790Sstevel@tonic-gate  * helpers are _duplicated_ across fork(2), and destroyed on exec(2).  No more
9800Sstevel@tonic-gate  * than dtrace_helpers_max are allowed per-process.
9810Sstevel@tonic-gate  */
9820Sstevel@tonic-gate #define	DTRACE_HELPER_ACTION_USTACK	0
9830Sstevel@tonic-gate #define	DTRACE_NHELPER_ACTIONS		1
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate typedef struct dtrace_helper_action {
9860Sstevel@tonic-gate 	dtrace_difo_t *dthp_predicate;		/* helper action predicate */
9870Sstevel@tonic-gate 	int dthp_nactions;			/* number of actions */
9880Sstevel@tonic-gate 	dtrace_difo_t **dthp_actions;		/* array of actions */
9890Sstevel@tonic-gate 	int dthp_generation;			/* helper action generation */
9900Sstevel@tonic-gate 	struct dtrace_helper_action *dthp_next;	/* next helper action */
9910Sstevel@tonic-gate } dtrace_helper_action_t;
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate typedef struct dtrace_helper_provider {
9940Sstevel@tonic-gate 	dof_helper_t dthp_prov;			/* DOF w/ provider and probes */
9950Sstevel@tonic-gate 	uint32_t dthp_ref;			/* reference count */
9960Sstevel@tonic-gate } dtrace_helper_provider_t;
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate typedef struct dtrace_helpers {
9990Sstevel@tonic-gate 	dtrace_helper_action_t **dthps_actions;	/* array of helper actions */
10000Sstevel@tonic-gate 	dtrace_vstate_t dthps_vstate;		/* helper action var. state */
10010Sstevel@tonic-gate 	dtrace_helper_provider_t **dthps_provs;	/* array of providers */
10020Sstevel@tonic-gate 	uint_t dthps_nprovs;			/* count of providers */
10030Sstevel@tonic-gate 	int dthps_generation;			/* current generation */
10040Sstevel@tonic-gate 	pid_t dthps_pid;			/* pid of associated proc */
10050Sstevel@tonic-gate 	struct dtrace_helpers *dthps_next;	/* next pointer */
10060Sstevel@tonic-gate 	struct dtrace_helpers *dthps_prev;	/* prev pointer */
10070Sstevel@tonic-gate } dtrace_helpers_t;
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate /*
10100Sstevel@tonic-gate  * DTrace Helper Action Tracing
10110Sstevel@tonic-gate  *
10120Sstevel@tonic-gate  * Debugging helper actions can be arduous.  To ease the development and
10130Sstevel@tonic-gate  * debugging of helpers, DTrace contains a tracing-framework-within-a-tracing-
10140Sstevel@tonic-gate  * framework: helper tracing.  If dtrace_helptrace_enabled is non-zero (which
10150Sstevel@tonic-gate  * it is by default on DEBUG kernels), all helper activity will be traced to a
10160Sstevel@tonic-gate  * global, in-kernel ring buffer.  Each entry includes a pointer to the specific
10170Sstevel@tonic-gate  * helper, the location within the helper, and a trace of all local variables.
10180Sstevel@tonic-gate  * The ring buffer may be displayed in a human-readable format with the
10190Sstevel@tonic-gate  * ::dtrace_helptrace mdb(1) dcmd.
10200Sstevel@tonic-gate  */
10210Sstevel@tonic-gate #define	DTRACE_HELPTRACE_NEXT	(-1)
10220Sstevel@tonic-gate #define	DTRACE_HELPTRACE_DONE	(-2)
10230Sstevel@tonic-gate #define	DTRACE_HELPTRACE_ERR	(-3)
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate typedef struct dtrace_helptrace {
10260Sstevel@tonic-gate 	dtrace_helper_action_t	*dtht_helper;	/* helper action */
10270Sstevel@tonic-gate 	int dtht_where;				/* where in helper action */
10280Sstevel@tonic-gate 	int dtht_nlocals;			/* number of locals */
10290Sstevel@tonic-gate 	uint64_t dtht_locals[1];		/* local variables */
10300Sstevel@tonic-gate } dtrace_helptrace_t;
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate /*
10330Sstevel@tonic-gate  * DTrace Credentials
10340Sstevel@tonic-gate  *
10350Sstevel@tonic-gate  * In probe context, we don't have the flexibility to examine the credentials
10360Sstevel@tonic-gate  * of the DTrace consumer that created a particular enabling.  Instead, we use
10370Sstevel@tonic-gate  * the Least Privilege interfaces to cache the consumer's credentials in a
10380Sstevel@tonic-gate  * dtrace_cred_t structure. That structure contains two important sets of
10390Sstevel@tonic-gate  * credentials that limit the consumer's breadth of visibility and what
10400Sstevel@tonic-gate  * actions the consumer may take.
10410Sstevel@tonic-gate  */
10420Sstevel@tonic-gate #define	DTRACE_CRV_ALLPROC		0x01
10430Sstevel@tonic-gate #define	DTRACE_CRV_KERNEL		0x02
10440Sstevel@tonic-gate 
10450Sstevel@tonic-gate #define	DTRACE_CRV_ALL		(DTRACE_CRV_ALLPROC | DTRACE_CRV_KERNEL)
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate #define	DTRACE_CRA_PROC			0x0001
10480Sstevel@tonic-gate #define	DTRACE_CRA_PROC_DESTRUCTIVE	0x0002
10490Sstevel@tonic-gate #define	DTRACE_CRA_PROC_CONTROL		0x0004
10500Sstevel@tonic-gate #define	DTRACE_CRA_KERNEL		0x0008
10510Sstevel@tonic-gate #define	DTRACE_CRA_KERNEL_DESTRUCTIVE	0x0010
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate #define	DTRACE_CRA_ALL		(DTRACE_CRA_PROC | \
10540Sstevel@tonic-gate 	DTRACE_CRA_PROC_DESTRUCTIVE | DTRACE_CRA_PROC_CONTROL | \
10550Sstevel@tonic-gate 	DTRACE_CRA_KERNEL | DTRACE_CRA_KERNEL_DESTRUCTIVE)
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate typedef struct dtrace_cred {
10580Sstevel@tonic-gate 	uid_t			dcr_uid;
10590Sstevel@tonic-gate 	gid_t			dcr_gid;
10600Sstevel@tonic-gate 	uint8_t			dcr_destructive;
10610Sstevel@tonic-gate 	uint8_t			dcr_visible;
10620Sstevel@tonic-gate 	uint16_t		dcr_action;
10630Sstevel@tonic-gate } dtrace_cred_t;
10640Sstevel@tonic-gate 
10650Sstevel@tonic-gate /*
10660Sstevel@tonic-gate  * DTrace Consumer State
10670Sstevel@tonic-gate  *
10680Sstevel@tonic-gate  * Each DTrace consumer has an associated dtrace_state structure that contains
10690Sstevel@tonic-gate  * its in-kernel DTrace state -- including options, credentials, statistics and
10700Sstevel@tonic-gate  * pointers to ECBs, buffers, speculations and formats.  A dtrace_state
10710Sstevel@tonic-gate  * structure is also allocated for anonymous enablings.  When anonymous state
10720Sstevel@tonic-gate  * is grabbed, the grabbing consumers dts_anon pointer is set to the grabbed
10730Sstevel@tonic-gate  * dtrace_state structure.
10740Sstevel@tonic-gate  */
10750Sstevel@tonic-gate struct dtrace_state {
10760Sstevel@tonic-gate 	dev_t dts_dev;				/* device */
10770Sstevel@tonic-gate 	int dts_necbs;				/* total number of ECBs */
10780Sstevel@tonic-gate 	dtrace_ecb_t **dts_ecbs;		/* array of ECBs */
10790Sstevel@tonic-gate 	dtrace_epid_t dts_epid;			/* next EPID to allocate */
10800Sstevel@tonic-gate 	size_t dts_needed;			/* greatest needed space */
10810Sstevel@tonic-gate 	struct dtrace_state *dts_anon;		/* anon. state, if grabbed */
10820Sstevel@tonic-gate 	dtrace_activity_t dts_activity;		/* current activity */
10830Sstevel@tonic-gate 	dtrace_vstate_t dts_vstate;		/* variable state */
10840Sstevel@tonic-gate 	dtrace_buffer_t *dts_buffer;		/* principal buffer */
10850Sstevel@tonic-gate 	dtrace_buffer_t *dts_aggbuffer;		/* aggregation buffer */
10860Sstevel@tonic-gate 	dtrace_speculation_t *dts_speculations;	/* speculation array */
10870Sstevel@tonic-gate 	int dts_nspeculations;			/* number of speculations */
10880Sstevel@tonic-gate 	int dts_naggregations;			/* number of aggregations */
10890Sstevel@tonic-gate 	dtrace_aggregation_t **dts_aggregations; /* aggregation array */
10900Sstevel@tonic-gate 	vmem_t *dts_aggid_arena;		/* arena for aggregation IDs */
10910Sstevel@tonic-gate 	uint32_t dts_speculations_busy;		/* number of spec. busy */
10920Sstevel@tonic-gate 	uint32_t dts_speculations_unavail;	/* number of spec unavail */
10930Sstevel@tonic-gate 	uint64_t dts_errors;			/* total number of errors */
10940Sstevel@tonic-gate 	uint32_t dts_reserve;			/* space reserved for END */
10950Sstevel@tonic-gate 	hrtime_t dts_laststatus;		/* time of last status */
10960Sstevel@tonic-gate 	cyclic_id_t dts_cleaner;		/* cleaning cyclic */
10970Sstevel@tonic-gate 	cyclic_id_t dts_deadman;		/* deadman cyclic */
10980Sstevel@tonic-gate 	hrtime_t dts_alive;			/* time last alive */
10990Sstevel@tonic-gate 	char dts_speculates;			/* boolean: has speculations */
11000Sstevel@tonic-gate 	char dts_destructive;			/* boolean: has dest. actions */
11010Sstevel@tonic-gate 	int dts_nformats;			/* number of formats */
11020Sstevel@tonic-gate 	char **dts_formats;			/* format string array */
11030Sstevel@tonic-gate 	dtrace_optval_t dts_options[DTRACEOPT_MAX]; /* options */
11040Sstevel@tonic-gate 	dtrace_cred_t dts_cred;			/* credentials */
11050Sstevel@tonic-gate 	size_t dts_nretained;			/* number of retained enabs */
11060Sstevel@tonic-gate };
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate struct dtrace_provider {
11090Sstevel@tonic-gate 	dtrace_pattr_t dtpv_attr;		/* provider attributes */
11100Sstevel@tonic-gate 	dtrace_ppriv_t dtpv_priv;		/* provider privileges */
11110Sstevel@tonic-gate 	dtrace_pops_t dtpv_pops;		/* provider operations */
11120Sstevel@tonic-gate 	char *dtpv_name;			/* provider name */
11130Sstevel@tonic-gate 	void *dtpv_arg;				/* provider argument */
11140Sstevel@tonic-gate 	uint_t dtpv_defunct;			/* boolean: defunct provider */
11150Sstevel@tonic-gate 	struct dtrace_provider *dtpv_next;	/* next provider */
11160Sstevel@tonic-gate };
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate struct dtrace_meta {
11190Sstevel@tonic-gate 	dtrace_mops_t dtm_mops;			/* meta provider operations */
11200Sstevel@tonic-gate 	char *dtm_name;				/* meta provider name */
11210Sstevel@tonic-gate 	void *dtm_arg;				/* meta provider user arg */
11220Sstevel@tonic-gate 	uint64_t dtm_count;			/* no. of associated provs. */
11230Sstevel@tonic-gate };
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate /*
11260Sstevel@tonic-gate  * DTrace Enablings
11270Sstevel@tonic-gate  *
11280Sstevel@tonic-gate  * A dtrace_enabling structure is used to track a collection of ECB
11290Sstevel@tonic-gate  * descriptions -- before they have been turned into actual ECBs.  This is
11300Sstevel@tonic-gate  * created as a result of DOF processing, and is generally used to generate
11310Sstevel@tonic-gate  * ECBs immediately thereafter.  However, enablings are also generally
11320Sstevel@tonic-gate  * retained should the probes they describe be created at a later time; as
11330Sstevel@tonic-gate  * each new module or provider registers with the framework, the retained
11340Sstevel@tonic-gate  * enablings are reevaluated, with any new match resulting in new ECBs.  To
11350Sstevel@tonic-gate  * prevent probes from being matched more than once, the enabling tracks the
11360Sstevel@tonic-gate  * last probe generation matched, and only matches probes from subsequent
11370Sstevel@tonic-gate  * generations.
11380Sstevel@tonic-gate  */
11390Sstevel@tonic-gate typedef struct dtrace_enabling {
11400Sstevel@tonic-gate 	dtrace_ecbdesc_t **dten_desc;		/* all ECB descriptions */
11410Sstevel@tonic-gate 	int dten_ndesc;				/* number of ECB descriptions */
11420Sstevel@tonic-gate 	int dten_maxdesc;			/* size of ECB array */
11430Sstevel@tonic-gate 	dtrace_vstate_t *dten_vstate;		/* associated variable state */
11440Sstevel@tonic-gate 	dtrace_genid_t dten_probegen;		/* matched probe generation */
11450Sstevel@tonic-gate 	dtrace_ecbdesc_t *dten_current;		/* current ECB description */
11460Sstevel@tonic-gate 	int dten_error;				/* current error value */
11470Sstevel@tonic-gate 	int dten_primed;			/* boolean: set if primed */
11480Sstevel@tonic-gate 	struct dtrace_enabling *dten_prev;	/* previous enabling */
11490Sstevel@tonic-gate 	struct dtrace_enabling *dten_next;	/* next enabling */
11500Sstevel@tonic-gate } dtrace_enabling_t;
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate /*
11530Sstevel@tonic-gate  * DTrace Anonymous Enablings
11540Sstevel@tonic-gate  *
11550Sstevel@tonic-gate  * Anonymous enablings are DTrace enablings that are not associated with a
11560Sstevel@tonic-gate  * controlling process, but rather derive their enabling from DOF stored as
11570Sstevel@tonic-gate  * properties in the dtrace.conf file.  If there is an anonymous enabling, a
11580Sstevel@tonic-gate  * DTrace consumer state and enabling are created on attach.  The state may be
11590Sstevel@tonic-gate  * subsequently grabbed by the first consumer specifying the "grabanon"
11600Sstevel@tonic-gate  * option.  As long as an anonymous DTrace enabling exists, dtrace(7D) will
11610Sstevel@tonic-gate  * refuse to unload.
11620Sstevel@tonic-gate  */
11630Sstevel@tonic-gate typedef struct dtrace_anon {
11640Sstevel@tonic-gate 	dtrace_state_t *dta_state;		/* DTrace consumer state */
11650Sstevel@tonic-gate 	dtrace_enabling_t *dta_enabling;	/* pointer to enabling */
11660Sstevel@tonic-gate 	processorid_t dta_beganon;		/* which CPU BEGIN ran on */
11670Sstevel@tonic-gate } dtrace_anon_t;
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate /*
11700Sstevel@tonic-gate  * DTrace Error Debugging
11710Sstevel@tonic-gate  */
11720Sstevel@tonic-gate #ifdef DEBUG
11730Sstevel@tonic-gate #define	DTRACE_ERRDEBUG
11740Sstevel@tonic-gate #endif
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate #ifdef DTRACE_ERRDEBUG
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate typedef struct dtrace_errhash {
11790Sstevel@tonic-gate 	const char	*dter_msg;	/* error message */
11800Sstevel@tonic-gate 	int		dter_count;	/* number of times seen */
11810Sstevel@tonic-gate } dtrace_errhash_t;
11820Sstevel@tonic-gate 
11830Sstevel@tonic-gate #define	DTRACE_ERRHASHSZ	256	/* must be > number of err msgs */
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate #endif	/* DTRACE_ERRDEBUG */
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate /*
11880Sstevel@tonic-gate  * DTrace Toxic Ranges
11890Sstevel@tonic-gate  *
11900Sstevel@tonic-gate  * DTrace supports safe loads from probe context; if the address turns out to
11910Sstevel@tonic-gate  * be invalid, a bit will be set by the kernel indicating that DTrace
11920Sstevel@tonic-gate  * encountered a memory error, and DTrace will propagate the error to the user
11930Sstevel@tonic-gate  * accordingly.  However, there may exist some regions of memory in which an
11940Sstevel@tonic-gate  * arbitrary load can change system state, and from which it is impossible to
11950Sstevel@tonic-gate  * recover from such a load after it has been attempted.  Examples of this may
11960Sstevel@tonic-gate  * include memory in which programmable I/O registers are mapped (for which a
11970Sstevel@tonic-gate  * read may have some implications for the device) or (in the specific case of
11980Sstevel@tonic-gate  * UltraSPARC-I and -II) the virtual address hole.  The platform is required
11990Sstevel@tonic-gate  * to make DTrace aware of these toxic ranges; DTrace will then check that
12000Sstevel@tonic-gate  * target addresses are not in a toxic range before attempting to issue a
12010Sstevel@tonic-gate  * safe load.
12020Sstevel@tonic-gate  */
12030Sstevel@tonic-gate typedef struct dtrace_toxrange {
12040Sstevel@tonic-gate 	uintptr_t	dtt_base;		/* base of toxic range */
12050Sstevel@tonic-gate 	uintptr_t	dtt_limit;		/* limit of toxic range */
12060Sstevel@tonic-gate } dtrace_toxrange_t;
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate extern uint64_t dtrace_getarg(int, int);
12090Sstevel@tonic-gate extern greg_t dtrace_getfp(void);
12100Sstevel@tonic-gate extern int dtrace_getipl(void);
12110Sstevel@tonic-gate extern uintptr_t dtrace_caller(int);
12120Sstevel@tonic-gate extern uint32_t dtrace_cas32(uint32_t *, uint32_t, uint32_t);
12130Sstevel@tonic-gate extern void *dtrace_casptr(void *, void *, void *);
12140Sstevel@tonic-gate extern void dtrace_copyin(uintptr_t, uintptr_t, size_t);
12150Sstevel@tonic-gate extern void dtrace_copyinstr(uintptr_t, uintptr_t, size_t);
12160Sstevel@tonic-gate extern void dtrace_copyout(uintptr_t, uintptr_t, size_t);
12170Sstevel@tonic-gate extern void dtrace_copyoutstr(uintptr_t, uintptr_t, size_t);
12180Sstevel@tonic-gate extern void dtrace_getpcstack(pc_t *, int, int, uint32_t *);
12190Sstevel@tonic-gate extern ulong_t dtrace_getreg(struct regs *, uint_t);
12200Sstevel@tonic-gate extern int dtrace_getstackdepth(int);
12210Sstevel@tonic-gate extern void dtrace_getupcstack(uint64_t *, int);
12220Sstevel@tonic-gate extern void dtrace_getufpstack(uint64_t *, uint64_t *, int);
1223*191Sahl extern int dtrace_getustackdepth(void);
12240Sstevel@tonic-gate extern uintptr_t dtrace_fulword(void *);
12250Sstevel@tonic-gate extern uint8_t dtrace_fuword8(void *);
12260Sstevel@tonic-gate extern uint16_t dtrace_fuword16(void *);
12270Sstevel@tonic-gate extern uint32_t dtrace_fuword32(void *);
12280Sstevel@tonic-gate extern uint64_t dtrace_fuword64(void *);
12290Sstevel@tonic-gate extern void dtrace_probe_error(dtrace_state_t *, dtrace_epid_t, int, int,
12300Sstevel@tonic-gate     int, uintptr_t);
12310Sstevel@tonic-gate extern int dtrace_assfail(const char *, const char *, int);
12320Sstevel@tonic-gate extern int dtrace_attached(void);
12330Sstevel@tonic-gate extern hrtime_t dtrace_gethrestime();
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate #ifdef __sparc
12360Sstevel@tonic-gate extern void dtrace_flush_windows(void);
12370Sstevel@tonic-gate extern void dtrace_flush_user_windows(void);
12380Sstevel@tonic-gate extern uint_t dtrace_getotherwin(void);
12390Sstevel@tonic-gate extern uint_t dtrace_getfprs(void);
12400Sstevel@tonic-gate #else
12410Sstevel@tonic-gate extern void dtrace_copy(uintptr_t, uintptr_t, size_t);
12420Sstevel@tonic-gate extern void dtrace_copystr(uintptr_t, uintptr_t, size_t);
12430Sstevel@tonic-gate #endif
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate /*
12460Sstevel@tonic-gate  * DTrace Assertions
12470Sstevel@tonic-gate  *
12480Sstevel@tonic-gate  * DTrace calls ASSERT from probe context.  To assure that a failed ASSERT
12490Sstevel@tonic-gate  * does not induce a markedly more catastrophic failure (e.g., one from which
12500Sstevel@tonic-gate  * a dump cannot be gleaned), DTrace must define its own ASSERT to be one that
12510Sstevel@tonic-gate  * may safely be called from probe context.  This header file must thus be
12520Sstevel@tonic-gate  * included by any DTrace component that calls ASSERT from probe context, and
12530Sstevel@tonic-gate  * _only_ by those components.  (The only exception to this is kernel
12540Sstevel@tonic-gate  * debugging infrastructure at user-level that doesn't depend on calling
12550Sstevel@tonic-gate  * ASSERT.)
12560Sstevel@tonic-gate  */
12570Sstevel@tonic-gate #undef ASSERT
12580Sstevel@tonic-gate #ifdef DEBUG
12590Sstevel@tonic-gate #define	ASSERT(EX)	((void)((EX) || \
12600Sstevel@tonic-gate 			dtrace_assfail(#EX, __FILE__, __LINE__)))
12610Sstevel@tonic-gate #else
12620Sstevel@tonic-gate #define	ASSERT(X)	((void)0)
12630Sstevel@tonic-gate #endif
12640Sstevel@tonic-gate 
12650Sstevel@tonic-gate #ifdef	__cplusplus
12660Sstevel@tonic-gate }
12670Sstevel@tonic-gate #endif
12680Sstevel@tonic-gate 
12690Sstevel@tonic-gate #endif /* _SYS_DTRACE_IMPL_H */
1270