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