]> git.saurik.com Git - apple/xnu.git/blame - bsd/sys/fasttrap_impl.h
xnu-4570.31.3.tar.gz
[apple/xnu.git] / bsd / sys / fasttrap_impl.h
CommitLineData
2d21ac55
A
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
22/*
b0d623f7 23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2d21ac55
A
24 * Use is subject to license terms.
25 */
26
27#ifndef _FASTTRAP_IMPL_H
28#define _FASTTRAP_IMPL_H
29
30/*
b0d623f7 31 * #pragma ident "@(#)fasttrap_impl.h 1.14 08/04/09 SMI"
2d21ac55
A
32 */
33
34#include <sys/types.h>
35#include <sys/dtrace.h>
36#include <sys/proc.h>
37#include <sys/user.h>
38#include <sys/fasttrap.h>
39#include <sys/fasttrap_isa.h>
40
b0d623f7
A
41/* Solaris proc_t is the struct. Darwin's proc_t is a pointer to it. */
42#define proc_t struct proc /* Steer clear of the Darwin typedef for proc_t */
2d21ac55
A
43
44#ifdef __cplusplus
45extern "C" {
46#endif
47
48/*
49 * Fasttrap Providers, Probes and Tracepoints
50 *
51 * Each Solaris process can have multiple providers -- the pid provider as
52 * well as any number of user-level statically defined tracing (USDT)
53 * providers. Those providers are each represented by a fasttrap_provider_t.
54 * All providers for a given process have a pointer to a shared
55 * fasttrap_proc_t. The fasttrap_proc_t has two states: active or defunct.
b0d623f7
A
56 * When the count of active providers goes to zero it becomes defunct; a
57 * provider drops its active count when it is removed individually or as part
58 * of a mass removal when a process exits or performs an exec.
2d21ac55
A
59 *
60 * Each probe is represented by a fasttrap_probe_t which has a pointer to
61 * its associated provider as well as a list of fasttrap_id_tp_t structures
62 * which are tuples combining a fasttrap_id_t and a fasttrap_tracepoint_t.
63 * A fasttrap_tracepoint_t represents the actual point of instrumentation
64 * and it contains two lists of fasttrap_id_t structures (to be fired pre-
65 * and post-instruction emulation) that identify the probes attached to the
66 * tracepoint. Tracepoints also have a pointer to the fasttrap_proc_t for the
b0d623f7
A
67 * process they trace which is used when looking up a tracepoint both when a
68 * probe fires and when enabling and disabling probes.
2d21ac55
A
69 *
70 * It's important to note that probes are preallocated with the necessary
71 * number of tracepoints, but that tracepoints can be shared by probes and
72 * swapped between probes. If a probe's preallocated tracepoint is enabled
73 * (and, therefore, the associated probe is enabled), and that probe is
74 * then disabled, ownership of that tracepoint may be exchanged for an
75 * unused tracepoint belonging to another probe that was attached to the
76 * enabled tracepoint.
77 */
78
79/*
80 * APPLE NOTE: All kmutex_t's have been converted to lck_mtx_t
81 */
82
83typedef struct fasttrap_proc {
84 pid_t ftpc_pid; /* process ID for this proc */
b0d623f7
A
85 uint64_t ftpc_acount; /* count of active providers */
86 uint64_t ftpc_rcount; /* count of extant providers */
87 lck_mtx_t ftpc_mtx; /* lock on all but acount */
2d21ac55
A
88 struct fasttrap_proc *ftpc_next; /* next proc in hash chain */
89} fasttrap_proc_t;
90
91typedef struct fasttrap_provider {
92 pid_t ftp_pid; /* process ID for this prov */
93 fasttrap_provider_type_t ftp_provider_type; /* type of this provider (usdt, pid, objc, oneshot) */
94 char ftp_name[DTRACE_PROVNAMELEN]; /* prov name (w/o the pid) */
95 dtrace_provider_id_t ftp_provid; /* DTrace provider handle */
96 uint_t ftp_marked; /* mark for possible removal */
97 uint_t ftp_retired; /* mark when retired */
98 lck_mtx_t ftp_mtx; /* provider lock */
99 lck_mtx_t ftp_cmtx; /* lock on creating probes */
5ba3f43e 100 uint64_t ftp_pcount; /* probes in provider count */
2d21ac55
A
101 uint64_t ftp_rcount; /* enabled probes ref count */
102 uint64_t ftp_ccount; /* consumers creating probes */
103 uint64_t ftp_mcount; /* meta provider count */
104 fasttrap_proc_t *ftp_proc; /* shared proc for all provs */
105 struct fasttrap_provider *ftp_next; /* next prov in hash chain */
106} fasttrap_provider_t;
107
108typedef struct fasttrap_id fasttrap_id_t;
109typedef struct fasttrap_probe fasttrap_probe_t;
110typedef struct fasttrap_tracepoint fasttrap_tracepoint_t;
111
112struct fasttrap_id {
113 fasttrap_probe_t *fti_probe; /* referrring probe */
114 fasttrap_id_t *fti_next; /* enabled probe list on tp */
115 fasttrap_probe_type_t fti_ptype; /* probe type */
116};
117
118typedef struct fasttrap_id_tp {
119 fasttrap_id_t fit_id;
120 fasttrap_tracepoint_t *fit_tp;
121} fasttrap_id_tp_t;
122
123struct fasttrap_probe {
124 dtrace_id_t ftp_id; /* DTrace probe identifier */
125 pid_t ftp_pid; /* pid for this probe */
126 fasttrap_provider_t *ftp_prov; /* this probe's provider */
127 user_addr_t ftp_faddr; /* associated function's addr */
128 size_t ftp_fsize; /* associated function's size */
129 uint64_t ftp_gen; /* modification generation */
130 uint64_t ftp_ntps; /* number of tracepoints */
131 uint8_t *ftp_argmap; /* native to translated args */
132 uint8_t ftp_nargs; /* translated argument count */
133 uint8_t ftp_enabled; /* is this probe enabled */
5ba3f43e 134 uint8_t ftp_triggered;
2d21ac55
A
135 char *ftp_xtypes; /* translated types index */
136 char *ftp_ntypes; /* native types index */
137 fasttrap_id_tp_t ftp_tps[1]; /* flexible array */
138};
139
140#define FASTTRAP_ID_INDEX(id) \
141((fasttrap_id_tp_t *)(((char *)(id) - offsetof(fasttrap_id_tp_t, fit_id))) - \
142&(id)->fti_probe->ftp_tps[0])
143
144struct fasttrap_tracepoint {
145 fasttrap_proc_t *ftt_proc; /* associated process struct */
146 user_addr_t ftt_pc; /* address of tracepoint */
147 pid_t ftt_pid; /* pid of tracepoint */
148 fasttrap_machtp_t ftt_mtp; /* ISA-specific portion */
149 fasttrap_id_t *ftt_ids; /* NULL-terminated list */
150 fasttrap_id_t *ftt_retids; /* NULL-terminated list */
151 fasttrap_tracepoint_t *ftt_next; /* link in global hash */
152};
153
154typedef struct fasttrap_bucket {
155 lck_mtx_t ftb_mtx; /* bucket lock */
156 void *ftb_data; /* data payload */
157
158 uint8_t ftb_pad[64 - sizeof (lck_mtx_t) - sizeof (void *)];
159} fasttrap_bucket_t;
160
161typedef struct fasttrap_hash {
162 ulong_t fth_nent; /* power-of-2 num. of entries */
163 ulong_t fth_mask; /* fth_nent - 1 */
164 fasttrap_bucket_t *fth_table; /* array of buckets */
165} fasttrap_hash_t;
166
167/*
168 * If at some future point these assembly functions become observable by
169 * DTrace, then these defines should become separate functions so that the
170 * fasttrap provider doesn't trigger probes during internal operations.
171 */
172#define fasttrap_copyout copyout
173#define fasttrap_fuword32 fuword32
174#define fasttrap_suword32 suword32
175
2d21ac55 176/*
fe8ab488
A
177 * APPLE NOTE: xnu supports both 32bit and 64bit user processes.
178 * We need to make size explicit.
2d21ac55
A
179 */
180#define fasttrap_fuword64 fuword64
181#define fasttrap_suword64 suword64
182#define fasttrap_fuword64_noerr fuword64_noerr
183#define fasttrap_fuword32_noerr fuword32_noerr
2d21ac55
A
184
185extern void fasttrap_sigtrap(proc_t *, uthread_t, user_addr_t);
186
187extern dtrace_id_t fasttrap_probe_id;
188extern fasttrap_hash_t fasttrap_tpoints;
189
190#define FASTTRAP_TPOINTS_INDEX(pid, pc) \
191 (((pc) / sizeof (fasttrap_instr_t) + (pid)) & fasttrap_tpoints.fth_mask)
192
5ba3f43e
A
193
194#ifdef CONFIG_EMBEDDED
195#define FASTTRAP_ASYNC_REMOVE
196#endif
197
198extern void fasttrap_tracepoint_retire(proc_t *p, fasttrap_tracepoint_t *tp);
199
2d21ac55
A
200/*
201 * Must be implemented by fasttrap_isa.c
202 */
203extern int fasttrap_tracepoint_init(proc_t *, fasttrap_tracepoint_t *,
204 user_addr_t, fasttrap_probe_type_t);
205extern int fasttrap_tracepoint_install(proc_t *, fasttrap_tracepoint_t *);
206extern int fasttrap_tracepoint_remove(proc_t *, fasttrap_tracepoint_t *);
207
39236c6e 208#if defined(__x86_64__)
2d21ac55
A
209extern int fasttrap_pid_probe(x86_saved_state_t *regs);
210extern int fasttrap_return_probe(x86_saved_state_t* regs);
5ba3f43e
A
211#elif defined(__arm__) || defined(__arm64__)
212extern int fasttrap_pid_probe(arm_saved_state_t *rp);
213extern int fasttrap_return_probe(arm_saved_state_t *regs);
2d21ac55
A
214#else
215#error architecture not supported
216#endif
217
218extern uint64_t fasttrap_pid_getarg(void *, dtrace_id_t, void *, int, int);
219extern uint64_t fasttrap_usdt_getarg(void *, dtrace_id_t, void *, int, int);
220
5ba3f43e 221
2d21ac55
A
222#ifdef __cplusplus
223}
224#endif
225
226#undef proc_t
227
228#endif /* _FASTTRAP_IMPL_H */