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