xref: /titanic_44/usr/src/uts/common/sys/fasttrap_impl.h (revision 9df12a23948bd40cbe37ce88d84e272c3894e675)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 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/fasttrap.h>
36 #include <sys/fasttrap_isa.h>
37 
38 #ifdef	__cplusplus
39 extern "C" {
40 #endif
41 
42 /*
43  * Fasttrap Providers, Probes and Tracepoints
44  *
45  * Each Solaris process can have multiple providers -- the pid provider as
46  * well as any number of user-level statically defined tracing (USDT)
47  * providers. Those providers are each represented by a fasttrap_provider_t.
48  * All providers for a given process have a pointer to a shared
49  * fasttrap_proc_t. The fasttrap_proc_t has two states: active or defunct.
50  * It becomes defunct when the process performs an exit or an exec.
51  *
52  * Each probe is represented by a fasttrap_probe_t which has a pointer to
53  * its associated provider as well as a list of fasttrap_id_tp_t structures
54  * which are tuples combining a fasttrap_id_t and a fasttrap_tracepoint_t.
55  * A fasttrap_tracepoint_t represents the actual point of instrumentation
56  * and it contains two lists of fasttrap_id_t structures (to be fired pre-
57  * and post-instruction emulation) that identify the probes attached to the
58  * tracepoint. Tracepoints also have a pointer to the fasttrap_proc_t for the
59  * process they trace which is used when looking up a tracepoint both at
60  * probe fire time and when enabling and disabling probes.
61  *
62  * It's important to note that probes are preallocated with the necessary
63  * number of tracepoints, but that tracepoints can be shared by probes and
64  * swapped between probes. If a probe's preallocated tracepoint is enabled
65  * (and, therefore, the associated probe is enabled), and that probe is
66  * then disabled, ownership of that tracepoint may be exchanged for an
67  * unused tracepoint belonging to another probe that was attached to the
68  * enabled tracepoint.
69  */
70 
71 typedef struct fasttrap_proc {
72 	pid_t ftpc_pid;				/* process ID for this proc */
73 	uint_t ftpc_defunct;			/* denotes a lame duck proc */
74 	uint64_t ftpc_count;			/* reference count */
75 	kmutex_t ftpc_mtx;			/* proc lock */
76 	struct fasttrap_proc *ftpc_next;	/* next proc in hash chain */
77 } fasttrap_proc_t;
78 
79 typedef struct fasttrap_provider {
80 	pid_t ftp_pid;				/* process ID for this prov */
81 	char ftp_name[DTRACE_PROVNAMELEN];	/* prov name (w/o the pid) */
82 	dtrace_provider_id_t ftp_provid;	/* DTrace provider handle */
83 	uint_t ftp_marked;			/* mark for possible removal */
84 	uint_t ftp_retired;			/* mark when retired */
85 	kmutex_t ftp_mtx;			/* provider lock */
86 	uint64_t ftp_rcount;			/* enabled probes ref count */
87 	uint64_t ftp_ccount;			/* consumers creating probes */
88 	fasttrap_proc_t *ftp_proc;		/* shared proc for all provs */
89 	struct fasttrap_provider *ftp_next;	/* next prov in hash chain */
90 } fasttrap_provider_t;
91 
92 typedef struct fasttrap_id fasttrap_id_t;
93 typedef struct fasttrap_probe fasttrap_probe_t;
94 typedef struct fasttrap_tracepoint fasttrap_tracepoint_t;
95 
96 struct fasttrap_id {
97 	fasttrap_probe_t *fti_probe;		/* referrring probe */
98 	fasttrap_id_t *fti_next;		/* enabled probe list on tp */
99 };
100 
101 typedef struct fasttrap_id_tp {
102 	fasttrap_id_t fit_id;
103 	fasttrap_tracepoint_t *fit_tp;
104 } fasttrap_id_tp_t;
105 
106 struct fasttrap_probe {
107 	dtrace_id_t ftp_id;			/* DTrace probe identifier */
108 	pid_t ftp_pid;				/* pid for this probe */
109 	fasttrap_provider_t *ftp_prov;		/* this probe's provider */
110 	uintptr_t ftp_faddr;			/* associated function's addr */
111 	size_t ftp_fsize;			/* associated function's size */
112 	fasttrap_probe_type_t ftp_type;		/* type of probe */
113 	uint_t ftp_enabled;			/* is this probe enabled */
114 	uint64_t ftp_gen;			/* modification generation */
115 	uint64_t ftp_ntps;			/* number of tracepoints */
116 	uint8_t *ftp_argmap;			/* native to translated args */
117 	uint8_t ftp_nargs;			/* translated argument count */
118 	char *ftp_xtypes;			/* translated types index */
119 	char *ftp_ntypes;			/* native types index */
120 	fasttrap_id_tp_t ftp_tps[1];		/* flexible array */
121 };
122 
123 #define	FASTTRAP_ID_INDEX(id)	\
124 ((fasttrap_id_tp_t *)(((char *)(id) - offsetof(fasttrap_id_tp_t, fit_id))) - \
125 &(id)->fti_probe->ftp_tps[0])
126 
127 struct fasttrap_tracepoint {
128 	fasttrap_proc_t		*ftt_proc;	/* associated process struct */
129 	uintptr_t		ftt_pc;		/* address of tracepoint */
130 	pid_t			ftt_pid;	/* pid of tracepoint */
131 	fasttrap_machtp_t	ftt_mtp;	/* ISA-specific portion */
132 	fasttrap_id_t		*ftt_ids;	/* NULL-terminated list */
133 	fasttrap_id_t		*ftt_retids;	/* NULL-terminated list */
134 	fasttrap_tracepoint_t	*ftt_next;	/* link in global hash */
135 };
136 
137 typedef struct fasttrap_bucket {
138 	kmutex_t		ftb_mtx;
139 	void 			*ftb_data;
140 
141 	uint8_t		ftb_pad[64 - sizeof (kmutex_t) - sizeof (void *)];
142 } fasttrap_bucket_t;
143 
144 typedef struct fasttrap_hash {
145 	ulong_t			fth_nent;
146 	ulong_t			fth_mask;
147 	fasttrap_bucket_t	*fth_table;
148 } fasttrap_hash_t;
149 
150 /*
151  * If at some future point these assembly functions become observable by
152  * DTrace, then these defines should become separate functions so that the
153  * fasttrap provider doesn't trigger probes during internal operations.
154  */
155 #define	fasttrap_copyout	copyout
156 #define	fasttrap_fuword32	fuword32
157 #define	fasttrap_suword32	suword32
158 
159 #define	fasttrap_fulword	fulword
160 #define	fasttrap_sulword	sulword
161 
162 extern void fasttrap_sigtrap(proc_t *, kthread_t *, uintptr_t);
163 
164 extern dtrace_id_t 		fasttrap_probe_id;
165 extern fasttrap_hash_t		fasttrap_tpoints;
166 
167 #define	FASTTRAP_TPOINTS_INDEX(pid, pc) \
168 	(((pc) / sizeof (fasttrap_instr_t) + (pid)) & fasttrap_tpoints.fth_mask)
169 
170 /*
171  * Must be implemented by fasttrap_isa.c
172  */
173 extern int fasttrap_tracepoint_init(proc_t *, fasttrap_probe_t *,
174     fasttrap_tracepoint_t *, uintptr_t);
175 extern int fasttrap_tracepoint_install(proc_t *, fasttrap_tracepoint_t *);
176 extern int fasttrap_tracepoint_remove(proc_t *, fasttrap_tracepoint_t *);
177 
178 extern int fasttrap_probe(struct regs *);
179 extern int fasttrap_pid_probe(struct regs *);
180 extern int fasttrap_return_probe(struct regs *);
181 
182 extern uint64_t fasttrap_getarg(void *, dtrace_id_t, void *, int, int);
183 extern uint64_t fasttrap_usdt_getarg(void *, dtrace_id_t, void *, int, int);
184 
185 #ifdef	__cplusplus
186 }
187 #endif
188 
189 #endif	/* _FASTTRAP_IMPL_H */
190