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