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