xref: /freebsd/sys/cddl/contrib/opensolaris/uts/common/sys/fasttrap_impl.h (revision 5bab62343899b8c1aeaa4902a179423ef8915ec0)
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 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef	_FASTTRAP_IMPL_H
28 #define	_FASTTRAP_IMPL_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <sys/types.h>
33 #include <sys/dtrace.h>
34 #include <sys/proc.h>
35 #include <sys/queue.h>
36 #include <sys/fasttrap.h>
37 #include <sys/fasttrap_isa.h>
38 
39 #ifdef	__cplusplus
40 extern "C" {
41 #endif
42 
43 /*
44  * Fasttrap Providers, Probes and Tracepoints
45  *
46  * Each Solaris process can have multiple providers -- the pid provider as
47  * well as any number of user-level statically defined tracing (USDT)
48  * providers. Those providers are each represented by a fasttrap_provider_t.
49  * All providers for a given process have a pointer to a shared
50  * fasttrap_proc_t. The fasttrap_proc_t has two states: active or defunct.
51  * When the count of active providers goes to zero it becomes defunct; a
52  * provider drops its active count when it is removed individually or as part
53  * of a mass removal when a process exits or performs an exec.
54  *
55  * Each probe is represented by a fasttrap_probe_t which has a pointer to
56  * its associated provider as well as a list of fasttrap_id_tp_t structures
57  * which are tuples combining a fasttrap_id_t and a fasttrap_tracepoint_t.
58  * A fasttrap_tracepoint_t represents the actual point of instrumentation
59  * and it contains two lists of fasttrap_id_t structures (to be fired pre-
60  * and post-instruction emulation) that identify the probes attached to the
61  * tracepoint. Tracepoints also have a pointer to the fasttrap_proc_t for the
62  * process they trace which is used when looking up a tracepoint both when a
63  * probe fires and when enabling and disabling probes.
64  *
65  * It's important to note that probes are preallocated with the necessary
66  * number of tracepoints, but that tracepoints can be shared by probes and
67  * swapped between probes. If a probe's preallocated tracepoint is enabled
68  * (and, therefore, the associated probe is enabled), and that probe is
69  * then disabled, ownership of that tracepoint may be exchanged for an
70  * unused tracepoint belonging to another probe that was attached to the
71  * enabled tracepoint.
72  *
73  * On FreeBSD, fasttrap providers also maintain per-thread scratch space for use
74  * by the ISA-specific fasttrap code. The fasttrap_scrblock_t type stores the
75  * virtual address of a page-sized memory block that is mapped into a process'
76  * address space. Each block is carved up into chunks (fasttrap_scrspace_t) for
77  * use by individual threads, which keep the address of their scratch space
78  * chunk in their struct kdtrace_thread. A thread's scratch space isn't released
79  * until it exits.
80  */
81 
82 #ifndef illumos
83 typedef struct fasttrap_scrblock {
84 	vm_offset_t ftsb_addr;			/* address of a scratch block */
85 	LIST_ENTRY(fasttrap_scrblock) ftsb_next;/* next block in list */
86 } fasttrap_scrblock_t;
87 #define	FASTTRAP_SCRBLOCK_SIZE	PAGE_SIZE
88 
89 typedef struct fasttrap_scrspace {
90 	uintptr_t ftss_addr;			/* scratch space address */
91 	LIST_ENTRY(fasttrap_scrspace) ftss_next;/* next in list */
92 } fasttrap_scrspace_t;
93 #define	FASTTRAP_SCRSPACE_SIZE	64
94 #endif
95 
96 typedef struct fasttrap_proc {
97 	pid_t ftpc_pid;				/* process ID for this proc */
98 	uint64_t ftpc_acount;			/* count of active providers */
99 	uint64_t ftpc_rcount;			/* count of extant providers */
100 	kmutex_t ftpc_mtx;			/* lock on all but acount */
101 	struct fasttrap_proc *ftpc_next;	/* next proc in hash chain */
102 #ifndef illumos
103 	LIST_HEAD(, fasttrap_scrblock) ftpc_scrblks; /* mapped scratch blocks */
104 	LIST_HEAD(, fasttrap_scrspace) ftpc_fscr; /* free scratch space */
105 	LIST_HEAD(, fasttrap_scrspace) ftpc_ascr; /* used scratch space */
106 #endif
107 } fasttrap_proc_t;
108 
109 typedef struct fasttrap_provider {
110 	pid_t ftp_pid;				/* process ID for this prov */
111 	char ftp_name[DTRACE_PROVNAMELEN];	/* prov name (w/o the pid) */
112 	dtrace_provider_id_t ftp_provid;	/* DTrace provider handle */
113 	uint_t ftp_marked;			/* mark for possible removal */
114 	uint_t ftp_retired;			/* mark when retired */
115 	kmutex_t ftp_mtx;			/* provider lock */
116 	kmutex_t ftp_cmtx;			/* lock on creating probes */
117 	uint64_t ftp_rcount;			/* enabled probes ref count */
118 	uint64_t ftp_ccount;			/* consumers creating probes */
119 	uint64_t ftp_mcount;			/* meta provider count */
120 	fasttrap_proc_t *ftp_proc;		/* shared proc for all provs */
121 	struct fasttrap_provider *ftp_next;	/* next prov in hash chain */
122 } fasttrap_provider_t;
123 
124 typedef struct fasttrap_id fasttrap_id_t;
125 typedef struct fasttrap_probe fasttrap_probe_t;
126 typedef struct fasttrap_tracepoint fasttrap_tracepoint_t;
127 
128 struct fasttrap_id {
129 	fasttrap_probe_t *fti_probe;		/* referrring probe */
130 	fasttrap_id_t *fti_next;		/* enabled probe list on tp */
131 	fasttrap_probe_type_t fti_ptype;	/* probe type */
132 };
133 
134 typedef struct fasttrap_id_tp {
135 	fasttrap_id_t fit_id;
136 	fasttrap_tracepoint_t *fit_tp;
137 } fasttrap_id_tp_t;
138 
139 struct fasttrap_probe {
140 	dtrace_id_t ftp_id;			/* DTrace probe identifier */
141 	pid_t ftp_pid;				/* pid for this probe */
142 	fasttrap_provider_t *ftp_prov;		/* this probe's provider */
143 	uintptr_t ftp_faddr;			/* associated function's addr */
144 	size_t ftp_fsize;			/* associated function's size */
145 	uint64_t ftp_gen;			/* modification generation */
146 	uint64_t ftp_ntps;			/* number of tracepoints */
147 	uint8_t *ftp_argmap;			/* native to translated args */
148 	uint8_t ftp_nargs;			/* translated argument count */
149 	uint8_t ftp_enabled;			/* is this probe enabled */
150 	char *ftp_xtypes;			/* translated types index */
151 	char *ftp_ntypes;			/* native types index */
152 	fasttrap_id_tp_t ftp_tps[1];		/* flexible array */
153 };
154 
155 #define	FASTTRAP_ID_INDEX(id)	\
156 ((fasttrap_id_tp_t *)(((char *)(id) - offsetof(fasttrap_id_tp_t, fit_id))) - \
157 &(id)->fti_probe->ftp_tps[0])
158 
159 struct fasttrap_tracepoint {
160 	fasttrap_proc_t *ftt_proc;		/* associated process struct */
161 	uintptr_t ftt_pc;			/* address of tracepoint */
162 	pid_t ftt_pid;				/* pid of tracepoint */
163 	fasttrap_machtp_t ftt_mtp;		/* ISA-specific portion */
164 	fasttrap_id_t *ftt_ids;			/* NULL-terminated list */
165 	fasttrap_id_t *ftt_retids;		/* NULL-terminated list */
166 	fasttrap_tracepoint_t *ftt_next;	/* link in global hash */
167 };
168 
169 typedef struct fasttrap_bucket {
170 	kmutex_t ftb_mtx;			/* bucket lock */
171 	void *ftb_data;				/* data payload */
172 
173 	uint8_t ftb_pad[64 - sizeof (kmutex_t) - sizeof (void *)];
174 } fasttrap_bucket_t;
175 
176 typedef struct fasttrap_hash {
177 	ulong_t fth_nent;			/* power-of-2 num. of entries */
178 	ulong_t fth_mask;			/* fth_nent - 1 */
179 	fasttrap_bucket_t *fth_table;		/* array of buckets */
180 } fasttrap_hash_t;
181 
182 /*
183  * If at some future point these assembly functions become observable by
184  * DTrace, then these defines should become separate functions so that the
185  * fasttrap provider doesn't trigger probes during internal operations.
186  */
187 #define	fasttrap_copyout	copyout
188 #define	fasttrap_fuword32	fuword32
189 #define	fasttrap_suword32	suword32
190 #define	fasttrap_suword64	suword64
191 
192 #ifdef __amd64__
193 #define	fasttrap_fulword	fuword64
194 #define	fasttrap_sulword	suword64
195 #else
196 #define	fasttrap_fulword	fuword32
197 #define	fasttrap_sulword	suword32
198 #endif
199 
200 extern void fasttrap_sigtrap(proc_t *, kthread_t *, uintptr_t);
201 #ifndef illumos
202 extern fasttrap_scrspace_t *fasttrap_scraddr(struct thread *,
203     fasttrap_proc_t *);
204 #endif
205 
206 extern dtrace_id_t 		fasttrap_probe_id;
207 extern fasttrap_hash_t		fasttrap_tpoints;
208 
209 #ifndef illumos
210 extern struct rmlock		fasttrap_tp_lock;
211 #endif
212 
213 #define	FASTTRAP_TPOINTS_INDEX(pid, pc) \
214 	(((pc) / sizeof (fasttrap_instr_t) + (pid)) & fasttrap_tpoints.fth_mask)
215 
216 /*
217  * Must be implemented by fasttrap_isa.c
218  */
219 extern int fasttrap_tracepoint_init(proc_t *, fasttrap_tracepoint_t *,
220     uintptr_t, fasttrap_probe_type_t);
221 extern int fasttrap_tracepoint_install(proc_t *, fasttrap_tracepoint_t *);
222 extern int fasttrap_tracepoint_remove(proc_t *, fasttrap_tracepoint_t *);
223 
224 struct trapframe;
225 extern int fasttrap_pid_probe(struct trapframe *);
226 extern int fasttrap_return_probe(struct trapframe *);
227 
228 extern uint64_t fasttrap_pid_getarg(void *, dtrace_id_t, void *, int, int);
229 extern uint64_t fasttrap_usdt_getarg(void *, dtrace_id_t, void *, int, int);
230 
231 #ifdef	__cplusplus
232 }
233 #endif
234 
235 #endif	/* _FASTTRAP_IMPL_H */
236