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