xref: /xnu-11417.101.15/bsd/sys/dtrace_impl.h (revision e3723e1f17661b24996789d8afc084c0c3303b26)
1*e3723e1fSApple OSS Distributions /*
2*e3723e1fSApple OSS Distributions  * CDDL HEADER START
3*e3723e1fSApple OSS Distributions  *
4*e3723e1fSApple OSS Distributions  * The contents of this file are subject to the terms of the
5*e3723e1fSApple OSS Distributions  * Common Development and Distribution License (the "License").
6*e3723e1fSApple OSS Distributions  * You may not use this file except in compliance with the License.
7*e3723e1fSApple OSS Distributions  *
8*e3723e1fSApple OSS Distributions  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*e3723e1fSApple OSS Distributions  * or http://www.opensolaris.org/os/licensing.
10*e3723e1fSApple OSS Distributions  * See the License for the specific language governing permissions
11*e3723e1fSApple OSS Distributions  * and limitations under the License.
12*e3723e1fSApple OSS Distributions  *
13*e3723e1fSApple OSS Distributions  * When distributing Covered Code, include this CDDL HEADER in each
14*e3723e1fSApple OSS Distributions  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*e3723e1fSApple OSS Distributions  * If applicable, add the following below this CDDL HEADER, with the
16*e3723e1fSApple OSS Distributions  * fields enclosed by brackets "[]" replaced with your own identifying
17*e3723e1fSApple OSS Distributions  * information: Portions Copyright [yyyy] [name of copyright owner]
18*e3723e1fSApple OSS Distributions  *
19*e3723e1fSApple OSS Distributions  * CDDL HEADER END
20*e3723e1fSApple OSS Distributions  */
21*e3723e1fSApple OSS Distributions 
22*e3723e1fSApple OSS Distributions /*
23*e3723e1fSApple OSS Distributions  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24*e3723e1fSApple OSS Distributions  * Use is subject to license terms.
25*e3723e1fSApple OSS Distributions  *
26*e3723e1fSApple OSS Distributions  * Portions Copyright (c) 2012 by Delphix. All rights reserved.
27*e3723e1fSApple OSS Distributions  * Portions Copyright (c) 2016 by Joyent, Inc.
28*e3723e1fSApple OSS Distributions  */
29*e3723e1fSApple OSS Distributions 
30*e3723e1fSApple OSS Distributions #ifndef _SYS_DTRACE_IMPL_H
31*e3723e1fSApple OSS Distributions #define	_SYS_DTRACE_IMPL_H
32*e3723e1fSApple OSS Distributions 
33*e3723e1fSApple OSS Distributions #ifdef	__cplusplus
34*e3723e1fSApple OSS Distributions extern "C" {
35*e3723e1fSApple OSS Distributions #endif
36*e3723e1fSApple OSS Distributions 
37*e3723e1fSApple OSS Distributions /*
38*e3723e1fSApple OSS Distributions  * DTrace Dynamic Tracing Software: Kernel Implementation Interfaces
39*e3723e1fSApple OSS Distributions  *
40*e3723e1fSApple OSS Distributions  * Note: The contents of this file are private to the implementation of the
41*e3723e1fSApple OSS Distributions  * Solaris system and DTrace subsystem and are subject to change at any time
42*e3723e1fSApple OSS Distributions  * without notice.  Applications and drivers using these interfaces will fail
43*e3723e1fSApple OSS Distributions  * to run on future releases.  These interfaces should not be used for any
44*e3723e1fSApple OSS Distributions  * purpose except those expressly outlined in dtrace(7D) and libdtrace(3LIB).
45*e3723e1fSApple OSS Distributions  * Please refer to the "Solaris Dynamic Tracing Guide" for more information.
46*e3723e1fSApple OSS Distributions  */
47*e3723e1fSApple OSS Distributions 
48*e3723e1fSApple OSS Distributions #include <sys/dtrace.h>
49*e3723e1fSApple OSS Distributions #include <kern/kalloc.h>
50*e3723e1fSApple OSS Distributions 
51*e3723e1fSApple OSS Distributions /*
52*e3723e1fSApple OSS Distributions  * DTrace Implementation Locks
53*e3723e1fSApple OSS Distributions  */
54*e3723e1fSApple OSS Distributions extern lck_attr_t dtrace_lck_attr;
55*e3723e1fSApple OSS Distributions extern lck_grp_t dtrace_lck_grp;
56*e3723e1fSApple OSS Distributions extern lck_mtx_t dtrace_procwaitfor_lock;
57*e3723e1fSApple OSS Distributions 
58*e3723e1fSApple OSS Distributions /*
59*e3723e1fSApple OSS Distributions  * DTrace Implementation Constants and Typedefs
60*e3723e1fSApple OSS Distributions  */
61*e3723e1fSApple OSS Distributions #define	DTRACE_MAXPROPLEN		128
62*e3723e1fSApple OSS Distributions #define	DTRACE_DYNVAR_CHUNKSIZE		256
63*e3723e1fSApple OSS Distributions 
64*e3723e1fSApple OSS Distributions struct dtrace_probe;
65*e3723e1fSApple OSS Distributions struct dtrace_ecb;
66*e3723e1fSApple OSS Distributions struct dtrace_predicate;
67*e3723e1fSApple OSS Distributions struct dtrace_action;
68*e3723e1fSApple OSS Distributions struct dtrace_provider;
69*e3723e1fSApple OSS Distributions struct dtrace_state;
70*e3723e1fSApple OSS Distributions 
71*e3723e1fSApple OSS Distributions typedef struct dtrace_probe dtrace_probe_t;
72*e3723e1fSApple OSS Distributions typedef struct dtrace_ecb dtrace_ecb_t;
73*e3723e1fSApple OSS Distributions typedef struct dtrace_predicate dtrace_predicate_t;
74*e3723e1fSApple OSS Distributions typedef struct dtrace_action dtrace_action_t;
75*e3723e1fSApple OSS Distributions typedef struct dtrace_provider dtrace_provider_t;
76*e3723e1fSApple OSS Distributions typedef struct dtrace_meta dtrace_meta_t;
77*e3723e1fSApple OSS Distributions typedef struct dtrace_state dtrace_state_t;
78*e3723e1fSApple OSS Distributions typedef uint32_t dtrace_optid_t;
79*e3723e1fSApple OSS Distributions typedef uint32_t dtrace_specid_t;
80*e3723e1fSApple OSS Distributions typedef uint64_t dtrace_genid_t;
81*e3723e1fSApple OSS Distributions 
82*e3723e1fSApple OSS Distributions /*
83*e3723e1fSApple OSS Distributions  * DTrace Probes
84*e3723e1fSApple OSS Distributions  *
85*e3723e1fSApple OSS Distributions  * The probe is the fundamental unit of the DTrace architecture.  Probes are
86*e3723e1fSApple OSS Distributions  * created by DTrace providers, and managed by the DTrace framework.  A probe
87*e3723e1fSApple OSS Distributions  * is identified by a unique <provider, module, function, name> tuple, and has
88*e3723e1fSApple OSS Distributions  * a unique probe identifier assigned to it.  (Some probes are not associated
89*e3723e1fSApple OSS Distributions  * with a specific point in text; these are called _unanchored probes_ and have
90*e3723e1fSApple OSS Distributions  * no module or function associated with them.)  Probes are represented as a
91*e3723e1fSApple OSS Distributions  * dtrace_probe structure.  To allow quick lookups based on each element of the
92*e3723e1fSApple OSS Distributions  * probe tuple, probes are hashed by each of provider, module, function and
93*e3723e1fSApple OSS Distributions  * name.  (If a lookup is performed based on a regular expression, a
94*e3723e1fSApple OSS Distributions  * dtrace_probekey is prepared, and a linear search is performed.) Each probe
95*e3723e1fSApple OSS Distributions  * is additionally pointed to by a linear array indexed by its identifier.  The
96*e3723e1fSApple OSS Distributions  * identifier is the provider's mechanism for indicating to the DTrace
97*e3723e1fSApple OSS Distributions  * framework that a probe has fired:  the identifier is passed as the first
98*e3723e1fSApple OSS Distributions  * argument to dtrace_probe(), where it is then mapped into the corresponding
99*e3723e1fSApple OSS Distributions  * dtrace_probe structure.  From the dtrace_probe structure, dtrace_probe() can
100*e3723e1fSApple OSS Distributions  * iterate over the probe's list of enabling control blocks; see "DTrace
101*e3723e1fSApple OSS Distributions  * Enabling Control Blocks", below.)
102*e3723e1fSApple OSS Distributions  */
103*e3723e1fSApple OSS Distributions struct dtrace_probe {
104*e3723e1fSApple OSS Distributions 	dtrace_id_t dtpr_id;			/* probe identifier */
105*e3723e1fSApple OSS Distributions 	dtrace_ecb_t *dtpr_ecb;			/* ECB list; see below */
106*e3723e1fSApple OSS Distributions 	dtrace_ecb_t *dtpr_ecb_last;		/* last ECB in list */
107*e3723e1fSApple OSS Distributions 	void *dtpr_arg;				/* provider argument */
108*e3723e1fSApple OSS Distributions 	dtrace_cacheid_t dtpr_predcache;	/* predicate cache ID */
109*e3723e1fSApple OSS Distributions 	int dtpr_aframes;			/* artificial frames */
110*e3723e1fSApple OSS Distributions 	dtrace_provider_t *dtpr_provider;	/* pointer to provider */
111*e3723e1fSApple OSS Distributions 	char *dtpr_mod;				/* probe's module name */
112*e3723e1fSApple OSS Distributions 	char *dtpr_func;			/* probe's function name */
113*e3723e1fSApple OSS Distributions 	char *dtpr_name;			/* probe's name */
114*e3723e1fSApple OSS Distributions 	dtrace_probe_t *dtpr_nextprov;		/* next in provider hash */
115*e3723e1fSApple OSS Distributions 	dtrace_probe_t *dtpr_prevprov;		/* previous in provider hash */
116*e3723e1fSApple OSS Distributions 	dtrace_probe_t *dtpr_nextmod;		/* next in module hash */
117*e3723e1fSApple OSS Distributions 	dtrace_probe_t *dtpr_prevmod;		/* previous in module hash */
118*e3723e1fSApple OSS Distributions 	dtrace_probe_t *dtpr_nextfunc;		/* next in function hash */
119*e3723e1fSApple OSS Distributions 	dtrace_probe_t *dtpr_prevfunc;		/* previous in function hash */
120*e3723e1fSApple OSS Distributions 	dtrace_probe_t *dtpr_nextname;		/* next in name hash */
121*e3723e1fSApple OSS Distributions 	dtrace_probe_t *dtpr_prevname;		/* previous in name hash */
122*e3723e1fSApple OSS Distributions 	dtrace_genid_t dtpr_gen;		/* probe generation ID */
123*e3723e1fSApple OSS Distributions };
124*e3723e1fSApple OSS Distributions 
125*e3723e1fSApple OSS Distributions typedef int dtrace_probekey_f(const char *, const char *, int);
126*e3723e1fSApple OSS Distributions 
127*e3723e1fSApple OSS Distributions typedef struct dtrace_probekey {
128*e3723e1fSApple OSS Distributions 	const char *dtpk_prov;			/* provider name to match */
129*e3723e1fSApple OSS Distributions 	dtrace_probekey_f *dtpk_pmatch;		/* provider matching function */
130*e3723e1fSApple OSS Distributions 	const char *dtpk_mod;			/* module name to match */
131*e3723e1fSApple OSS Distributions 	dtrace_probekey_f *dtpk_mmatch;		/* module matching function */
132*e3723e1fSApple OSS Distributions 	const char *dtpk_func;			/* func name to match */
133*e3723e1fSApple OSS Distributions 	dtrace_probekey_f *dtpk_fmatch;		/* func matching function */
134*e3723e1fSApple OSS Distributions 	const char *dtpk_name;			/* name to match */
135*e3723e1fSApple OSS Distributions 	dtrace_probekey_f *dtpk_nmatch;		/* name matching function */
136*e3723e1fSApple OSS Distributions 	dtrace_id_t dtpk_id;			/* identifier to match */
137*e3723e1fSApple OSS Distributions } dtrace_probekey_t;
138*e3723e1fSApple OSS Distributions 
139*e3723e1fSApple OSS Distributions typedef struct dtrace_hashbucket {
140*e3723e1fSApple OSS Distributions 	struct dtrace_hashbucket *dthb_next;	/* next on hash chain */
141*e3723e1fSApple OSS Distributions 	void *dthb_chain;			/* chain of elements */
142*e3723e1fSApple OSS Distributions 	int dthb_len;				/* number of probes here */
143*e3723e1fSApple OSS Distributions } dtrace_hashbucket_t;
144*e3723e1fSApple OSS Distributions 
145*e3723e1fSApple OSS Distributions typedef const char* dtrace_strkey_f(void*, uintptr_t);
146*e3723e1fSApple OSS Distributions 
147*e3723e1fSApple OSS Distributions typedef struct dtrace_hash {
148*e3723e1fSApple OSS Distributions 	dtrace_hashbucket_t **dth_tab;	/* hash table */
149*e3723e1fSApple OSS Distributions 	int dth_size;			/* size of hash table */
150*e3723e1fSApple OSS Distributions 	int dth_mask;			/* mask to index into table */
151*e3723e1fSApple OSS Distributions 	int dth_nbuckets;		/* total number of buckets */
152*e3723e1fSApple OSS Distributions 	uintptr_t dth_nextoffs;		/* offset of next in element */
153*e3723e1fSApple OSS Distributions 	uintptr_t dth_prevoffs;		/* offset of prev in element */
154*e3723e1fSApple OSS Distributions 	dtrace_strkey_f *dth_getstr;	/* func to retrieve str in element */
155*e3723e1fSApple OSS Distributions 	uintptr_t dth_stroffs;		/* offset of str in element */
156*e3723e1fSApple OSS Distributions } dtrace_hash_t;
157*e3723e1fSApple OSS Distributions 
158*e3723e1fSApple OSS Distributions /*
159*e3723e1fSApple OSS Distributions  * DTrace Enabling Control Blocks
160*e3723e1fSApple OSS Distributions  *
161*e3723e1fSApple OSS Distributions  * When a provider wishes to fire a probe, it calls into dtrace_probe(),
162*e3723e1fSApple OSS Distributions  * passing the probe identifier as the first argument.  As described above,
163*e3723e1fSApple OSS Distributions  * dtrace_probe() maps the identifier into a pointer to a dtrace_probe_t
164*e3723e1fSApple OSS Distributions  * structure.  This structure contains information about the probe, and a
165*e3723e1fSApple OSS Distributions  * pointer to the list of Enabling Control Blocks (ECBs).  Each ECB points to
166*e3723e1fSApple OSS Distributions  * DTrace consumer state, and contains an optional predicate, and a list of
167*e3723e1fSApple OSS Distributions  * actions.  (Shown schematically below.)  The ECB abstraction allows a single
168*e3723e1fSApple OSS Distributions  * probe to be multiplexed across disjoint consumers, or across disjoint
169*e3723e1fSApple OSS Distributions  * enablings of a single probe within one consumer.
170*e3723e1fSApple OSS Distributions  *
171*e3723e1fSApple OSS Distributions  *   Enabling Control Block
172*e3723e1fSApple OSS Distributions  *        dtrace_ecb_t
173*e3723e1fSApple OSS Distributions  * +------------------------+
174*e3723e1fSApple OSS Distributions  * | dtrace_epid_t ---------+--------------> Enabled Probe ID (EPID)
175*e3723e1fSApple OSS Distributions  * | dtrace_state_t * ------+--------------> State associated with this ECB
176*e3723e1fSApple OSS Distributions  * | dtrace_predicate_t * --+---------+
177*e3723e1fSApple OSS Distributions  * | dtrace_action_t * -----+----+    |
178*e3723e1fSApple OSS Distributions  * | dtrace_ecb_t * ---+    |    |    |       Predicate (if any)
179*e3723e1fSApple OSS Distributions  * +-------------------+----+    |    |       dtrace_predicate_t
180*e3723e1fSApple OSS Distributions  *                     |         |    +---> +--------------------+
181*e3723e1fSApple OSS Distributions  *                     |         |          | dtrace_difo_t * ---+----> DIFO
182*e3723e1fSApple OSS Distributions  *                     |         |          +--------------------+
183*e3723e1fSApple OSS Distributions  *                     |         |
184*e3723e1fSApple OSS Distributions  *            Next ECB |         |           Action
185*e3723e1fSApple OSS Distributions  *            (if any) |         |       dtrace_action_t
186*e3723e1fSApple OSS Distributions  *                     :         +--> +-------------------+
187*e3723e1fSApple OSS Distributions  *                     :              | dtrace_actkind_t -+------> kind
188*e3723e1fSApple OSS Distributions  *                     v              | dtrace_difo_t * --+------> DIFO (if any)
189*e3723e1fSApple OSS Distributions  *                                    | dtrace_recdesc_t -+------> record descr.
190*e3723e1fSApple OSS Distributions  *                                    | dtrace_action_t * +------+
191*e3723e1fSApple OSS Distributions  *                                    +-------------------+      |
192*e3723e1fSApple OSS Distributions  *                                                               | Next action
193*e3723e1fSApple OSS Distributions  *                               +-------------------------------+  (if any)
194*e3723e1fSApple OSS Distributions  *                               |
195*e3723e1fSApple OSS Distributions  *                               |           Action
196*e3723e1fSApple OSS Distributions  *                               |       dtrace_action_t
197*e3723e1fSApple OSS Distributions  *                               +--> +-------------------+
198*e3723e1fSApple OSS Distributions  *                                    | dtrace_actkind_t -+------> kind
199*e3723e1fSApple OSS Distributions  *                                    | dtrace_difo_t * --+------> DIFO (if any)
200*e3723e1fSApple OSS Distributions  *                                    | dtrace_action_t * +------+
201*e3723e1fSApple OSS Distributions  *                                    +-------------------+      |
202*e3723e1fSApple OSS Distributions  *                                                               | Next action
203*e3723e1fSApple OSS Distributions  *                               +-------------------------------+  (if any)
204*e3723e1fSApple OSS Distributions  *                               |
205*e3723e1fSApple OSS Distributions  *                               :
206*e3723e1fSApple OSS Distributions  *                               v
207*e3723e1fSApple OSS Distributions  *
208*e3723e1fSApple OSS Distributions  *
209*e3723e1fSApple OSS Distributions  * dtrace_probe() iterates over the ECB list.  If the ECB needs less space
210*e3723e1fSApple OSS Distributions  * than is available in the principal buffer, the ECB is processed:  if the
211*e3723e1fSApple OSS Distributions  * predicate is non-NULL, the DIF object is executed.  If the result is
212*e3723e1fSApple OSS Distributions  * non-zero, the action list is processed, with each action being executed
213*e3723e1fSApple OSS Distributions  * accordingly.  When the action list has been completely executed, processing
214*e3723e1fSApple OSS Distributions  * advances to the next ECB. The ECB abstraction allows disjoint consumers
215*e3723e1fSApple OSS Distributions  * to multiplex on single probes.
216*e3723e1fSApple OSS Distributions  *
217*e3723e1fSApple OSS Distributions  * Execution of the ECB results in consuming dte_size bytes in the buffer
218*e3723e1fSApple OSS Distributions  * to record data.  During execution, dte_needed bytes must be available in
219*e3723e1fSApple OSS Distributions  * the buffer.  This space is used for both recorded data and tuple data.
220*e3723e1fSApple OSS Distributions  */
221*e3723e1fSApple OSS Distributions struct dtrace_ecb {
222*e3723e1fSApple OSS Distributions 	dtrace_epid_t dte_epid;			/* enabled probe ID */
223*e3723e1fSApple OSS Distributions 	uint32_t dte_alignment;			/* required alignment */
224*e3723e1fSApple OSS Distributions 	size_t dte_needed;			/* space needed for execution */
225*e3723e1fSApple OSS Distributions 	size_t dte_size;			/* size of recorded payload */
226*e3723e1fSApple OSS Distributions 	dtrace_predicate_t *dte_predicate;	/* predicate, if any */
227*e3723e1fSApple OSS Distributions 	dtrace_action_t *dte_action;		/* actions, if any */
228*e3723e1fSApple OSS Distributions 	dtrace_ecb_t *dte_next;			/* next ECB on probe */
229*e3723e1fSApple OSS Distributions 	dtrace_state_t *dte_state;		/* pointer to state */
230*e3723e1fSApple OSS Distributions 	uint32_t dte_cond;			/* security condition */
231*e3723e1fSApple OSS Distributions 	dtrace_probe_t *dte_probe;		/* pointer to probe */
232*e3723e1fSApple OSS Distributions 	dtrace_action_t *dte_action_last;	/* last action on ECB */
233*e3723e1fSApple OSS Distributions 	uint64_t dte_uarg;			/* library argument */
234*e3723e1fSApple OSS Distributions };
235*e3723e1fSApple OSS Distributions 
236*e3723e1fSApple OSS Distributions struct dtrace_predicate {
237*e3723e1fSApple OSS Distributions 	dtrace_difo_t *dtp_difo;		/* DIF object */
238*e3723e1fSApple OSS Distributions 	dtrace_cacheid_t dtp_cacheid;		/* cache identifier */
239*e3723e1fSApple OSS Distributions 	int dtp_refcnt;				/* reference count */
240*e3723e1fSApple OSS Distributions };
241*e3723e1fSApple OSS Distributions 
242*e3723e1fSApple OSS Distributions struct dtrace_action {
243*e3723e1fSApple OSS Distributions 	dtrace_actkind_t dta_kind;		/* kind of action */
244*e3723e1fSApple OSS Distributions 	uint16_t dta_intuple;			/* boolean:  in aggregation */
245*e3723e1fSApple OSS Distributions 	uint32_t dta_refcnt;			/* reference count */
246*e3723e1fSApple OSS Distributions 	dtrace_difo_t *dta_difo;		/* pointer to DIFO */
247*e3723e1fSApple OSS Distributions 	dtrace_recdesc_t dta_rec;		/* record description */
248*e3723e1fSApple OSS Distributions 	dtrace_action_t *dta_prev;		/* previous action */
249*e3723e1fSApple OSS Distributions 	dtrace_action_t *dta_next;		/* next action */
250*e3723e1fSApple OSS Distributions };
251*e3723e1fSApple OSS Distributions 
252*e3723e1fSApple OSS Distributions typedef struct dtrace_aggregation {
253*e3723e1fSApple OSS Distributions 	dtrace_action_t dtag_action;		/* action; must be first */
254*e3723e1fSApple OSS Distributions 	dtrace_aggid_t dtag_id;			/* identifier */
255*e3723e1fSApple OSS Distributions 	dtrace_ecb_t *dtag_ecb;			/* corresponding ECB */
256*e3723e1fSApple OSS Distributions 	dtrace_action_t *dtag_first;		/* first action in tuple */
257*e3723e1fSApple OSS Distributions 	uint32_t dtag_base;			/* base of aggregation */
258*e3723e1fSApple OSS Distributions 	uint8_t dtag_hasarg;			/* boolean:  has argument */
259*e3723e1fSApple OSS Distributions 	uint64_t dtag_initial;			/* initial value */
260*e3723e1fSApple OSS Distributions 	void (*dtag_aggregate)(uint64_t *, uint64_t, uint64_t);
261*e3723e1fSApple OSS Distributions } dtrace_aggregation_t;
262*e3723e1fSApple OSS Distributions 
263*e3723e1fSApple OSS Distributions /*
264*e3723e1fSApple OSS Distributions  * DTrace Buffers
265*e3723e1fSApple OSS Distributions  *
266*e3723e1fSApple OSS Distributions  * Principal buffers, aggregation buffers, and speculative buffers are all
267*e3723e1fSApple OSS Distributions  * managed with the dtrace_buffer structure.  By default, this structure
268*e3723e1fSApple OSS Distributions  * includes twin data buffers -- dtb_tomax and dtb_xamot -- that serve as the
269*e3723e1fSApple OSS Distributions  * active and passive buffers, respectively.  For speculative buffers,
270*e3723e1fSApple OSS Distributions  * dtb_xamot will be NULL; for "ring" and "fill" buffers, dtb_xamot will point
271*e3723e1fSApple OSS Distributions  * to a scratch buffer.  For all buffer types, the dtrace_buffer structure is
272*e3723e1fSApple OSS Distributions  * always allocated on a per-CPU basis; a single dtrace_buffer structure is
273*e3723e1fSApple OSS Distributions  * never shared among CPUs.  (That is, there is never true sharing of the
274*e3723e1fSApple OSS Distributions  * dtrace_buffer structure; to prevent false sharing of the structure, it must
275*e3723e1fSApple OSS Distributions  * always be aligned to the coherence granularity -- generally 64 bytes.)
276*e3723e1fSApple OSS Distributions  *
277*e3723e1fSApple OSS Distributions  * One of the critical design decisions of DTrace is that a given ECB always
278*e3723e1fSApple OSS Distributions  * stores the same quantity and type of data.  This is done to assure that the
279*e3723e1fSApple OSS Distributions  * only metadata required for an ECB's traced data is the EPID.  That is, from
280*e3723e1fSApple OSS Distributions  * the EPID, the consumer can determine the data layout.  (The data buffer
281*e3723e1fSApple OSS Distributions  * layout is shown schematically below.)  By assuring that one can determine
282*e3723e1fSApple OSS Distributions  * data layout from the EPID, the metadata stream can be separated from the
283*e3723e1fSApple OSS Distributions  * data stream -- simplifying the data stream enormously.  The ECB always
284*e3723e1fSApple OSS Distributions  * proceeds the recorded data as part of the dtrace_rechdr_t structure that
285*e3723e1fSApple OSS Distributions  * includes the EPID and a high-resolution timestamp used for output ordering
286*e3723e1fSApple OSS Distributions  * consistency.
287*e3723e1fSApple OSS Distributions  *
288*e3723e1fSApple OSS Distributions  *      base of data buffer --->  +--------+--------------------+--------+
289*e3723e1fSApple OSS Distributions  *                                | rechdr | data               | rechdr |
290*e3723e1fSApple OSS Distributions  *                                +--------+------+--------+----+--------+
291*e3723e1fSApple OSS Distributions  *                                | data          | rechdr | data        |
292*e3723e1fSApple OSS Distributions  *                                +---------------+--------+-------------+
293*e3723e1fSApple OSS Distributions  *                                | data, cont.                          |
294*e3723e1fSApple OSS Distributions  *                                +--------+--------------------+--------+
295*e3723e1fSApple OSS Distributions  *                                | rechdr | data               |        |
296*e3723e1fSApple OSS Distributions  *                                +--------+--------------------+        |
297*e3723e1fSApple OSS Distributions  *                                |                ||                    |
298*e3723e1fSApple OSS Distributions  *                                |                ||                    |
299*e3723e1fSApple OSS Distributions  *                                |                \/                    |
300*e3723e1fSApple OSS Distributions  *                                :                                      :
301*e3723e1fSApple OSS Distributions  *                                .                                      .
302*e3723e1fSApple OSS Distributions  *                                .                                      .
303*e3723e1fSApple OSS Distributions  *                                .                                      .
304*e3723e1fSApple OSS Distributions  *                                :                                      :
305*e3723e1fSApple OSS Distributions  *                                |                                      |
306*e3723e1fSApple OSS Distributions  *     limit of data buffer --->  +--------------------------------------+
307*e3723e1fSApple OSS Distributions  *
308*e3723e1fSApple OSS Distributions  * When evaluating an ECB, dtrace_probe() determines if the ECB's needs of the
309*e3723e1fSApple OSS Distributions  * principal buffer (both scratch and payload) exceed the available space.  If
310*e3723e1fSApple OSS Distributions  * the ECB's needs exceed available space (and if the principal buffer policy
311*e3723e1fSApple OSS Distributions  * is the default "switch" policy), the ECB is dropped, the buffer's drop count
312*e3723e1fSApple OSS Distributions  * is incremented, and processing advances to the next ECB.  If the ECB's needs
313*e3723e1fSApple OSS Distributions  * can be met with the available space, the ECB is processed, but the offset in
314*e3723e1fSApple OSS Distributions  * the principal buffer is only advanced if the ECB completes processing
315*e3723e1fSApple OSS Distributions  * without error.
316*e3723e1fSApple OSS Distributions  *
317*e3723e1fSApple OSS Distributions  * When a buffer is to be switched (either because the buffer is the principal
318*e3723e1fSApple OSS Distributions  * buffer with a "switch" policy or because it is an aggregation buffer), a
319*e3723e1fSApple OSS Distributions  * cross call is issued to the CPU associated with the buffer.  In the cross
320*e3723e1fSApple OSS Distributions  * call context, interrupts are disabled, and the active and the inactive
321*e3723e1fSApple OSS Distributions  * buffers are atomically switched.  This involves switching the data pointers,
322*e3723e1fSApple OSS Distributions  * copying the various state fields (offset, drops, errors, etc.) into their
323*e3723e1fSApple OSS Distributions  * inactive equivalents, and clearing the state fields.  Because interrupts are
324*e3723e1fSApple OSS Distributions  * disabled during this procedure, the switch is guaranteed to appear atomic to
325*e3723e1fSApple OSS Distributions  * dtrace_probe().
326*e3723e1fSApple OSS Distributions  *
327*e3723e1fSApple OSS Distributions  * DTrace Ring Buffering
328*e3723e1fSApple OSS Distributions  *
329*e3723e1fSApple OSS Distributions  * To process a ring buffer correctly, one must know the oldest valid record.
330*e3723e1fSApple OSS Distributions  * Processing starts at the oldest record in the buffer and continues until
331*e3723e1fSApple OSS Distributions  * the end of the buffer is reached.  Processing then resumes starting with
332*e3723e1fSApple OSS Distributions  * the record stored at offset 0 in the buffer, and continues until the
333*e3723e1fSApple OSS Distributions  * youngest record is processed.  If trace records are of a fixed-length,
334*e3723e1fSApple OSS Distributions  * determining the oldest record is trivial:
335*e3723e1fSApple OSS Distributions  *
336*e3723e1fSApple OSS Distributions  *   - If the ring buffer has not wrapped, the oldest record is the record
337*e3723e1fSApple OSS Distributions  *     stored at offset 0.
338*e3723e1fSApple OSS Distributions  *
339*e3723e1fSApple OSS Distributions  *   - If the ring buffer has wrapped, the oldest record is the record stored
340*e3723e1fSApple OSS Distributions  *     at the current offset.
341*e3723e1fSApple OSS Distributions  *
342*e3723e1fSApple OSS Distributions  * With variable length records, however, just knowing the current offset
343*e3723e1fSApple OSS Distributions  * doesn't suffice for determining the oldest valid record:  assuming that one
344*e3723e1fSApple OSS Distributions  * allows for arbitrary data, one has no way of searching forward from the
345*e3723e1fSApple OSS Distributions  * current offset to find the oldest valid record.  (That is, one has no way
346*e3723e1fSApple OSS Distributions  * of separating data from metadata.) It would be possible to simply refuse to
347*e3723e1fSApple OSS Distributions  * process any data in the ring buffer between the current offset and the
348*e3723e1fSApple OSS Distributions  * limit, but this leaves (potentially) an enormous amount of otherwise valid
349*e3723e1fSApple OSS Distributions  * data unprocessed.
350*e3723e1fSApple OSS Distributions  *
351*e3723e1fSApple OSS Distributions  * To effect ring buffering, we track two offsets in the buffer:  the current
352*e3723e1fSApple OSS Distributions  * offset and the _wrapped_ offset.  If a request is made to reserve some
353*e3723e1fSApple OSS Distributions  * amount of data, and the buffer has wrapped, the wrapped offset is
354*e3723e1fSApple OSS Distributions  * incremented until the wrapped offset minus the current offset is greater
355*e3723e1fSApple OSS Distributions  * than or equal to the reserve request.  This is done by repeatedly looking
356*e3723e1fSApple OSS Distributions  * up the ECB corresponding to the EPID at the current wrapped offset, and
357*e3723e1fSApple OSS Distributions  * incrementing the wrapped offset by the size of the data payload
358*e3723e1fSApple OSS Distributions  * corresponding to that ECB.  If this offset is greater than or equal to the
359*e3723e1fSApple OSS Distributions  * limit of the data buffer, the wrapped offset is set to 0.  Thus, the
360*e3723e1fSApple OSS Distributions  * current offset effectively "chases" the wrapped offset around the buffer.
361*e3723e1fSApple OSS Distributions  * Schematically:
362*e3723e1fSApple OSS Distributions  *
363*e3723e1fSApple OSS Distributions  *      base of data buffer --->  +------+--------------------+------+
364*e3723e1fSApple OSS Distributions  *                                | EPID | data               | EPID |
365*e3723e1fSApple OSS Distributions  *                                +------+--------+------+----+------+
366*e3723e1fSApple OSS Distributions  *                                | data          | EPID | data      |
367*e3723e1fSApple OSS Distributions  *                                +---------------+------+-----------+
368*e3723e1fSApple OSS Distributions  *                                | data, cont.                      |
369*e3723e1fSApple OSS Distributions  *                                +------+---------------------------+
370*e3723e1fSApple OSS Distributions  *                                | EPID | data                      |
371*e3723e1fSApple OSS Distributions  *           current offset --->  +------+---------------------------+
372*e3723e1fSApple OSS Distributions  *                                | invalid data                     |
373*e3723e1fSApple OSS Distributions  *           wrapped offset --->  +------+--------------------+------+
374*e3723e1fSApple OSS Distributions  *                                | EPID | data               | EPID |
375*e3723e1fSApple OSS Distributions  *                                +------+--------+------+----+------+
376*e3723e1fSApple OSS Distributions  *                                | data          | EPID | data      |
377*e3723e1fSApple OSS Distributions  *                                +---------------+------+-----------+
378*e3723e1fSApple OSS Distributions  *                                :                                  :
379*e3723e1fSApple OSS Distributions  *                                .                                  .
380*e3723e1fSApple OSS Distributions  *                                .        ... valid data ...        .
381*e3723e1fSApple OSS Distributions  *                                .                                  .
382*e3723e1fSApple OSS Distributions  *                                :                                  :
383*e3723e1fSApple OSS Distributions  *                                +------+-------------+------+------+
384*e3723e1fSApple OSS Distributions  *                                | EPID | data        | EPID | data |
385*e3723e1fSApple OSS Distributions  *                                +------+------------++------+------+
386*e3723e1fSApple OSS Distributions  *                                | data, cont.       | leftover     |
387*e3723e1fSApple OSS Distributions  *     limit of data buffer --->  +-------------------+--------------+
388*e3723e1fSApple OSS Distributions  *
389*e3723e1fSApple OSS Distributions  * If the amount of requested buffer space exceeds the amount of space
390*e3723e1fSApple OSS Distributions  * available between the current offset and the end of the buffer:
391*e3723e1fSApple OSS Distributions  *
392*e3723e1fSApple OSS Distributions  *  (1)  all words in the data buffer between the current offset and the limit
393*e3723e1fSApple OSS Distributions  *       of the data buffer (marked "leftover", above) are set to
394*e3723e1fSApple OSS Distributions  *       DTRACE_EPIDNONE
395*e3723e1fSApple OSS Distributions  *
396*e3723e1fSApple OSS Distributions  *  (2)  the wrapped offset is set to zero
397*e3723e1fSApple OSS Distributions  *
398*e3723e1fSApple OSS Distributions  *  (3)  the iteration process described above occurs until the wrapped offset
399*e3723e1fSApple OSS Distributions  *       is greater than the amount of desired space.
400*e3723e1fSApple OSS Distributions  *
401*e3723e1fSApple OSS Distributions  * The wrapped offset is implemented by (re-)using the inactive offset.
402*e3723e1fSApple OSS Distributions  * In a "switch" buffer policy, the inactive offset stores the offset in
403*e3723e1fSApple OSS Distributions  * the inactive buffer; in a "ring" buffer policy, it stores the wrapped
404*e3723e1fSApple OSS Distributions  * offset.
405*e3723e1fSApple OSS Distributions  *
406*e3723e1fSApple OSS Distributions  * DTrace Scratch Buffering
407*e3723e1fSApple OSS Distributions  *
408*e3723e1fSApple OSS Distributions  * Some ECBs may wish to allocate dynamically-sized temporary scratch memory.
409*e3723e1fSApple OSS Distributions  * To accommodate such requests easily, scratch memory may be allocated in
410*e3723e1fSApple OSS Distributions  * the buffer beyond the current offset plus the needed memory of the current
411*e3723e1fSApple OSS Distributions  * ECB.  If there isn't sufficient room in the buffer for the requested amount
412*e3723e1fSApple OSS Distributions  * of scratch space, the allocation fails and an error is generated.  Scratch
413*e3723e1fSApple OSS Distributions  * memory is tracked in the dtrace_mstate_t and is automatically freed when
414*e3723e1fSApple OSS Distributions  * the ECB ceases processing.  Note that ring buffers cannot allocate their
415*e3723e1fSApple OSS Distributions  * scratch from the principal buffer -- lest they needlessly overwrite older,
416*e3723e1fSApple OSS Distributions  * valid data.  Ring buffers therefore have their own dedicated scratch buffer
417*e3723e1fSApple OSS Distributions  * from which scratch is allocated.
418*e3723e1fSApple OSS Distributions  */
419*e3723e1fSApple OSS Distributions #define	DTRACEBUF_RING		0x0001		/* bufpolicy set to "ring" */
420*e3723e1fSApple OSS Distributions #define	DTRACEBUF_FILL		0x0002		/* bufpolicy set to "fill" */
421*e3723e1fSApple OSS Distributions #define	DTRACEBUF_NOSWITCH	0x0004		/* do not switch buffer */
422*e3723e1fSApple OSS Distributions #define	DTRACEBUF_WRAPPED	0x0008		/* ring buffer has wrapped */
423*e3723e1fSApple OSS Distributions #define	DTRACEBUF_DROPPED	0x0010		/* drops occurred */
424*e3723e1fSApple OSS Distributions #define	DTRACEBUF_ERROR		0x0020		/* errors occurred */
425*e3723e1fSApple OSS Distributions #define	DTRACEBUF_FULL		0x0040		/* "fill" buffer is full */
426*e3723e1fSApple OSS Distributions #define	DTRACEBUF_CONSUMED	0x0080		/* buffer has been consumed */
427*e3723e1fSApple OSS Distributions #define	DTRACEBUF_INACTIVE	0x0100		/* buffer is not yet active */
428*e3723e1fSApple OSS Distributions 
429*e3723e1fSApple OSS Distributions typedef struct dtrace_buffer {
430*e3723e1fSApple OSS Distributions 	uint64_t dtb_offset;			/* current offset in buffer */
431*e3723e1fSApple OSS Distributions 	uint64_t dtb_cur_limit;			/* current limit before signaling/dropping */
432*e3723e1fSApple OSS Distributions 	uint64_t dtb_limit;			/* limit before signaling */
433*e3723e1fSApple OSS Distributions 	uint64_t dtb_size;			/* size of buffer */
434*e3723e1fSApple OSS Distributions 	uint32_t dtb_flags;			/* flags */
435*e3723e1fSApple OSS Distributions 	uint32_t dtb_drops;			/* number of drops */
436*e3723e1fSApple OSS Distributions 	caddr_t dtb_tomax;			/* active buffer */
437*e3723e1fSApple OSS Distributions 	caddr_t dtb_xamot;			/* inactive buffer */
438*e3723e1fSApple OSS Distributions 	uint32_t dtb_xamot_flags;		/* inactive flags */
439*e3723e1fSApple OSS Distributions 	uint32_t dtb_xamot_drops;		/* drops in inactive buffer */
440*e3723e1fSApple OSS Distributions 	uint64_t dtb_xamot_offset;		/* offset in inactive buffer */
441*e3723e1fSApple OSS Distributions 	uint32_t dtb_errors;			/* number of errors */
442*e3723e1fSApple OSS Distributions 	uint32_t dtb_xamot_errors;		/* errors in inactive buffer */
443*e3723e1fSApple OSS Distributions #ifndef _LP64
444*e3723e1fSApple OSS Distributions 	uint64_t dtb_pad1;
445*e3723e1fSApple OSS Distributions #endif
446*e3723e1fSApple OSS Distributions 	uint64_t dtb_switched;			/* time of last switch */
447*e3723e1fSApple OSS Distributions 	uint64_t dtb_interval;			/* observed switch interval */
448*e3723e1fSApple OSS Distributions 	uint64_t dtb_pad2[4];			/* pad to avoid false sharing */
449*e3723e1fSApple OSS Distributions } dtrace_buffer_t;
450*e3723e1fSApple OSS Distributions 
451*e3723e1fSApple OSS Distributions /*
452*e3723e1fSApple OSS Distributions  * DTrace Aggregation Buffers
453*e3723e1fSApple OSS Distributions  *
454*e3723e1fSApple OSS Distributions  * Aggregation buffers use much of the same mechanism as described above
455*e3723e1fSApple OSS Distributions  * ("DTrace Buffers").  However, because an aggregation is fundamentally a
456*e3723e1fSApple OSS Distributions  * hash, there exists dynamic metadata associated with an aggregation buffer
457*e3723e1fSApple OSS Distributions  * that is not associated with other kinds of buffers.  This aggregation
458*e3723e1fSApple OSS Distributions  * metadata is _only_ relevant for the in-kernel implementation of
459*e3723e1fSApple OSS Distributions  * aggregations; it is not actually relevant to user-level consumers.  To do
460*e3723e1fSApple OSS Distributions  * this, we allocate dynamic aggregation data (hash keys and hash buckets)
461*e3723e1fSApple OSS Distributions  * starting below the _limit_ of the buffer, and we allocate data from the
462*e3723e1fSApple OSS Distributions  * _base_ of the buffer.  When the aggregation buffer is copied out, _only_ the
463*e3723e1fSApple OSS Distributions  * data is copied out; the metadata is simply discarded.  Schematically,
464*e3723e1fSApple OSS Distributions  * aggregation buffers look like:
465*e3723e1fSApple OSS Distributions  *
466*e3723e1fSApple OSS Distributions  *      base of data buffer --->  +-------+------+-----------+-------+
467*e3723e1fSApple OSS Distributions  *                                | aggid | key  | value     | aggid |
468*e3723e1fSApple OSS Distributions  *                                +-------+------+-----------+-------+
469*e3723e1fSApple OSS Distributions  *                                | key                              |
470*e3723e1fSApple OSS Distributions  *                                +-------+-------+-----+------------+
471*e3723e1fSApple OSS Distributions  *                                | value | aggid | key | value      |
472*e3723e1fSApple OSS Distributions  *                                +-------+------++-----+------+-----+
473*e3723e1fSApple OSS Distributions  *                                | aggid | key  | value       |     |
474*e3723e1fSApple OSS Distributions  *                                +-------+------+-------------+     |
475*e3723e1fSApple OSS Distributions  *                                |                ||                |
476*e3723e1fSApple OSS Distributions  *                                |                ||                |
477*e3723e1fSApple OSS Distributions  *                                |                \/                |
478*e3723e1fSApple OSS Distributions  *                                :                                  :
479*e3723e1fSApple OSS Distributions  *                                .                                  .
480*e3723e1fSApple OSS Distributions  *                                .                                  .
481*e3723e1fSApple OSS Distributions  *                                .                                  .
482*e3723e1fSApple OSS Distributions  *                                :                                  :
483*e3723e1fSApple OSS Distributions  *                                |                /\                |
484*e3723e1fSApple OSS Distributions  *                                |                ||   +------------+
485*e3723e1fSApple OSS Distributions  *                                |                ||   |            |
486*e3723e1fSApple OSS Distributions  *                                +---------------------+            |
487*e3723e1fSApple OSS Distributions  *                                | hash keys                        |
488*e3723e1fSApple OSS Distributions  *                                | (dtrace_aggkey structures)       |
489*e3723e1fSApple OSS Distributions  *                                |                                  |
490*e3723e1fSApple OSS Distributions  *                                +----------------------------------+
491*e3723e1fSApple OSS Distributions  *                                | hash buckets                     |
492*e3723e1fSApple OSS Distributions  *                                | (dtrace_aggbuffer structure)     |
493*e3723e1fSApple OSS Distributions  *                                |                                  |
494*e3723e1fSApple OSS Distributions  *     limit of data buffer --->  +----------------------------------+
495*e3723e1fSApple OSS Distributions  *
496*e3723e1fSApple OSS Distributions  *
497*e3723e1fSApple OSS Distributions  * As implied above, just as we assure that ECBs always store a constant
498*e3723e1fSApple OSS Distributions  * amount of data, we assure that a given aggregation -- identified by its
499*e3723e1fSApple OSS Distributions  * aggregation ID -- always stores data of a constant quantity and type.
500*e3723e1fSApple OSS Distributions  * As with EPIDs, this allows the aggregation ID to serve as the metadata for a
501*e3723e1fSApple OSS Distributions  * given record.
502*e3723e1fSApple OSS Distributions  *
503*e3723e1fSApple OSS Distributions  * Note that the size of the dtrace_aggkey structure must be sizeof (uintptr_t)
504*e3723e1fSApple OSS Distributions  * aligned.  (If this the structure changes such that this becomes false, an
505*e3723e1fSApple OSS Distributions  * assertion will fail in dtrace_aggregate().)
506*e3723e1fSApple OSS Distributions  */
507*e3723e1fSApple OSS Distributions typedef struct dtrace_aggkey {
508*e3723e1fSApple OSS Distributions 	uint32_t dtak_hashval;			/* hash value */
509*e3723e1fSApple OSS Distributions 	uint32_t dtak_action:4;			/* action -- 4 bits */
510*e3723e1fSApple OSS Distributions 	uint32_t dtak_size:28;			/* size -- 28 bits */
511*e3723e1fSApple OSS Distributions 	caddr_t dtak_data;			/* data pointer */
512*e3723e1fSApple OSS Distributions 	struct dtrace_aggkey *dtak_next;	/* next in hash chain */
513*e3723e1fSApple OSS Distributions } dtrace_aggkey_t;
514*e3723e1fSApple OSS Distributions 
515*e3723e1fSApple OSS Distributions typedef struct dtrace_aggbuffer {
516*e3723e1fSApple OSS Distributions 	uintptr_t dtagb_hashsize;		/* number of buckets */
517*e3723e1fSApple OSS Distributions 	uintptr_t dtagb_free;			/* free list of keys */
518*e3723e1fSApple OSS Distributions 	dtrace_aggkey_t **dtagb_hash;		/* hash table */
519*e3723e1fSApple OSS Distributions } dtrace_aggbuffer_t;
520*e3723e1fSApple OSS Distributions 
521*e3723e1fSApple OSS Distributions /*
522*e3723e1fSApple OSS Distributions  * DTrace Speculations
523*e3723e1fSApple OSS Distributions  *
524*e3723e1fSApple OSS Distributions  * Speculations have a per-CPU buffer and a global state.  Once a speculation
525*e3723e1fSApple OSS Distributions  * buffer has been comitted or discarded, it cannot be reused until all CPUs
526*e3723e1fSApple OSS Distributions  * have taken the same action (commit or discard) on their respective
527*e3723e1fSApple OSS Distributions  * speculative buffer.  However, because DTrace probes may execute in arbitrary
528*e3723e1fSApple OSS Distributions  * context, other CPUs cannot simply be cross-called at probe firing time to
529*e3723e1fSApple OSS Distributions  * perform the necessary commit or discard.  The speculation states thus
530*e3723e1fSApple OSS Distributions  * optimize for the case that a speculative buffer is only active on one CPU at
531*e3723e1fSApple OSS Distributions  * the time of a commit() or discard() -- for if this is the case, other CPUs
532*e3723e1fSApple OSS Distributions  * need not take action, and the speculation is immediately available for
533*e3723e1fSApple OSS Distributions  * reuse.  If the speculation is active on multiple CPUs, it must be
534*e3723e1fSApple OSS Distributions  * asynchronously cleaned -- potentially leading to a higher rate of dirty
535*e3723e1fSApple OSS Distributions  * speculative drops.  The speculation states are as follows:
536*e3723e1fSApple OSS Distributions  *
537*e3723e1fSApple OSS Distributions  *  DTRACESPEC_INACTIVE       <= Initial state; inactive speculation
538*e3723e1fSApple OSS Distributions  *  DTRACESPEC_ACTIVE         <= Allocated, but not yet speculatively traced to
539*e3723e1fSApple OSS Distributions  *  DTRACESPEC_ACTIVEONE      <= Speculatively traced to on one CPU
540*e3723e1fSApple OSS Distributions  *  DTRACESPEC_ACTIVEMANY     <= Speculatively traced to on more than one CPU
541*e3723e1fSApple OSS Distributions  *  DTRACESPEC_COMMITTING     <= Currently being commited on one CPU
542*e3723e1fSApple OSS Distributions  *  DTRACESPEC_COMMITTINGMANY <= Currently being commited on many CPUs
543*e3723e1fSApple OSS Distributions  *  DTRACESPEC_DISCARDING     <= Currently being discarded on many CPUs
544*e3723e1fSApple OSS Distributions  *
545*e3723e1fSApple OSS Distributions  * The state transition diagram is as follows:
546*e3723e1fSApple OSS Distributions  *
547*e3723e1fSApple OSS Distributions  *     +----------------------------------------------------------+
548*e3723e1fSApple OSS Distributions  *     |                                                          |
549*e3723e1fSApple OSS Distributions  *     |                      +------------+                      |
550*e3723e1fSApple OSS Distributions  *     |  +-------------------| COMMITTING |<-----------------+   |
551*e3723e1fSApple OSS Distributions  *     |  |                   +------------+                  |   |
552*e3723e1fSApple OSS Distributions  *     |  | copied spec.            ^             commit() on |   | discard() on
553*e3723e1fSApple OSS Distributions  *     |  | into principal          |              active CPU |   | active CPU
554*e3723e1fSApple OSS Distributions  *     |  |                         | commit()                |   |
555*e3723e1fSApple OSS Distributions  *     V  V                         |                         |   |
556*e3723e1fSApple OSS Distributions  * +----------+                 +--------+                +-----------+
557*e3723e1fSApple OSS Distributions  * | INACTIVE |---------------->| ACTIVE |--------------->| ACTIVEONE |
558*e3723e1fSApple OSS Distributions  * +----------+  speculation()  +--------+  speculate()   +-----------+
559*e3723e1fSApple OSS Distributions  *     ^  ^                         |                         |   |
560*e3723e1fSApple OSS Distributions  *     |  |                         | discard()               |   |
561*e3723e1fSApple OSS Distributions  *     |  | asynchronously          |            discard() on |   | speculate()
562*e3723e1fSApple OSS Distributions  *     |  | cleaned                 V            inactive CPU |   | on inactive
563*e3723e1fSApple OSS Distributions  *     |  |                   +------------+                  |   | CPU
564*e3723e1fSApple OSS Distributions  *     |  +-------------------| DISCARDING |<-----------------+   |
565*e3723e1fSApple OSS Distributions  *     |                      +------------+                      |
566*e3723e1fSApple OSS Distributions  *     | asynchronously             ^                             |
567*e3723e1fSApple OSS Distributions  *     | copied spec.               |       discard()             |
568*e3723e1fSApple OSS Distributions  *     | into principal             +------------------------+    |
569*e3723e1fSApple OSS Distributions  *     |                                                     |    V
570*e3723e1fSApple OSS Distributions  *  +----------------+             commit()              +------------+
571*e3723e1fSApple OSS Distributions  *  | COMMITTINGMANY |<----------------------------------| ACTIVEMANY |
572*e3723e1fSApple OSS Distributions  *  +----------------+                                   +------------+
573*e3723e1fSApple OSS Distributions  */
574*e3723e1fSApple OSS Distributions typedef enum dtrace_speculation_state {
575*e3723e1fSApple OSS Distributions 	DTRACESPEC_INACTIVE = 0,
576*e3723e1fSApple OSS Distributions 	DTRACESPEC_ACTIVE,
577*e3723e1fSApple OSS Distributions 	DTRACESPEC_ACTIVEONE,
578*e3723e1fSApple OSS Distributions 	DTRACESPEC_ACTIVEMANY,
579*e3723e1fSApple OSS Distributions 	DTRACESPEC_COMMITTING,
580*e3723e1fSApple OSS Distributions 	DTRACESPEC_COMMITTINGMANY,
581*e3723e1fSApple OSS Distributions 	DTRACESPEC_DISCARDING
582*e3723e1fSApple OSS Distributions } dtrace_speculation_state_t;
583*e3723e1fSApple OSS Distributions 
584*e3723e1fSApple OSS Distributions typedef struct dtrace_speculation {
585*e3723e1fSApple OSS Distributions 	dtrace_speculation_state_t dtsp_state;	/* current speculation state */
586*e3723e1fSApple OSS Distributions 	int dtsp_cleaning;			/* non-zero if being cleaned */
587*e3723e1fSApple OSS Distributions 	dtrace_buffer_t *dtsp_buffer;		/* speculative buffer */
588*e3723e1fSApple OSS Distributions } dtrace_speculation_t;
589*e3723e1fSApple OSS Distributions 
590*e3723e1fSApple OSS Distributions /*
591*e3723e1fSApple OSS Distributions  * DTrace Dynamic Variables
592*e3723e1fSApple OSS Distributions  *
593*e3723e1fSApple OSS Distributions  * The dynamic variable problem is obviously decomposed into two subproblems:
594*e3723e1fSApple OSS Distributions  * allocating new dynamic storage, and freeing old dynamic storage.  The
595*e3723e1fSApple OSS Distributions  * presence of the second problem makes the first much more complicated -- or
596*e3723e1fSApple OSS Distributions  * rather, the absence of the second renders the first trivial.  This is the
597*e3723e1fSApple OSS Distributions  * case with aggregations, for which there is effectively no deallocation of
598*e3723e1fSApple OSS Distributions  * dynamic storage.  (Or more accurately, all dynamic storage is deallocated
599*e3723e1fSApple OSS Distributions  * when a snapshot is taken of the aggregation.)  As DTrace dynamic variables
600*e3723e1fSApple OSS Distributions  * allow for both dynamic allocation and dynamic deallocation, the
601*e3723e1fSApple OSS Distributions  * implementation of dynamic variables is quite a bit more complicated than
602*e3723e1fSApple OSS Distributions  * that of their aggregation kin.
603*e3723e1fSApple OSS Distributions  *
604*e3723e1fSApple OSS Distributions  * We observe that allocating new dynamic storage is tricky only because the
605*e3723e1fSApple OSS Distributions  * size can vary -- the allocation problem is much easier if allocation sizes
606*e3723e1fSApple OSS Distributions  * are uniform.  We further observe that in D, the size of dynamic variables is
607*e3723e1fSApple OSS Distributions  * actually _not_ dynamic -- dynamic variable sizes may be determined by static
608*e3723e1fSApple OSS Distributions  * analysis of DIF text.  (This is true even of putatively dynamically-sized
609*e3723e1fSApple OSS Distributions  * objects like strings and stacks, the sizes of which are dictated by the
610*e3723e1fSApple OSS Distributions  * "stringsize" and "stackframes" variables, respectively.)  We exploit this by
611*e3723e1fSApple OSS Distributions  * performing this analysis on all DIF before enabling any probes.  For each
612*e3723e1fSApple OSS Distributions  * dynamic load or store, we calculate the dynamically-allocated size plus the
613*e3723e1fSApple OSS Distributions  * size of the dtrace_dynvar structure plus the storage required to key the
614*e3723e1fSApple OSS Distributions  * data.  For all DIF, we take the largest value and dub it the _chunksize_.
615*e3723e1fSApple OSS Distributions  * We then divide dynamic memory into two parts:  a hash table that is wide
616*e3723e1fSApple OSS Distributions  * enough to have every chunk in its own bucket, and a larger region of equal
617*e3723e1fSApple OSS Distributions  * chunksize units.  Whenever we wish to dynamically allocate a variable, we
618*e3723e1fSApple OSS Distributions  * always allocate a single chunk of memory.  Depending on the uniformity of
619*e3723e1fSApple OSS Distributions  * allocation, this will waste some amount of memory -- but it eliminates the
620*e3723e1fSApple OSS Distributions  * non-determinism inherent in traditional heap fragmentation.
621*e3723e1fSApple OSS Distributions  *
622*e3723e1fSApple OSS Distributions  * Dynamic objects are allocated by storing a non-zero value to them; they are
623*e3723e1fSApple OSS Distributions  * deallocated by storing a zero value to them.  Dynamic variables are
624*e3723e1fSApple OSS Distributions  * complicated enormously by being shared between CPUs.  In particular,
625*e3723e1fSApple OSS Distributions  * consider the following scenario:
626*e3723e1fSApple OSS Distributions  *
627*e3723e1fSApple OSS Distributions  *                 CPU A                                 CPU B
628*e3723e1fSApple OSS Distributions  *  +---------------------------------+   +---------------------------------+
629*e3723e1fSApple OSS Distributions  *  |                                 |   |                                 |
630*e3723e1fSApple OSS Distributions  *  | allocates dynamic object a[123] |   |                                 |
631*e3723e1fSApple OSS Distributions  *  | by storing the value 345 to it  |   |                                 |
632*e3723e1fSApple OSS Distributions  *  |                               --------->                              |
633*e3723e1fSApple OSS Distributions  *  |                                 |   | wishing to load from object     |
634*e3723e1fSApple OSS Distributions  *  |                                 |   | a[123], performs lookup in      |
635*e3723e1fSApple OSS Distributions  *  |                                 |   | dynamic variable space          |
636*e3723e1fSApple OSS Distributions  *  |                               <---------                              |
637*e3723e1fSApple OSS Distributions  *  | deallocates object a[123] by    |   |                                 |
638*e3723e1fSApple OSS Distributions  *  | storing 0 to it                 |   |                                 |
639*e3723e1fSApple OSS Distributions  *  |                                 |   |                                 |
640*e3723e1fSApple OSS Distributions  *  | allocates dynamic object b[567] |   | performs load from a[123]       |
641*e3723e1fSApple OSS Distributions  *  | by storing the value 789 to it  |   |                                 |
642*e3723e1fSApple OSS Distributions  *  :                                 :   :                                 :
643*e3723e1fSApple OSS Distributions  *  .                                 .   .                                 .
644*e3723e1fSApple OSS Distributions  *
645*e3723e1fSApple OSS Distributions  * This is obviously a race in the D program, but there are nonetheless only
646*e3723e1fSApple OSS Distributions  * two valid values for CPU B's load from a[123]:  345 or 0.  Most importantly,
647*e3723e1fSApple OSS Distributions  * CPU B may _not_ see the value 789 for a[123].
648*e3723e1fSApple OSS Distributions  *
649*e3723e1fSApple OSS Distributions  * There are essentially two ways to deal with this:
650*e3723e1fSApple OSS Distributions  *
651*e3723e1fSApple OSS Distributions  *  (1)  Explicitly spin-lock variables.  That is, if CPU B wishes to load
652*e3723e1fSApple OSS Distributions  *       from a[123], it needs to lock a[123] and hold the lock for the
653*e3723e1fSApple OSS Distributions  *       duration that it wishes to manipulate it.
654*e3723e1fSApple OSS Distributions  *
655*e3723e1fSApple OSS Distributions  *  (2)  Avoid reusing freed chunks until it is known that no CPU is referring
656*e3723e1fSApple OSS Distributions  *       to them.
657*e3723e1fSApple OSS Distributions  *
658*e3723e1fSApple OSS Distributions  * The implementation of (1) is rife with complexity, because it requires the
659*e3723e1fSApple OSS Distributions  * user of a dynamic variable to explicitly decree when they are done using it.
660*e3723e1fSApple OSS Distributions  * Were all variables by value, this perhaps wouldn't be debilitating -- but
661*e3723e1fSApple OSS Distributions  * dynamic variables of non-scalar types are tracked by reference.  That is, if
662*e3723e1fSApple OSS Distributions  * a dynamic variable is, say, a string, and that variable is to be traced to,
663*e3723e1fSApple OSS Distributions  * say, the principal buffer, the DIF emulation code returns to the main
664*e3723e1fSApple OSS Distributions  * dtrace_probe() loop a pointer to the underlying storage, not the contents of
665*e3723e1fSApple OSS Distributions  * the storage.  Further, code calling on DIF emulation would have to be aware
666*e3723e1fSApple OSS Distributions  * that the DIF emulation has returned a reference to a dynamic variable that
667*e3723e1fSApple OSS Distributions  * has been potentially locked.  The variable would have to be unlocked after
668*e3723e1fSApple OSS Distributions  * the main dtrace_probe() loop is finished with the variable, and the main
669*e3723e1fSApple OSS Distributions  * dtrace_probe() loop would have to be careful to not call any further DIF
670*e3723e1fSApple OSS Distributions  * emulation while the variable is locked to avoid deadlock.  More generally,
671*e3723e1fSApple OSS Distributions  * if one were to implement (1), DIF emulation code dealing with dynamic
672*e3723e1fSApple OSS Distributions  * variables could only deal with one dynamic variable at a time (lest deadlock
673*e3723e1fSApple OSS Distributions  * result).  To sum, (1) exports too much subtlety to the users of dynamic
674*e3723e1fSApple OSS Distributions  * variables -- increasing maintenance burden and imposing serious constraints
675*e3723e1fSApple OSS Distributions  * on future DTrace development.
676*e3723e1fSApple OSS Distributions  *
677*e3723e1fSApple OSS Distributions  * The implementation of (2) is also complex, but the complexity is more
678*e3723e1fSApple OSS Distributions  * manageable.  We need to be sure that when a variable is deallocated, it is
679*e3723e1fSApple OSS Distributions  * not placed on a traditional free list, but rather on a _dirty_ list.  Once a
680*e3723e1fSApple OSS Distributions  * variable is on a dirty list, it cannot be found by CPUs performing a
681*e3723e1fSApple OSS Distributions  * subsequent lookup of the variable -- but it may still be in use by other
682*e3723e1fSApple OSS Distributions  * CPUs.  To assure that all CPUs that may be seeing the old variable have
683*e3723e1fSApple OSS Distributions  * cleared out of probe context, a dtrace_sync() can be issued.  Once the
684*e3723e1fSApple OSS Distributions  * dtrace_sync() has completed, it can be known that all CPUs are done
685*e3723e1fSApple OSS Distributions  * manipulating the dynamic variable -- the dirty list can be atomically
686*e3723e1fSApple OSS Distributions  * appended to the free list.  Unfortunately, there's a slight hiccup in this
687*e3723e1fSApple OSS Distributions  * mechanism:  dtrace_sync() may not be issued from probe context.  The
688*e3723e1fSApple OSS Distributions  * dtrace_sync() must be therefore issued asynchronously from non-probe
689*e3723e1fSApple OSS Distributions  * context.  For this we rely on the DTrace cleaner, a cyclic that runs at the
690*e3723e1fSApple OSS Distributions  * "cleanrate" frequency.  To ease this implementation, we define several chunk
691*e3723e1fSApple OSS Distributions  * lists:
692*e3723e1fSApple OSS Distributions  *
693*e3723e1fSApple OSS Distributions  *   - Dirty.  Deallocated chunks, not yet cleaned.  Not available.
694*e3723e1fSApple OSS Distributions  *
695*e3723e1fSApple OSS Distributions  *   - Rinsing.  Formerly dirty chunks that are currently being asynchronously
696*e3723e1fSApple OSS Distributions  *     cleaned.  Not available, but will be shortly.  Dynamic variable
697*e3723e1fSApple OSS Distributions  *     allocation may not spin or block for availability, however.
698*e3723e1fSApple OSS Distributions  *
699*e3723e1fSApple OSS Distributions  *   - Clean.  Clean chunks, ready for allocation -- but not on the free list.
700*e3723e1fSApple OSS Distributions  *
701*e3723e1fSApple OSS Distributions  *   - Free.  Available for allocation.
702*e3723e1fSApple OSS Distributions  *
703*e3723e1fSApple OSS Distributions  * Moreover, to avoid absurd contention, _each_ of these lists is implemented
704*e3723e1fSApple OSS Distributions  * on a per-CPU basis.  This is only for performance, not correctness; chunks
705*e3723e1fSApple OSS Distributions  * may be allocated from another CPU's free list.  The algorithm for allocation
706*e3723e1fSApple OSS Distributions  * then is this:
707*e3723e1fSApple OSS Distributions  *
708*e3723e1fSApple OSS Distributions  *   (1)  Attempt to atomically allocate from current CPU's free list.  If list
709*e3723e1fSApple OSS Distributions  *        is non-empty and allocation is successful, allocation is complete.
710*e3723e1fSApple OSS Distributions  *
711*e3723e1fSApple OSS Distributions  *   (2)  If the clean list is non-empty, atomically move it to the free list,
712*e3723e1fSApple OSS Distributions  *        and reattempt (1).
713*e3723e1fSApple OSS Distributions  *
714*e3723e1fSApple OSS Distributions  *   (3)  If the dynamic variable space is in the CLEAN state, look for free
715*e3723e1fSApple OSS Distributions  *        and clean lists on other CPUs by setting the current CPU to the next
716*e3723e1fSApple OSS Distributions  *        CPU, and reattempting (1).  If the next CPU is the current CPU (that
717*e3723e1fSApple OSS Distributions  *        is, if all CPUs have been checked), atomically switch the state of
718*e3723e1fSApple OSS Distributions  *        the dynamic variable space based on the following:
719*e3723e1fSApple OSS Distributions  *
720*e3723e1fSApple OSS Distributions  *        - If no free chunks were found and no dirty chunks were found,
721*e3723e1fSApple OSS Distributions  *          atomically set the state to EMPTY.
722*e3723e1fSApple OSS Distributions  *
723*e3723e1fSApple OSS Distributions  *        - If dirty chunks were found, atomically set the state to DIRTY.
724*e3723e1fSApple OSS Distributions  *
725*e3723e1fSApple OSS Distributions  *        - If rinsing chunks were found, atomically set the state to RINSING.
726*e3723e1fSApple OSS Distributions  *
727*e3723e1fSApple OSS Distributions  *   (4)  Based on state of dynamic variable space state, increment appropriate
728*e3723e1fSApple OSS Distributions  *        counter to indicate dynamic drops (if in EMPTY state) vs. dynamic
729*e3723e1fSApple OSS Distributions  *        dirty drops (if in DIRTY state) vs. dynamic rinsing drops (if in
730*e3723e1fSApple OSS Distributions  *        RINSING state).  Fail the allocation.
731*e3723e1fSApple OSS Distributions  *
732*e3723e1fSApple OSS Distributions  * The cleaning cyclic operates with the following algorithm:  for all CPUs
733*e3723e1fSApple OSS Distributions  * with a non-empty dirty list, atomically move the dirty list to the rinsing
734*e3723e1fSApple OSS Distributions  * list.  Perform a dtrace_sync().  For all CPUs with a non-empty rinsing list,
735*e3723e1fSApple OSS Distributions  * atomically move the rinsing list to the clean list.  Perform another
736*e3723e1fSApple OSS Distributions  * dtrace_sync().  By this point, all CPUs have seen the new clean list; the
737*e3723e1fSApple OSS Distributions  * state of the dynamic variable space can be restored to CLEAN.
738*e3723e1fSApple OSS Distributions  *
739*e3723e1fSApple OSS Distributions  * There exist two final races that merit explanation.  The first is a simple
740*e3723e1fSApple OSS Distributions  * allocation race:
741*e3723e1fSApple OSS Distributions  *
742*e3723e1fSApple OSS Distributions  *                 CPU A                                 CPU B
743*e3723e1fSApple OSS Distributions  *  +---------------------------------+   +---------------------------------+
744*e3723e1fSApple OSS Distributions  *  |                                 |   |                                 |
745*e3723e1fSApple OSS Distributions  *  | allocates dynamic object a[123] |   | allocates dynamic object a[123] |
746*e3723e1fSApple OSS Distributions  *  | by storing the value 345 to it  |   | by storing the value 567 to it  |
747*e3723e1fSApple OSS Distributions  *  |                                 |   |                                 |
748*e3723e1fSApple OSS Distributions  *  :                                 :   :                                 :
749*e3723e1fSApple OSS Distributions  *  .                                 .   .                                 .
750*e3723e1fSApple OSS Distributions  *
751*e3723e1fSApple OSS Distributions  * Again, this is a race in the D program.  It can be resolved by having a[123]
752*e3723e1fSApple OSS Distributions  * hold the value 345 or a[123] hold the value 567 -- but it must be true that
753*e3723e1fSApple OSS Distributions  * a[123] have only _one_ of these values.  (That is, the racing CPUs may not
754*e3723e1fSApple OSS Distributions  * put the same element twice on the same hash chain.)  This is resolved
755*e3723e1fSApple OSS Distributions  * simply:  before the allocation is undertaken, the start of the new chunk's
756*e3723e1fSApple OSS Distributions  * hash chain is noted.  Later, after the allocation is complete, the hash
757*e3723e1fSApple OSS Distributions  * chain is atomically switched to point to the new element.  If this fails
758*e3723e1fSApple OSS Distributions  * (because of either concurrent allocations or an allocation concurrent with a
759*e3723e1fSApple OSS Distributions  * deletion), the newly allocated chunk is deallocated to the dirty list, and
760*e3723e1fSApple OSS Distributions  * the whole process of looking up (and potentially allocating) the dynamic
761*e3723e1fSApple OSS Distributions  * variable is reattempted.
762*e3723e1fSApple OSS Distributions  *
763*e3723e1fSApple OSS Distributions  * The final race is a simple deallocation race:
764*e3723e1fSApple OSS Distributions  *
765*e3723e1fSApple OSS Distributions  *                 CPU A                                 CPU B
766*e3723e1fSApple OSS Distributions  *  +---------------------------------+   +---------------------------------+
767*e3723e1fSApple OSS Distributions  *  |                                 |   |                                 |
768*e3723e1fSApple OSS Distributions  *  | deallocates dynamic object      |   | deallocates dynamic object      |
769*e3723e1fSApple OSS Distributions  *  | a[123] by storing the value 0   |   | a[123] by storing the value 0   |
770*e3723e1fSApple OSS Distributions  *  | to it                           |   | to it                           |
771*e3723e1fSApple OSS Distributions  *  |                                 |   |                                 |
772*e3723e1fSApple OSS Distributions  *  :                                 :   :                                 :
773*e3723e1fSApple OSS Distributions  *  .                                 .   .                                 .
774*e3723e1fSApple OSS Distributions  *
775*e3723e1fSApple OSS Distributions  * Once again, this is a race in the D program, but it is one that we must
776*e3723e1fSApple OSS Distributions  * handle without corrupting the underlying data structures.  Because
777*e3723e1fSApple OSS Distributions  * deallocations require the deletion of a chunk from the middle of a hash
778*e3723e1fSApple OSS Distributions  * chain, we cannot use a single-word atomic operation to remove it.  For this,
779*e3723e1fSApple OSS Distributions  * we add a spin lock to the hash buckets that is _only_ used for deallocations
780*e3723e1fSApple OSS Distributions  * (allocation races are handled as above).  Further, this spin lock is _only_
781*e3723e1fSApple OSS Distributions  * held for the duration of the delete; before control is returned to the DIF
782*e3723e1fSApple OSS Distributions  * emulation code, the hash bucket is unlocked.
783*e3723e1fSApple OSS Distributions  */
784*e3723e1fSApple OSS Distributions typedef struct dtrace_key {
785*e3723e1fSApple OSS Distributions 	uint64_t dttk_value;			/* data value or data pointer */
786*e3723e1fSApple OSS Distributions 	uint64_t dttk_size;			/* 0 if by-val, >0 if by-ref */
787*e3723e1fSApple OSS Distributions } dtrace_key_t;
788*e3723e1fSApple OSS Distributions 
789*e3723e1fSApple OSS Distributions typedef struct dtrace_tuple {
790*e3723e1fSApple OSS Distributions 	uint32_t dtt_nkeys;			/* number of keys in tuple */
791*e3723e1fSApple OSS Distributions 	uint32_t dtt_pad;			/* padding */
792*e3723e1fSApple OSS Distributions 	dtrace_key_t dtt_key[1];		/* array of tuple keys */
793*e3723e1fSApple OSS Distributions } dtrace_tuple_t;
794*e3723e1fSApple OSS Distributions 
795*e3723e1fSApple OSS Distributions typedef struct dtrace_dynvar {
796*e3723e1fSApple OSS Distributions 	uint64_t dtdv_hashval;			/* hash value -- 0 if free */
797*e3723e1fSApple OSS Distributions 	struct dtrace_dynvar *dtdv_next;	/* next on list or hash chain */
798*e3723e1fSApple OSS Distributions 	void *dtdv_data;			/* pointer to data */
799*e3723e1fSApple OSS Distributions 	dtrace_tuple_t dtdv_tuple;		/* tuple key */
800*e3723e1fSApple OSS Distributions } dtrace_dynvar_t;
801*e3723e1fSApple OSS Distributions 
802*e3723e1fSApple OSS Distributions typedef enum dtrace_dynvar_op {
803*e3723e1fSApple OSS Distributions 	DTRACE_DYNVAR_ALLOC,
804*e3723e1fSApple OSS Distributions 	DTRACE_DYNVAR_NOALLOC,
805*e3723e1fSApple OSS Distributions 	DTRACE_DYNVAR_DEALLOC
806*e3723e1fSApple OSS Distributions } dtrace_dynvar_op_t;
807*e3723e1fSApple OSS Distributions 
808*e3723e1fSApple OSS Distributions typedef struct dtrace_dynhash {
809*e3723e1fSApple OSS Distributions 	dtrace_dynvar_t *dtdh_chain;		/* hash chain for this bucket */
810*e3723e1fSApple OSS Distributions 	uintptr_t dtdh_lock;			/* deallocation lock */
811*e3723e1fSApple OSS Distributions #ifdef _LP64
812*e3723e1fSApple OSS Distributions 	uintptr_t dtdh_pad[6];			/* pad to avoid false sharing */
813*e3723e1fSApple OSS Distributions #else
814*e3723e1fSApple OSS Distributions 	uintptr_t dtdh_pad[14];			/* pad to avoid false sharing */
815*e3723e1fSApple OSS Distributions #endif
816*e3723e1fSApple OSS Distributions } dtrace_dynhash_t;
817*e3723e1fSApple OSS Distributions 
818*e3723e1fSApple OSS Distributions typedef struct dtrace_dstate_percpu {
819*e3723e1fSApple OSS Distributions 	dtrace_dynvar_t *dtdsc_free;		/* free list for this CPU */
820*e3723e1fSApple OSS Distributions 	dtrace_dynvar_t *dtdsc_dirty;		/* dirty list for this CPU */
821*e3723e1fSApple OSS Distributions 	dtrace_dynvar_t *dtdsc_rinsing;		/* rinsing list for this CPU */
822*e3723e1fSApple OSS Distributions 	dtrace_dynvar_t *dtdsc_clean;		/* clean list for this CPU */
823*e3723e1fSApple OSS Distributions 	uint64_t dtdsc_drops;			/* number of capacity drops */
824*e3723e1fSApple OSS Distributions 	uint64_t dtdsc_dirty_drops;		/* number of dirty drops */
825*e3723e1fSApple OSS Distributions 	uint64_t dtdsc_rinsing_drops;		/* number of rinsing drops */
826*e3723e1fSApple OSS Distributions } dtrace_dstate_percpu_t;
827*e3723e1fSApple OSS Distributions 
828*e3723e1fSApple OSS Distributions typedef enum dtrace_dstate_state {
829*e3723e1fSApple OSS Distributions 	DTRACE_DSTATE_CLEAN = 0,
830*e3723e1fSApple OSS Distributions 	DTRACE_DSTATE_EMPTY,
831*e3723e1fSApple OSS Distributions 	DTRACE_DSTATE_DIRTY,
832*e3723e1fSApple OSS Distributions 	DTRACE_DSTATE_RINSING
833*e3723e1fSApple OSS Distributions } dtrace_dstate_state_t;
834*e3723e1fSApple OSS Distributions 
835*e3723e1fSApple OSS Distributions typedef struct dtrace_dstate {
836*e3723e1fSApple OSS Distributions 	void *dtds_base;			/* base of dynamic var. space */
837*e3723e1fSApple OSS Distributions 	size_t dtds_size;			/* size of dynamic var. space */
838*e3723e1fSApple OSS Distributions 	size_t dtds_hashsize;			/* number of buckets in hash */
839*e3723e1fSApple OSS Distributions 	size_t dtds_chunksize;			/* size of each chunk */
840*e3723e1fSApple OSS Distributions 	dtrace_dynhash_t *dtds_hash;		/* pointer to hash table */
841*e3723e1fSApple OSS Distributions 	dtrace_dstate_state_t dtds_state;	/* current dynamic var. state */
842*e3723e1fSApple OSS Distributions 	dtrace_dstate_percpu_t *__zpercpu dtds_percpu;	/* per-CPU dyn. var. state */
843*e3723e1fSApple OSS Distributions } dtrace_dstate_t;
844*e3723e1fSApple OSS Distributions 
845*e3723e1fSApple OSS Distributions /*
846*e3723e1fSApple OSS Distributions  * DTrace Variable State
847*e3723e1fSApple OSS Distributions  *
848*e3723e1fSApple OSS Distributions  * The DTrace variable state tracks user-defined variables in its dtrace_vstate
849*e3723e1fSApple OSS Distributions  * structure.  Each DTrace consumer has exactly one dtrace_vstate structure,
850*e3723e1fSApple OSS Distributions  * but some dtrace_vstate structures may exist without a corresponding DTrace
851*e3723e1fSApple OSS Distributions  * consumer (see "DTrace Helpers", below).  As described in <sys/dtrace.h>,
852*e3723e1fSApple OSS Distributions  * user-defined variables can have one of three scopes:
853*e3723e1fSApple OSS Distributions  *
854*e3723e1fSApple OSS Distributions  *  DIFV_SCOPE_GLOBAL  =>  global scope
855*e3723e1fSApple OSS Distributions  *  DIFV_SCOPE_THREAD  =>  thread-local scope (i.e. "self->" variables)
856*e3723e1fSApple OSS Distributions  *  DIFV_SCOPE_LOCAL   =>  clause-local scope (i.e. "this->" variables)
857*e3723e1fSApple OSS Distributions  *
858*e3723e1fSApple OSS Distributions  * The variable state tracks variables by both their scope and their allocation
859*e3723e1fSApple OSS Distributions  * type:
860*e3723e1fSApple OSS Distributions  *
861*e3723e1fSApple OSS Distributions  *  - The dtvs_globals and dtvs_locals members each point to an array of
862*e3723e1fSApple OSS Distributions  *    dtrace_statvar structures.  These structures contain both the variable
863*e3723e1fSApple OSS Distributions  *    metadata (dtrace_difv structures) and the underlying storage for all
864*e3723e1fSApple OSS Distributions  *    statically allocated variables, including statically allocated
865*e3723e1fSApple OSS Distributions  *    DIFV_SCOPE_GLOBAL variables and all DIFV_SCOPE_LOCAL variables.
866*e3723e1fSApple OSS Distributions  *
867*e3723e1fSApple OSS Distributions  *  - The dtvs_tlocals member points to an array of dtrace_difv structures for
868*e3723e1fSApple OSS Distributions  *    DIFV_SCOPE_THREAD variables.  As such, this array tracks _only_ the
869*e3723e1fSApple OSS Distributions  *    variable metadata for DIFV_SCOPE_THREAD variables; the underlying storage
870*e3723e1fSApple OSS Distributions  *    is allocated out of the dynamic variable space.
871*e3723e1fSApple OSS Distributions  *
872*e3723e1fSApple OSS Distributions  *  - The dtvs_dynvars member is the dynamic variable state associated with the
873*e3723e1fSApple OSS Distributions  *    variable state.  The dynamic variable state (described in "DTrace Dynamic
874*e3723e1fSApple OSS Distributions  *    Variables", above) tracks all DIFV_SCOPE_THREAD variables and all
875*e3723e1fSApple OSS Distributions  *    dynamically-allocated DIFV_SCOPE_GLOBAL variables.
876*e3723e1fSApple OSS Distributions  */
877*e3723e1fSApple OSS Distributions typedef struct dtrace_statvar {
878*e3723e1fSApple OSS Distributions 	uint64_t dtsv_data;			/* data or pointer to it */
879*e3723e1fSApple OSS Distributions 	size_t dtsv_size;			/* size of pointed-to data */
880*e3723e1fSApple OSS Distributions 	int dtsv_refcnt;			/* reference count */
881*e3723e1fSApple OSS Distributions 	dtrace_difv_t dtsv_var;			/* variable metadata */
882*e3723e1fSApple OSS Distributions } dtrace_statvar_t;
883*e3723e1fSApple OSS Distributions 
884*e3723e1fSApple OSS Distributions typedef struct dtrace_vstate {
885*e3723e1fSApple OSS Distributions 	dtrace_state_t *dtvs_state;		/* back pointer to state */
886*e3723e1fSApple OSS Distributions 	dtrace_statvar_t **dtvs_globals;	/* statically-allocated glbls */
887*e3723e1fSApple OSS Distributions 	int dtvs_nglobals;			/* number of globals */
888*e3723e1fSApple OSS Distributions 	dtrace_difv_t *dtvs_tlocals;		/* thread-local metadata */
889*e3723e1fSApple OSS Distributions 	int dtvs_ntlocals;			/* number of thread-locals */
890*e3723e1fSApple OSS Distributions 	dtrace_statvar_t **dtvs_locals;		/* clause-local data */
891*e3723e1fSApple OSS Distributions 	int dtvs_nlocals;			/* number of clause-locals */
892*e3723e1fSApple OSS Distributions 	dtrace_dstate_t dtvs_dynvars;		/* dynamic variable state */
893*e3723e1fSApple OSS Distributions } dtrace_vstate_t;
894*e3723e1fSApple OSS Distributions 
895*e3723e1fSApple OSS Distributions /*
896*e3723e1fSApple OSS Distributions  * DTrace Machine State
897*e3723e1fSApple OSS Distributions  *
898*e3723e1fSApple OSS Distributions  * In the process of processing a fired probe, DTrace needs to track and/or
899*e3723e1fSApple OSS Distributions  * cache some per-CPU state associated with that particular firing.  This is
900*e3723e1fSApple OSS Distributions  * state that is always discarded after the probe firing has completed, and
901*e3723e1fSApple OSS Distributions  * much of it is not specific to any DTrace consumer, remaining valid across
902*e3723e1fSApple OSS Distributions  * all ECBs.  This state is tracked in the dtrace_mstate structure.
903*e3723e1fSApple OSS Distributions  */
904*e3723e1fSApple OSS Distributions #define	DTRACE_MSTATE_ARGS		0x00000001
905*e3723e1fSApple OSS Distributions #define	DTRACE_MSTATE_PROBE		0x00000002
906*e3723e1fSApple OSS Distributions #define	DTRACE_MSTATE_EPID		0x00000004
907*e3723e1fSApple OSS Distributions #define	DTRACE_MSTATE_TIMESTAMP		0x00000008
908*e3723e1fSApple OSS Distributions #define	DTRACE_MSTATE_STACKDEPTH	0x00000010
909*e3723e1fSApple OSS Distributions #define	DTRACE_MSTATE_CALLER		0x00000020
910*e3723e1fSApple OSS Distributions #define	DTRACE_MSTATE_IPL		0x00000040
911*e3723e1fSApple OSS Distributions #define	DTRACE_MSTATE_FLTOFFS		0x00000080
912*e3723e1fSApple OSS Distributions #define	DTRACE_MSTATE_WALLTIMESTAMP	0x00000100
913*e3723e1fSApple OSS Distributions #define	DTRACE_MSTATE_USTACKDEPTH	0x00000200
914*e3723e1fSApple OSS Distributions #define	DTRACE_MSTATE_UCALLER		0x00000400
915*e3723e1fSApple OSS Distributions #define	DTRACE_MSTATE_MACHTIMESTAMP	0x00000800
916*e3723e1fSApple OSS Distributions #define	DTRACE_MSTATE_MACHCTIMESTAMP	0x00001000
917*e3723e1fSApple OSS Distributions 
918*e3723e1fSApple OSS Distributions typedef struct dtrace_mstate {
919*e3723e1fSApple OSS Distributions 	uintptr_t dtms_scratch_base;		/* base of scratch space */
920*e3723e1fSApple OSS Distributions 	uintptr_t dtms_scratch_ptr;		/* current scratch pointer */
921*e3723e1fSApple OSS Distributions 	size_t dtms_scratch_size;		/* scratch size */
922*e3723e1fSApple OSS Distributions 	uint32_t dtms_present;			/* variables that are present */
923*e3723e1fSApple OSS Distributions 	uint64_t dtms_arg[5];			/* cached arguments */
924*e3723e1fSApple OSS Distributions 	dtrace_epid_t dtms_epid;		/* current EPID */
925*e3723e1fSApple OSS Distributions 	uint64_t dtms_timestamp;		/* cached timestamp */
926*e3723e1fSApple OSS Distributions 	hrtime_t dtms_walltimestamp;		/* cached wall timestamp */
927*e3723e1fSApple OSS Distributions 	uint64_t dtms_machtimestamp;		/* cached mach absolute timestamp */
928*e3723e1fSApple OSS Distributions 	uint64_t dtms_machctimestamp;		/* cached mach continuous timestamp */
929*e3723e1fSApple OSS Distributions 	int dtms_stackdepth;			/* cached stackdepth */
930*e3723e1fSApple OSS Distributions 	int dtms_ustackdepth;			/* cached ustackdepth */
931*e3723e1fSApple OSS Distributions 	struct dtrace_probe *dtms_probe;	/* current probe */
932*e3723e1fSApple OSS Distributions 	uintptr_t dtms_caller;			/* cached caller */
933*e3723e1fSApple OSS Distributions 	uint64_t dtms_ucaller;			/* cached user-level caller */
934*e3723e1fSApple OSS Distributions 	int dtms_ipl;				/* cached interrupt pri lev */
935*e3723e1fSApple OSS Distributions 	int dtms_fltoffs;			/* faulting DIFO offset */
936*e3723e1fSApple OSS Distributions 	uintptr_t dtms_strtok;			/* saved strtok() pointer */
937*e3723e1fSApple OSS Distributions 	uintptr_t dtms_strtok_limit;		/* upper bound of strtok ptr */
938*e3723e1fSApple OSS Distributions 	uint32_t dtms_access;			/* memory access rights */
939*e3723e1fSApple OSS Distributions 	dtrace_difo_t *dtms_difo;		/* current dif object */
940*e3723e1fSApple OSS Distributions } dtrace_mstate_t;
941*e3723e1fSApple OSS Distributions 
942*e3723e1fSApple OSS Distributions #define	DTRACE_COND_OWNER	0x1
943*e3723e1fSApple OSS Distributions #define	DTRACE_COND_USERMODE	0x2
944*e3723e1fSApple OSS Distributions #define	DTRACE_COND_ZONEOWNER	0x4
945*e3723e1fSApple OSS Distributions 
946*e3723e1fSApple OSS Distributions #define	DTRACE_PROBEKEY_MAXDEPTH	8	/* max glob recursion depth */
947*e3723e1fSApple OSS Distributions 
948*e3723e1fSApple OSS Distributions /*
949*e3723e1fSApple OSS Distributions  * Access flag used by dtrace_mstate.dtms_access.
950*e3723e1fSApple OSS Distributions  */
951*e3723e1fSApple OSS Distributions #define	DTRACE_ACCESS_KERNEL	0x1		/* the priv to read kmem */
952*e3723e1fSApple OSS Distributions 
953*e3723e1fSApple OSS Distributions 
954*e3723e1fSApple OSS Distributions /*
955*e3723e1fSApple OSS Distributions  * DTrace Activity
956*e3723e1fSApple OSS Distributions  *
957*e3723e1fSApple OSS Distributions  * Each DTrace consumer is in one of several states, which (for purposes of
958*e3723e1fSApple OSS Distributions  * avoiding yet-another overloading of the noun "state") we call the current
959*e3723e1fSApple OSS Distributions  * _activity_.  The activity transitions on dtrace_go() (from DTRACIOCGO), on
960*e3723e1fSApple OSS Distributions  * dtrace_stop() (from DTRACIOCSTOP) and on the exit() action.  Activities may
961*e3723e1fSApple OSS Distributions  * only transition in one direction; the activity transition diagram is a
962*e3723e1fSApple OSS Distributions  * directed acyclic graph.  The activity transition diagram is as follows:
963*e3723e1fSApple OSS Distributions  *
964*e3723e1fSApple OSS Distributions  *
965*e3723e1fSApple OSS Distributions  *
966*e3723e1fSApple OSS Distributions  * +----------+                   +--------+                   +--------+
967*e3723e1fSApple OSS Distributions  * | INACTIVE |------------------>| WARMUP |------------------>| ACTIVE |
968*e3723e1fSApple OSS Distributions  * +----------+   dtrace_go(),    +--------+   dtrace_go(),    +--------+
969*e3723e1fSApple OSS Distributions  *                before BEGIN        |        after BEGIN       |  |  |
970*e3723e1fSApple OSS Distributions  *                                    |                          |  |  |
971*e3723e1fSApple OSS Distributions  *                      exit() action |                          |  |  |
972*e3723e1fSApple OSS Distributions  *                     from BEGIN ECB |                          |  |  |
973*e3723e1fSApple OSS Distributions  *                                    |                          |  |  |
974*e3723e1fSApple OSS Distributions  *                                    v                          |  |  |
975*e3723e1fSApple OSS Distributions  *                               +----------+     exit() action  |  |  |
976*e3723e1fSApple OSS Distributions  * +-----------------------------| DRAINING |<-------------------+  |  |
977*e3723e1fSApple OSS Distributions  * |                             +----------+                       |  |
978*e3723e1fSApple OSS Distributions  * |                                  |                             |  |
979*e3723e1fSApple OSS Distributions  * |                   dtrace_stop(), |                             |  |
980*e3723e1fSApple OSS Distributions  * |                     before END   |                             |  |
981*e3723e1fSApple OSS Distributions  * |                                  |                             |  |
982*e3723e1fSApple OSS Distributions  * |                                  v                             |  |
983*e3723e1fSApple OSS Distributions  * | +---------+                 +----------+                       |  |
984*e3723e1fSApple OSS Distributions  * | | STOPPED |<----------------| COOLDOWN |<----------------------+  |
985*e3723e1fSApple OSS Distributions  * | +---------+  dtrace_stop(), +----------+     dtrace_stop(),       |
986*e3723e1fSApple OSS Distributions  * |                after END                       before END         |
987*e3723e1fSApple OSS Distributions  * |                                                                   |
988*e3723e1fSApple OSS Distributions  * |                              +--------+                           |
989*e3723e1fSApple OSS Distributions  * +----------------------------->| KILLED |<--------------------------+
990*e3723e1fSApple OSS Distributions  *       deadman timeout or       +--------+     deadman timeout or
991*e3723e1fSApple OSS Distributions  *        killed consumer                         killed consumer
992*e3723e1fSApple OSS Distributions  *
993*e3723e1fSApple OSS Distributions  * Note that once a DTrace consumer has stopped tracing, there is no way to
994*e3723e1fSApple OSS Distributions  * restart it; if a DTrace consumer wishes to restart tracing, it must reopen
995*e3723e1fSApple OSS Distributions  * the DTrace pseudodevice.
996*e3723e1fSApple OSS Distributions  */
997*e3723e1fSApple OSS Distributions typedef enum dtrace_activity {
998*e3723e1fSApple OSS Distributions 	DTRACE_ACTIVITY_INACTIVE = 0,		/* not yet running */
999*e3723e1fSApple OSS Distributions 	DTRACE_ACTIVITY_WARMUP,			/* while starting */
1000*e3723e1fSApple OSS Distributions 	DTRACE_ACTIVITY_ACTIVE,			/* running */
1001*e3723e1fSApple OSS Distributions 	DTRACE_ACTIVITY_DRAINING,		/* before stopping */
1002*e3723e1fSApple OSS Distributions 	DTRACE_ACTIVITY_COOLDOWN,		/* while stopping */
1003*e3723e1fSApple OSS Distributions 	DTRACE_ACTIVITY_STOPPED,		/* after stopping */
1004*e3723e1fSApple OSS Distributions 	DTRACE_ACTIVITY_KILLED			/* killed */
1005*e3723e1fSApple OSS Distributions } dtrace_activity_t;
1006*e3723e1fSApple OSS Distributions 
1007*e3723e1fSApple OSS Distributions 
1008*e3723e1fSApple OSS Distributions /*
1009*e3723e1fSApple OSS Distributions  * APPLE NOTE:  DTrace dof modes implementation
1010*e3723e1fSApple OSS Distributions  *
1011*e3723e1fSApple OSS Distributions  * DTrace has four "dof modes". They are:
1012*e3723e1fSApple OSS Distributions  *
1013*e3723e1fSApple OSS Distributions  * DTRACE_DOF_MODE_NEVER	Never load any dof, period.
1014*e3723e1fSApple OSS Distributions  * DTRACE_DOF_MODE_LAZY_ON	Defer loading dof until later
1015*e3723e1fSApple OSS Distributions  * DTRACE_DOF_MODE_LAZY_OFF	Load all deferred dof now, and any new dof
1016*e3723e1fSApple OSS Distributions  * DTRACE_DOF_MODE_NON_LAZY	Load all dof immediately.
1017*e3723e1fSApple OSS Distributions  *
1018*e3723e1fSApple OSS Distributions  * It is legal to transition between the two lazy modes. The NEVER and
1019*e3723e1fSApple OSS Distributions  * NON_LAZY modes are permanent, and must not change once set.
1020*e3723e1fSApple OSS Distributions  *
1021*e3723e1fSApple OSS Distributions  * The current dof mode is kept in dtrace_dof_mode, which is protected by the
1022*e3723e1fSApple OSS Distributions  * dtrace_dof_mode_lock. This is a RW lock, reads require shared access, writes
1023*e3723e1fSApple OSS Distributions  * require exclusive access. Because NEVER and NON_LAZY are permanent states,
1024*e3723e1fSApple OSS Distributions  * it is legal to test for those modes without holding the dof mode lock.
1025*e3723e1fSApple OSS Distributions  *
1026*e3723e1fSApple OSS Distributions  * Lock ordering is dof mode lock before any dtrace lock, and before the
1027*e3723e1fSApple OSS Distributions  * process p_dtrace_sprlock. In general, other locks should not be held when
1028*e3723e1fSApple OSS Distributions  * taking the dof mode lock. Acquiring the dof mode lock in exclusive mode
1029*e3723e1fSApple OSS Distributions  * will block process fork, exec, and exit, so it should be held exclusive
1030*e3723e1fSApple OSS Distributions  * for as short a time as possible.
1031*e3723e1fSApple OSS Distributions  */
1032*e3723e1fSApple OSS Distributions 
1033*e3723e1fSApple OSS Distributions #define DTRACE_DOF_MODE_NEVER 		0
1034*e3723e1fSApple OSS Distributions #define DTRACE_DOF_MODE_LAZY_ON		1
1035*e3723e1fSApple OSS Distributions #define DTRACE_DOF_MODE_LAZY_OFF	2
1036*e3723e1fSApple OSS Distributions #define DTRACE_DOF_MODE_NON_LAZY	3
1037*e3723e1fSApple OSS Distributions 
1038*e3723e1fSApple OSS Distributions /*
1039*e3723e1fSApple OSS Distributions  * dtrace kernel symbol modes are used to control when the kernel may dispose of
1040*e3723e1fSApple OSS Distributions  * symbol information used by the fbt/sdt provider. The kernel itself, as well as
1041*e3723e1fSApple OSS Distributions  * every kext, has symbol table/nlist info that has historically been preserved
1042*e3723e1fSApple OSS Distributions  * for dtrace's use. This allowed dtrace to be lazy about allocating fbt/sdt probes,
1043*e3723e1fSApple OSS Distributions  * at the expense of keeping the symbol info in the kernel permanently.
1044*e3723e1fSApple OSS Distributions  *
1045*e3723e1fSApple OSS Distributions  * Starting in 10.7+, fbt probes may be created from userspace, in the same
1046*e3723e1fSApple OSS Distributions  * fashion as pid probes. The kernel allows dtrace "first right of refusal"
1047*e3723e1fSApple OSS Distributions  * whenever symbol data becomes available (such as a kext load). If dtrace is
1048*e3723e1fSApple OSS Distributions  * active, it will immediately read/copy the needed data, and then the kernel
1049*e3723e1fSApple OSS Distributions  * may free it. If dtrace is not active, it returns immediately, having done
1050*e3723e1fSApple OSS Distributions  * no work or allocations, and the symbol data is freed. Should dtrace need
1051*e3723e1fSApple OSS Distributions  * this data later, it is expected that the userspace client will push the
1052*e3723e1fSApple OSS Distributions  * data into the kernel via ioctl calls.
1053*e3723e1fSApple OSS Distributions  *
1054*e3723e1fSApple OSS Distributions  * The kernel symbol modes are used to control what dtrace does with symbol data:
1055*e3723e1fSApple OSS Distributions  *
1056*e3723e1fSApple OSS Distributions  * DTRACE_KERNEL_SYMBOLS_NEVER			Effectively disables fbt/sdt
1057*e3723e1fSApple OSS Distributions  * DTRACE_KERNEL_SYMBOLS_FROM_KERNEL		Immediately read/copy symbol data
1058*e3723e1fSApple OSS Distributions  * DTRACE_KERNEL_SYMBOLS_FROM_USERSPACE		Wait for symbols from userspace
1059*e3723e1fSApple OSS Distributions  * DTRACE_KERNEL_SYMBOLS_ALWAYS_FROM_KERNEL	Immediately read/copy symbol data
1060*e3723e1fSApple OSS Distributions  *
1061*e3723e1fSApple OSS Distributions  * It is legal to transition between DTRACE_KERNEL_SYMBOLS_FROM_KERNEL and
1062*e3723e1fSApple OSS Distributions  * DTRACE_KERNEL_SYMBOLS_FROM_USERSPACE. The DTRACE_KERNEL_SYMBOLS_NEVER and
1063*e3723e1fSApple OSS Distributions  * DTRACE_KERNEL_SYMBOLS_ALWAYS_FROM_KERNEL are permanent modes, intended to
1064*e3723e1fSApple OSS Distributions  * disable fbt probes entirely, or prevent any symbols being loaded from
1065*e3723e1fSApple OSS Distributions  * userspace.
1066*e3723e1fSApple OSS Distributions *
1067*e3723e1fSApple OSS Distributions  * The kernel symbol mode is kept in dtrace_kernel_symbol_mode, which is protected
1068*e3723e1fSApple OSS Distributions  * by the dtrace_lock.
1069*e3723e1fSApple OSS Distributions  */
1070*e3723e1fSApple OSS Distributions 
1071*e3723e1fSApple OSS Distributions #define DTRACE_KERNEL_SYMBOLS_NEVER 			0
1072*e3723e1fSApple OSS Distributions #define DTRACE_KERNEL_SYMBOLS_FROM_KERNEL		1
1073*e3723e1fSApple OSS Distributions #define DTRACE_KERNEL_SYMBOLS_FROM_USERSPACE		2
1074*e3723e1fSApple OSS Distributions #define DTRACE_KERNEL_SYMBOLS_ALWAYS_FROM_KERNEL	3
1075*e3723e1fSApple OSS Distributions 
1076*e3723e1fSApple OSS Distributions 
1077*e3723e1fSApple OSS Distributions /*
1078*e3723e1fSApple OSS Distributions  * DTrace Helper Implementation
1079*e3723e1fSApple OSS Distributions  *
1080*e3723e1fSApple OSS Distributions  * A description of the helper architecture may be found in <sys/dtrace.h>.
1081*e3723e1fSApple OSS Distributions  * Each process contains a pointer to its helpers in its p_dtrace_helpers
1082*e3723e1fSApple OSS Distributions  * member.  This is a pointer to a dtrace_helpers structure, which contains an
1083*e3723e1fSApple OSS Distributions  * array of pointers to dtrace_helper structures, helper variable state (shared
1084*e3723e1fSApple OSS Distributions  * among a process's helpers) and a generation count.  (The generation count is
1085*e3723e1fSApple OSS Distributions  * used to provide an identifier when a helper is added so that it may be
1086*e3723e1fSApple OSS Distributions  * subsequently removed.)  The dtrace_helper structure is self-explanatory,
1087*e3723e1fSApple OSS Distributions  * containing pointers to the objects needed to execute the helper.  Note that
1088*e3723e1fSApple OSS Distributions  * helpers are _duplicated_ across fork(2), and destroyed on exec(2).  No more
1089*e3723e1fSApple OSS Distributions  * than dtrace_helpers_max are allowed per-process.
1090*e3723e1fSApple OSS Distributions  */
1091*e3723e1fSApple OSS Distributions #define	DTRACE_HELPER_ACTION_USTACK	0
1092*e3723e1fSApple OSS Distributions #define	DTRACE_NHELPER_ACTIONS		1
1093*e3723e1fSApple OSS Distributions 
1094*e3723e1fSApple OSS Distributions typedef struct dtrace_helper_action {
1095*e3723e1fSApple OSS Distributions 	int dtha_generation;			/* helper action generation */
1096*e3723e1fSApple OSS Distributions 	int dtha_nactions;			/* number of actions */
1097*e3723e1fSApple OSS Distributions 	dtrace_difo_t *dtha_predicate;		/* helper action predicate */
1098*e3723e1fSApple OSS Distributions 	dtrace_difo_t **dtha_actions;		/* array of actions */
1099*e3723e1fSApple OSS Distributions 	struct dtrace_helper_action *dtha_next;	/* next helper action */
1100*e3723e1fSApple OSS Distributions } dtrace_helper_action_t;
1101*e3723e1fSApple OSS Distributions 
1102*e3723e1fSApple OSS Distributions typedef struct dtrace_helper_provider {
1103*e3723e1fSApple OSS Distributions 	int dthp_generation;			/* helper provider generation */
1104*e3723e1fSApple OSS Distributions 	uint32_t dthp_ref;			/* reference count */
1105*e3723e1fSApple OSS Distributions 	dof_helper_t dthp_prov;			/* DOF w/ provider and probes */
1106*e3723e1fSApple OSS Distributions } dtrace_helper_provider_t;
1107*e3723e1fSApple OSS Distributions 
1108*e3723e1fSApple OSS Distributions typedef struct dtrace_helpers {
1109*e3723e1fSApple OSS Distributions 	dtrace_helper_action_t **dthps_actions;	/* array of helper actions */
1110*e3723e1fSApple OSS Distributions 	dtrace_vstate_t dthps_vstate;		/* helper action var. state */
1111*e3723e1fSApple OSS Distributions 	dtrace_helper_provider_t **dthps_provs;	/* array of providers */
1112*e3723e1fSApple OSS Distributions 	uint_t dthps_nprovs;			/* count of providers */
1113*e3723e1fSApple OSS Distributions 	uint_t dthps_maxprovs;			/* provider array size */
1114*e3723e1fSApple OSS Distributions 	int dthps_generation;			/* current generation */
1115*e3723e1fSApple OSS Distributions 	pid_t dthps_pid;			/* pid of associated proc */
1116*e3723e1fSApple OSS Distributions 	int dthps_deferred;			/* helper in deferred list */
1117*e3723e1fSApple OSS Distributions 	struct dtrace_helpers *dthps_next;	/* next pointer */
1118*e3723e1fSApple OSS Distributions 	struct dtrace_helpers *dthps_prev;	/* prev pointer */
1119*e3723e1fSApple OSS Distributions } dtrace_helpers_t;
1120*e3723e1fSApple OSS Distributions 
1121*e3723e1fSApple OSS Distributions /*
1122*e3723e1fSApple OSS Distributions  * DTrace Helper Action Tracing
1123*e3723e1fSApple OSS Distributions  *
1124*e3723e1fSApple OSS Distributions  * Debugging helper actions can be arduous.  To ease the development and
1125*e3723e1fSApple OSS Distributions  * debugging of helpers, DTrace contains a tracing-framework-within-a-tracing-
1126*e3723e1fSApple OSS Distributions  * framework: helper tracing.  If dtrace_helptrace_enabled is non-zero (which
1127*e3723e1fSApple OSS Distributions  * it is by default on DEBUG kernels), all helper activity will be traced to a
1128*e3723e1fSApple OSS Distributions  * global, in-kernel ring buffer.  Each entry includes a pointer to the specific
1129*e3723e1fSApple OSS Distributions  * helper, the location within the helper, and a trace of all local variables.
1130*e3723e1fSApple OSS Distributions  * The ring buffer may be displayed in a human-readable format with the
1131*e3723e1fSApple OSS Distributions  * ::dtrace_helptrace mdb(1) dcmd.
1132*e3723e1fSApple OSS Distributions  */
1133*e3723e1fSApple OSS Distributions #define	DTRACE_HELPTRACE_NEXT	(-1)
1134*e3723e1fSApple OSS Distributions #define	DTRACE_HELPTRACE_DONE	(-2)
1135*e3723e1fSApple OSS Distributions #define	DTRACE_HELPTRACE_ERR	(-3)
1136*e3723e1fSApple OSS Distributions 
1137*e3723e1fSApple OSS Distributions 
1138*e3723e1fSApple OSS Distributions typedef struct dtrace_helptrace {
1139*e3723e1fSApple OSS Distributions 	dtrace_helper_action_t	*dtht_helper;	/* helper action */
1140*e3723e1fSApple OSS Distributions 	int dtht_where;				/* where in helper action */
1141*e3723e1fSApple OSS Distributions 	int dtht_nlocals;			/* number of locals */
1142*e3723e1fSApple OSS Distributions 	int dtht_fault;				/* type of fault (if any) */
1143*e3723e1fSApple OSS Distributions 	int dtht_fltoffs;			/* DIF offset */
1144*e3723e1fSApple OSS Distributions 	uint64_t dtht_illval;			/* faulting value */
1145*e3723e1fSApple OSS Distributions 	uint64_t dtht_locals[1];		/* local variables */
1146*e3723e1fSApple OSS Distributions } dtrace_helptrace_t;
1147*e3723e1fSApple OSS Distributions 
1148*e3723e1fSApple OSS Distributions /*
1149*e3723e1fSApple OSS Distributions  * DTrace Credentials
1150*e3723e1fSApple OSS Distributions  *
1151*e3723e1fSApple OSS Distributions  * In probe context, we have limited flexibility to examine the credentials
1152*e3723e1fSApple OSS Distributions  * of the DTrace consumer that created a particular enabling.  We use
1153*e3723e1fSApple OSS Distributions  * the Least Privilege interfaces to cache the consumer's cred pointer and
1154*e3723e1fSApple OSS Distributions  * some facts about that credential in a dtrace_cred_t structure. These
1155*e3723e1fSApple OSS Distributions  * can limit the consumer's breadth of visibility and what actions the
1156*e3723e1fSApple OSS Distributions  * consumer may take.
1157*e3723e1fSApple OSS Distributions  */
1158*e3723e1fSApple OSS Distributions #define	DTRACE_CRV_ALLPROC		0x01
1159*e3723e1fSApple OSS Distributions #define	DTRACE_CRV_KERNEL		0x02
1160*e3723e1fSApple OSS Distributions #define	DTRACE_CRV_ALLZONE		0x04
1161*e3723e1fSApple OSS Distributions 
1162*e3723e1fSApple OSS Distributions #define	DTRACE_CRV_ALL		(DTRACE_CRV_ALLPROC | DTRACE_CRV_KERNEL | \
1163*e3723e1fSApple OSS Distributions 	DTRACE_CRV_ALLZONE)
1164*e3723e1fSApple OSS Distributions 
1165*e3723e1fSApple OSS Distributions #define	DTRACE_CRA_PROC				0x0001
1166*e3723e1fSApple OSS Distributions #define	DTRACE_CRA_PROC_CONTROL			0x0002
1167*e3723e1fSApple OSS Distributions #define	DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER	0x0004
1168*e3723e1fSApple OSS Distributions #define	DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE	0x0008
1169*e3723e1fSApple OSS Distributions #define	DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG	0x0010
1170*e3723e1fSApple OSS Distributions #define	DTRACE_CRA_KERNEL			0x0020
1171*e3723e1fSApple OSS Distributions #define	DTRACE_CRA_KERNEL_DESTRUCTIVE		0x0040
1172*e3723e1fSApple OSS Distributions 
1173*e3723e1fSApple OSS Distributions #define	DTRACE_CRA_ALL		(DTRACE_CRA_PROC | \
1174*e3723e1fSApple OSS Distributions 	DTRACE_CRA_PROC_CONTROL | \
1175*e3723e1fSApple OSS Distributions 	DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER | \
1176*e3723e1fSApple OSS Distributions 	DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE | \
1177*e3723e1fSApple OSS Distributions 	DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG | \
1178*e3723e1fSApple OSS Distributions 	DTRACE_CRA_KERNEL | \
1179*e3723e1fSApple OSS Distributions 	DTRACE_CRA_KERNEL_DESTRUCTIVE)
1180*e3723e1fSApple OSS Distributions 
1181*e3723e1fSApple OSS Distributions typedef struct dtrace_cred {
1182*e3723e1fSApple OSS Distributions 	cred_t			*dcr_cred;
1183*e3723e1fSApple OSS Distributions 	uint8_t			dcr_destructive;
1184*e3723e1fSApple OSS Distributions 	uint8_t			dcr_visible;
1185*e3723e1fSApple OSS Distributions 	uint16_t		dcr_action;
1186*e3723e1fSApple OSS Distributions } dtrace_cred_t;
1187*e3723e1fSApple OSS Distributions 
1188*e3723e1fSApple OSS Distributions typedef struct dtrace_format {
1189*e3723e1fSApple OSS Distributions 	uint64_t dtf_refcount;
1190*e3723e1fSApple OSS Distributions 	char dtf_str[];
1191*e3723e1fSApple OSS Distributions } dtrace_format_t;
1192*e3723e1fSApple OSS Distributions 
1193*e3723e1fSApple OSS Distributions #define DTRACE_FORMAT_SIZE(fmt) (strlen(fmt->dtf_str) + 1 + sizeof(dtrace_format_t))
1194*e3723e1fSApple OSS Distributions 
1195*e3723e1fSApple OSS Distributions /*
1196*e3723e1fSApple OSS Distributions  * DTrace Consumer State
1197*e3723e1fSApple OSS Distributions  *
1198*e3723e1fSApple OSS Distributions  * Each DTrace consumer has an associated dtrace_state structure that contains
1199*e3723e1fSApple OSS Distributions  * its in-kernel DTrace state -- including options, credentials, statistics and
1200*e3723e1fSApple OSS Distributions  * pointers to ECBs, buffers, speculations and formats.  A dtrace_state
1201*e3723e1fSApple OSS Distributions  * structure is also allocated for anonymous enablings.  When anonymous state
1202*e3723e1fSApple OSS Distributions  * is grabbed, the grabbing consumers dts_anon pointer is set to the grabbed
1203*e3723e1fSApple OSS Distributions  * dtrace_state structure.
1204*e3723e1fSApple OSS Distributions  */
1205*e3723e1fSApple OSS Distributions struct dtrace_state {
1206*e3723e1fSApple OSS Distributions 	dev_t dts_dev;				/* device */
1207*e3723e1fSApple OSS Distributions 	int dts_necbs;				/* total number of ECBs */
1208*e3723e1fSApple OSS Distributions 	dtrace_ecb_t **dts_ecbs;		/* array of ECBs */
1209*e3723e1fSApple OSS Distributions 	dtrace_epid_t dts_epid;			/* next EPID to allocate */
1210*e3723e1fSApple OSS Distributions 	size_t dts_needed;			/* greatest needed space */
1211*e3723e1fSApple OSS Distributions 	struct dtrace_state *dts_anon;		/* anon. state, if grabbed */
1212*e3723e1fSApple OSS Distributions 	dtrace_activity_t dts_activity;		/* current activity */
1213*e3723e1fSApple OSS Distributions 	dtrace_vstate_t dts_vstate;		/* variable state */
1214*e3723e1fSApple OSS Distributions 	dtrace_buffer_t *dts_buffer;		/* principal buffer */
1215*e3723e1fSApple OSS Distributions 	dtrace_buffer_t *dts_aggbuffer;		/* aggregation buffer */
1216*e3723e1fSApple OSS Distributions 	dtrace_speculation_t *dts_speculations;	/* speculation array */
1217*e3723e1fSApple OSS Distributions 	int dts_nspeculations;			/* number of speculations */
1218*e3723e1fSApple OSS Distributions 	int dts_naggregations;			/* number of aggregations */
1219*e3723e1fSApple OSS Distributions 	dtrace_aggregation_t **dts_aggregations; /* aggregation array */
1220*e3723e1fSApple OSS Distributions 	vmem_t *dts_aggid_arena;		/* arena for aggregation IDs */
1221*e3723e1fSApple OSS Distributions 	uint64_t dts_errors;			/* total number of errors */
1222*e3723e1fSApple OSS Distributions 	uint32_t dts_speculations_busy;		/* number of spec. busy */
1223*e3723e1fSApple OSS Distributions 	uint32_t dts_speculations_unavail;	/* number of spec unavail */
1224*e3723e1fSApple OSS Distributions 	uint32_t dts_stkstroverflows;		/* stack string tab overflows */
1225*e3723e1fSApple OSS Distributions 	uint32_t dts_dblerrors;			/* errors in ERROR probes */
1226*e3723e1fSApple OSS Distributions 	uint32_t dts_reserve;			/* space reserved for END */
1227*e3723e1fSApple OSS Distributions 	hrtime_t dts_laststatus;		/* time of last status */
1228*e3723e1fSApple OSS Distributions 	cyclic_id_t dts_cleaner;		/* cleaning cyclic */
1229*e3723e1fSApple OSS Distributions 	cyclic_id_t dts_deadman;		/* deadman cyclic */
1230*e3723e1fSApple OSS Distributions 	hrtime_t dts_alive;			/* time last alive */
1231*e3723e1fSApple OSS Distributions 	char dts_speculates;			/* boolean: has speculations */
1232*e3723e1fSApple OSS Distributions 	char dts_destructive;			/* boolean: has dest. actions */
1233*e3723e1fSApple OSS Distributions 	int dts_nformats;			/* number of formats */
1234*e3723e1fSApple OSS Distributions 	dtrace_format_t **dts_formats;		/* format string array */
1235*e3723e1fSApple OSS Distributions 	dtrace_optval_t dts_options[DTRACEOPT_MAX]; /* options */
1236*e3723e1fSApple OSS Distributions 	dtrace_cred_t dts_cred;			/* credentials */
1237*e3723e1fSApple OSS Distributions 	size_t dts_nretained;			/* number of retained enabs */
1238*e3723e1fSApple OSS Distributions 	uint64_t dts_arg_error_illval;
1239*e3723e1fSApple OSS Distributions 	uint32_t dts_buf_over_limit;		/* number of bufs over dtb_limit */
1240*e3723e1fSApple OSS Distributions 	uint64_t **dts_rstate;			/* per-CPU random state */
1241*e3723e1fSApple OSS Distributions };
1242*e3723e1fSApple OSS Distributions 
1243*e3723e1fSApple OSS Distributions struct dtrace_provider {
1244*e3723e1fSApple OSS Distributions 	dtrace_pattr_t dtpv_attr;		/* provider attributes */
1245*e3723e1fSApple OSS Distributions 	dtrace_ppriv_t dtpv_priv;		/* provider privileges */
1246*e3723e1fSApple OSS Distributions 	dtrace_pops_t dtpv_pops;		/* provider operations */
1247*e3723e1fSApple OSS Distributions 	char *dtpv_name;			/* provider name */
1248*e3723e1fSApple OSS Distributions 	void *dtpv_arg;				/* provider argument */
1249*e3723e1fSApple OSS Distributions 	uint_t dtpv_defunct;			/* boolean: defunct provider */
1250*e3723e1fSApple OSS Distributions 	struct dtrace_provider *dtpv_next;	/* next provider */
1251*e3723e1fSApple OSS Distributions 	uint64_t dtpv_probe_count;		/* number of associated probes */
1252*e3723e1fSApple OSS Distributions 	uint64_t dtpv_ecb_count;		/* number of associated enabled ECBs */
1253*e3723e1fSApple OSS Distributions };
1254*e3723e1fSApple OSS Distributions 
1255*e3723e1fSApple OSS Distributions struct dtrace_meta {
1256*e3723e1fSApple OSS Distributions 	dtrace_mops_t dtm_mops;			/* meta provider operations */
1257*e3723e1fSApple OSS Distributions 	char *dtm_name;				/* meta provider name */
1258*e3723e1fSApple OSS Distributions 	void *dtm_arg;				/* meta provider user arg */
1259*e3723e1fSApple OSS Distributions 	uint64_t dtm_count;			/* number of associated providers */
1260*e3723e1fSApple OSS Distributions };
1261*e3723e1fSApple OSS Distributions 
1262*e3723e1fSApple OSS Distributions /*
1263*e3723e1fSApple OSS Distributions  * DTrace Enablings
1264*e3723e1fSApple OSS Distributions  *
1265*e3723e1fSApple OSS Distributions  * A dtrace_enabling structure is used to track a collection of ECB
1266*e3723e1fSApple OSS Distributions  * descriptions -- before they have been turned into actual ECBs.  This is
1267*e3723e1fSApple OSS Distributions  * created as a result of DOF processing, and is generally used to generate
1268*e3723e1fSApple OSS Distributions  * ECBs immediately thereafter.  However, enablings are also generally
1269*e3723e1fSApple OSS Distributions  * retained should the probes they describe be created at a later time; as
1270*e3723e1fSApple OSS Distributions  * each new module or provider registers with the framework, the retained
1271*e3723e1fSApple OSS Distributions  * enablings are reevaluated, with any new match resulting in new ECBs.  To
1272*e3723e1fSApple OSS Distributions  * prevent probes from being matched more than once, the enabling tracks the
1273*e3723e1fSApple OSS Distributions  * last probe generation matched, and only matches probes from subsequent
1274*e3723e1fSApple OSS Distributions  * generations.
1275*e3723e1fSApple OSS Distributions  */
1276*e3723e1fSApple OSS Distributions typedef struct dtrace_enabling {
1277*e3723e1fSApple OSS Distributions 	dtrace_ecbdesc_t **dten_desc;		/* all ECB descriptions */
1278*e3723e1fSApple OSS Distributions 	int dten_ndesc;				/* number of ECB descriptions */
1279*e3723e1fSApple OSS Distributions 	int dten_maxdesc;			/* size of ECB array */
1280*e3723e1fSApple OSS Distributions 	dtrace_vstate_t *dten_vstate;		/* associated variable state */
1281*e3723e1fSApple OSS Distributions 	dtrace_genid_t dten_probegen;		/* matched probe generation */
1282*e3723e1fSApple OSS Distributions 	dtrace_ecbdesc_t *dten_current;		/* current ECB description */
1283*e3723e1fSApple OSS Distributions 	int dten_error;				/* current error value */
1284*e3723e1fSApple OSS Distributions 	int dten_primed;			/* boolean: set if primed */
1285*e3723e1fSApple OSS Distributions 	struct dtrace_enabling *dten_prev;	/* previous enabling */
1286*e3723e1fSApple OSS Distributions 	struct dtrace_enabling *dten_next;	/* next enabling */
1287*e3723e1fSApple OSS Distributions } dtrace_enabling_t;
1288*e3723e1fSApple OSS Distributions 
1289*e3723e1fSApple OSS Distributions /*
1290*e3723e1fSApple OSS Distributions  * DTrace Anonymous Enablings
1291*e3723e1fSApple OSS Distributions  *
1292*e3723e1fSApple OSS Distributions  * Anonymous enablings are DTrace enablings that are not associated with a
1293*e3723e1fSApple OSS Distributions  * controlling process, but rather derive their enabling from DOF stored as
1294*e3723e1fSApple OSS Distributions  * properties in the dtrace.conf file.  If there is an anonymous enabling, a
1295*e3723e1fSApple OSS Distributions  * DTrace consumer state and enabling are created on attach.  The state may be
1296*e3723e1fSApple OSS Distributions  * subsequently grabbed by the first consumer specifying the "grabanon"
1297*e3723e1fSApple OSS Distributions  * option.  As long as an anonymous DTrace enabling exists, dtrace(7D) will
1298*e3723e1fSApple OSS Distributions  * refuse to unload.
1299*e3723e1fSApple OSS Distributions  */
1300*e3723e1fSApple OSS Distributions typedef struct dtrace_anon {
1301*e3723e1fSApple OSS Distributions 	dtrace_state_t *dta_state;		/* DTrace consumer state */
1302*e3723e1fSApple OSS Distributions 	dtrace_enabling_t *dta_enabling;	/* pointer to enabling */
1303*e3723e1fSApple OSS Distributions 	processorid_t dta_beganon;		/* which CPU BEGIN ran on */
1304*e3723e1fSApple OSS Distributions } dtrace_anon_t;
1305*e3723e1fSApple OSS Distributions 
1306*e3723e1fSApple OSS Distributions /*
1307*e3723e1fSApple OSS Distributions  * DTrace Error Debugging
1308*e3723e1fSApple OSS Distributions  */
1309*e3723e1fSApple OSS Distributions #if DEBUG
1310*e3723e1fSApple OSS Distributions #define	DTRACE_ERRDEBUG
1311*e3723e1fSApple OSS Distributions #endif
1312*e3723e1fSApple OSS Distributions 
1313*e3723e1fSApple OSS Distributions #ifdef DTRACE_ERRDEBUG
1314*e3723e1fSApple OSS Distributions 
1315*e3723e1fSApple OSS Distributions typedef struct dtrace_errhash {
1316*e3723e1fSApple OSS Distributions 	const char	*dter_msg;	/* error message */
1317*e3723e1fSApple OSS Distributions 	int		dter_count;	/* number of times seen */
1318*e3723e1fSApple OSS Distributions } dtrace_errhash_t;
1319*e3723e1fSApple OSS Distributions 
1320*e3723e1fSApple OSS Distributions #define	DTRACE_ERRHASHSZ	256	/* must be > number of err msgs */
1321*e3723e1fSApple OSS Distributions 
1322*e3723e1fSApple OSS Distributions #endif	/* DTRACE_ERRDEBUG */
1323*e3723e1fSApple OSS Distributions 
1324*e3723e1fSApple OSS Distributions typedef struct dtrace_string dtrace_string_t;
1325*e3723e1fSApple OSS Distributions 
1326*e3723e1fSApple OSS Distributions typedef struct dtrace_string {
1327*e3723e1fSApple OSS Distributions 	dtrace_string_t *dtst_next;
1328*e3723e1fSApple OSS Distributions 	dtrace_string_t *dtst_prev;
1329*e3723e1fSApple OSS Distributions 	uint32_t dtst_refcount;
1330*e3723e1fSApple OSS Distributions 	char dtst_str[];
1331*e3723e1fSApple OSS Distributions } dtrace_string_t;
1332*e3723e1fSApple OSS Distributions 
1333*e3723e1fSApple OSS Distributions /**
1334*e3723e1fSApple OSS Distributions  * DTrace Matching pre-conditions
1335*e3723e1fSApple OSS Distributions  *
1336*e3723e1fSApple OSS Distributions  * Used when matching new probes to discard matching of enablings that
1337*e3723e1fSApple OSS Distributions  * doesn't match the condition tested by dmc_func
1338*e3723e1fSApple OSS Distributions  */
1339*e3723e1fSApple OSS Distributions typedef struct dtrace_match_cond {
1340*e3723e1fSApple OSS Distributions 	int (*dmc_func)(dtrace_probedesc_t*, void*);
1341*e3723e1fSApple OSS Distributions 	void *dmc_data;
1342*e3723e1fSApple OSS Distributions } dtrace_match_cond_t;
1343*e3723e1fSApple OSS Distributions 
1344*e3723e1fSApple OSS Distributions 
1345*e3723e1fSApple OSS Distributions /*
1346*e3723e1fSApple OSS Distributions  * DTrace Toxic Ranges
1347*e3723e1fSApple OSS Distributions  *
1348*e3723e1fSApple OSS Distributions  * DTrace supports safe loads from probe context; if the address turns out to
1349*e3723e1fSApple OSS Distributions  * be invalid, a bit will be set by the kernel indicating that DTrace
1350*e3723e1fSApple OSS Distributions  * encountered a memory error, and DTrace will propagate the error to the user
1351*e3723e1fSApple OSS Distributions  * accordingly.  However, there may exist some regions of memory in which an
1352*e3723e1fSApple OSS Distributions  * arbitrary load can change system state, and from which it is impossible to
1353*e3723e1fSApple OSS Distributions  * recover from such a load after it has been attempted.  Examples of this may
1354*e3723e1fSApple OSS Distributions  * include memory in which programmable I/O registers are mapped (for which a
1355*e3723e1fSApple OSS Distributions  * read may have some implications for the device) or (in the specific case of
1356*e3723e1fSApple OSS Distributions  * UltraSPARC-I and -II) the virtual address hole.  The platform is required
1357*e3723e1fSApple OSS Distributions  * to make DTrace aware of these toxic ranges; DTrace will then check that
1358*e3723e1fSApple OSS Distributions  * target addresses are not in a toxic range before attempting to issue a
1359*e3723e1fSApple OSS Distributions  * safe load.
1360*e3723e1fSApple OSS Distributions  */
1361*e3723e1fSApple OSS Distributions typedef struct dtrace_toxrange {
1362*e3723e1fSApple OSS Distributions 	uintptr_t	dtt_base;		/* base of toxic range */
1363*e3723e1fSApple OSS Distributions 	uintptr_t	dtt_limit;		/* limit of toxic range */
1364*e3723e1fSApple OSS Distributions } dtrace_toxrange_t;
1365*e3723e1fSApple OSS Distributions 
1366*e3723e1fSApple OSS Distributions extern uint64_t dtrace_getarg(int, int, dtrace_mstate_t*, dtrace_vstate_t*);
1367*e3723e1fSApple OSS Distributions extern int dtrace_getipl(void);
1368*e3723e1fSApple OSS Distributions extern uintptr_t dtrace_caller(int);
1369*e3723e1fSApple OSS Distributions extern uint32_t dtrace_cas32(uint32_t *, uint32_t, uint32_t);
1370*e3723e1fSApple OSS Distributions extern void *dtrace_casptr(void *, void *, void *);
1371*e3723e1fSApple OSS Distributions extern void dtrace_copyin(user_addr_t, uintptr_t, size_t, volatile uint16_t *);
1372*e3723e1fSApple OSS Distributions extern void dtrace_copyinstr(user_addr_t, uintptr_t, size_t, volatile uint16_t *);
1373*e3723e1fSApple OSS Distributions extern void dtrace_copyout(uintptr_t, user_addr_t, size_t, volatile uint16_t *);
1374*e3723e1fSApple OSS Distributions extern void dtrace_copyoutstr(uintptr_t, user_addr_t, size_t, volatile uint16_t *);
1375*e3723e1fSApple OSS Distributions extern void dtrace_getpcstack(pc_t *, int, int, uint32_t *);
1376*e3723e1fSApple OSS Distributions extern uint64_t dtrace_load64(uintptr_t);
1377*e3723e1fSApple OSS Distributions extern int dtrace_canload(uint64_t, size_t, dtrace_mstate_t*, dtrace_vstate_t*);
1378*e3723e1fSApple OSS Distributions 
1379*e3723e1fSApple OSS Distributions extern uint64_t dtrace_getreg(struct regs *, uint_t);
1380*e3723e1fSApple OSS Distributions extern uint64_t dtrace_getvmreg(uint_t);
1381*e3723e1fSApple OSS Distributions extern int dtrace_getstackdepth(int);
1382*e3723e1fSApple OSS Distributions extern void dtrace_getupcstack(uint64_t *, int);
1383*e3723e1fSApple OSS Distributions extern void dtrace_getufpstack(uint64_t *, uint64_t *, int);
1384*e3723e1fSApple OSS Distributions extern int dtrace_getustackdepth(void);
1385*e3723e1fSApple OSS Distributions extern uintptr_t dtrace_fulword(void *);
1386*e3723e1fSApple OSS Distributions extern uint8_t dtrace_fuword8(user_addr_t);
1387*e3723e1fSApple OSS Distributions extern uint16_t dtrace_fuword16(user_addr_t);
1388*e3723e1fSApple OSS Distributions extern uint32_t dtrace_fuword32(user_addr_t);
1389*e3723e1fSApple OSS Distributions extern uint64_t dtrace_fuword64(user_addr_t);
1390*e3723e1fSApple OSS Distributions extern int dtrace_proc_waitfor(dtrace_procdesc_t*);
1391*e3723e1fSApple OSS Distributions extern void dtrace_probe_error(dtrace_state_t *, dtrace_epid_t, int, int,
1392*e3723e1fSApple OSS Distributions     int, uint64_t);
1393*e3723e1fSApple OSS Distributions extern int dtrace_assfail(const char *, const char *, int);
1394*e3723e1fSApple OSS Distributions extern int dtrace_attached(void);
1395*e3723e1fSApple OSS Distributions extern hrtime_t dtrace_gethrestime(void);
1396*e3723e1fSApple OSS Distributions 
1397*e3723e1fSApple OSS Distributions extern void dtrace_flush_caches(void);
1398*e3723e1fSApple OSS Distributions 
1399*e3723e1fSApple OSS Distributions extern void dtrace_copy(uintptr_t, uintptr_t, size_t);
1400*e3723e1fSApple OSS Distributions extern void dtrace_copystr(uintptr_t, uintptr_t, size_t, volatile uint16_t *);
1401*e3723e1fSApple OSS Distributions 
1402*e3723e1fSApple OSS Distributions extern void* dtrace_ptrauth_strip(void*, uint64_t);
1403*e3723e1fSApple OSS Distributions extern int dtrace_is_valid_ptrauth_key(uint64_t);
1404*e3723e1fSApple OSS Distributions 
1405*e3723e1fSApple OSS Distributions extern uint64_t dtrace_physmem_read(uint64_t, size_t);
1406*e3723e1fSApple OSS Distributions extern void dtrace_physmem_write(uint64_t, uint64_t, size_t);
1407*e3723e1fSApple OSS Distributions 
1408*e3723e1fSApple OSS Distributions extern void dtrace_livedump(char *, size_t);
1409*e3723e1fSApple OSS Distributions 
1410*e3723e1fSApple OSS Distributions /*
1411*e3723e1fSApple OSS Distributions  * DTrace state handling
1412*e3723e1fSApple OSS Distributions  */
1413*e3723e1fSApple OSS Distributions extern minor_t dtrace_state_reserve(void);
1414*e3723e1fSApple OSS Distributions extern dtrace_state_t* dtrace_state_allocate(minor_t minor);
1415*e3723e1fSApple OSS Distributions extern dtrace_state_t* dtrace_state_get(minor_t minor);
1416*e3723e1fSApple OSS Distributions extern void dtrace_state_free(minor_t minor);
1417*e3723e1fSApple OSS Distributions 
1418*e3723e1fSApple OSS Distributions /*
1419*e3723e1fSApple OSS Distributions  * DTrace restriction checks
1420*e3723e1fSApple OSS Distributions  */
1421*e3723e1fSApple OSS Distributions extern void dtrace_restriction_policy_load(void);
1422*e3723e1fSApple OSS Distributions extern boolean_t dtrace_is_restricted(void);
1423*e3723e1fSApple OSS Distributions extern boolean_t dtrace_are_restrictions_relaxed(void);
1424*e3723e1fSApple OSS Distributions extern boolean_t dtrace_fbt_probes_restricted(void);
1425*e3723e1fSApple OSS Distributions extern boolean_t dtrace_sdt_probes_restricted(void);
1426*e3723e1fSApple OSS Distributions extern boolean_t dtrace_can_attach_to_proc(proc_t);
1427*e3723e1fSApple OSS Distributions 
1428*e3723e1fSApple OSS Distributions /*
1429*e3723e1fSApple OSS Distributions  * DTrace Assertions
1430*e3723e1fSApple OSS Distributions  *
1431*e3723e1fSApple OSS Distributions  * DTrace calls ASSERT and VERIFY from probe context.  To assure that a failed
1432*e3723e1fSApple OSS Distributions  * ASSERT or VERIFYdoes not induce a markedly more catastrophic failure (e.g.,
1433*e3723e1fSApple OSS Distributions  * one from which a dump cannot be gleaned), DTrace must define its own ASSERT
1434*e3723e1fSApple OSS Distributions  * and VERIFY macros to be ones that may safely be called from probe context.
1435*e3723e1fSApple OSS Distributions  * This header file must thus be included by any DTrace component that calls
1436*e3723e1fSApple OSS Distributions  * ASSERT and/or VERIFY from probe context, and _only_ by those components.
1437*e3723e1fSApple OSS Distributions  * (The only exception to this is kernel debugging infrastructure at user-level
1438*e3723e1fSApple OSS Distributions  * that doesn't depend on calling ASSERT.)
1439*e3723e1fSApple OSS Distributions  */
1440*e3723e1fSApple OSS Distributions #undef ASSERT
1441*e3723e1fSApple OSS Distributions #undef VERIFY
1442*e3723e1fSApple OSS Distributions 
1443*e3723e1fSApple OSS Distributions #define	VERIFY(EX)	((void)((EX) || \
1444*e3723e1fSApple OSS Distributions 			dtrace_assfail(#EX, __FILE__, __LINE__)))
1445*e3723e1fSApple OSS Distributions 
1446*e3723e1fSApple OSS Distributions #if DEBUG
1447*e3723e1fSApple OSS Distributions #define	ASSERT(EX)	((void)((EX) || \
1448*e3723e1fSApple OSS Distributions 			dtrace_assfail(#EX, __FILE__, __LINE__)))
1449*e3723e1fSApple OSS Distributions #else
1450*e3723e1fSApple OSS Distributions #define	ASSERT(X)	((void)0)
1451*e3723e1fSApple OSS Distributions #endif
1452*e3723e1fSApple OSS Distributions 
1453*e3723e1fSApple OSS Distributions #ifdef	__cplusplus
1454*e3723e1fSApple OSS Distributions }
1455*e3723e1fSApple OSS Distributions #endif
1456*e3723e1fSApple OSS Distributions 
1457*e3723e1fSApple OSS Distributions #endif /* _SYS_DTRACE_IMPL_H */
1458*e3723e1fSApple OSS Distributions 
1459