xref: /xnu-10002.1.13/bsd/dev/dtrace/fasttrap.c (revision 1031c584a5e37aff177559b9f69dbd3c8c3fd30a)
1*1031c584SApple OSS Distributions /*
2*1031c584SApple OSS Distributions  * CDDL HEADER START
3*1031c584SApple OSS Distributions  *
4*1031c584SApple OSS Distributions  * The contents of this file are subject to the terms of the
5*1031c584SApple OSS Distributions  * Common Development and Distribution License (the "License").
6*1031c584SApple OSS Distributions  * You may not use this file except in compliance with the License.
7*1031c584SApple OSS Distributions  *
8*1031c584SApple OSS Distributions  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*1031c584SApple OSS Distributions  * or http://www.opensolaris.org/os/licensing.
10*1031c584SApple OSS Distributions  * See the License for the specific language governing permissions
11*1031c584SApple OSS Distributions  * and limitations under the License.
12*1031c584SApple OSS Distributions  *
13*1031c584SApple OSS Distributions  * When distributing Covered Code, include this CDDL HEADER in each
14*1031c584SApple OSS Distributions  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*1031c584SApple OSS Distributions  * If applicable, add the following below this CDDL HEADER, with the
16*1031c584SApple OSS Distributions  * fields enclosed by brackets "[]" replaced with your own identifying
17*1031c584SApple OSS Distributions  * information: Portions Copyright [yyyy] [name of copyright owner]
18*1031c584SApple OSS Distributions  *
19*1031c584SApple OSS Distributions  * CDDL HEADER END
20*1031c584SApple OSS Distributions  */
21*1031c584SApple OSS Distributions 
22*1031c584SApple OSS Distributions /*
23*1031c584SApple OSS Distributions  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24*1031c584SApple OSS Distributions  * Use is subject to license terms.
25*1031c584SApple OSS Distributions  */
26*1031c584SApple OSS Distributions 
27*1031c584SApple OSS Distributions #include <sys/types.h>
28*1031c584SApple OSS Distributions #include <sys/time.h>
29*1031c584SApple OSS Distributions 
30*1031c584SApple OSS Distributions #include <sys/codesign.h>
31*1031c584SApple OSS Distributions #include <sys/errno.h>
32*1031c584SApple OSS Distributions #include <sys/stat.h>
33*1031c584SApple OSS Distributions #include <sys/conf.h>
34*1031c584SApple OSS Distributions #include <sys/systm.h>
35*1031c584SApple OSS Distributions #include <sys/kauth.h>
36*1031c584SApple OSS Distributions #include <sys/utfconv.h>
37*1031c584SApple OSS Distributions 
38*1031c584SApple OSS Distributions #include <sys/fasttrap.h>
39*1031c584SApple OSS Distributions #include <sys/fasttrap_impl.h>
40*1031c584SApple OSS Distributions #include <sys/fasttrap_isa.h>
41*1031c584SApple OSS Distributions #include <sys/dtrace.h>
42*1031c584SApple OSS Distributions #include <sys/dtrace_impl.h>
43*1031c584SApple OSS Distributions #include <sys/proc.h>
44*1031c584SApple OSS Distributions 
45*1031c584SApple OSS Distributions #include <security/mac_framework.h>
46*1031c584SApple OSS Distributions 
47*1031c584SApple OSS Distributions #include <miscfs/devfs/devfs.h>
48*1031c584SApple OSS Distributions #include <sys/proc_internal.h>
49*1031c584SApple OSS Distributions #include <sys/dtrace_glue.h>
50*1031c584SApple OSS Distributions #include <sys/dtrace_ptss.h>
51*1031c584SApple OSS Distributions 
52*1031c584SApple OSS Distributions #include <kern/cs_blobs.h>
53*1031c584SApple OSS Distributions #include <kern/thread.h>
54*1031c584SApple OSS Distributions #include <kern/zalloc.h>
55*1031c584SApple OSS Distributions 
56*1031c584SApple OSS Distributions #include <mach/thread_act.h>
57*1031c584SApple OSS Distributions 
58*1031c584SApple OSS Distributions extern kern_return_t kernel_thread_start_priority(thread_continue_t continuation, void *parameter, integer_t priority, thread_t *new_thread);
59*1031c584SApple OSS Distributions 
60*1031c584SApple OSS Distributions /* Solaris proc_t is the struct. Darwin's proc_t is a pointer to it. */
61*1031c584SApple OSS Distributions #define proc_t struct proc /* Steer clear of the Darwin typedef for proc_t */
62*1031c584SApple OSS Distributions 
63*1031c584SApple OSS Distributions __private_extern__
64*1031c584SApple OSS Distributions void
65*1031c584SApple OSS Distributions qsort(void *a, size_t n, size_t es, int (*cmp)(const void *, const void *));
66*1031c584SApple OSS Distributions 
67*1031c584SApple OSS Distributions /*
68*1031c584SApple OSS Distributions  * User-Land Trap-Based Tracing
69*1031c584SApple OSS Distributions  * ----------------------------
70*1031c584SApple OSS Distributions  *
71*1031c584SApple OSS Distributions  * The fasttrap provider allows DTrace consumers to instrument any user-level
72*1031c584SApple OSS Distributions  * instruction to gather data; this includes probes with semantic
73*1031c584SApple OSS Distributions  * signifigance like entry and return as well as simple offsets into the
74*1031c584SApple OSS Distributions  * function. While the specific techniques used are very ISA specific, the
75*1031c584SApple OSS Distributions  * methodology is generalizable to any architecture.
76*1031c584SApple OSS Distributions  *
77*1031c584SApple OSS Distributions  *
78*1031c584SApple OSS Distributions  * The General Methodology
79*1031c584SApple OSS Distributions  * -----------------------
80*1031c584SApple OSS Distributions  *
81*1031c584SApple OSS Distributions  * With the primary goal of tracing every user-land instruction and the
82*1031c584SApple OSS Distributions  * limitation that we can't trust user space so don't want to rely on much
83*1031c584SApple OSS Distributions  * information there, we begin by replacing the instructions we want to trace
84*1031c584SApple OSS Distributions  * with trap instructions. Each instruction we overwrite is saved into a hash
85*1031c584SApple OSS Distributions  * table keyed by process ID and pc address. When we enter the kernel due to
86*1031c584SApple OSS Distributions  * this trap instruction, we need the effects of the replaced instruction to
87*1031c584SApple OSS Distributions  * appear to have occurred before we proceed with the user thread's
88*1031c584SApple OSS Distributions  * execution.
89*1031c584SApple OSS Distributions  *
90*1031c584SApple OSS Distributions  * Each user level thread is represented by a ulwp_t structure which is
91*1031c584SApple OSS Distributions  * always easily accessible through a register. The most basic way to produce
92*1031c584SApple OSS Distributions  * the effects of the instruction we replaced is to copy that instruction out
93*1031c584SApple OSS Distributions  * to a bit of scratch space reserved in the user thread's ulwp_t structure
94*1031c584SApple OSS Distributions  * (a sort of kernel-private thread local storage), set the PC to that
95*1031c584SApple OSS Distributions  * scratch space and single step. When we reenter the kernel after single
96*1031c584SApple OSS Distributions  * stepping the instruction we must then adjust the PC to point to what would
97*1031c584SApple OSS Distributions  * normally be the next instruction. Of course, special care must be taken
98*1031c584SApple OSS Distributions  * for branches and jumps, but these represent such a small fraction of any
99*1031c584SApple OSS Distributions  * instruction set that writing the code to emulate these in the kernel is
100*1031c584SApple OSS Distributions  * not too difficult.
101*1031c584SApple OSS Distributions  *
102*1031c584SApple OSS Distributions  * Return probes may require several tracepoints to trace every return site,
103*1031c584SApple OSS Distributions  * and, conversely, each tracepoint may activate several probes (the entry
104*1031c584SApple OSS Distributions  * and offset 0 probes, for example). To solve this muliplexing problem,
105*1031c584SApple OSS Distributions  * tracepoints contain lists of probes to activate and probes contain lists
106*1031c584SApple OSS Distributions  * of tracepoints to enable. If a probe is activated, it adds its ID to
107*1031c584SApple OSS Distributions  * existing tracepoints or creates new ones as necessary.
108*1031c584SApple OSS Distributions  *
109*1031c584SApple OSS Distributions  * Most probes are activated _before_ the instruction is executed, but return
110*1031c584SApple OSS Distributions  * probes are activated _after_ the effects of the last instruction of the
111*1031c584SApple OSS Distributions  * function are visible. Return probes must be fired _after_ we have
112*1031c584SApple OSS Distributions  * single-stepped the instruction whereas all other probes are fired
113*1031c584SApple OSS Distributions  * beforehand.
114*1031c584SApple OSS Distributions  *
115*1031c584SApple OSS Distributions  *
116*1031c584SApple OSS Distributions  * Lock Ordering
117*1031c584SApple OSS Distributions  * -------------
118*1031c584SApple OSS Distributions  *
119*1031c584SApple OSS Distributions  * The lock ordering below -- both internally and with respect to the DTrace
120*1031c584SApple OSS Distributions  * framework -- is a little tricky and bears some explanation. Each provider
121*1031c584SApple OSS Distributions  * has a lock (ftp_mtx) that protects its members including reference counts
122*1031c584SApple OSS Distributions  * for enabled probes (ftp_rcount), consumers actively creating probes
123*1031c584SApple OSS Distributions  * (ftp_ccount) and USDT consumers (ftp_mcount); all three prevent a provider
124*1031c584SApple OSS Distributions  * from being freed. A provider is looked up by taking the bucket lock for the
125*1031c584SApple OSS Distributions  * provider hash table, and is returned with its lock held. The provider lock
126*1031c584SApple OSS Distributions  * may be taken in functions invoked by the DTrace framework, but may not be
127*1031c584SApple OSS Distributions  * held while calling functions in the DTrace framework.
128*1031c584SApple OSS Distributions  *
129*1031c584SApple OSS Distributions  * To ensure consistency over multiple calls to the DTrace framework, the
130*1031c584SApple OSS Distributions  * creation lock (ftp_cmtx) should be held. Naturally, the creation lock may
131*1031c584SApple OSS Distributions  * not be taken when holding the provider lock as that would create a cyclic
132*1031c584SApple OSS Distributions  * lock ordering. In situations where one would naturally take the provider
133*1031c584SApple OSS Distributions  * lock and then the creation lock, we instead up a reference count to prevent
134*1031c584SApple OSS Distributions  * the provider from disappearing, drop the provider lock, and acquire the
135*1031c584SApple OSS Distributions  * creation lock.
136*1031c584SApple OSS Distributions  *
137*1031c584SApple OSS Distributions  * Briefly:
138*1031c584SApple OSS Distributions  * 	bucket lock before provider lock
139*1031c584SApple OSS Distributions  *	DTrace before provider lock
140*1031c584SApple OSS Distributions  *	creation lock before DTrace
141*1031c584SApple OSS Distributions  *	never hold the provider lock and creation lock simultaneously
142*1031c584SApple OSS Distributions  */
143*1031c584SApple OSS Distributions 
144*1031c584SApple OSS Distributions static dtrace_meta_provider_id_t fasttrap_meta_id;
145*1031c584SApple OSS Distributions 
146*1031c584SApple OSS Distributions static thread_t fasttrap_cleanup_thread;
147*1031c584SApple OSS Distributions 
148*1031c584SApple OSS Distributions static LCK_GRP_DECLARE(fasttrap_lck_grp, "fasttrap");
149*1031c584SApple OSS Distributions static LCK_ATTR_DECLARE(fasttrap_lck_attr, 0, 0);
150*1031c584SApple OSS Distributions static LCK_MTX_DECLARE_ATTR(fasttrap_cleanup_mtx,
151*1031c584SApple OSS Distributions     &fasttrap_lck_grp, &fasttrap_lck_attr);
152*1031c584SApple OSS Distributions 
153*1031c584SApple OSS Distributions 
154*1031c584SApple OSS Distributions #define FASTTRAP_CLEANUP_PROVIDER 0x1
155*1031c584SApple OSS Distributions #define FASTTRAP_CLEANUP_TRACEPOINT 0x2
156*1031c584SApple OSS Distributions 
157*1031c584SApple OSS Distributions static uint32_t fasttrap_cleanup_work = 0;
158*1031c584SApple OSS Distributions 
159*1031c584SApple OSS Distributions /*
160*1031c584SApple OSS Distributions  * Generation count on modifications to the global tracepoint lookup table.
161*1031c584SApple OSS Distributions  */
162*1031c584SApple OSS Distributions static volatile uint64_t fasttrap_mod_gen;
163*1031c584SApple OSS Distributions 
164*1031c584SApple OSS Distributions /*
165*1031c584SApple OSS Distributions  * APPLE NOTE: When the fasttrap provider is loaded, fasttrap_max is computed
166*1031c584SApple OSS Distributions  * base on system memory.  Each time a probe is created, fasttrap_total is
167*1031c584SApple OSS Distributions  * incremented by the number of tracepoints that may be associated with that
168*1031c584SApple OSS Distributions  * probe; fasttrap_total is capped at fasttrap_max.
169*1031c584SApple OSS Distributions  */
170*1031c584SApple OSS Distributions 
171*1031c584SApple OSS Distributions static uint32_t fasttrap_max;
172*1031c584SApple OSS Distributions static uint32_t fasttrap_retired;
173*1031c584SApple OSS Distributions static uint32_t fasttrap_total;
174*1031c584SApple OSS Distributions 
175*1031c584SApple OSS Distributions 
176*1031c584SApple OSS Distributions #define	FASTTRAP_TPOINTS_DEFAULT_SIZE	0x4000
177*1031c584SApple OSS Distributions #define	FASTTRAP_PROVIDERS_DEFAULT_SIZE	0x100
178*1031c584SApple OSS Distributions #define	FASTTRAP_PROCS_DEFAULT_SIZE	0x100
179*1031c584SApple OSS Distributions 
180*1031c584SApple OSS Distributions fasttrap_hash_t			fasttrap_tpoints;
181*1031c584SApple OSS Distributions static fasttrap_hash_t		fasttrap_provs;
182*1031c584SApple OSS Distributions static fasttrap_hash_t		fasttrap_procs;
183*1031c584SApple OSS Distributions 
184*1031c584SApple OSS Distributions static uint64_t			fasttrap_pid_count;	/* pid ref count */
185*1031c584SApple OSS Distributions static LCK_MTX_DECLARE_ATTR(fasttrap_count_mtx,	/* lock on ref count */
186*1031c584SApple OSS Distributions     &fasttrap_lck_grp, &fasttrap_lck_attr);
187*1031c584SApple OSS Distributions 
188*1031c584SApple OSS Distributions #define	FASTTRAP_ENABLE_FAIL	1
189*1031c584SApple OSS Distributions #define	FASTTRAP_ENABLE_PARTIAL	2
190*1031c584SApple OSS Distributions 
191*1031c584SApple OSS Distributions static int fasttrap_tracepoint_enable(proc_t *, fasttrap_probe_t *, uint_t);
192*1031c584SApple OSS Distributions static void fasttrap_tracepoint_disable(proc_t *, fasttrap_probe_t *, uint_t);
193*1031c584SApple OSS Distributions 
194*1031c584SApple OSS Distributions static fasttrap_provider_t *fasttrap_provider_lookup(proc_t*, fasttrap_provider_type_t, const char *,
195*1031c584SApple OSS Distributions     const dtrace_pattr_t *);
196*1031c584SApple OSS Distributions static void fasttrap_provider_retire(proc_t*, const char *, int);
197*1031c584SApple OSS Distributions static void fasttrap_provider_free(fasttrap_provider_t *);
198*1031c584SApple OSS Distributions 
199*1031c584SApple OSS Distributions static fasttrap_proc_t *fasttrap_proc_lookup(pid_t);
200*1031c584SApple OSS Distributions static void fasttrap_proc_release(fasttrap_proc_t *);
201*1031c584SApple OSS Distributions 
202*1031c584SApple OSS Distributions #define	FASTTRAP_PROVS_INDEX(pid, name) \
203*1031c584SApple OSS Distributions 	((fasttrap_hash_str(name) + (pid)) & fasttrap_provs.fth_mask)
204*1031c584SApple OSS Distributions 
205*1031c584SApple OSS Distributions #define	FASTTRAP_PROCS_INDEX(pid) ((pid) & fasttrap_procs.fth_mask)
206*1031c584SApple OSS Distributions 
207*1031c584SApple OSS Distributions /*
208*1031c584SApple OSS Distributions  * APPLE NOTE: To save memory, some common memory allocations are given
209*1031c584SApple OSS Distributions  * a unique zone. For example, dtrace_probe_t is 72 bytes in size,
210*1031c584SApple OSS Distributions  * which means it would fall into the kalloc.128 bucket. With
211*1031c584SApple OSS Distributions  * 20k elements allocated, the space saved is substantial.
212*1031c584SApple OSS Distributions  */
213*1031c584SApple OSS Distributions 
214*1031c584SApple OSS Distributions ZONE_DEFINE(fasttrap_tracepoint_t_zone, "dtrace.fasttrap_tracepoint_t",
215*1031c584SApple OSS Distributions     sizeof(fasttrap_tracepoint_t), ZC_NONE);
216*1031c584SApple OSS Distributions 
217*1031c584SApple OSS Distributions /*
218*1031c584SApple OSS Distributions  * APPLE NOTE: fasttrap_probe_t's are variable in size. Some quick profiling has shown
219*1031c584SApple OSS Distributions  * that the sweet spot for reducing memory footprint is covering the first
220*1031c584SApple OSS Distributions  * three sizes. Everything larger goes into the common pool.
221*1031c584SApple OSS Distributions  */
222*1031c584SApple OSS Distributions #define FASTTRAP_PROBE_T_ZONE_MAX_TRACEPOINTS 4
223*1031c584SApple OSS Distributions 
224*1031c584SApple OSS Distributions struct zone *fasttrap_probe_t_zones[FASTTRAP_PROBE_T_ZONE_MAX_TRACEPOINTS];
225*1031c584SApple OSS Distributions 
226*1031c584SApple OSS Distributions static const char *fasttrap_probe_t_zone_names[FASTTRAP_PROBE_T_ZONE_MAX_TRACEPOINTS] = {
227*1031c584SApple OSS Distributions 	"",
228*1031c584SApple OSS Distributions 	"dtrace.fasttrap_probe_t[1]",
229*1031c584SApple OSS Distributions 	"dtrace.fasttrap_probe_t[2]",
230*1031c584SApple OSS Distributions 	"dtrace.fasttrap_probe_t[3]"
231*1031c584SApple OSS Distributions };
232*1031c584SApple OSS Distributions 
233*1031c584SApple OSS Distributions static int
fasttrap_highbit(ulong_t i)234*1031c584SApple OSS Distributions fasttrap_highbit(ulong_t i)
235*1031c584SApple OSS Distributions {
236*1031c584SApple OSS Distributions 	int h = 1;
237*1031c584SApple OSS Distributions 
238*1031c584SApple OSS Distributions 	if (i == 0)
239*1031c584SApple OSS Distributions 		return (0);
240*1031c584SApple OSS Distributions #ifdef _LP64
241*1031c584SApple OSS Distributions 	if (i & 0xffffffff00000000ul) {
242*1031c584SApple OSS Distributions 		h += 32; i >>= 32;
243*1031c584SApple OSS Distributions 	}
244*1031c584SApple OSS Distributions #endif
245*1031c584SApple OSS Distributions 	if (i & 0xffff0000) {
246*1031c584SApple OSS Distributions 		h += 16; i >>= 16;
247*1031c584SApple OSS Distributions 	}
248*1031c584SApple OSS Distributions 	if (i & 0xff00) {
249*1031c584SApple OSS Distributions 		h += 8; i >>= 8;
250*1031c584SApple OSS Distributions 	}
251*1031c584SApple OSS Distributions 	if (i & 0xf0) {
252*1031c584SApple OSS Distributions 		h += 4; i >>= 4;
253*1031c584SApple OSS Distributions 	}
254*1031c584SApple OSS Distributions 	if (i & 0xc) {
255*1031c584SApple OSS Distributions 		h += 2; i >>= 2;
256*1031c584SApple OSS Distributions 	}
257*1031c584SApple OSS Distributions 	if (i & 0x2) {
258*1031c584SApple OSS Distributions 		h += 1;
259*1031c584SApple OSS Distributions 	}
260*1031c584SApple OSS Distributions 	return (h);
261*1031c584SApple OSS Distributions }
262*1031c584SApple OSS Distributions 
263*1031c584SApple OSS Distributions static uint_t
fasttrap_hash_str(const char * p)264*1031c584SApple OSS Distributions fasttrap_hash_str(const char *p)
265*1031c584SApple OSS Distributions {
266*1031c584SApple OSS Distributions 	unsigned int g;
267*1031c584SApple OSS Distributions 	uint_t hval = 0;
268*1031c584SApple OSS Distributions 
269*1031c584SApple OSS Distributions 	while (*p) {
270*1031c584SApple OSS Distributions 		hval = (hval << 4) + *p++;
271*1031c584SApple OSS Distributions 		if ((g = (hval & 0xf0000000)) != 0)
272*1031c584SApple OSS Distributions 			hval ^= g >> 24;
273*1031c584SApple OSS Distributions 		hval &= ~g;
274*1031c584SApple OSS Distributions 	}
275*1031c584SApple OSS Distributions 	return (hval);
276*1031c584SApple OSS Distributions }
277*1031c584SApple OSS Distributions 
278*1031c584SApple OSS Distributions /*
279*1031c584SApple OSS Distributions  * APPLE NOTE: fasttrap_sigtrap not implemented
280*1031c584SApple OSS Distributions  */
281*1031c584SApple OSS Distributions void
fasttrap_sigtrap(proc_t * p,uthread_t t,user_addr_t pc)282*1031c584SApple OSS Distributions fasttrap_sigtrap(proc_t *p, uthread_t t, user_addr_t pc)
283*1031c584SApple OSS Distributions {
284*1031c584SApple OSS Distributions #pragma unused(p, t, pc)
285*1031c584SApple OSS Distributions 
286*1031c584SApple OSS Distributions #if !defined(__APPLE__)
287*1031c584SApple OSS Distributions 	sigqueue_t *sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
288*1031c584SApple OSS Distributions 
289*1031c584SApple OSS Distributions 	sqp->sq_info.si_signo = SIGTRAP;
290*1031c584SApple OSS Distributions 	sqp->sq_info.si_code = TRAP_DTRACE;
291*1031c584SApple OSS Distributions 	sqp->sq_info.si_addr = (caddr_t)pc;
292*1031c584SApple OSS Distributions 
293*1031c584SApple OSS Distributions 	mutex_enter(&p->p_lock);
294*1031c584SApple OSS Distributions 	sigaddqa(p, t, sqp);
295*1031c584SApple OSS Distributions 	mutex_exit(&p->p_lock);
296*1031c584SApple OSS Distributions 
297*1031c584SApple OSS Distributions 	if (t != NULL)
298*1031c584SApple OSS Distributions 		aston(t);
299*1031c584SApple OSS Distributions #endif /* __APPLE__ */
300*1031c584SApple OSS Distributions 
301*1031c584SApple OSS Distributions 	printf("fasttrap_sigtrap called with no implementation.\n");
302*1031c584SApple OSS Distributions }
303*1031c584SApple OSS Distributions 
304*1031c584SApple OSS Distributions /*
305*1031c584SApple OSS Distributions  * This function ensures that no threads are actively using the memory
306*1031c584SApple OSS Distributions  * associated with probes that were formerly live.
307*1031c584SApple OSS Distributions  */
308*1031c584SApple OSS Distributions static void
fasttrap_mod_barrier(uint64_t gen)309*1031c584SApple OSS Distributions fasttrap_mod_barrier(uint64_t gen)
310*1031c584SApple OSS Distributions {
311*1031c584SApple OSS Distributions 	unsigned int i;
312*1031c584SApple OSS Distributions 
313*1031c584SApple OSS Distributions 	if (gen < fasttrap_mod_gen)
314*1031c584SApple OSS Distributions 		return;
315*1031c584SApple OSS Distributions 
316*1031c584SApple OSS Distributions 	fasttrap_mod_gen++;
317*1031c584SApple OSS Distributions 
318*1031c584SApple OSS Distributions 	for (i = 0; i < NCPU; i++) {
319*1031c584SApple OSS Distributions 		lck_mtx_lock(&cpu_core[i].cpuc_pid_lock);
320*1031c584SApple OSS Distributions 		lck_mtx_unlock(&cpu_core[i].cpuc_pid_lock);
321*1031c584SApple OSS Distributions 	}
322*1031c584SApple OSS Distributions }
323*1031c584SApple OSS Distributions 
324*1031c584SApple OSS Distributions static void fasttrap_pid_cleanup(uint32_t);
325*1031c584SApple OSS Distributions 
326*1031c584SApple OSS Distributions static unsigned int
fasttrap_pid_cleanup_providers(void)327*1031c584SApple OSS Distributions fasttrap_pid_cleanup_providers(void)
328*1031c584SApple OSS Distributions {
329*1031c584SApple OSS Distributions 	fasttrap_provider_t **fpp, *fp;
330*1031c584SApple OSS Distributions 	fasttrap_bucket_t *bucket;
331*1031c584SApple OSS Distributions 	dtrace_provider_id_t provid;
332*1031c584SApple OSS Distributions 	unsigned int later = 0, i;
333*1031c584SApple OSS Distributions 
334*1031c584SApple OSS Distributions 	/*
335*1031c584SApple OSS Distributions 	 * Iterate over all the providers trying to remove the marked
336*1031c584SApple OSS Distributions 	 * ones. If a provider is marked but not retired, we just
337*1031c584SApple OSS Distributions 	 * have to take a crack at removing it -- it's no big deal if
338*1031c584SApple OSS Distributions 	 * we can't.
339*1031c584SApple OSS Distributions 	 */
340*1031c584SApple OSS Distributions 	for (i = 0; i < fasttrap_provs.fth_nent; i++) {
341*1031c584SApple OSS Distributions 		bucket = &fasttrap_provs.fth_table[i];
342*1031c584SApple OSS Distributions 		lck_mtx_lock(&bucket->ftb_mtx);
343*1031c584SApple OSS Distributions 		fpp = (fasttrap_provider_t **)&bucket->ftb_data;
344*1031c584SApple OSS Distributions 
345*1031c584SApple OSS Distributions 		while ((fp = *fpp) != NULL) {
346*1031c584SApple OSS Distributions 			if (!fp->ftp_marked) {
347*1031c584SApple OSS Distributions 				fpp = &fp->ftp_next;
348*1031c584SApple OSS Distributions 				continue;
349*1031c584SApple OSS Distributions 			}
350*1031c584SApple OSS Distributions 
351*1031c584SApple OSS Distributions 			lck_mtx_lock(&fp->ftp_mtx);
352*1031c584SApple OSS Distributions 
353*1031c584SApple OSS Distributions 			/*
354*1031c584SApple OSS Distributions 			 * If this provider has consumers actively
355*1031c584SApple OSS Distributions 			 * creating probes (ftp_ccount) or is a USDT
356*1031c584SApple OSS Distributions 			 * provider (ftp_mcount), we can't unregister
357*1031c584SApple OSS Distributions 			 * or even condense.
358*1031c584SApple OSS Distributions 			 */
359*1031c584SApple OSS Distributions 			if (fp->ftp_ccount != 0 ||
360*1031c584SApple OSS Distributions 			    fp->ftp_mcount != 0) {
361*1031c584SApple OSS Distributions 				fp->ftp_marked = 0;
362*1031c584SApple OSS Distributions 				lck_mtx_unlock(&fp->ftp_mtx);
363*1031c584SApple OSS Distributions 				continue;
364*1031c584SApple OSS Distributions 			}
365*1031c584SApple OSS Distributions 
366*1031c584SApple OSS Distributions 			if (!fp->ftp_retired || fp->ftp_rcount != 0)
367*1031c584SApple OSS Distributions 				fp->ftp_marked = 0;
368*1031c584SApple OSS Distributions 
369*1031c584SApple OSS Distributions 			lck_mtx_unlock(&fp->ftp_mtx);
370*1031c584SApple OSS Distributions 
371*1031c584SApple OSS Distributions 			/*
372*1031c584SApple OSS Distributions 			 * If we successfully unregister this
373*1031c584SApple OSS Distributions 			 * provider we can remove it from the hash
374*1031c584SApple OSS Distributions 			 * chain and free the memory. If our attempt
375*1031c584SApple OSS Distributions 			 * to unregister fails and this is a retired
376*1031c584SApple OSS Distributions 			 * provider, increment our flag to try again
377*1031c584SApple OSS Distributions 			 * pretty soon. If we've consumed more than
378*1031c584SApple OSS Distributions 			 * half of our total permitted number of
379*1031c584SApple OSS Distributions 			 * probes call dtrace_condense() to try to
380*1031c584SApple OSS Distributions 			 * clean out the unenabled probes.
381*1031c584SApple OSS Distributions 			 */
382*1031c584SApple OSS Distributions 			provid = fp->ftp_provid;
383*1031c584SApple OSS Distributions 			if (dtrace_unregister(provid) != 0) {
384*1031c584SApple OSS Distributions 				if (fasttrap_total > fasttrap_max / 2)
385*1031c584SApple OSS Distributions 					(void) dtrace_condense(provid);
386*1031c584SApple OSS Distributions 				later += fp->ftp_marked;
387*1031c584SApple OSS Distributions 				fpp = &fp->ftp_next;
388*1031c584SApple OSS Distributions 			} else {
389*1031c584SApple OSS Distributions 				*fpp = fp->ftp_next;
390*1031c584SApple OSS Distributions 				fasttrap_provider_free(fp);
391*1031c584SApple OSS Distributions 			}
392*1031c584SApple OSS Distributions 		}
393*1031c584SApple OSS Distributions 		lck_mtx_unlock(&bucket->ftb_mtx);
394*1031c584SApple OSS Distributions 	}
395*1031c584SApple OSS Distributions 
396*1031c584SApple OSS Distributions 	return later;
397*1031c584SApple OSS Distributions }
398*1031c584SApple OSS Distributions 
399*1031c584SApple OSS Distributions typedef struct fasttrap_tracepoint_spec {
400*1031c584SApple OSS Distributions 	pid_t fttps_pid;
401*1031c584SApple OSS Distributions 	user_addr_t fttps_pc;
402*1031c584SApple OSS Distributions } fasttrap_tracepoint_spec_t;
403*1031c584SApple OSS Distributions 
404*1031c584SApple OSS Distributions static fasttrap_tracepoint_spec_t *fasttrap_retired_spec;
405*1031c584SApple OSS Distributions static size_t fasttrap_cur_retired = 0, fasttrap_retired_size;
406*1031c584SApple OSS Distributions static LCK_MTX_DECLARE_ATTR(fasttrap_retired_mtx,
407*1031c584SApple OSS Distributions     &fasttrap_lck_grp, &fasttrap_lck_attr);
408*1031c584SApple OSS Distributions 
409*1031c584SApple OSS Distributions #define DEFAULT_RETIRED_SIZE 256
410*1031c584SApple OSS Distributions 
411*1031c584SApple OSS Distributions static void
fasttrap_tracepoint_cleanup(void)412*1031c584SApple OSS Distributions fasttrap_tracepoint_cleanup(void)
413*1031c584SApple OSS Distributions {
414*1031c584SApple OSS Distributions 	size_t i;
415*1031c584SApple OSS Distributions 	pid_t pid = 0;
416*1031c584SApple OSS Distributions 	user_addr_t pc;
417*1031c584SApple OSS Distributions 	proc_t *p = PROC_NULL;
418*1031c584SApple OSS Distributions 	fasttrap_tracepoint_t *tp = NULL;
419*1031c584SApple OSS Distributions 	lck_mtx_lock(&fasttrap_retired_mtx);
420*1031c584SApple OSS Distributions 	fasttrap_bucket_t *bucket;
421*1031c584SApple OSS Distributions 	for (i = 0; i < fasttrap_cur_retired; i++) {
422*1031c584SApple OSS Distributions 		pc = fasttrap_retired_spec[i].fttps_pc;
423*1031c584SApple OSS Distributions 		if (fasttrap_retired_spec[i].fttps_pid != pid) {
424*1031c584SApple OSS Distributions 			pid = fasttrap_retired_spec[i].fttps_pid;
425*1031c584SApple OSS Distributions 			if (p != PROC_NULL) {
426*1031c584SApple OSS Distributions 				sprunlock(p);
427*1031c584SApple OSS Distributions 			}
428*1031c584SApple OSS Distributions 			if ((p = sprlock(pid)) == PROC_NULL) {
429*1031c584SApple OSS Distributions 				pid = 0;
430*1031c584SApple OSS Distributions 				continue;
431*1031c584SApple OSS Distributions 			}
432*1031c584SApple OSS Distributions 		}
433*1031c584SApple OSS Distributions 		bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
434*1031c584SApple OSS Distributions 		lck_mtx_lock(&bucket->ftb_mtx);
435*1031c584SApple OSS Distributions 		for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
436*1031c584SApple OSS Distributions 			if (pid == tp->ftt_pid && pc == tp->ftt_pc &&
437*1031c584SApple OSS Distributions 			tp->ftt_proc->ftpc_acount != 0)
438*1031c584SApple OSS Distributions 				break;
439*1031c584SApple OSS Distributions 		}
440*1031c584SApple OSS Distributions 		/*
441*1031c584SApple OSS Distributions 		 * Check that the tracepoint is not gone or has not been
442*1031c584SApple OSS Distributions 		 * re-activated for another probe
443*1031c584SApple OSS Distributions 		 */
444*1031c584SApple OSS Distributions 		if (tp == NULL || tp->ftt_retired == 0) {
445*1031c584SApple OSS Distributions 			lck_mtx_unlock(&bucket->ftb_mtx);
446*1031c584SApple OSS Distributions 			continue;
447*1031c584SApple OSS Distributions 		}
448*1031c584SApple OSS Distributions 		fasttrap_tracepoint_remove(p, tp);
449*1031c584SApple OSS Distributions 		lck_mtx_unlock(&bucket->ftb_mtx);
450*1031c584SApple OSS Distributions 	}
451*1031c584SApple OSS Distributions 	if (p != PROC_NULL) {
452*1031c584SApple OSS Distributions 		sprunlock(p);
453*1031c584SApple OSS Distributions 	}
454*1031c584SApple OSS Distributions 
455*1031c584SApple OSS Distributions 	fasttrap_cur_retired = 0;
456*1031c584SApple OSS Distributions 
457*1031c584SApple OSS Distributions 	lck_mtx_unlock(&fasttrap_retired_mtx);
458*1031c584SApple OSS Distributions }
459*1031c584SApple OSS Distributions 
460*1031c584SApple OSS Distributions void
fasttrap_tracepoint_retire(proc_t * p,fasttrap_tracepoint_t * tp)461*1031c584SApple OSS Distributions fasttrap_tracepoint_retire(proc_t *p, fasttrap_tracepoint_t *tp)
462*1031c584SApple OSS Distributions {
463*1031c584SApple OSS Distributions 	if (tp->ftt_retired)
464*1031c584SApple OSS Distributions 		return;
465*1031c584SApple OSS Distributions 	lck_mtx_lock(&fasttrap_retired_mtx);
466*1031c584SApple OSS Distributions 	fasttrap_tracepoint_spec_t *s = &fasttrap_retired_spec[fasttrap_cur_retired++];
467*1031c584SApple OSS Distributions 	s->fttps_pid = proc_getpid(p);
468*1031c584SApple OSS Distributions 	s->fttps_pc = tp->ftt_pc;
469*1031c584SApple OSS Distributions 
470*1031c584SApple OSS Distributions 	if (fasttrap_cur_retired == fasttrap_retired_size) {
471*1031c584SApple OSS Distributions 		fasttrap_tracepoint_spec_t *new_retired = kmem_zalloc(
472*1031c584SApple OSS Distributions 					fasttrap_retired_size * 2 *
473*1031c584SApple OSS Distributions 					sizeof(*fasttrap_retired_spec),
474*1031c584SApple OSS Distributions 					KM_SLEEP);
475*1031c584SApple OSS Distributions 		memcpy(new_retired, fasttrap_retired_spec, sizeof(*fasttrap_retired_spec) * fasttrap_retired_size);
476*1031c584SApple OSS Distributions 		kmem_free(fasttrap_retired_spec, sizeof(*fasttrap_retired_spec) * fasttrap_retired_size);
477*1031c584SApple OSS Distributions 		fasttrap_retired_size *= 2;
478*1031c584SApple OSS Distributions 		fasttrap_retired_spec = new_retired;
479*1031c584SApple OSS Distributions 	}
480*1031c584SApple OSS Distributions 
481*1031c584SApple OSS Distributions 	lck_mtx_unlock(&fasttrap_retired_mtx);
482*1031c584SApple OSS Distributions 
483*1031c584SApple OSS Distributions 	tp->ftt_retired = 1;
484*1031c584SApple OSS Distributions 
485*1031c584SApple OSS Distributions 	fasttrap_pid_cleanup(FASTTRAP_CLEANUP_TRACEPOINT);
486*1031c584SApple OSS Distributions }
487*1031c584SApple OSS Distributions 
488*1031c584SApple OSS Distributions static void
fasttrap_pid_cleanup_compute_priority(void)489*1031c584SApple OSS Distributions fasttrap_pid_cleanup_compute_priority(void)
490*1031c584SApple OSS Distributions {
491*1031c584SApple OSS Distributions 	if (fasttrap_total > (fasttrap_max / 100 * 90) || fasttrap_retired > fasttrap_max / 2) {
492*1031c584SApple OSS Distributions 		thread_precedence_policy_data_t precedence = {12 /* BASEPRI_PREEMPT_HIGH */};
493*1031c584SApple OSS Distributions 		thread_policy_set(fasttrap_cleanup_thread, THREAD_PRECEDENCE_POLICY, (thread_policy_t) &precedence, THREAD_PRECEDENCE_POLICY_COUNT);
494*1031c584SApple OSS Distributions 	}
495*1031c584SApple OSS Distributions 	else {
496*1031c584SApple OSS Distributions 		thread_precedence_policy_data_t precedence = {-39 /* BASEPRI_USER_INITIATED */};
497*1031c584SApple OSS Distributions 		thread_policy_set(fasttrap_cleanup_thread, THREAD_PRECEDENCE_POLICY, (thread_policy_t) &precedence, THREAD_PRECEDENCE_POLICY_COUNT);
498*1031c584SApple OSS Distributions 
499*1031c584SApple OSS Distributions 	}
500*1031c584SApple OSS Distributions }
501*1031c584SApple OSS Distributions 
502*1031c584SApple OSS Distributions /*
503*1031c584SApple OSS Distributions  * This is the timeout's callback for cleaning up the providers and their
504*1031c584SApple OSS Distributions  * probes.
505*1031c584SApple OSS Distributions  */
506*1031c584SApple OSS Distributions /*ARGSUSED*/
507*1031c584SApple OSS Distributions __attribute__((noreturn))
508*1031c584SApple OSS Distributions static void
fasttrap_pid_cleanup_cb(void)509*1031c584SApple OSS Distributions fasttrap_pid_cleanup_cb(void)
510*1031c584SApple OSS Distributions {
511*1031c584SApple OSS Distributions 	uint32_t work = 0;
512*1031c584SApple OSS Distributions 	lck_mtx_lock(&fasttrap_cleanup_mtx);
513*1031c584SApple OSS Distributions 	msleep(&fasttrap_pid_cleanup_cb, &fasttrap_cleanup_mtx, PRIBIO, "fasttrap_pid_cleanup_cb", NULL);
514*1031c584SApple OSS Distributions 	while (1) {
515*1031c584SApple OSS Distributions 		unsigned int later = 0;
516*1031c584SApple OSS Distributions 
517*1031c584SApple OSS Distributions 		work = os_atomic_xchg(&fasttrap_cleanup_work, 0, relaxed);
518*1031c584SApple OSS Distributions 		lck_mtx_unlock(&fasttrap_cleanup_mtx);
519*1031c584SApple OSS Distributions 		if (work & FASTTRAP_CLEANUP_PROVIDER) {
520*1031c584SApple OSS Distributions 			later = fasttrap_pid_cleanup_providers();
521*1031c584SApple OSS Distributions 		}
522*1031c584SApple OSS Distributions 		if (work & FASTTRAP_CLEANUP_TRACEPOINT) {
523*1031c584SApple OSS Distributions 			fasttrap_tracepoint_cleanup();
524*1031c584SApple OSS Distributions 		}
525*1031c584SApple OSS Distributions 		lck_mtx_lock(&fasttrap_cleanup_mtx);
526*1031c584SApple OSS Distributions 
527*1031c584SApple OSS Distributions 		fasttrap_pid_cleanup_compute_priority();
528*1031c584SApple OSS Distributions 		if (!fasttrap_cleanup_work) {
529*1031c584SApple OSS Distributions 			/*
530*1031c584SApple OSS Distributions 			 * If we were unable to remove a retired provider, try again after
531*1031c584SApple OSS Distributions 			 * a second. This situation can occur in certain circumstances where
532*1031c584SApple OSS Distributions 			 * providers cannot be unregistered even though they have no probes
533*1031c584SApple OSS Distributions 			 * enabled because of an execution of dtrace -l or something similar.
534*1031c584SApple OSS Distributions 			 * If the timeout has been disabled (set to 1 because we're trying
535*1031c584SApple OSS Distributions 			 * to detach), we set fasttrap_cleanup_work to ensure that we'll
536*1031c584SApple OSS Distributions 			 * get a chance to do that work if and when the timeout is reenabled
537*1031c584SApple OSS Distributions 			 * (if detach fails).
538*1031c584SApple OSS Distributions 			 */
539*1031c584SApple OSS Distributions 			if (later > 0) {
540*1031c584SApple OSS Distributions 				struct timespec t = {.tv_sec = 1, .tv_nsec = 0};
541*1031c584SApple OSS Distributions 				msleep(&fasttrap_pid_cleanup_cb, &fasttrap_cleanup_mtx, PRIBIO, "fasttrap_pid_cleanup_cb", &t);
542*1031c584SApple OSS Distributions 			}
543*1031c584SApple OSS Distributions 			else
544*1031c584SApple OSS Distributions 				msleep(&fasttrap_pid_cleanup_cb, &fasttrap_cleanup_mtx, PRIBIO, "fasttrap_pid_cleanup_cb", NULL);
545*1031c584SApple OSS Distributions 		}
546*1031c584SApple OSS Distributions 	}
547*1031c584SApple OSS Distributions 
548*1031c584SApple OSS Distributions }
549*1031c584SApple OSS Distributions 
550*1031c584SApple OSS Distributions /*
551*1031c584SApple OSS Distributions  * Activates the asynchronous cleanup mechanism.
552*1031c584SApple OSS Distributions  */
553*1031c584SApple OSS Distributions static void
fasttrap_pid_cleanup(uint32_t work)554*1031c584SApple OSS Distributions fasttrap_pid_cleanup(uint32_t work)
555*1031c584SApple OSS Distributions {
556*1031c584SApple OSS Distributions 	lck_mtx_lock(&fasttrap_cleanup_mtx);
557*1031c584SApple OSS Distributions 	os_atomic_or(&fasttrap_cleanup_work, work, relaxed);
558*1031c584SApple OSS Distributions 	fasttrap_pid_cleanup_compute_priority();
559*1031c584SApple OSS Distributions 	wakeup(&fasttrap_pid_cleanup_cb);
560*1031c584SApple OSS Distributions 	lck_mtx_unlock(&fasttrap_cleanup_mtx);
561*1031c584SApple OSS Distributions }
562*1031c584SApple OSS Distributions 
563*1031c584SApple OSS Distributions static int
fasttrap_setdebug(proc_t * p)564*1031c584SApple OSS Distributions fasttrap_setdebug(proc_t *p)
565*1031c584SApple OSS Distributions {
566*1031c584SApple OSS Distributions 	LCK_MTX_ASSERT(&p->p_mlock, LCK_MTX_ASSERT_OWNED);
567*1031c584SApple OSS Distributions 
568*1031c584SApple OSS Distributions 	/*
569*1031c584SApple OSS Distributions 	 * CS_KILL and CS_HARD will cause code-signing to kill the process
570*1031c584SApple OSS Distributions 	 * when the process text is modified, so register the intent
571*1031c584SApple OSS Distributions 	 * to allow invalid access beforehand.
572*1031c584SApple OSS Distributions 	 */
573*1031c584SApple OSS Distributions 	if ((proc_getcsflags(p) & (CS_KILL|CS_HARD))) {
574*1031c584SApple OSS Distributions 		proc_unlock(p);
575*1031c584SApple OSS Distributions 		for (int i = 0; i < DTRACE_NCLIENTS; i++) {
576*1031c584SApple OSS Distributions 			dtrace_state_t *state = dtrace_state_get(i);
577*1031c584SApple OSS Distributions 			if (state == NULL)
578*1031c584SApple OSS Distributions 				continue;
579*1031c584SApple OSS Distributions 			if (state->dts_cred.dcr_cred == NULL)
580*1031c584SApple OSS Distributions 				continue;
581*1031c584SApple OSS Distributions 			/*
582*1031c584SApple OSS Distributions 			 * The get_task call flags whether the process should
583*1031c584SApple OSS Distributions 			 * be flagged to have the cs_allow_invalid call
584*1031c584SApple OSS Distributions 			 * succeed. We want the best credential that any dtrace
585*1031c584SApple OSS Distributions 			 * client has, so try all of them.
586*1031c584SApple OSS Distributions 			 */
587*1031c584SApple OSS Distributions 
588*1031c584SApple OSS Distributions 			/*
589*1031c584SApple OSS Distributions 			 * mac_proc_check_get_task() can trigger upcalls. It's
590*1031c584SApple OSS Distributions 			 * not safe to hold proc references accross upcalls, so
591*1031c584SApple OSS Distributions 			 * just drop the reference.  Given the context, it
592*1031c584SApple OSS Distributions 			 * should not be possible for the process to actually
593*1031c584SApple OSS Distributions 			 * disappear.
594*1031c584SApple OSS Distributions 			 */
595*1031c584SApple OSS Distributions 			struct proc_ident pident = proc_ident(p);
596*1031c584SApple OSS Distributions 			sprunlock(p);
597*1031c584SApple OSS Distributions 			p = PROC_NULL;
598*1031c584SApple OSS Distributions 
599*1031c584SApple OSS Distributions 			(void) mac_proc_check_get_task(state->dts_cred.dcr_cred, &pident, TASK_FLAVOR_CONTROL);
600*1031c584SApple OSS Distributions 
601*1031c584SApple OSS Distributions 			p = sprlock(pident.p_pid);
602*1031c584SApple OSS Distributions 			if (p == PROC_NULL) {
603*1031c584SApple OSS Distributions 				return (ESRCH);
604*1031c584SApple OSS Distributions 			}
605*1031c584SApple OSS Distributions 		}
606*1031c584SApple OSS Distributions 		int rc = cs_allow_invalid(p);
607*1031c584SApple OSS Distributions 		proc_lock(p);
608*1031c584SApple OSS Distributions 		if (rc == 0) {
609*1031c584SApple OSS Distributions 			return (EACCES);
610*1031c584SApple OSS Distributions 		}
611*1031c584SApple OSS Distributions 	}
612*1031c584SApple OSS Distributions 	return (0);
613*1031c584SApple OSS Distributions }
614*1031c584SApple OSS Distributions 
615*1031c584SApple OSS Distributions /*
616*1031c584SApple OSS Distributions  * This is called from cfork() via dtrace_fasttrap_fork(). The child
617*1031c584SApple OSS Distributions  * process's address space is a (roughly) a copy of the parent process's so
618*1031c584SApple OSS Distributions  * we have to remove all the instrumentation we had previously enabled in the
619*1031c584SApple OSS Distributions  * parent.
620*1031c584SApple OSS Distributions  */
621*1031c584SApple OSS Distributions static void
fasttrap_fork(proc_t * p,proc_t * cp)622*1031c584SApple OSS Distributions fasttrap_fork(proc_t *p, proc_t *cp)
623*1031c584SApple OSS Distributions {
624*1031c584SApple OSS Distributions 	pid_t ppid = proc_getpid(p);
625*1031c584SApple OSS Distributions 	unsigned int i;
626*1031c584SApple OSS Distributions 
627*1031c584SApple OSS Distributions 	ASSERT(current_proc() == p);
628*1031c584SApple OSS Distributions 	LCK_MTX_ASSERT(&p->p_dtrace_sprlock, LCK_MTX_ASSERT_OWNED);
629*1031c584SApple OSS Distributions 	ASSERT(p->p_dtrace_count > 0);
630*1031c584SApple OSS Distributions 	ASSERT(cp->p_dtrace_count == 0);
631*1031c584SApple OSS Distributions 
632*1031c584SApple OSS Distributions 	/*
633*1031c584SApple OSS Distributions 	 * This would be simpler and faster if we maintained per-process
634*1031c584SApple OSS Distributions 	 * hash tables of enabled tracepoints. It could, however, potentially
635*1031c584SApple OSS Distributions 	 * slow down execution of a tracepoint since we'd need to go
636*1031c584SApple OSS Distributions 	 * through two levels of indirection. In the future, we should
637*1031c584SApple OSS Distributions 	 * consider either maintaining per-process ancillary lists of
638*1031c584SApple OSS Distributions 	 * enabled tracepoints or hanging a pointer to a per-process hash
639*1031c584SApple OSS Distributions 	 * table of enabled tracepoints off the proc structure.
640*1031c584SApple OSS Distributions 	 */
641*1031c584SApple OSS Distributions 
642*1031c584SApple OSS Distributions 	/*
643*1031c584SApple OSS Distributions 	 * We don't have to worry about the child process disappearing
644*1031c584SApple OSS Distributions 	 * because we're in fork().
645*1031c584SApple OSS Distributions 	 */
646*1031c584SApple OSS Distributions 	if (cp != sprlock(proc_getpid(cp))) {
647*1031c584SApple OSS Distributions 		printf("fasttrap_fork: sprlock(%d) returned a different proc\n", proc_getpid(cp));
648*1031c584SApple OSS Distributions 		return;
649*1031c584SApple OSS Distributions 	}
650*1031c584SApple OSS Distributions 
651*1031c584SApple OSS Distributions 	proc_lock(cp);
652*1031c584SApple OSS Distributions 	if (fasttrap_setdebug(cp) == ESRCH) {
653*1031c584SApple OSS Distributions 		printf("fasttrap_fork: failed to re-acquire proc\n");
654*1031c584SApple OSS Distributions 		return;
655*1031c584SApple OSS Distributions 	}
656*1031c584SApple OSS Distributions 	proc_unlock(cp);
657*1031c584SApple OSS Distributions 
658*1031c584SApple OSS Distributions 	/*
659*1031c584SApple OSS Distributions 	 * Iterate over every tracepoint looking for ones that belong to the
660*1031c584SApple OSS Distributions 	 * parent process, and remove each from the child process.
661*1031c584SApple OSS Distributions 	 */
662*1031c584SApple OSS Distributions 	for (i = 0; i < fasttrap_tpoints.fth_nent; i++) {
663*1031c584SApple OSS Distributions 		fasttrap_tracepoint_t *tp;
664*1031c584SApple OSS Distributions 		fasttrap_bucket_t *bucket = &fasttrap_tpoints.fth_table[i];
665*1031c584SApple OSS Distributions 
666*1031c584SApple OSS Distributions 		lck_mtx_lock(&bucket->ftb_mtx);
667*1031c584SApple OSS Distributions 		for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
668*1031c584SApple OSS Distributions 			if (tp->ftt_pid == ppid &&
669*1031c584SApple OSS Distributions 			    tp->ftt_proc->ftpc_acount != 0) {
670*1031c584SApple OSS Distributions 				fasttrap_tracepoint_remove(cp, tp);
671*1031c584SApple OSS Distributions 
672*1031c584SApple OSS Distributions 				/*
673*1031c584SApple OSS Distributions 				 * The count of active providers can only be
674*1031c584SApple OSS Distributions 				 * decremented (i.e. to zero) during exec,
675*1031c584SApple OSS Distributions 				 * exit, and removal of a meta provider so it
676*1031c584SApple OSS Distributions 				 * should be impossible to drop the count
677*1031c584SApple OSS Distributions 				 * mid-fork.
678*1031c584SApple OSS Distributions 				 */
679*1031c584SApple OSS Distributions 				 ASSERT(tp->ftt_proc->ftpc_acount != 0);
680*1031c584SApple OSS Distributions 			}
681*1031c584SApple OSS Distributions 		}
682*1031c584SApple OSS Distributions 		lck_mtx_unlock(&bucket->ftb_mtx);
683*1031c584SApple OSS Distributions 	}
684*1031c584SApple OSS Distributions 
685*1031c584SApple OSS Distributions 	/*
686*1031c584SApple OSS Distributions 	 * Free any ptss pages/entries in the child.
687*1031c584SApple OSS Distributions 	 */
688*1031c584SApple OSS Distributions 	dtrace_ptss_fork(p, cp);
689*1031c584SApple OSS Distributions 
690*1031c584SApple OSS Distributions 	sprunlock(cp);
691*1031c584SApple OSS Distributions }
692*1031c584SApple OSS Distributions 
693*1031c584SApple OSS Distributions /*
694*1031c584SApple OSS Distributions  * This is called from proc_exit() or from exec_common() if p_dtrace_probes
695*1031c584SApple OSS Distributions  * is set on the proc structure to indicate that there is a pid provider
696*1031c584SApple OSS Distributions  * associated with this process.
697*1031c584SApple OSS Distributions  */
698*1031c584SApple OSS Distributions static void
fasttrap_exec_exit(proc_t * p)699*1031c584SApple OSS Distributions fasttrap_exec_exit(proc_t *p)
700*1031c584SApple OSS Distributions {
701*1031c584SApple OSS Distributions 	ASSERT(p == current_proc());
702*1031c584SApple OSS Distributions 	LCK_MTX_ASSERT(&p->p_mlock, LCK_MTX_ASSERT_OWNED);
703*1031c584SApple OSS Distributions 	LCK_MTX_ASSERT(&p->p_dtrace_sprlock, LCK_MTX_ASSERT_NOTOWNED);
704*1031c584SApple OSS Distributions 
705*1031c584SApple OSS Distributions 
706*1031c584SApple OSS Distributions 	/* APPLE NOTE: Okay, the locking here is really odd and needs some
707*1031c584SApple OSS Distributions 	 * explaining. This method is always called with the proc_lock held.
708*1031c584SApple OSS Distributions 	 * We must drop the proc_lock before calling fasttrap_provider_retire
709*1031c584SApple OSS Distributions 	 * to avoid a deadlock when it takes the bucket lock.
710*1031c584SApple OSS Distributions 	 *
711*1031c584SApple OSS Distributions 	 * Next, the dtrace_ptss_exec_exit function requires the sprlock
712*1031c584SApple OSS Distributions 	 * be held, but not the proc_lock.
713*1031c584SApple OSS Distributions 	 *
714*1031c584SApple OSS Distributions 	 * Finally, we must re-acquire the proc_lock
715*1031c584SApple OSS Distributions 	 */
716*1031c584SApple OSS Distributions 	proc_unlock(p);
717*1031c584SApple OSS Distributions 
718*1031c584SApple OSS Distributions 	/*
719*1031c584SApple OSS Distributions 	 * We clean up the pid provider for this process here; user-land
720*1031c584SApple OSS Distributions 	 * static probes are handled by the meta-provider remove entry point.
721*1031c584SApple OSS Distributions 	 */
722*1031c584SApple OSS Distributions 	fasttrap_provider_retire(p, FASTTRAP_PID_NAME, 0);
723*1031c584SApple OSS Distributions 
724*1031c584SApple OSS Distributions 	/*
725*1031c584SApple OSS Distributions 	 * APPLE NOTE: We also need to remove any aliased providers.
726*1031c584SApple OSS Distributions 	 * XXX optimization: track which provider types are instantiated
727*1031c584SApple OSS Distributions 	 * and only retire as needed.
728*1031c584SApple OSS Distributions 	 */
729*1031c584SApple OSS Distributions 	fasttrap_provider_retire(p, FASTTRAP_OBJC_NAME, 0);
730*1031c584SApple OSS Distributions 	fasttrap_provider_retire(p, FASTTRAP_ONESHOT_NAME, 0);
731*1031c584SApple OSS Distributions 
732*1031c584SApple OSS Distributions 	/*
733*1031c584SApple OSS Distributions 	 * This should be called after it is no longer possible for a user
734*1031c584SApple OSS Distributions 	 * thread to execute (potentially dtrace instrumented) instructions.
735*1031c584SApple OSS Distributions 	 */
736*1031c584SApple OSS Distributions 	lck_mtx_lock(&p->p_dtrace_sprlock);
737*1031c584SApple OSS Distributions 	dtrace_ptss_exec_exit(p);
738*1031c584SApple OSS Distributions 	lck_mtx_unlock(&p->p_dtrace_sprlock);
739*1031c584SApple OSS Distributions 
740*1031c584SApple OSS Distributions 	proc_lock(p);
741*1031c584SApple OSS Distributions }
742*1031c584SApple OSS Distributions 
743*1031c584SApple OSS Distributions 
744*1031c584SApple OSS Distributions /*ARGSUSED*/
745*1031c584SApple OSS Distributions static void
fasttrap_pid_provide(void * arg,const dtrace_probedesc_t * desc)746*1031c584SApple OSS Distributions fasttrap_pid_provide(void *arg, const dtrace_probedesc_t *desc)
747*1031c584SApple OSS Distributions {
748*1031c584SApple OSS Distributions #pragma unused(arg, desc)
749*1031c584SApple OSS Distributions 	/*
750*1031c584SApple OSS Distributions 	 * There are no "default" pid probes.
751*1031c584SApple OSS Distributions 	 */
752*1031c584SApple OSS Distributions }
753*1031c584SApple OSS Distributions 
754*1031c584SApple OSS Distributions static int
fasttrap_tracepoint_enable(proc_t * p,fasttrap_probe_t * probe,uint_t index)755*1031c584SApple OSS Distributions fasttrap_tracepoint_enable(proc_t *p, fasttrap_probe_t *probe, uint_t index)
756*1031c584SApple OSS Distributions {
757*1031c584SApple OSS Distributions 	fasttrap_tracepoint_t *tp, *new_tp = NULL;
758*1031c584SApple OSS Distributions 	fasttrap_bucket_t *bucket;
759*1031c584SApple OSS Distributions 	fasttrap_id_t *id;
760*1031c584SApple OSS Distributions 	pid_t pid;
761*1031c584SApple OSS Distributions 	user_addr_t pc;
762*1031c584SApple OSS Distributions 
763*1031c584SApple OSS Distributions 	ASSERT(index < probe->ftp_ntps);
764*1031c584SApple OSS Distributions 
765*1031c584SApple OSS Distributions 	pid = probe->ftp_pid;
766*1031c584SApple OSS Distributions 	pc = probe->ftp_tps[index].fit_tp->ftt_pc;
767*1031c584SApple OSS Distributions 	id = &probe->ftp_tps[index].fit_id;
768*1031c584SApple OSS Distributions 
769*1031c584SApple OSS Distributions 	ASSERT(probe->ftp_tps[index].fit_tp->ftt_pid == pid);
770*1031c584SApple OSS Distributions 
771*1031c584SApple OSS Distributions 	/*
772*1031c584SApple OSS Distributions 	 * Before we make any modifications, make sure we've imposed a barrier
773*1031c584SApple OSS Distributions 	 * on the generation in which this probe was last modified.
774*1031c584SApple OSS Distributions 	 */
775*1031c584SApple OSS Distributions 	fasttrap_mod_barrier(probe->ftp_gen);
776*1031c584SApple OSS Distributions 
777*1031c584SApple OSS Distributions 	bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
778*1031c584SApple OSS Distributions 
779*1031c584SApple OSS Distributions 	/*
780*1031c584SApple OSS Distributions 	 * If the tracepoint has already been enabled, just add our id to the
781*1031c584SApple OSS Distributions 	 * list of interested probes. This may be our second time through
782*1031c584SApple OSS Distributions 	 * this path in which case we'll have constructed the tracepoint we'd
783*1031c584SApple OSS Distributions 	 * like to install. If we can't find a match, and have an allocated
784*1031c584SApple OSS Distributions 	 * tracepoint ready to go, enable that one now.
785*1031c584SApple OSS Distributions 	 *
786*1031c584SApple OSS Distributions 	 * A tracepoint whose process is defunct is also considered defunct.
787*1031c584SApple OSS Distributions 	 */
788*1031c584SApple OSS Distributions again:
789*1031c584SApple OSS Distributions 	lck_mtx_lock(&bucket->ftb_mtx);
790*1031c584SApple OSS Distributions 	for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
791*1031c584SApple OSS Distributions 		int rc = 0;
792*1031c584SApple OSS Distributions 		/*
793*1031c584SApple OSS Distributions 		 * Note that it's safe to access the active count on the
794*1031c584SApple OSS Distributions 		 * associated proc structure because we know that at least one
795*1031c584SApple OSS Distributions 		 * provider (this one) will still be around throughout this
796*1031c584SApple OSS Distributions 		 * operation.
797*1031c584SApple OSS Distributions 		 */
798*1031c584SApple OSS Distributions 		if (tp->ftt_pid != pid || tp->ftt_pc != pc ||
799*1031c584SApple OSS Distributions 		    tp->ftt_proc->ftpc_acount == 0)
800*1031c584SApple OSS Distributions 			continue;
801*1031c584SApple OSS Distributions 
802*1031c584SApple OSS Distributions 		/*
803*1031c584SApple OSS Distributions 		 * Now that we've found a matching tracepoint, it would be
804*1031c584SApple OSS Distributions 		 * a decent idea to confirm that the tracepoint is still
805*1031c584SApple OSS Distributions 		 * enabled and the trap instruction hasn't been overwritten.
806*1031c584SApple OSS Distributions 		 * Since this is a little hairy, we'll punt for now.
807*1031c584SApple OSS Distributions 		 */
808*1031c584SApple OSS Distributions 		if (!tp->ftt_installed) {
809*1031c584SApple OSS Distributions 			if (fasttrap_tracepoint_install(p, tp) != 0)
810*1031c584SApple OSS Distributions 				rc = FASTTRAP_ENABLE_PARTIAL;
811*1031c584SApple OSS Distributions 		}
812*1031c584SApple OSS Distributions 		/*
813*1031c584SApple OSS Distributions 		 * This can't be the first interested probe. We don't have
814*1031c584SApple OSS Distributions 		 * to worry about another thread being in the midst of
815*1031c584SApple OSS Distributions 		 * deleting this tracepoint (which would be the only valid
816*1031c584SApple OSS Distributions 		 * reason for a tracepoint to have no interested probes)
817*1031c584SApple OSS Distributions 		 * since we're holding P_PR_LOCK for this process.
818*1031c584SApple OSS Distributions 		 */
819*1031c584SApple OSS Distributions 		ASSERT(tp->ftt_ids != NULL || tp->ftt_retids != NULL);
820*1031c584SApple OSS Distributions 
821*1031c584SApple OSS Distributions 		switch (id->fti_ptype) {
822*1031c584SApple OSS Distributions 		case DTFTP_ENTRY:
823*1031c584SApple OSS Distributions 		case DTFTP_OFFSETS:
824*1031c584SApple OSS Distributions 		case DTFTP_IS_ENABLED:
825*1031c584SApple OSS Distributions 			id->fti_next = tp->ftt_ids;
826*1031c584SApple OSS Distributions 			dtrace_membar_producer();
827*1031c584SApple OSS Distributions 			tp->ftt_ids = id;
828*1031c584SApple OSS Distributions 			dtrace_membar_producer();
829*1031c584SApple OSS Distributions 			break;
830*1031c584SApple OSS Distributions 
831*1031c584SApple OSS Distributions 		case DTFTP_RETURN:
832*1031c584SApple OSS Distributions 		case DTFTP_POST_OFFSETS:
833*1031c584SApple OSS Distributions 			id->fti_next = tp->ftt_retids;
834*1031c584SApple OSS Distributions 			dtrace_membar_producer();
835*1031c584SApple OSS Distributions 			tp->ftt_retids = id;
836*1031c584SApple OSS Distributions 			dtrace_membar_producer();
837*1031c584SApple OSS Distributions 			break;
838*1031c584SApple OSS Distributions 
839*1031c584SApple OSS Distributions 		default:
840*1031c584SApple OSS Distributions 			ASSERT(0);
841*1031c584SApple OSS Distributions 		}
842*1031c584SApple OSS Distributions 
843*1031c584SApple OSS Distributions 		tp->ftt_retired = 0;
844*1031c584SApple OSS Distributions 
845*1031c584SApple OSS Distributions 		lck_mtx_unlock(&bucket->ftb_mtx);
846*1031c584SApple OSS Distributions 
847*1031c584SApple OSS Distributions 		if (new_tp != NULL) {
848*1031c584SApple OSS Distributions 			new_tp->ftt_ids = NULL;
849*1031c584SApple OSS Distributions 			new_tp->ftt_retids = NULL;
850*1031c584SApple OSS Distributions 		}
851*1031c584SApple OSS Distributions 
852*1031c584SApple OSS Distributions 		return rc;
853*1031c584SApple OSS Distributions 	}
854*1031c584SApple OSS Distributions 
855*1031c584SApple OSS Distributions 	/*
856*1031c584SApple OSS Distributions 	 * If we have a good tracepoint ready to go, install it now while
857*1031c584SApple OSS Distributions 	 * we have the lock held and no one can screw with us.
858*1031c584SApple OSS Distributions 	 */
859*1031c584SApple OSS Distributions 	if (new_tp != NULL) {
860*1031c584SApple OSS Distributions 		int rc = 0;
861*1031c584SApple OSS Distributions 
862*1031c584SApple OSS Distributions 		new_tp->ftt_next = bucket->ftb_data;
863*1031c584SApple OSS Distributions 		dtrace_membar_producer();
864*1031c584SApple OSS Distributions 		bucket->ftb_data = new_tp;
865*1031c584SApple OSS Distributions 		dtrace_membar_producer();
866*1031c584SApple OSS Distributions 		lck_mtx_unlock(&bucket->ftb_mtx);
867*1031c584SApple OSS Distributions 
868*1031c584SApple OSS Distributions 		/*
869*1031c584SApple OSS Distributions 		 * Activate the tracepoint in the ISA-specific manner.
870*1031c584SApple OSS Distributions 		 * If this fails, we need to report the failure, but
871*1031c584SApple OSS Distributions 		 * indicate that this tracepoint must still be disabled
872*1031c584SApple OSS Distributions 		 * by calling fasttrap_tracepoint_disable().
873*1031c584SApple OSS Distributions 		 */
874*1031c584SApple OSS Distributions 		if (fasttrap_tracepoint_install(p, new_tp) != 0)
875*1031c584SApple OSS Distributions 			rc = FASTTRAP_ENABLE_PARTIAL;
876*1031c584SApple OSS Distributions 		/*
877*1031c584SApple OSS Distributions 		 * Increment the count of the number of tracepoints active in
878*1031c584SApple OSS Distributions 		 * the victim process.
879*1031c584SApple OSS Distributions 		 */
880*1031c584SApple OSS Distributions 		//ASSERT(p->p_proc_flag & P_PR_LOCK);
881*1031c584SApple OSS Distributions 		p->p_dtrace_count++;
882*1031c584SApple OSS Distributions 
883*1031c584SApple OSS Distributions 
884*1031c584SApple OSS Distributions 		return (rc);
885*1031c584SApple OSS Distributions 	}
886*1031c584SApple OSS Distributions 
887*1031c584SApple OSS Distributions 	lck_mtx_unlock(&bucket->ftb_mtx);
888*1031c584SApple OSS Distributions 
889*1031c584SApple OSS Distributions 	/*
890*1031c584SApple OSS Distributions 	 * Initialize the tracepoint that's been preallocated with the probe.
891*1031c584SApple OSS Distributions 	 */
892*1031c584SApple OSS Distributions 	new_tp = probe->ftp_tps[index].fit_tp;
893*1031c584SApple OSS Distributions 	new_tp->ftt_retired = 0;
894*1031c584SApple OSS Distributions 
895*1031c584SApple OSS Distributions 	ASSERT(new_tp->ftt_pid == pid);
896*1031c584SApple OSS Distributions 	ASSERT(new_tp->ftt_pc == pc);
897*1031c584SApple OSS Distributions 	ASSERT(new_tp->ftt_proc == probe->ftp_prov->ftp_proc);
898*1031c584SApple OSS Distributions 	ASSERT(new_tp->ftt_ids == NULL);
899*1031c584SApple OSS Distributions 	ASSERT(new_tp->ftt_retids == NULL);
900*1031c584SApple OSS Distributions 
901*1031c584SApple OSS Distributions 	switch (id->fti_ptype) {
902*1031c584SApple OSS Distributions 	case DTFTP_ENTRY:
903*1031c584SApple OSS Distributions 	case DTFTP_OFFSETS:
904*1031c584SApple OSS Distributions 	case DTFTP_IS_ENABLED:
905*1031c584SApple OSS Distributions 		id->fti_next = NULL;
906*1031c584SApple OSS Distributions 		new_tp->ftt_ids = id;
907*1031c584SApple OSS Distributions 		break;
908*1031c584SApple OSS Distributions 
909*1031c584SApple OSS Distributions 	case DTFTP_RETURN:
910*1031c584SApple OSS Distributions 	case DTFTP_POST_OFFSETS:
911*1031c584SApple OSS Distributions 		id->fti_next = NULL;
912*1031c584SApple OSS Distributions 		new_tp->ftt_retids = id;
913*1031c584SApple OSS Distributions 		break;
914*1031c584SApple OSS Distributions 
915*1031c584SApple OSS Distributions 	default:
916*1031c584SApple OSS Distributions 		ASSERT(0);
917*1031c584SApple OSS Distributions 	}
918*1031c584SApple OSS Distributions 
919*1031c584SApple OSS Distributions 	/*
920*1031c584SApple OSS Distributions 	 * If the ISA-dependent initialization goes to plan, go back to the
921*1031c584SApple OSS Distributions 	 * beginning and try to install this freshly made tracepoint.
922*1031c584SApple OSS Distributions 	 */
923*1031c584SApple OSS Distributions 	if (fasttrap_tracepoint_init(p, new_tp, pc, id->fti_ptype) == 0)
924*1031c584SApple OSS Distributions 		goto again;
925*1031c584SApple OSS Distributions 
926*1031c584SApple OSS Distributions 	new_tp->ftt_ids = NULL;
927*1031c584SApple OSS Distributions 	new_tp->ftt_retids = NULL;
928*1031c584SApple OSS Distributions 
929*1031c584SApple OSS Distributions 	return (FASTTRAP_ENABLE_FAIL);
930*1031c584SApple OSS Distributions }
931*1031c584SApple OSS Distributions 
932*1031c584SApple OSS Distributions static void
fasttrap_tracepoint_disable(proc_t * p,fasttrap_probe_t * probe,uint_t index)933*1031c584SApple OSS Distributions fasttrap_tracepoint_disable(proc_t *p, fasttrap_probe_t *probe, uint_t index)
934*1031c584SApple OSS Distributions {
935*1031c584SApple OSS Distributions 	fasttrap_bucket_t *bucket;
936*1031c584SApple OSS Distributions 	fasttrap_provider_t *provider = probe->ftp_prov;
937*1031c584SApple OSS Distributions 	fasttrap_tracepoint_t **pp, *tp;
938*1031c584SApple OSS Distributions 	fasttrap_id_t *id, **idp;
939*1031c584SApple OSS Distributions 	pid_t pid;
940*1031c584SApple OSS Distributions 	user_addr_t pc;
941*1031c584SApple OSS Distributions 
942*1031c584SApple OSS Distributions 	ASSERT(index < probe->ftp_ntps);
943*1031c584SApple OSS Distributions 
944*1031c584SApple OSS Distributions 	pid = probe->ftp_pid;
945*1031c584SApple OSS Distributions 	pc = probe->ftp_tps[index].fit_tp->ftt_pc;
946*1031c584SApple OSS Distributions 	id = &probe->ftp_tps[index].fit_id;
947*1031c584SApple OSS Distributions 
948*1031c584SApple OSS Distributions 	ASSERT(probe->ftp_tps[index].fit_tp->ftt_pid == pid);
949*1031c584SApple OSS Distributions 
950*1031c584SApple OSS Distributions 	/*
951*1031c584SApple OSS Distributions 	 * Find the tracepoint and make sure that our id is one of the
952*1031c584SApple OSS Distributions 	 * ones registered with it.
953*1031c584SApple OSS Distributions 	 */
954*1031c584SApple OSS Distributions 	bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
955*1031c584SApple OSS Distributions 	lck_mtx_lock(&bucket->ftb_mtx);
956*1031c584SApple OSS Distributions 	for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
957*1031c584SApple OSS Distributions 		if (tp->ftt_pid == pid && tp->ftt_pc == pc &&
958*1031c584SApple OSS Distributions 		    tp->ftt_proc == provider->ftp_proc)
959*1031c584SApple OSS Distributions 			break;
960*1031c584SApple OSS Distributions 	}
961*1031c584SApple OSS Distributions 
962*1031c584SApple OSS Distributions 	/*
963*1031c584SApple OSS Distributions 	 * If we somehow lost this tracepoint, we're in a world of hurt.
964*1031c584SApple OSS Distributions 	 */
965*1031c584SApple OSS Distributions 	ASSERT(tp != NULL);
966*1031c584SApple OSS Distributions 
967*1031c584SApple OSS Distributions 	switch (id->fti_ptype) {
968*1031c584SApple OSS Distributions 		case DTFTP_ENTRY:
969*1031c584SApple OSS Distributions 		case DTFTP_OFFSETS:
970*1031c584SApple OSS Distributions 		case DTFTP_IS_ENABLED:
971*1031c584SApple OSS Distributions 			ASSERT(tp->ftt_ids != NULL);
972*1031c584SApple OSS Distributions 			idp = &tp->ftt_ids;
973*1031c584SApple OSS Distributions 			break;
974*1031c584SApple OSS Distributions 
975*1031c584SApple OSS Distributions 		case DTFTP_RETURN:
976*1031c584SApple OSS Distributions 		case DTFTP_POST_OFFSETS:
977*1031c584SApple OSS Distributions 			ASSERT(tp->ftt_retids != NULL);
978*1031c584SApple OSS Distributions 			idp = &tp->ftt_retids;
979*1031c584SApple OSS Distributions 			break;
980*1031c584SApple OSS Distributions 
981*1031c584SApple OSS Distributions 		default:
982*1031c584SApple OSS Distributions 			/* Fix compiler warning... */
983*1031c584SApple OSS Distributions 			idp = NULL;
984*1031c584SApple OSS Distributions 			ASSERT(0);
985*1031c584SApple OSS Distributions 	}
986*1031c584SApple OSS Distributions 
987*1031c584SApple OSS Distributions 	while ((*idp)->fti_probe != probe) {
988*1031c584SApple OSS Distributions 		idp = &(*idp)->fti_next;
989*1031c584SApple OSS Distributions 		ASSERT(*idp != NULL);
990*1031c584SApple OSS Distributions 	}
991*1031c584SApple OSS Distributions 
992*1031c584SApple OSS Distributions 	id = *idp;
993*1031c584SApple OSS Distributions 	*idp = id->fti_next;
994*1031c584SApple OSS Distributions 	dtrace_membar_producer();
995*1031c584SApple OSS Distributions 
996*1031c584SApple OSS Distributions 	ASSERT(id->fti_probe == probe);
997*1031c584SApple OSS Distributions 
998*1031c584SApple OSS Distributions 	/*
999*1031c584SApple OSS Distributions 	 * If there are other registered enablings of this tracepoint, we're
1000*1031c584SApple OSS Distributions 	 * all done, but if this was the last probe assocated with this
1001*1031c584SApple OSS Distributions 	 * this tracepoint, we need to remove and free it.
1002*1031c584SApple OSS Distributions 	 */
1003*1031c584SApple OSS Distributions 	if (tp->ftt_ids != NULL || tp->ftt_retids != NULL) {
1004*1031c584SApple OSS Distributions 
1005*1031c584SApple OSS Distributions 		/*
1006*1031c584SApple OSS Distributions 		 * If the current probe's tracepoint is in use, swap it
1007*1031c584SApple OSS Distributions 		 * for an unused tracepoint.
1008*1031c584SApple OSS Distributions 		 */
1009*1031c584SApple OSS Distributions 		if (tp == probe->ftp_tps[index].fit_tp) {
1010*1031c584SApple OSS Distributions 			fasttrap_probe_t *tmp_probe;
1011*1031c584SApple OSS Distributions 			fasttrap_tracepoint_t **tmp_tp;
1012*1031c584SApple OSS Distributions 			uint_t tmp_index;
1013*1031c584SApple OSS Distributions 
1014*1031c584SApple OSS Distributions 			if (tp->ftt_ids != NULL) {
1015*1031c584SApple OSS Distributions 				tmp_probe = tp->ftt_ids->fti_probe;
1016*1031c584SApple OSS Distributions 				/* LINTED - alignment */
1017*1031c584SApple OSS Distributions 				tmp_index = FASTTRAP_ID_INDEX(tp->ftt_ids);
1018*1031c584SApple OSS Distributions 				tmp_tp = &tmp_probe->ftp_tps[tmp_index].fit_tp;
1019*1031c584SApple OSS Distributions 			} else {
1020*1031c584SApple OSS Distributions 				tmp_probe = tp->ftt_retids->fti_probe;
1021*1031c584SApple OSS Distributions 				/* LINTED - alignment */
1022*1031c584SApple OSS Distributions 				tmp_index = FASTTRAP_ID_INDEX(tp->ftt_retids);
1023*1031c584SApple OSS Distributions 				tmp_tp = &tmp_probe->ftp_tps[tmp_index].fit_tp;
1024*1031c584SApple OSS Distributions 			}
1025*1031c584SApple OSS Distributions 
1026*1031c584SApple OSS Distributions 			ASSERT(*tmp_tp != NULL);
1027*1031c584SApple OSS Distributions 			ASSERT(*tmp_tp != probe->ftp_tps[index].fit_tp);
1028*1031c584SApple OSS Distributions 			ASSERT((*tmp_tp)->ftt_ids == NULL);
1029*1031c584SApple OSS Distributions 			ASSERT((*tmp_tp)->ftt_retids == NULL);
1030*1031c584SApple OSS Distributions 
1031*1031c584SApple OSS Distributions 			probe->ftp_tps[index].fit_tp = *tmp_tp;
1032*1031c584SApple OSS Distributions 			*tmp_tp = tp;
1033*1031c584SApple OSS Distributions 
1034*1031c584SApple OSS Distributions 		}
1035*1031c584SApple OSS Distributions 
1036*1031c584SApple OSS Distributions 		lck_mtx_unlock(&bucket->ftb_mtx);
1037*1031c584SApple OSS Distributions 
1038*1031c584SApple OSS Distributions 		/*
1039*1031c584SApple OSS Distributions 		 * Tag the modified probe with the generation in which it was
1040*1031c584SApple OSS Distributions 		 * changed.
1041*1031c584SApple OSS Distributions 		 */
1042*1031c584SApple OSS Distributions 		probe->ftp_gen = fasttrap_mod_gen;
1043*1031c584SApple OSS Distributions 		return;
1044*1031c584SApple OSS Distributions 	}
1045*1031c584SApple OSS Distributions 
1046*1031c584SApple OSS Distributions 	lck_mtx_unlock(&bucket->ftb_mtx);
1047*1031c584SApple OSS Distributions 
1048*1031c584SApple OSS Distributions 	/*
1049*1031c584SApple OSS Distributions 	 * We can't safely remove the tracepoint from the set of active
1050*1031c584SApple OSS Distributions 	 * tracepoints until we've actually removed the fasttrap instruction
1051*1031c584SApple OSS Distributions 	 * from the process's text. We can, however, operate on this
1052*1031c584SApple OSS Distributions 	 * tracepoint secure in the knowledge that no other thread is going to
1053*1031c584SApple OSS Distributions 	 * be looking at it since we hold P_PR_LOCK on the process if it's
1054*1031c584SApple OSS Distributions 	 * live or we hold the provider lock on the process if it's dead and
1055*1031c584SApple OSS Distributions 	 * gone.
1056*1031c584SApple OSS Distributions 	 */
1057*1031c584SApple OSS Distributions 
1058*1031c584SApple OSS Distributions 	/*
1059*1031c584SApple OSS Distributions 	 * We only need to remove the actual instruction if we're looking
1060*1031c584SApple OSS Distributions 	 * at an existing process
1061*1031c584SApple OSS Distributions 	 */
1062*1031c584SApple OSS Distributions 	if (p != NULL) {
1063*1031c584SApple OSS Distributions 		/*
1064*1031c584SApple OSS Distributions 		 * If we fail to restore the instruction we need to kill
1065*1031c584SApple OSS Distributions 		 * this process since it's in a completely unrecoverable
1066*1031c584SApple OSS Distributions 		 * state.
1067*1031c584SApple OSS Distributions 		 */
1068*1031c584SApple OSS Distributions 		if (fasttrap_tracepoint_remove(p, tp) != 0)
1069*1031c584SApple OSS Distributions 			fasttrap_sigtrap(p, NULL, pc);
1070*1031c584SApple OSS Distributions 
1071*1031c584SApple OSS Distributions 		/*
1072*1031c584SApple OSS Distributions 		 * Decrement the count of the number of tracepoints active
1073*1031c584SApple OSS Distributions 		 * in the victim process.
1074*1031c584SApple OSS Distributions 		 */
1075*1031c584SApple OSS Distributions 		//ASSERT(p->p_proc_flag & P_PR_LOCK);
1076*1031c584SApple OSS Distributions 		p->p_dtrace_count--;
1077*1031c584SApple OSS Distributions 	}
1078*1031c584SApple OSS Distributions 
1079*1031c584SApple OSS Distributions 	/*
1080*1031c584SApple OSS Distributions 	 * Remove the probe from the hash table of active tracepoints.
1081*1031c584SApple OSS Distributions 	 */
1082*1031c584SApple OSS Distributions 	lck_mtx_lock(&bucket->ftb_mtx);
1083*1031c584SApple OSS Distributions 	pp = (fasttrap_tracepoint_t **)&bucket->ftb_data;
1084*1031c584SApple OSS Distributions 	ASSERT(*pp != NULL);
1085*1031c584SApple OSS Distributions 	while (*pp != tp) {
1086*1031c584SApple OSS Distributions 		pp = &(*pp)->ftt_next;
1087*1031c584SApple OSS Distributions 		ASSERT(*pp != NULL);
1088*1031c584SApple OSS Distributions 	}
1089*1031c584SApple OSS Distributions 
1090*1031c584SApple OSS Distributions 	*pp = tp->ftt_next;
1091*1031c584SApple OSS Distributions 	dtrace_membar_producer();
1092*1031c584SApple OSS Distributions 
1093*1031c584SApple OSS Distributions 	lck_mtx_unlock(&bucket->ftb_mtx);
1094*1031c584SApple OSS Distributions 
1095*1031c584SApple OSS Distributions 	/*
1096*1031c584SApple OSS Distributions 	 * Tag the modified probe with the generation in which it was changed.
1097*1031c584SApple OSS Distributions 	 */
1098*1031c584SApple OSS Distributions 	probe->ftp_gen = fasttrap_mod_gen;
1099*1031c584SApple OSS Distributions }
1100*1031c584SApple OSS Distributions 
1101*1031c584SApple OSS Distributions static void
fasttrap_enable_callbacks(void)1102*1031c584SApple OSS Distributions fasttrap_enable_callbacks(void)
1103*1031c584SApple OSS Distributions {
1104*1031c584SApple OSS Distributions 	/*
1105*1031c584SApple OSS Distributions 	 * We don't have to play the rw lock game here because we're
1106*1031c584SApple OSS Distributions 	 * providing something rather than taking something away --
1107*1031c584SApple OSS Distributions 	 * we can be sure that no threads have tried to follow this
1108*1031c584SApple OSS Distributions 	 * function pointer yet.
1109*1031c584SApple OSS Distributions 	 */
1110*1031c584SApple OSS Distributions 	lck_mtx_lock(&fasttrap_count_mtx);
1111*1031c584SApple OSS Distributions 	if (fasttrap_pid_count == 0) {
1112*1031c584SApple OSS Distributions 		ASSERT(dtrace_pid_probe_ptr == NULL);
1113*1031c584SApple OSS Distributions 		ASSERT(dtrace_return_probe_ptr == NULL);
1114*1031c584SApple OSS Distributions 		dtrace_pid_probe_ptr = &fasttrap_pid_probe;
1115*1031c584SApple OSS Distributions 		dtrace_return_probe_ptr = &fasttrap_return_probe;
1116*1031c584SApple OSS Distributions 	}
1117*1031c584SApple OSS Distributions 	ASSERT(dtrace_pid_probe_ptr == &fasttrap_pid_probe);
1118*1031c584SApple OSS Distributions 	ASSERT(dtrace_return_probe_ptr == &fasttrap_return_probe);
1119*1031c584SApple OSS Distributions 	fasttrap_pid_count++;
1120*1031c584SApple OSS Distributions 	lck_mtx_unlock(&fasttrap_count_mtx);
1121*1031c584SApple OSS Distributions }
1122*1031c584SApple OSS Distributions 
1123*1031c584SApple OSS Distributions static void
fasttrap_disable_callbacks(void)1124*1031c584SApple OSS Distributions fasttrap_disable_callbacks(void)
1125*1031c584SApple OSS Distributions {
1126*1031c584SApple OSS Distributions 	//ASSERT(MUTEX_HELD(&cpu_lock));
1127*1031c584SApple OSS Distributions 
1128*1031c584SApple OSS Distributions 	lck_mtx_lock(&fasttrap_count_mtx);
1129*1031c584SApple OSS Distributions 	ASSERT(fasttrap_pid_count > 0);
1130*1031c584SApple OSS Distributions 	fasttrap_pid_count--;
1131*1031c584SApple OSS Distributions 	if (fasttrap_pid_count == 0) {
1132*1031c584SApple OSS Distributions 		dtrace_cpu_t *cur, *cpu = CPU;
1133*1031c584SApple OSS Distributions 
1134*1031c584SApple OSS Distributions 		/*
1135*1031c584SApple OSS Distributions 		 * APPLE NOTE: This loop seems broken, it touches every CPU
1136*1031c584SApple OSS Distributions 		 * but the one we're actually running on. Need to ask Sun folks
1137*1031c584SApple OSS Distributions 		 * if that is safe. Scenario is this: We're running on CPU A,
1138*1031c584SApple OSS Distributions 		 * and lock all but A. Then we get preempted, and start running
1139*1031c584SApple OSS Distributions 		 * on CPU B. A probe fires on A, and is allowed to enter. BOOM!
1140*1031c584SApple OSS Distributions 		 */
1141*1031c584SApple OSS Distributions 		for (cur = cpu->cpu_next; cur != cpu; cur = cur->cpu_next) {
1142*1031c584SApple OSS Distributions 			lck_rw_lock_exclusive(&cur->cpu_ft_lock);
1143*1031c584SApple OSS Distributions 			// rw_enter(&cur->cpu_ft_lock, RW_WRITER);
1144*1031c584SApple OSS Distributions 		}
1145*1031c584SApple OSS Distributions 
1146*1031c584SApple OSS Distributions 		dtrace_pid_probe_ptr = NULL;
1147*1031c584SApple OSS Distributions 		dtrace_return_probe_ptr = NULL;
1148*1031c584SApple OSS Distributions 
1149*1031c584SApple OSS Distributions 		for (cur = cpu->cpu_next; cur != cpu; cur = cur->cpu_next) {
1150*1031c584SApple OSS Distributions 			lck_rw_unlock_exclusive(&cur->cpu_ft_lock);
1151*1031c584SApple OSS Distributions 			// rw_exit(&cur->cpu_ft_lock);
1152*1031c584SApple OSS Distributions 		}
1153*1031c584SApple OSS Distributions 	}
1154*1031c584SApple OSS Distributions 	lck_mtx_unlock(&fasttrap_count_mtx);
1155*1031c584SApple OSS Distributions }
1156*1031c584SApple OSS Distributions 
1157*1031c584SApple OSS Distributions /*ARGSUSED*/
1158*1031c584SApple OSS Distributions static int
fasttrap_pid_enable(void * arg,dtrace_id_t id,void * parg)1159*1031c584SApple OSS Distributions fasttrap_pid_enable(void *arg, dtrace_id_t id, void *parg)
1160*1031c584SApple OSS Distributions {
1161*1031c584SApple OSS Distributions #pragma unused(arg, id)
1162*1031c584SApple OSS Distributions 	fasttrap_probe_t *probe = parg;
1163*1031c584SApple OSS Distributions 	proc_t *p;
1164*1031c584SApple OSS Distributions 	int i, rc;
1165*1031c584SApple OSS Distributions 
1166*1031c584SApple OSS Distributions 	ASSERT(probe != NULL);
1167*1031c584SApple OSS Distributions 	ASSERT(!probe->ftp_enabled);
1168*1031c584SApple OSS Distributions 	ASSERT(id == probe->ftp_id);
1169*1031c584SApple OSS Distributions 	// ASSERT(MUTEX_HELD(&cpu_lock));
1170*1031c584SApple OSS Distributions 
1171*1031c584SApple OSS Distributions 	/*
1172*1031c584SApple OSS Distributions 	 * Increment the count of enabled probes on this probe's provider;
1173*1031c584SApple OSS Distributions 	 * the provider can't go away while the probe still exists. We
1174*1031c584SApple OSS Distributions 	 * must increment this even if we aren't able to properly enable
1175*1031c584SApple OSS Distributions 	 * this probe.
1176*1031c584SApple OSS Distributions 	 */
1177*1031c584SApple OSS Distributions 	lck_mtx_lock(&probe->ftp_prov->ftp_mtx);
1178*1031c584SApple OSS Distributions 	probe->ftp_prov->ftp_rcount++;
1179*1031c584SApple OSS Distributions 	lck_mtx_unlock(&probe->ftp_prov->ftp_mtx);
1180*1031c584SApple OSS Distributions 
1181*1031c584SApple OSS Distributions 	/*
1182*1031c584SApple OSS Distributions 	 * If this probe's provider is retired (meaning it was valid in a
1183*1031c584SApple OSS Distributions 	 * previously exec'ed incarnation of this address space), bail out. The
1184*1031c584SApple OSS Distributions 	 * provider can't go away while we're in this code path.
1185*1031c584SApple OSS Distributions 	 */
1186*1031c584SApple OSS Distributions 	if (probe->ftp_prov->ftp_retired)
1187*1031c584SApple OSS Distributions 	    return(0);
1188*1031c584SApple OSS Distributions 
1189*1031c584SApple OSS Distributions 	/*
1190*1031c584SApple OSS Distributions 	 * If we can't find the process, it may be that we're in the context of
1191*1031c584SApple OSS Distributions 	 * a fork in which the traced process is being born and we're copying
1192*1031c584SApple OSS Distributions 	 * USDT probes. Otherwise, the process is gone so bail.
1193*1031c584SApple OSS Distributions 	 */
1194*1031c584SApple OSS Distributions 	if ((p = sprlock(probe->ftp_pid)) == PROC_NULL) {
1195*1031c584SApple OSS Distributions 		/*
1196*1031c584SApple OSS Distributions 		 * APPLE NOTE: We should never end up here. The Solaris sprlock()
1197*1031c584SApple OSS Distributions 		 * does not return process's with SIDL set, but we always return
1198*1031c584SApple OSS Distributions 		 * the child process.
1199*1031c584SApple OSS Distributions 		 */
1200*1031c584SApple OSS Distributions 	    return(0);
1201*1031c584SApple OSS Distributions 	}
1202*1031c584SApple OSS Distributions 
1203*1031c584SApple OSS Distributions 	proc_lock(p);
1204*1031c584SApple OSS Distributions 	int p_pid = proc_pid(p);
1205*1031c584SApple OSS Distributions 
1206*1031c584SApple OSS Distributions 	rc = fasttrap_setdebug(p);
1207*1031c584SApple OSS Distributions 	switch (rc) {
1208*1031c584SApple OSS Distributions 	case EACCES:
1209*1031c584SApple OSS Distributions 		proc_unlock(p);
1210*1031c584SApple OSS Distributions 		sprunlock(p);
1211*1031c584SApple OSS Distributions 		cmn_err(CE_WARN, "Failed to install fasttrap probe for pid %d: "
1212*1031c584SApple OSS Distributions 		    "Process does not allow invalid code pages\n", p_pid);
1213*1031c584SApple OSS Distributions 		return (0);
1214*1031c584SApple OSS Distributions 	case ESRCH:
1215*1031c584SApple OSS Distributions 		cmn_err(CE_WARN, "Failed to install fasttrap probe for pid %d: "
1216*1031c584SApple OSS Distributions 		    "Failed to re-acquire process\n", p_pid);
1217*1031c584SApple OSS Distributions 		return (0);
1218*1031c584SApple OSS Distributions 	default:
1219*1031c584SApple OSS Distributions 		assert(rc == 0);
1220*1031c584SApple OSS Distributions 		break;
1221*1031c584SApple OSS Distributions 	}
1222*1031c584SApple OSS Distributions 
1223*1031c584SApple OSS Distributions 	/*
1224*1031c584SApple OSS Distributions 	 * APPLE NOTE: We do not have an equivalent thread structure to Solaris.
1225*1031c584SApple OSS Distributions 	 * Solaris uses its ulwp_t struct for scratch space to support the pid provider.
1226*1031c584SApple OSS Distributions 	 * To mimic this, we allocate on demand scratch space. If this is the first
1227*1031c584SApple OSS Distributions 	 * time a probe has been enabled in this process, we need to allocate scratch
1228*1031c584SApple OSS Distributions 	 * space for each already existing thread. Now is a good time to do this, as
1229*1031c584SApple OSS Distributions 	 * the target process is suspended and the proc_lock is held.
1230*1031c584SApple OSS Distributions 	 */
1231*1031c584SApple OSS Distributions 	if (p->p_dtrace_ptss_pages == NULL) {
1232*1031c584SApple OSS Distributions 		dtrace_ptss_enable(p);
1233*1031c584SApple OSS Distributions 	}
1234*1031c584SApple OSS Distributions 
1235*1031c584SApple OSS Distributions 	proc_unlock(p);
1236*1031c584SApple OSS Distributions 
1237*1031c584SApple OSS Distributions 	/*
1238*1031c584SApple OSS Distributions 	 * We have to enable the trap entry point before any user threads have
1239*1031c584SApple OSS Distributions 	 * the chance to execute the trap instruction we're about to place
1240*1031c584SApple OSS Distributions 	 * in their process's text.
1241*1031c584SApple OSS Distributions 	 */
1242*1031c584SApple OSS Distributions 	fasttrap_enable_callbacks();
1243*1031c584SApple OSS Distributions 
1244*1031c584SApple OSS Distributions 	/*
1245*1031c584SApple OSS Distributions 	 * Enable all the tracepoints and add this probe's id to each
1246*1031c584SApple OSS Distributions 	 * tracepoint's list of active probes.
1247*1031c584SApple OSS Distributions 	 */
1248*1031c584SApple OSS Distributions 	for (i = 0; i < (int)probe->ftp_ntps; i++) {
1249*1031c584SApple OSS Distributions 		if ((rc = fasttrap_tracepoint_enable(p, probe, i)) != 0) {
1250*1031c584SApple OSS Distributions 			/*
1251*1031c584SApple OSS Distributions 			 * If enabling the tracepoint failed completely,
1252*1031c584SApple OSS Distributions 			 * we don't have to disable it; if the failure
1253*1031c584SApple OSS Distributions 			 * was only partial we must disable it.
1254*1031c584SApple OSS Distributions 			 */
1255*1031c584SApple OSS Distributions 			if (rc == FASTTRAP_ENABLE_FAIL)
1256*1031c584SApple OSS Distributions 				i--;
1257*1031c584SApple OSS Distributions 			else
1258*1031c584SApple OSS Distributions 				ASSERT(rc == FASTTRAP_ENABLE_PARTIAL);
1259*1031c584SApple OSS Distributions 
1260*1031c584SApple OSS Distributions 			/*
1261*1031c584SApple OSS Distributions 			 * Back up and pull out all the tracepoints we've
1262*1031c584SApple OSS Distributions 			 * created so far for this probe.
1263*1031c584SApple OSS Distributions 			 */
1264*1031c584SApple OSS Distributions 			while (i >= 0) {
1265*1031c584SApple OSS Distributions 				fasttrap_tracepoint_disable(p, probe, i);
1266*1031c584SApple OSS Distributions 				i--;
1267*1031c584SApple OSS Distributions 			}
1268*1031c584SApple OSS Distributions 
1269*1031c584SApple OSS Distributions 			sprunlock(p);
1270*1031c584SApple OSS Distributions 
1271*1031c584SApple OSS Distributions 			/*
1272*1031c584SApple OSS Distributions 			 * Since we're not actually enabling this probe,
1273*1031c584SApple OSS Distributions 			 * drop our reference on the trap table entry.
1274*1031c584SApple OSS Distributions 			 */
1275*1031c584SApple OSS Distributions 			fasttrap_disable_callbacks();
1276*1031c584SApple OSS Distributions 			return(0);
1277*1031c584SApple OSS Distributions 		}
1278*1031c584SApple OSS Distributions 	}
1279*1031c584SApple OSS Distributions 
1280*1031c584SApple OSS Distributions 	sprunlock(p);
1281*1031c584SApple OSS Distributions 
1282*1031c584SApple OSS Distributions 	probe->ftp_enabled = 1;
1283*1031c584SApple OSS Distributions 	return (0);
1284*1031c584SApple OSS Distributions }
1285*1031c584SApple OSS Distributions 
1286*1031c584SApple OSS Distributions /*ARGSUSED*/
1287*1031c584SApple OSS Distributions static void
fasttrap_pid_disable(void * arg,dtrace_id_t id,void * parg)1288*1031c584SApple OSS Distributions fasttrap_pid_disable(void *arg, dtrace_id_t id, void *parg)
1289*1031c584SApple OSS Distributions {
1290*1031c584SApple OSS Distributions #pragma unused(arg, id)
1291*1031c584SApple OSS Distributions 	fasttrap_probe_t *probe = parg;
1292*1031c584SApple OSS Distributions 	fasttrap_provider_t *provider = probe->ftp_prov;
1293*1031c584SApple OSS Distributions 	proc_t *p;
1294*1031c584SApple OSS Distributions 	int i, whack = 0;
1295*1031c584SApple OSS Distributions 
1296*1031c584SApple OSS Distributions 	ASSERT(id == probe->ftp_id);
1297*1031c584SApple OSS Distributions 
1298*1031c584SApple OSS Distributions 	/*
1299*1031c584SApple OSS Distributions 	 * We won't be able to acquire a /proc-esque lock on the process
1300*1031c584SApple OSS Distributions 	 * iff the process is dead and gone. In this case, we rely on the
1301*1031c584SApple OSS Distributions 	 * provider lock as a point of mutual exclusion to prevent other
1302*1031c584SApple OSS Distributions 	 * DTrace consumers from disabling this probe.
1303*1031c584SApple OSS Distributions 	 */
1304*1031c584SApple OSS Distributions 	p = sprlock(probe->ftp_pid);
1305*1031c584SApple OSS Distributions 
1306*1031c584SApple OSS Distributions 	lck_mtx_lock(&provider->ftp_mtx);
1307*1031c584SApple OSS Distributions 
1308*1031c584SApple OSS Distributions 	/*
1309*1031c584SApple OSS Distributions 	 * Disable all the associated tracepoints (for fully enabled probes).
1310*1031c584SApple OSS Distributions 	 */
1311*1031c584SApple OSS Distributions 	if (probe->ftp_enabled) {
1312*1031c584SApple OSS Distributions 		for (i = 0; i < (int)probe->ftp_ntps; i++) {
1313*1031c584SApple OSS Distributions 			fasttrap_tracepoint_disable(p, probe, i);
1314*1031c584SApple OSS Distributions 		}
1315*1031c584SApple OSS Distributions 	}
1316*1031c584SApple OSS Distributions 
1317*1031c584SApple OSS Distributions 	ASSERT(provider->ftp_rcount > 0);
1318*1031c584SApple OSS Distributions 	provider->ftp_rcount--;
1319*1031c584SApple OSS Distributions 
1320*1031c584SApple OSS Distributions 	if (p != NULL) {
1321*1031c584SApple OSS Distributions 		/*
1322*1031c584SApple OSS Distributions 		 * Even though we may not be able to remove it entirely, we
1323*1031c584SApple OSS Distributions 		 * mark this retired provider to get a chance to remove some
1324*1031c584SApple OSS Distributions 		 * of the associated probes.
1325*1031c584SApple OSS Distributions 		 */
1326*1031c584SApple OSS Distributions 		if (provider->ftp_retired && !provider->ftp_marked)
1327*1031c584SApple OSS Distributions 			whack = provider->ftp_marked = 1;
1328*1031c584SApple OSS Distributions 		lck_mtx_unlock(&provider->ftp_mtx);
1329*1031c584SApple OSS Distributions 
1330*1031c584SApple OSS Distributions 		sprunlock(p);
1331*1031c584SApple OSS Distributions 	} else {
1332*1031c584SApple OSS Distributions 		/*
1333*1031c584SApple OSS Distributions 		 * If the process is dead, we're just waiting for the
1334*1031c584SApple OSS Distributions 		 * last probe to be disabled to be able to free it.
1335*1031c584SApple OSS Distributions 		 */
1336*1031c584SApple OSS Distributions 		if (provider->ftp_rcount == 0 && !provider->ftp_marked)
1337*1031c584SApple OSS Distributions 			whack = provider->ftp_marked = 1;
1338*1031c584SApple OSS Distributions 		lck_mtx_unlock(&provider->ftp_mtx);
1339*1031c584SApple OSS Distributions 	}
1340*1031c584SApple OSS Distributions 
1341*1031c584SApple OSS Distributions 	if (whack) {
1342*1031c584SApple OSS Distributions 		fasttrap_pid_cleanup(FASTTRAP_CLEANUP_PROVIDER);
1343*1031c584SApple OSS Distributions 	}
1344*1031c584SApple OSS Distributions 
1345*1031c584SApple OSS Distributions 	if (!probe->ftp_enabled)
1346*1031c584SApple OSS Distributions 		return;
1347*1031c584SApple OSS Distributions 
1348*1031c584SApple OSS Distributions 	probe->ftp_enabled = 0;
1349*1031c584SApple OSS Distributions 
1350*1031c584SApple OSS Distributions 	// ASSERT(MUTEX_HELD(&cpu_lock));
1351*1031c584SApple OSS Distributions 	fasttrap_disable_callbacks();
1352*1031c584SApple OSS Distributions }
1353*1031c584SApple OSS Distributions 
1354*1031c584SApple OSS Distributions /*ARGSUSED*/
1355*1031c584SApple OSS Distributions static void
fasttrap_pid_getargdesc(void * arg,dtrace_id_t id,void * parg,dtrace_argdesc_t * desc)1356*1031c584SApple OSS Distributions fasttrap_pid_getargdesc(void *arg, dtrace_id_t id, void *parg,
1357*1031c584SApple OSS Distributions     dtrace_argdesc_t *desc)
1358*1031c584SApple OSS Distributions {
1359*1031c584SApple OSS Distributions #pragma unused(arg, id)
1360*1031c584SApple OSS Distributions 	fasttrap_probe_t *probe = parg;
1361*1031c584SApple OSS Distributions 	char *str;
1362*1031c584SApple OSS Distributions 	int i, ndx;
1363*1031c584SApple OSS Distributions 
1364*1031c584SApple OSS Distributions 	desc->dtargd_native[0] = '\0';
1365*1031c584SApple OSS Distributions 	desc->dtargd_xlate[0] = '\0';
1366*1031c584SApple OSS Distributions 
1367*1031c584SApple OSS Distributions 	if (probe->ftp_prov->ftp_retired != 0 ||
1368*1031c584SApple OSS Distributions 		desc->dtargd_ndx < 0 ||
1369*1031c584SApple OSS Distributions 	    desc->dtargd_ndx >= probe->ftp_nargs) {
1370*1031c584SApple OSS Distributions 		desc->dtargd_ndx = DTRACE_ARGNONE;
1371*1031c584SApple OSS Distributions 		return;
1372*1031c584SApple OSS Distributions 	}
1373*1031c584SApple OSS Distributions 
1374*1031c584SApple OSS Distributions 	ndx = (probe->ftp_argmap != NULL) ?
1375*1031c584SApple OSS Distributions 		probe->ftp_argmap[desc->dtargd_ndx] : desc->dtargd_ndx;
1376*1031c584SApple OSS Distributions 
1377*1031c584SApple OSS Distributions 	if (probe->ftp_ntypes == NULL)
1378*1031c584SApple OSS Distributions 		return;
1379*1031c584SApple OSS Distributions 
1380*1031c584SApple OSS Distributions 	str = probe->ftp_ntypes;
1381*1031c584SApple OSS Distributions 	for (i = 0; i < ndx; i++) {
1382*1031c584SApple OSS Distributions 		str += strlen(str) + 1;
1383*1031c584SApple OSS Distributions 	}
1384*1031c584SApple OSS Distributions 
1385*1031c584SApple OSS Distributions 	(void) strlcpy(desc->dtargd_native, str, sizeof(desc->dtargd_native));
1386*1031c584SApple OSS Distributions 
1387*1031c584SApple OSS Distributions 	if (probe->ftp_xtypes == NULL)
1388*1031c584SApple OSS Distributions 		return;
1389*1031c584SApple OSS Distributions 
1390*1031c584SApple OSS Distributions 	str = probe->ftp_xtypes;
1391*1031c584SApple OSS Distributions 	for (i = 0; i < desc->dtargd_ndx; i++) {
1392*1031c584SApple OSS Distributions 		str += strlen(str) + 1;
1393*1031c584SApple OSS Distributions 	}
1394*1031c584SApple OSS Distributions 
1395*1031c584SApple OSS Distributions 	(void) strlcpy(desc->dtargd_xlate, str, sizeof(desc->dtargd_xlate));
1396*1031c584SApple OSS Distributions }
1397*1031c584SApple OSS Distributions 
1398*1031c584SApple OSS Distributions /*ARGSUSED*/
1399*1031c584SApple OSS Distributions static void
fasttrap_pid_destroy(void * arg,dtrace_id_t id,void * parg)1400*1031c584SApple OSS Distributions fasttrap_pid_destroy(void *arg, dtrace_id_t id, void *parg)
1401*1031c584SApple OSS Distributions {
1402*1031c584SApple OSS Distributions #pragma unused(arg, id)
1403*1031c584SApple OSS Distributions 	fasttrap_probe_t *probe = parg;
1404*1031c584SApple OSS Distributions 	unsigned int i;
1405*1031c584SApple OSS Distributions 
1406*1031c584SApple OSS Distributions 	ASSERT(probe != NULL);
1407*1031c584SApple OSS Distributions 	ASSERT(!probe->ftp_enabled);
1408*1031c584SApple OSS Distributions 	ASSERT(fasttrap_total >= probe->ftp_ntps);
1409*1031c584SApple OSS Distributions 
1410*1031c584SApple OSS Distributions 	os_atomic_sub(&fasttrap_total, probe->ftp_ntps, relaxed);
1411*1031c584SApple OSS Distributions 	os_atomic_sub(&fasttrap_retired, probe->ftp_ntps, relaxed);
1412*1031c584SApple OSS Distributions 
1413*1031c584SApple OSS Distributions 	if (probe->ftp_gen + 1 >= fasttrap_mod_gen)
1414*1031c584SApple OSS Distributions 		fasttrap_mod_barrier(probe->ftp_gen);
1415*1031c584SApple OSS Distributions 
1416*1031c584SApple OSS Distributions 	for (i = 0; i < probe->ftp_ntps; i++) {
1417*1031c584SApple OSS Distributions 		zfree(fasttrap_tracepoint_t_zone, probe->ftp_tps[i].fit_tp);
1418*1031c584SApple OSS Distributions 	}
1419*1031c584SApple OSS Distributions 
1420*1031c584SApple OSS Distributions 	if (probe->ftp_ntps < FASTTRAP_PROBE_T_ZONE_MAX_TRACEPOINTS) {
1421*1031c584SApple OSS Distributions 		zfree(fasttrap_probe_t_zones[probe->ftp_ntps], probe);
1422*1031c584SApple OSS Distributions 	} else {
1423*1031c584SApple OSS Distributions 		size_t size = offsetof(fasttrap_probe_t, ftp_tps[probe->ftp_ntps]);
1424*1031c584SApple OSS Distributions 		kmem_free(probe, size);
1425*1031c584SApple OSS Distributions 	}
1426*1031c584SApple OSS Distributions }
1427*1031c584SApple OSS Distributions 
1428*1031c584SApple OSS Distributions 
1429*1031c584SApple OSS Distributions static const dtrace_pattr_t pid_attr = {
1430*1031c584SApple OSS Distributions { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
1431*1031c584SApple OSS Distributions { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
1432*1031c584SApple OSS Distributions { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
1433*1031c584SApple OSS Distributions { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
1434*1031c584SApple OSS Distributions { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
1435*1031c584SApple OSS Distributions };
1436*1031c584SApple OSS Distributions 
1437*1031c584SApple OSS Distributions static dtrace_pops_t pid_pops = {
1438*1031c584SApple OSS Distributions 	.dtps_provide =		fasttrap_pid_provide,
1439*1031c584SApple OSS Distributions 	.dtps_provide_module =	NULL,
1440*1031c584SApple OSS Distributions 	.dtps_enable =		fasttrap_pid_enable,
1441*1031c584SApple OSS Distributions 	.dtps_disable =		fasttrap_pid_disable,
1442*1031c584SApple OSS Distributions 	.dtps_suspend =		NULL,
1443*1031c584SApple OSS Distributions 	.dtps_resume =		NULL,
1444*1031c584SApple OSS Distributions 	.dtps_getargdesc =	fasttrap_pid_getargdesc,
1445*1031c584SApple OSS Distributions 	.dtps_getargval =	fasttrap_pid_getarg,
1446*1031c584SApple OSS Distributions 	.dtps_usermode =	NULL,
1447*1031c584SApple OSS Distributions 	.dtps_destroy =		fasttrap_pid_destroy
1448*1031c584SApple OSS Distributions };
1449*1031c584SApple OSS Distributions 
1450*1031c584SApple OSS Distributions static dtrace_pops_t usdt_pops = {
1451*1031c584SApple OSS Distributions 	.dtps_provide =		fasttrap_pid_provide,
1452*1031c584SApple OSS Distributions 	.dtps_provide_module =	NULL,
1453*1031c584SApple OSS Distributions 	.dtps_enable =		fasttrap_pid_enable,
1454*1031c584SApple OSS Distributions 	.dtps_disable =		fasttrap_pid_disable,
1455*1031c584SApple OSS Distributions 	.dtps_suspend =		NULL,
1456*1031c584SApple OSS Distributions 	.dtps_resume =		NULL,
1457*1031c584SApple OSS Distributions 	.dtps_getargdesc =	fasttrap_pid_getargdesc,
1458*1031c584SApple OSS Distributions 	.dtps_getargval =	fasttrap_usdt_getarg,
1459*1031c584SApple OSS Distributions 	.dtps_usermode =	NULL,
1460*1031c584SApple OSS Distributions 	.dtps_destroy =		fasttrap_pid_destroy
1461*1031c584SApple OSS Distributions };
1462*1031c584SApple OSS Distributions 
1463*1031c584SApple OSS Distributions static fasttrap_proc_t *
fasttrap_proc_lookup(pid_t pid)1464*1031c584SApple OSS Distributions fasttrap_proc_lookup(pid_t pid)
1465*1031c584SApple OSS Distributions {
1466*1031c584SApple OSS Distributions 	fasttrap_bucket_t *bucket;
1467*1031c584SApple OSS Distributions 	fasttrap_proc_t *fprc, *new_fprc;
1468*1031c584SApple OSS Distributions 
1469*1031c584SApple OSS Distributions 	bucket = &fasttrap_procs.fth_table[FASTTRAP_PROCS_INDEX(pid)];
1470*1031c584SApple OSS Distributions 	lck_mtx_lock(&bucket->ftb_mtx);
1471*1031c584SApple OSS Distributions 
1472*1031c584SApple OSS Distributions 	for (fprc = bucket->ftb_data; fprc != NULL; fprc = fprc->ftpc_next) {
1473*1031c584SApple OSS Distributions 		if (fprc->ftpc_pid == pid && fprc->ftpc_acount != 0) {
1474*1031c584SApple OSS Distributions 			lck_mtx_lock(&fprc->ftpc_mtx);
1475*1031c584SApple OSS Distributions 			lck_mtx_unlock(&bucket->ftb_mtx);
1476*1031c584SApple OSS Distributions 			fprc->ftpc_rcount++;
1477*1031c584SApple OSS Distributions 			os_atomic_inc(&fprc->ftpc_acount, relaxed);
1478*1031c584SApple OSS Distributions 			ASSERT(fprc->ftpc_acount <= fprc->ftpc_rcount);
1479*1031c584SApple OSS Distributions 			lck_mtx_unlock(&fprc->ftpc_mtx);
1480*1031c584SApple OSS Distributions 
1481*1031c584SApple OSS Distributions 			return (fprc);
1482*1031c584SApple OSS Distributions 		}
1483*1031c584SApple OSS Distributions 	}
1484*1031c584SApple OSS Distributions 
1485*1031c584SApple OSS Distributions 	/*
1486*1031c584SApple OSS Distributions 	 * Drop the bucket lock so we don't try to perform a sleeping
1487*1031c584SApple OSS Distributions 	 * allocation under it.
1488*1031c584SApple OSS Distributions 	 */
1489*1031c584SApple OSS Distributions 	lck_mtx_unlock(&bucket->ftb_mtx);
1490*1031c584SApple OSS Distributions 
1491*1031c584SApple OSS Distributions 	new_fprc = kmem_zalloc(sizeof (fasttrap_proc_t), KM_SLEEP);
1492*1031c584SApple OSS Distributions 	ASSERT(new_fprc != NULL);
1493*1031c584SApple OSS Distributions 	new_fprc->ftpc_pid = pid;
1494*1031c584SApple OSS Distributions 	new_fprc->ftpc_rcount = 1;
1495*1031c584SApple OSS Distributions 	new_fprc->ftpc_acount = 1;
1496*1031c584SApple OSS Distributions 
1497*1031c584SApple OSS Distributions 	lck_mtx_lock(&bucket->ftb_mtx);
1498*1031c584SApple OSS Distributions 
1499*1031c584SApple OSS Distributions 	/*
1500*1031c584SApple OSS Distributions 	 * Take another lap through the list to make sure a proc hasn't
1501*1031c584SApple OSS Distributions 	 * been created for this pid while we weren't under the bucket lock.
1502*1031c584SApple OSS Distributions 	 */
1503*1031c584SApple OSS Distributions 	for (fprc = bucket->ftb_data; fprc != NULL; fprc = fprc->ftpc_next) {
1504*1031c584SApple OSS Distributions 		if (fprc->ftpc_pid == pid && fprc->ftpc_acount != 0) {
1505*1031c584SApple OSS Distributions 			lck_mtx_lock(&fprc->ftpc_mtx);
1506*1031c584SApple OSS Distributions 			lck_mtx_unlock(&bucket->ftb_mtx);
1507*1031c584SApple OSS Distributions 			fprc->ftpc_rcount++;
1508*1031c584SApple OSS Distributions 			os_atomic_inc(&fprc->ftpc_acount, relaxed);
1509*1031c584SApple OSS Distributions 			ASSERT(fprc->ftpc_acount <= fprc->ftpc_rcount);
1510*1031c584SApple OSS Distributions 			lck_mtx_unlock(&fprc->ftpc_mtx);
1511*1031c584SApple OSS Distributions 
1512*1031c584SApple OSS Distributions 			kmem_free(new_fprc, sizeof (fasttrap_proc_t));
1513*1031c584SApple OSS Distributions 
1514*1031c584SApple OSS Distributions 			return (fprc);
1515*1031c584SApple OSS Distributions 		}
1516*1031c584SApple OSS Distributions 	}
1517*1031c584SApple OSS Distributions 
1518*1031c584SApple OSS Distributions 	/*
1519*1031c584SApple OSS Distributions 	 * APPLE NOTE: We have to initialize all locks explicitly
1520*1031c584SApple OSS Distributions 	 */
1521*1031c584SApple OSS Distributions 	lck_mtx_init(&new_fprc->ftpc_mtx, &fasttrap_lck_grp, &fasttrap_lck_attr);
1522*1031c584SApple OSS Distributions 
1523*1031c584SApple OSS Distributions 	new_fprc->ftpc_next = bucket->ftb_data;
1524*1031c584SApple OSS Distributions 	bucket->ftb_data = new_fprc;
1525*1031c584SApple OSS Distributions 
1526*1031c584SApple OSS Distributions 	lck_mtx_unlock(&bucket->ftb_mtx);
1527*1031c584SApple OSS Distributions 
1528*1031c584SApple OSS Distributions 	return (new_fprc);
1529*1031c584SApple OSS Distributions }
1530*1031c584SApple OSS Distributions 
1531*1031c584SApple OSS Distributions static void
fasttrap_proc_release(fasttrap_proc_t * proc)1532*1031c584SApple OSS Distributions fasttrap_proc_release(fasttrap_proc_t *proc)
1533*1031c584SApple OSS Distributions {
1534*1031c584SApple OSS Distributions 	fasttrap_bucket_t *bucket;
1535*1031c584SApple OSS Distributions 	fasttrap_proc_t *fprc, **fprcp;
1536*1031c584SApple OSS Distributions 	pid_t pid = proc->ftpc_pid;
1537*1031c584SApple OSS Distributions 
1538*1031c584SApple OSS Distributions 	lck_mtx_lock(&proc->ftpc_mtx);
1539*1031c584SApple OSS Distributions 
1540*1031c584SApple OSS Distributions 	ASSERT(proc->ftpc_rcount != 0);
1541*1031c584SApple OSS Distributions 	ASSERT(proc->ftpc_acount <= proc->ftpc_rcount);
1542*1031c584SApple OSS Distributions 
1543*1031c584SApple OSS Distributions 	if (--proc->ftpc_rcount != 0) {
1544*1031c584SApple OSS Distributions 		lck_mtx_unlock(&proc->ftpc_mtx);
1545*1031c584SApple OSS Distributions 		return;
1546*1031c584SApple OSS Distributions 	}
1547*1031c584SApple OSS Distributions 
1548*1031c584SApple OSS Distributions 	lck_mtx_unlock(&proc->ftpc_mtx);
1549*1031c584SApple OSS Distributions 
1550*1031c584SApple OSS Distributions 	/*
1551*1031c584SApple OSS Distributions 	 * There should definitely be no live providers associated with this
1552*1031c584SApple OSS Distributions 	 * process at this point.
1553*1031c584SApple OSS Distributions 	 */
1554*1031c584SApple OSS Distributions 	 ASSERT(proc->ftpc_acount == 0);
1555*1031c584SApple OSS Distributions 
1556*1031c584SApple OSS Distributions 	bucket = &fasttrap_procs.fth_table[FASTTRAP_PROCS_INDEX(pid)];
1557*1031c584SApple OSS Distributions 	lck_mtx_lock(&bucket->ftb_mtx);
1558*1031c584SApple OSS Distributions 
1559*1031c584SApple OSS Distributions 	fprcp = (fasttrap_proc_t **)&bucket->ftb_data;
1560*1031c584SApple OSS Distributions 	while ((fprc = *fprcp) != NULL) {
1561*1031c584SApple OSS Distributions 		if (fprc == proc)
1562*1031c584SApple OSS Distributions 			break;
1563*1031c584SApple OSS Distributions 
1564*1031c584SApple OSS Distributions 		fprcp = &fprc->ftpc_next;
1565*1031c584SApple OSS Distributions 	}
1566*1031c584SApple OSS Distributions 
1567*1031c584SApple OSS Distributions 	/*
1568*1031c584SApple OSS Distributions 	 * Something strange has happened if we can't find the proc.
1569*1031c584SApple OSS Distributions 	 */
1570*1031c584SApple OSS Distributions 	ASSERT(fprc != NULL);
1571*1031c584SApple OSS Distributions 
1572*1031c584SApple OSS Distributions 	*fprcp = fprc->ftpc_next;
1573*1031c584SApple OSS Distributions 
1574*1031c584SApple OSS Distributions 	lck_mtx_unlock(&bucket->ftb_mtx);
1575*1031c584SApple OSS Distributions 
1576*1031c584SApple OSS Distributions 	/*
1577*1031c584SApple OSS Distributions 	 * APPLE NOTE: explicit lock management. Not 100% certain we need this, the
1578*1031c584SApple OSS Distributions 	 * memory is freed even without the destroy. Maybe accounting cleanup?
1579*1031c584SApple OSS Distributions 	 */
1580*1031c584SApple OSS Distributions 	lck_mtx_destroy(&fprc->ftpc_mtx, &fasttrap_lck_grp);
1581*1031c584SApple OSS Distributions 
1582*1031c584SApple OSS Distributions 	kmem_free(fprc, sizeof (fasttrap_proc_t));
1583*1031c584SApple OSS Distributions }
1584*1031c584SApple OSS Distributions 
1585*1031c584SApple OSS Distributions /*
1586*1031c584SApple OSS Distributions  * Lookup a fasttrap-managed provider based on its name and associated proc.
1587*1031c584SApple OSS Distributions  * A reference to the proc must be held for the duration of the call.
1588*1031c584SApple OSS Distributions  * If the pattr argument is non-NULL, this function instantiates the provider
1589*1031c584SApple OSS Distributions  * if it doesn't exist otherwise it returns NULL. The provider is returned
1590*1031c584SApple OSS Distributions  * with its lock held.
1591*1031c584SApple OSS Distributions  */
1592*1031c584SApple OSS Distributions static fasttrap_provider_t *
fasttrap_provider_lookup(proc_t * p,fasttrap_provider_type_t provider_type,const char * name,const dtrace_pattr_t * pattr)1593*1031c584SApple OSS Distributions fasttrap_provider_lookup(proc_t *p, fasttrap_provider_type_t provider_type, const char *name,
1594*1031c584SApple OSS Distributions     const dtrace_pattr_t *pattr)
1595*1031c584SApple OSS Distributions {
1596*1031c584SApple OSS Distributions 	pid_t pid = proc_getpid(p);
1597*1031c584SApple OSS Distributions 	fasttrap_provider_t *fp, *new_fp = NULL;
1598*1031c584SApple OSS Distributions 	fasttrap_bucket_t *bucket;
1599*1031c584SApple OSS Distributions 	char provname[DTRACE_PROVNAMELEN];
1600*1031c584SApple OSS Distributions 	cred_t *cred;
1601*1031c584SApple OSS Distributions 
1602*1031c584SApple OSS Distributions 	ASSERT(strlen(name) < sizeof (fp->ftp_name));
1603*1031c584SApple OSS Distributions 	ASSERT(pattr != NULL);
1604*1031c584SApple OSS Distributions 
1605*1031c584SApple OSS Distributions 	bucket = &fasttrap_provs.fth_table[FASTTRAP_PROVS_INDEX(pid, name)];
1606*1031c584SApple OSS Distributions 	lck_mtx_lock(&bucket->ftb_mtx);
1607*1031c584SApple OSS Distributions 
1608*1031c584SApple OSS Distributions 	/*
1609*1031c584SApple OSS Distributions 	 * Take a lap through the list and return the match if we find it.
1610*1031c584SApple OSS Distributions 	 */
1611*1031c584SApple OSS Distributions 	for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) {
1612*1031c584SApple OSS Distributions 		if (fp->ftp_pid == pid &&
1613*1031c584SApple OSS Distributions 		    fp->ftp_provider_type == provider_type &&
1614*1031c584SApple OSS Distributions 		    strncmp(fp->ftp_name, name, sizeof(fp->ftp_name)) == 0 &&
1615*1031c584SApple OSS Distributions 		    !fp->ftp_retired) {
1616*1031c584SApple OSS Distributions 			lck_mtx_lock(&fp->ftp_mtx);
1617*1031c584SApple OSS Distributions 			lck_mtx_unlock(&bucket->ftb_mtx);
1618*1031c584SApple OSS Distributions 			return (fp);
1619*1031c584SApple OSS Distributions 		}
1620*1031c584SApple OSS Distributions 	}
1621*1031c584SApple OSS Distributions 
1622*1031c584SApple OSS Distributions 	/*
1623*1031c584SApple OSS Distributions 	 * Drop the bucket lock so we don't try to perform a sleeping
1624*1031c584SApple OSS Distributions 	 * allocation under it.
1625*1031c584SApple OSS Distributions 	 */
1626*1031c584SApple OSS Distributions 	lck_mtx_unlock(&bucket->ftb_mtx);
1627*1031c584SApple OSS Distributions 
1628*1031c584SApple OSS Distributions 	/*
1629*1031c584SApple OSS Distributions 	 * Make sure the process isn't a child
1630*1031c584SApple OSS Distributions 	 * isn't a zombie (but may be in fork).
1631*1031c584SApple OSS Distributions 	 */
1632*1031c584SApple OSS Distributions 	proc_lock(p);
1633*1031c584SApple OSS Distributions 	if (p->p_lflag & P_LEXIT) {
1634*1031c584SApple OSS Distributions 		proc_unlock(p);
1635*1031c584SApple OSS Distributions 		return (NULL);
1636*1031c584SApple OSS Distributions 	}
1637*1031c584SApple OSS Distributions 
1638*1031c584SApple OSS Distributions 	/*
1639*1031c584SApple OSS Distributions 	 * Increment p_dtrace_probes so that the process knows to inform us
1640*1031c584SApple OSS Distributions 	 * when it exits or execs. fasttrap_provider_free() decrements this
1641*1031c584SApple OSS Distributions 	 * when we're done with this provider.
1642*1031c584SApple OSS Distributions 	 */
1643*1031c584SApple OSS Distributions 	p->p_dtrace_probes++;
1644*1031c584SApple OSS Distributions 
1645*1031c584SApple OSS Distributions 	/*
1646*1031c584SApple OSS Distributions 	 * Grab the credentials for this process so we have
1647*1031c584SApple OSS Distributions 	 * something to pass to dtrace_register().
1648*1031c584SApple OSS Distributions 	 * APPLE NOTE:  We have no equivalent to crhold,
1649*1031c584SApple OSS Distributions 	 * even though there is a cr_ref filed in ucred.
1650*1031c584SApple OSS Distributions 	 */
1651*1031c584SApple OSS Distributions 	cred = kauth_cred_proc_ref(p);
1652*1031c584SApple OSS Distributions 	proc_unlock(p);
1653*1031c584SApple OSS Distributions 
1654*1031c584SApple OSS Distributions 	new_fp = kmem_zalloc(sizeof (fasttrap_provider_t), KM_SLEEP);
1655*1031c584SApple OSS Distributions 	ASSERT(new_fp != NULL);
1656*1031c584SApple OSS Distributions 	new_fp->ftp_pid = proc_getpid(p);
1657*1031c584SApple OSS Distributions 	new_fp->ftp_proc = fasttrap_proc_lookup(pid);
1658*1031c584SApple OSS Distributions 	new_fp->ftp_provider_type = provider_type;
1659*1031c584SApple OSS Distributions 
1660*1031c584SApple OSS Distributions 	/*
1661*1031c584SApple OSS Distributions 	 * APPLE NOTE:  locks require explicit init
1662*1031c584SApple OSS Distributions 	 */
1663*1031c584SApple OSS Distributions 	lck_mtx_init(&new_fp->ftp_mtx, &fasttrap_lck_grp, &fasttrap_lck_attr);
1664*1031c584SApple OSS Distributions 	lck_mtx_init(&new_fp->ftp_cmtx, &fasttrap_lck_grp, &fasttrap_lck_attr);
1665*1031c584SApple OSS Distributions 
1666*1031c584SApple OSS Distributions 	ASSERT(new_fp->ftp_proc != NULL);
1667*1031c584SApple OSS Distributions 
1668*1031c584SApple OSS Distributions 	lck_mtx_lock(&bucket->ftb_mtx);
1669*1031c584SApple OSS Distributions 
1670*1031c584SApple OSS Distributions 	/*
1671*1031c584SApple OSS Distributions 	 * Take another lap through the list to make sure a provider hasn't
1672*1031c584SApple OSS Distributions 	 * been created for this pid while we weren't under the bucket lock.
1673*1031c584SApple OSS Distributions 	 */
1674*1031c584SApple OSS Distributions 	for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) {
1675*1031c584SApple OSS Distributions 		if (fp->ftp_pid == pid && strncmp(fp->ftp_name, name, sizeof(fp->ftp_name)) == 0 &&
1676*1031c584SApple OSS Distributions 		    !fp->ftp_retired) {
1677*1031c584SApple OSS Distributions 			lck_mtx_lock(&fp->ftp_mtx);
1678*1031c584SApple OSS Distributions 			lck_mtx_unlock(&bucket->ftb_mtx);
1679*1031c584SApple OSS Distributions 			fasttrap_provider_free(new_fp);
1680*1031c584SApple OSS Distributions 			kauth_cred_unref(&cred);
1681*1031c584SApple OSS Distributions 			return (fp);
1682*1031c584SApple OSS Distributions 		}
1683*1031c584SApple OSS Distributions 	}
1684*1031c584SApple OSS Distributions 
1685*1031c584SApple OSS Distributions 	(void) strlcpy(new_fp->ftp_name, name, sizeof(new_fp->ftp_name));
1686*1031c584SApple OSS Distributions 
1687*1031c584SApple OSS Distributions 	/*
1688*1031c584SApple OSS Distributions 	 * Fail and return NULL if either the provider name is too long
1689*1031c584SApple OSS Distributions 	 * or we fail to register this new provider with the DTrace
1690*1031c584SApple OSS Distributions 	 * framework. Note that this is the only place we ever construct
1691*1031c584SApple OSS Distributions 	 * the full provider name -- we keep it in pieces in the provider
1692*1031c584SApple OSS Distributions 	 * structure.
1693*1031c584SApple OSS Distributions 	 */
1694*1031c584SApple OSS Distributions 	if (snprintf(provname, sizeof (provname), "%s%u", name, (uint_t)pid) >=
1695*1031c584SApple OSS Distributions 	    (int)sizeof (provname) ||
1696*1031c584SApple OSS Distributions 	    dtrace_register(provname, pattr,
1697*1031c584SApple OSS Distributions 	    DTRACE_PRIV_PROC | DTRACE_PRIV_OWNER | DTRACE_PRIV_ZONEOWNER, cred,
1698*1031c584SApple OSS Distributions 	    pattr == &pid_attr ? &pid_pops : &usdt_pops, new_fp,
1699*1031c584SApple OSS Distributions 	    &new_fp->ftp_provid) != 0) {
1700*1031c584SApple OSS Distributions 		lck_mtx_unlock(&bucket->ftb_mtx);
1701*1031c584SApple OSS Distributions 		fasttrap_provider_free(new_fp);
1702*1031c584SApple OSS Distributions 		kauth_cred_unref(&cred);
1703*1031c584SApple OSS Distributions 		return (NULL);
1704*1031c584SApple OSS Distributions 	}
1705*1031c584SApple OSS Distributions 
1706*1031c584SApple OSS Distributions 	new_fp->ftp_next = bucket->ftb_data;
1707*1031c584SApple OSS Distributions 	bucket->ftb_data = new_fp;
1708*1031c584SApple OSS Distributions 
1709*1031c584SApple OSS Distributions 	lck_mtx_lock(&new_fp->ftp_mtx);
1710*1031c584SApple OSS Distributions 	lck_mtx_unlock(&bucket->ftb_mtx);
1711*1031c584SApple OSS Distributions 
1712*1031c584SApple OSS Distributions 	kauth_cred_unref(&cred);
1713*1031c584SApple OSS Distributions 
1714*1031c584SApple OSS Distributions 	return (new_fp);
1715*1031c584SApple OSS Distributions }
1716*1031c584SApple OSS Distributions 
1717*1031c584SApple OSS Distributions static void
fasttrap_provider_free(fasttrap_provider_t * provider)1718*1031c584SApple OSS Distributions fasttrap_provider_free(fasttrap_provider_t *provider)
1719*1031c584SApple OSS Distributions {
1720*1031c584SApple OSS Distributions 	pid_t pid = provider->ftp_pid;
1721*1031c584SApple OSS Distributions 	proc_t *p;
1722*1031c584SApple OSS Distributions 
1723*1031c584SApple OSS Distributions 	/*
1724*1031c584SApple OSS Distributions 	 * There need to be no associated enabled probes, no consumers
1725*1031c584SApple OSS Distributions 	 * creating probes, and no meta providers referencing this provider.
1726*1031c584SApple OSS Distributions 	 */
1727*1031c584SApple OSS Distributions 	ASSERT(provider->ftp_rcount == 0);
1728*1031c584SApple OSS Distributions 	ASSERT(provider->ftp_ccount == 0);
1729*1031c584SApple OSS Distributions 	ASSERT(provider->ftp_mcount == 0);
1730*1031c584SApple OSS Distributions 
1731*1031c584SApple OSS Distributions 	/*
1732*1031c584SApple OSS Distributions 	 * If this provider hasn't been retired, we need to explicitly drop the
1733*1031c584SApple OSS Distributions 	 * count of active providers on the associated process structure.
1734*1031c584SApple OSS Distributions 	 */
1735*1031c584SApple OSS Distributions 	if (!provider->ftp_retired) {
1736*1031c584SApple OSS Distributions 		os_atomic_dec(&provider->ftp_proc->ftpc_acount, relaxed);
1737*1031c584SApple OSS Distributions 		ASSERT(provider->ftp_proc->ftpc_acount <
1738*1031c584SApple OSS Distributions 		provider->ftp_proc->ftpc_rcount);
1739*1031c584SApple OSS Distributions 	}
1740*1031c584SApple OSS Distributions 
1741*1031c584SApple OSS Distributions 	fasttrap_proc_release(provider->ftp_proc);
1742*1031c584SApple OSS Distributions 
1743*1031c584SApple OSS Distributions 	/*
1744*1031c584SApple OSS Distributions 	 * APPLE NOTE:  explicit lock management. Not 100% certain we need this, the
1745*1031c584SApple OSS Distributions 	 * memory is freed even without the destroy. Maybe accounting cleanup?
1746*1031c584SApple OSS Distributions 	 */
1747*1031c584SApple OSS Distributions 	lck_mtx_destroy(&provider->ftp_mtx, &fasttrap_lck_grp);
1748*1031c584SApple OSS Distributions 	lck_mtx_destroy(&provider->ftp_cmtx, &fasttrap_lck_grp);
1749*1031c584SApple OSS Distributions 
1750*1031c584SApple OSS Distributions 	kmem_free(provider, sizeof (fasttrap_provider_t));
1751*1031c584SApple OSS Distributions 
1752*1031c584SApple OSS Distributions 	/*
1753*1031c584SApple OSS Distributions 	 * Decrement p_dtrace_probes on the process whose provider we're
1754*1031c584SApple OSS Distributions 	 * freeing. We don't have to worry about clobbering somone else's
1755*1031c584SApple OSS Distributions 	 * modifications to it because we have locked the bucket that
1756*1031c584SApple OSS Distributions 	 * corresponds to this process's hash chain in the provider hash
1757*1031c584SApple OSS Distributions 	 * table. Don't sweat it if we can't find the process.
1758*1031c584SApple OSS Distributions 	 */
1759*1031c584SApple OSS Distributions 	if ((p = proc_find(pid)) == NULL) {
1760*1031c584SApple OSS Distributions 		return;
1761*1031c584SApple OSS Distributions 	}
1762*1031c584SApple OSS Distributions 
1763*1031c584SApple OSS Distributions 	proc_lock(p);
1764*1031c584SApple OSS Distributions 	p->p_dtrace_probes--;
1765*1031c584SApple OSS Distributions 	proc_unlock(p);
1766*1031c584SApple OSS Distributions 
1767*1031c584SApple OSS Distributions 	proc_rele(p);
1768*1031c584SApple OSS Distributions }
1769*1031c584SApple OSS Distributions 
1770*1031c584SApple OSS Distributions static void
fasttrap_provider_retire(proc_t * p,const char * name,int mprov)1771*1031c584SApple OSS Distributions fasttrap_provider_retire(proc_t *p, const char *name, int mprov)
1772*1031c584SApple OSS Distributions {
1773*1031c584SApple OSS Distributions 	fasttrap_provider_t *fp;
1774*1031c584SApple OSS Distributions 	fasttrap_bucket_t *bucket;
1775*1031c584SApple OSS Distributions 	dtrace_provider_id_t provid;
1776*1031c584SApple OSS Distributions 	ASSERT(strlen(name) < sizeof (fp->ftp_name));
1777*1031c584SApple OSS Distributions 
1778*1031c584SApple OSS Distributions 	bucket = &fasttrap_provs.fth_table[FASTTRAP_PROVS_INDEX(proc_getpid(p), name)];
1779*1031c584SApple OSS Distributions 	lck_mtx_lock(&bucket->ftb_mtx);
1780*1031c584SApple OSS Distributions 
1781*1031c584SApple OSS Distributions 	for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) {
1782*1031c584SApple OSS Distributions 		if (fp->ftp_pid == proc_getpid(p) && strncmp(fp->ftp_name, name, sizeof(fp->ftp_name)) == 0 &&
1783*1031c584SApple OSS Distributions 		    !fp->ftp_retired)
1784*1031c584SApple OSS Distributions 			break;
1785*1031c584SApple OSS Distributions 	}
1786*1031c584SApple OSS Distributions 
1787*1031c584SApple OSS Distributions 	if (fp == NULL) {
1788*1031c584SApple OSS Distributions 		lck_mtx_unlock(&bucket->ftb_mtx);
1789*1031c584SApple OSS Distributions 		return;
1790*1031c584SApple OSS Distributions 	}
1791*1031c584SApple OSS Distributions 
1792*1031c584SApple OSS Distributions 	lck_mtx_lock(&fp->ftp_mtx);
1793*1031c584SApple OSS Distributions 	ASSERT(!mprov || fp->ftp_mcount > 0);
1794*1031c584SApple OSS Distributions 	if (mprov && --fp->ftp_mcount != 0)  {
1795*1031c584SApple OSS Distributions 		lck_mtx_unlock(&fp->ftp_mtx);
1796*1031c584SApple OSS Distributions 		lck_mtx_unlock(&bucket->ftb_mtx);
1797*1031c584SApple OSS Distributions 		return;
1798*1031c584SApple OSS Distributions 	}
1799*1031c584SApple OSS Distributions 
1800*1031c584SApple OSS Distributions 	/*
1801*1031c584SApple OSS Distributions 	 * Mark the provider to be removed in our post-processing step, mark it
1802*1031c584SApple OSS Distributions 	 * retired, and drop the active count on its proc. Marking it indicates
1803*1031c584SApple OSS Distributions 	 * that we should try to remove it; setting the retired flag indicates
1804*1031c584SApple OSS Distributions 	 * that we're done with this provider; dropping the active the proc
1805*1031c584SApple OSS Distributions 	 * releases our hold, and when this reaches zero (as it will during
1806*1031c584SApple OSS Distributions 	 * exit or exec) the proc and associated providers become defunct.
1807*1031c584SApple OSS Distributions 	 *
1808*1031c584SApple OSS Distributions 	 * We obviously need to take the bucket lock before the provider lock
1809*1031c584SApple OSS Distributions 	 * to perform the lookup, but we need to drop the provider lock
1810*1031c584SApple OSS Distributions 	 * before calling into the DTrace framework since we acquire the
1811*1031c584SApple OSS Distributions 	 * provider lock in callbacks invoked from the DTrace framework. The
1812*1031c584SApple OSS Distributions 	 * bucket lock therefore protects the integrity of the provider hash
1813*1031c584SApple OSS Distributions 	 * table.
1814*1031c584SApple OSS Distributions 	 */
1815*1031c584SApple OSS Distributions 	os_atomic_dec(&fp->ftp_proc->ftpc_acount, relaxed);
1816*1031c584SApple OSS Distributions 	ASSERT(fp->ftp_proc->ftpc_acount < fp->ftp_proc->ftpc_rcount);
1817*1031c584SApple OSS Distributions 
1818*1031c584SApple OSS Distributions 	/*
1819*1031c584SApple OSS Distributions 	 * Add this provider probes to the retired count and
1820*1031c584SApple OSS Distributions 	 * make sure we don't add them twice
1821*1031c584SApple OSS Distributions 	 */
1822*1031c584SApple OSS Distributions 	os_atomic_add(&fasttrap_retired, fp->ftp_pcount, relaxed);
1823*1031c584SApple OSS Distributions 	fp->ftp_pcount = 0;
1824*1031c584SApple OSS Distributions 
1825*1031c584SApple OSS Distributions 	fp->ftp_retired = 1;
1826*1031c584SApple OSS Distributions 	fp->ftp_marked = 1;
1827*1031c584SApple OSS Distributions 	provid = fp->ftp_provid;
1828*1031c584SApple OSS Distributions 	lck_mtx_unlock(&fp->ftp_mtx);
1829*1031c584SApple OSS Distributions 
1830*1031c584SApple OSS Distributions 	/*
1831*1031c584SApple OSS Distributions 	 * We don't have to worry about invalidating the same provider twice
1832*1031c584SApple OSS Distributions 	 * since fasttrap_provider_lookup() will ignore providers that have
1833*1031c584SApple OSS Distributions 	 * been marked as retired.
1834*1031c584SApple OSS Distributions 	 */
1835*1031c584SApple OSS Distributions 	dtrace_invalidate(provid);
1836*1031c584SApple OSS Distributions 
1837*1031c584SApple OSS Distributions 	lck_mtx_unlock(&bucket->ftb_mtx);
1838*1031c584SApple OSS Distributions 
1839*1031c584SApple OSS Distributions 	fasttrap_pid_cleanup(FASTTRAP_CLEANUP_PROVIDER);
1840*1031c584SApple OSS Distributions }
1841*1031c584SApple OSS Distributions 
1842*1031c584SApple OSS Distributions static int
fasttrap_uint32_cmp(const void * ap,const void * bp)1843*1031c584SApple OSS Distributions fasttrap_uint32_cmp(const void *ap, const void *bp)
1844*1031c584SApple OSS Distributions {
1845*1031c584SApple OSS Distributions 	return (*(const uint32_t *)ap - *(const uint32_t *)bp);
1846*1031c584SApple OSS Distributions }
1847*1031c584SApple OSS Distributions 
1848*1031c584SApple OSS Distributions static int
fasttrap_uint64_cmp(const void * ap,const void * bp)1849*1031c584SApple OSS Distributions fasttrap_uint64_cmp(const void *ap, const void *bp)
1850*1031c584SApple OSS Distributions {
1851*1031c584SApple OSS Distributions 	return (*(const uint64_t *)ap - *(const uint64_t *)bp);
1852*1031c584SApple OSS Distributions }
1853*1031c584SApple OSS Distributions 
1854*1031c584SApple OSS Distributions static int
fasttrap_add_probe(fasttrap_probe_spec_t * pdata)1855*1031c584SApple OSS Distributions fasttrap_add_probe(fasttrap_probe_spec_t *pdata)
1856*1031c584SApple OSS Distributions {
1857*1031c584SApple OSS Distributions 	proc_t *p;
1858*1031c584SApple OSS Distributions 	fasttrap_provider_t *provider;
1859*1031c584SApple OSS Distributions 	fasttrap_probe_t *pp;
1860*1031c584SApple OSS Distributions 	fasttrap_tracepoint_t *tp;
1861*1031c584SApple OSS Distributions 	const char *name;
1862*1031c584SApple OSS Distributions 	unsigned int i, aframes, whack;
1863*1031c584SApple OSS Distributions 
1864*1031c584SApple OSS Distributions 	/*
1865*1031c584SApple OSS Distributions 	 * There needs to be at least one desired trace point.
1866*1031c584SApple OSS Distributions 	 */
1867*1031c584SApple OSS Distributions 	 if (pdata->ftps_noffs == 0)
1868*1031c584SApple OSS Distributions 		return (EINVAL);
1869*1031c584SApple OSS Distributions 
1870*1031c584SApple OSS Distributions 	switch (pdata->ftps_probe_type) {
1871*1031c584SApple OSS Distributions 	case DTFTP_ENTRY:
1872*1031c584SApple OSS Distributions 		name = "entry";
1873*1031c584SApple OSS Distributions 		aframes = FASTTRAP_ENTRY_AFRAMES;
1874*1031c584SApple OSS Distributions 		break;
1875*1031c584SApple OSS Distributions 	case DTFTP_RETURN:
1876*1031c584SApple OSS Distributions 		name = "return";
1877*1031c584SApple OSS Distributions 		aframes = FASTTRAP_RETURN_AFRAMES;
1878*1031c584SApple OSS Distributions 		break;
1879*1031c584SApple OSS Distributions 	case DTFTP_OFFSETS:
1880*1031c584SApple OSS Distributions 		aframes = 0;
1881*1031c584SApple OSS Distributions 		name = NULL;
1882*1031c584SApple OSS Distributions 		break;
1883*1031c584SApple OSS Distributions 	default:
1884*1031c584SApple OSS Distributions 		return (EINVAL);
1885*1031c584SApple OSS Distributions 	}
1886*1031c584SApple OSS Distributions 
1887*1031c584SApple OSS Distributions 	const char* provider_name;
1888*1031c584SApple OSS Distributions 	switch (pdata->ftps_provider_type) {
1889*1031c584SApple OSS Distributions 		case DTFTP_PROVIDER_PID:
1890*1031c584SApple OSS Distributions 			provider_name = FASTTRAP_PID_NAME;
1891*1031c584SApple OSS Distributions 			break;
1892*1031c584SApple OSS Distributions 		case DTFTP_PROVIDER_OBJC:
1893*1031c584SApple OSS Distributions 			provider_name = FASTTRAP_OBJC_NAME;
1894*1031c584SApple OSS Distributions 			break;
1895*1031c584SApple OSS Distributions 		case DTFTP_PROVIDER_ONESHOT:
1896*1031c584SApple OSS Distributions 			provider_name = FASTTRAP_ONESHOT_NAME;
1897*1031c584SApple OSS Distributions 			break;
1898*1031c584SApple OSS Distributions 		default:
1899*1031c584SApple OSS Distributions 			return (EINVAL);
1900*1031c584SApple OSS Distributions 	}
1901*1031c584SApple OSS Distributions 
1902*1031c584SApple OSS Distributions 	p = proc_find(pdata->ftps_pid);
1903*1031c584SApple OSS Distributions 	if (p == PROC_NULL)
1904*1031c584SApple OSS Distributions 		return (ESRCH);
1905*1031c584SApple OSS Distributions 
1906*1031c584SApple OSS Distributions 	if ((provider = fasttrap_provider_lookup(p, pdata->ftps_provider_type,
1907*1031c584SApple OSS Distributions 						 provider_name, &pid_attr)) == NULL) {
1908*1031c584SApple OSS Distributions 		proc_rele(p);
1909*1031c584SApple OSS Distributions 		return (ESRCH);
1910*1031c584SApple OSS Distributions 	}
1911*1031c584SApple OSS Distributions 
1912*1031c584SApple OSS Distributions 	proc_rele(p);
1913*1031c584SApple OSS Distributions 	/*
1914*1031c584SApple OSS Distributions 	 * Increment this reference count to indicate that a consumer is
1915*1031c584SApple OSS Distributions 	 * actively adding a new probe associated with this provider. This
1916*1031c584SApple OSS Distributions 	 * prevents the provider from being deleted -- we'll need to check
1917*1031c584SApple OSS Distributions 	 * for pending deletions when we drop this reference count.
1918*1031c584SApple OSS Distributions 	 */
1919*1031c584SApple OSS Distributions 	provider->ftp_ccount++;
1920*1031c584SApple OSS Distributions 	lck_mtx_unlock(&provider->ftp_mtx);
1921*1031c584SApple OSS Distributions 
1922*1031c584SApple OSS Distributions 	/*
1923*1031c584SApple OSS Distributions 	 * Grab the creation lock to ensure consistency between calls to
1924*1031c584SApple OSS Distributions 	 * dtrace_probe_lookup() and dtrace_probe_create() in the face of
1925*1031c584SApple OSS Distributions 	 * other threads creating probes. We must drop the provider lock
1926*1031c584SApple OSS Distributions 	 * before taking this lock to avoid a three-way deadlock with the
1927*1031c584SApple OSS Distributions 	 * DTrace framework.
1928*1031c584SApple OSS Distributions 	 */
1929*1031c584SApple OSS Distributions 	lck_mtx_lock(&provider->ftp_cmtx);
1930*1031c584SApple OSS Distributions 
1931*1031c584SApple OSS Distributions 	if (name == NULL) {
1932*1031c584SApple OSS Distributions 		for (i = 0; i < pdata->ftps_noffs; i++) {
1933*1031c584SApple OSS Distributions 			char name_str[17];
1934*1031c584SApple OSS Distributions 
1935*1031c584SApple OSS Distributions 			(void) snprintf(name_str, sizeof(name_str), "%llx",
1936*1031c584SApple OSS Distributions 			    (uint64_t)pdata->ftps_offs[i]);
1937*1031c584SApple OSS Distributions 
1938*1031c584SApple OSS Distributions 			if (dtrace_probe_lookup(provider->ftp_provid,
1939*1031c584SApple OSS Distributions 			    pdata->ftps_mod, pdata->ftps_func, name_str) != 0)
1940*1031c584SApple OSS Distributions 				continue;
1941*1031c584SApple OSS Distributions 
1942*1031c584SApple OSS Distributions 			os_atomic_inc(&fasttrap_total, relaxed);
1943*1031c584SApple OSS Distributions 			if (fasttrap_total > fasttrap_max) {
1944*1031c584SApple OSS Distributions 				os_atomic_dec(&fasttrap_total, relaxed);
1945*1031c584SApple OSS Distributions 				goto no_mem;
1946*1031c584SApple OSS Distributions 			}
1947*1031c584SApple OSS Distributions 			provider->ftp_pcount++;
1948*1031c584SApple OSS Distributions 
1949*1031c584SApple OSS Distributions 			pp = zalloc_flags(fasttrap_probe_t_zones[1], Z_WAITOK | Z_ZERO);
1950*1031c584SApple OSS Distributions 
1951*1031c584SApple OSS Distributions 			pp->ftp_prov = provider;
1952*1031c584SApple OSS Distributions 			pp->ftp_faddr = pdata->ftps_pc;
1953*1031c584SApple OSS Distributions 			pp->ftp_fsize = pdata->ftps_size;
1954*1031c584SApple OSS Distributions 			pp->ftp_pid = pdata->ftps_pid;
1955*1031c584SApple OSS Distributions 			pp->ftp_ntps = 1;
1956*1031c584SApple OSS Distributions 
1957*1031c584SApple OSS Distributions 			tp = zalloc_flags(fasttrap_tracepoint_t_zone, Z_WAITOK | Z_ZERO);
1958*1031c584SApple OSS Distributions 
1959*1031c584SApple OSS Distributions 			tp->ftt_proc = provider->ftp_proc;
1960*1031c584SApple OSS Distributions 			tp->ftt_pc = pdata->ftps_offs[i] + pdata->ftps_pc;
1961*1031c584SApple OSS Distributions 			tp->ftt_pid = pdata->ftps_pid;
1962*1031c584SApple OSS Distributions 
1963*1031c584SApple OSS Distributions #if defined(__arm64__)
1964*1031c584SApple OSS Distributions 			/*
1965*1031c584SApple OSS Distributions 			 * On arm the subinfo is used to distinguish between arm
1966*1031c584SApple OSS Distributions 			 * and thumb modes.  On arm64 there is no thumb mode, so
1967*1031c584SApple OSS Distributions 			 * this field is simply initialized to 0 on its way
1968*1031c584SApple OSS Distributions 			 * into the kernel.
1969*1031c584SApple OSS Distributions 			 */
1970*1031c584SApple OSS Distributions 			tp->ftt_fntype = pdata->ftps_arch_subinfo;
1971*1031c584SApple OSS Distributions #endif
1972*1031c584SApple OSS Distributions 
1973*1031c584SApple OSS Distributions 			pp->ftp_tps[0].fit_tp = tp;
1974*1031c584SApple OSS Distributions 			pp->ftp_tps[0].fit_id.fti_probe = pp;
1975*1031c584SApple OSS Distributions 			pp->ftp_tps[0].fit_id.fti_ptype = pdata->ftps_probe_type;
1976*1031c584SApple OSS Distributions 			pp->ftp_id = dtrace_probe_create(provider->ftp_provid,
1977*1031c584SApple OSS Distributions 			    pdata->ftps_mod, pdata->ftps_func, name_str,
1978*1031c584SApple OSS Distributions 			    FASTTRAP_OFFSET_AFRAMES, pp);
1979*1031c584SApple OSS Distributions 		}
1980*1031c584SApple OSS Distributions 
1981*1031c584SApple OSS Distributions 	} else if (dtrace_probe_lookup(provider->ftp_provid, pdata->ftps_mod,
1982*1031c584SApple OSS Distributions 	    pdata->ftps_func, name) == 0) {
1983*1031c584SApple OSS Distributions 		os_atomic_add(&fasttrap_total, pdata->ftps_noffs, relaxed);
1984*1031c584SApple OSS Distributions 
1985*1031c584SApple OSS Distributions 		if (fasttrap_total > fasttrap_max) {
1986*1031c584SApple OSS Distributions 			os_atomic_sub(&fasttrap_total, pdata->ftps_noffs, relaxed);
1987*1031c584SApple OSS Distributions 			goto no_mem;
1988*1031c584SApple OSS Distributions 		}
1989*1031c584SApple OSS Distributions 
1990*1031c584SApple OSS Distributions 		/*
1991*1031c584SApple OSS Distributions 		 * Make sure all tracepoint program counter values are unique.
1992*1031c584SApple OSS Distributions 		 * We later assume that each probe has exactly one tracepoint
1993*1031c584SApple OSS Distributions 		 * for a given pc.
1994*1031c584SApple OSS Distributions 		 */
1995*1031c584SApple OSS Distributions 		qsort(pdata->ftps_offs, pdata->ftps_noffs,
1996*1031c584SApple OSS Distributions 			sizeof (uint64_t), fasttrap_uint64_cmp);
1997*1031c584SApple OSS Distributions 		for (i = 1; i < pdata->ftps_noffs; i++) {
1998*1031c584SApple OSS Distributions 			if (pdata->ftps_offs[i] > pdata->ftps_offs[i - 1])
1999*1031c584SApple OSS Distributions 				continue;
2000*1031c584SApple OSS Distributions 
2001*1031c584SApple OSS Distributions 			os_atomic_sub(&fasttrap_total, pdata->ftps_noffs, relaxed);
2002*1031c584SApple OSS Distributions 			goto no_mem;
2003*1031c584SApple OSS Distributions 		}
2004*1031c584SApple OSS Distributions 		provider->ftp_pcount += pdata->ftps_noffs;
2005*1031c584SApple OSS Distributions 		ASSERT(pdata->ftps_noffs > 0);
2006*1031c584SApple OSS Distributions 		if (pdata->ftps_noffs < FASTTRAP_PROBE_T_ZONE_MAX_TRACEPOINTS) {
2007*1031c584SApple OSS Distributions 			pp = zalloc_flags(fasttrap_probe_t_zones[pdata->ftps_noffs],
2008*1031c584SApple OSS Distributions 			    Z_WAITOK | Z_ZERO);
2009*1031c584SApple OSS Distributions 		} else {
2010*1031c584SApple OSS Distributions 			pp = kmem_zalloc(offsetof(fasttrap_probe_t, ftp_tps[pdata->ftps_noffs]), KM_SLEEP);
2011*1031c584SApple OSS Distributions 		}
2012*1031c584SApple OSS Distributions 
2013*1031c584SApple OSS Distributions 		pp->ftp_prov = provider;
2014*1031c584SApple OSS Distributions 		pp->ftp_faddr = pdata->ftps_pc;
2015*1031c584SApple OSS Distributions 		pp->ftp_fsize = pdata->ftps_size;
2016*1031c584SApple OSS Distributions 		pp->ftp_pid = pdata->ftps_pid;
2017*1031c584SApple OSS Distributions 		pp->ftp_ntps = pdata->ftps_noffs;
2018*1031c584SApple OSS Distributions 
2019*1031c584SApple OSS Distributions 		for (i = 0; i < pdata->ftps_noffs; i++) {
2020*1031c584SApple OSS Distributions 			tp = zalloc_flags(fasttrap_tracepoint_t_zone, Z_WAITOK | Z_ZERO);
2021*1031c584SApple OSS Distributions 			tp->ftt_proc = provider->ftp_proc;
2022*1031c584SApple OSS Distributions 			tp->ftt_pc = pdata->ftps_offs[i] + pdata->ftps_pc;
2023*1031c584SApple OSS Distributions 			tp->ftt_pid = pdata->ftps_pid;
2024*1031c584SApple OSS Distributions 
2025*1031c584SApple OSS Distributions #if defined (__arm64__)
2026*1031c584SApple OSS Distributions 			/*
2027*1031c584SApple OSS Distributions 			 * On arm the subinfo is used to distinguish between arm
2028*1031c584SApple OSS Distributions 			 * and thumb modes.  On arm64 there is no thumb mode, so
2029*1031c584SApple OSS Distributions 			 * this field is simply initialized to 0 on its way
2030*1031c584SApple OSS Distributions 			 * into the kernel.
2031*1031c584SApple OSS Distributions 			 */
2032*1031c584SApple OSS Distributions 
2033*1031c584SApple OSS Distributions 			tp->ftt_fntype = pdata->ftps_arch_subinfo;
2034*1031c584SApple OSS Distributions #endif
2035*1031c584SApple OSS Distributions 			pp->ftp_tps[i].fit_tp = tp;
2036*1031c584SApple OSS Distributions 			pp->ftp_tps[i].fit_id.fti_probe = pp;
2037*1031c584SApple OSS Distributions 			pp->ftp_tps[i].fit_id.fti_ptype = pdata->ftps_probe_type;
2038*1031c584SApple OSS Distributions 		}
2039*1031c584SApple OSS Distributions 
2040*1031c584SApple OSS Distributions 		pp->ftp_id = dtrace_probe_create(provider->ftp_provid,
2041*1031c584SApple OSS Distributions 		    pdata->ftps_mod, pdata->ftps_func, name, aframes, pp);
2042*1031c584SApple OSS Distributions 	}
2043*1031c584SApple OSS Distributions 
2044*1031c584SApple OSS Distributions 	lck_mtx_unlock(&provider->ftp_cmtx);
2045*1031c584SApple OSS Distributions 
2046*1031c584SApple OSS Distributions 	/*
2047*1031c584SApple OSS Distributions 	 * We know that the provider is still valid since we incremented the
2048*1031c584SApple OSS Distributions 	 * creation reference count. If someone tried to clean up this provider
2049*1031c584SApple OSS Distributions 	 * while we were using it (e.g. because the process called exec(2) or
2050*1031c584SApple OSS Distributions 	 * exit(2)), take note of that and try to clean it up now.
2051*1031c584SApple OSS Distributions 	 */
2052*1031c584SApple OSS Distributions 	lck_mtx_lock(&provider->ftp_mtx);
2053*1031c584SApple OSS Distributions 	provider->ftp_ccount--;
2054*1031c584SApple OSS Distributions 	whack = provider->ftp_retired;
2055*1031c584SApple OSS Distributions 	lck_mtx_unlock(&provider->ftp_mtx);
2056*1031c584SApple OSS Distributions 
2057*1031c584SApple OSS Distributions 	if (whack)
2058*1031c584SApple OSS Distributions 		fasttrap_pid_cleanup(FASTTRAP_CLEANUP_PROVIDER);
2059*1031c584SApple OSS Distributions 
2060*1031c584SApple OSS Distributions 	return (0);
2061*1031c584SApple OSS Distributions 
2062*1031c584SApple OSS Distributions no_mem:
2063*1031c584SApple OSS Distributions 	/*
2064*1031c584SApple OSS Distributions 	 * If we've exhausted the allowable resources, we'll try to remove
2065*1031c584SApple OSS Distributions 	 * this provider to free some up. This is to cover the case where
2066*1031c584SApple OSS Distributions 	 * the user has accidentally created many more probes than was
2067*1031c584SApple OSS Distributions 	 * intended (e.g. pid123:::).
2068*1031c584SApple OSS Distributions 	 */
2069*1031c584SApple OSS Distributions 	lck_mtx_unlock(&provider->ftp_cmtx);
2070*1031c584SApple OSS Distributions 	lck_mtx_lock(&provider->ftp_mtx);
2071*1031c584SApple OSS Distributions 	provider->ftp_ccount--;
2072*1031c584SApple OSS Distributions 	provider->ftp_marked = 1;
2073*1031c584SApple OSS Distributions 	lck_mtx_unlock(&provider->ftp_mtx);
2074*1031c584SApple OSS Distributions 
2075*1031c584SApple OSS Distributions 	fasttrap_pid_cleanup(FASTTRAP_CLEANUP_PROVIDER);
2076*1031c584SApple OSS Distributions 
2077*1031c584SApple OSS Distributions 	return (ENOMEM);
2078*1031c584SApple OSS Distributions }
2079*1031c584SApple OSS Distributions 
2080*1031c584SApple OSS Distributions /*ARGSUSED*/
2081*1031c584SApple OSS Distributions static void *
fasttrap_meta_provide(void * arg,dtrace_helper_provdesc_t * dhpv,proc_t * p)2082*1031c584SApple OSS Distributions fasttrap_meta_provide(void *arg, dtrace_helper_provdesc_t *dhpv, proc_t *p)
2083*1031c584SApple OSS Distributions {
2084*1031c584SApple OSS Distributions #pragma unused(arg)
2085*1031c584SApple OSS Distributions 	fasttrap_provider_t *provider;
2086*1031c584SApple OSS Distributions 
2087*1031c584SApple OSS Distributions 	/*
2088*1031c584SApple OSS Distributions 	 * A 32-bit unsigned integer (like a pid for example) can be
2089*1031c584SApple OSS Distributions 	 * expressed in 10 or fewer decimal digits. Make sure that we'll
2090*1031c584SApple OSS Distributions 	 * have enough space for the provider name.
2091*1031c584SApple OSS Distributions 	 */
2092*1031c584SApple OSS Distributions 	if (strlen(dhpv->dthpv_provname) + 10 >=
2093*1031c584SApple OSS Distributions 	    sizeof (provider->ftp_name)) {
2094*1031c584SApple OSS Distributions 		cmn_err(CE_WARN, "failed to instantiate provider %s: "
2095*1031c584SApple OSS Distributions 		    "name too long to accomodate pid", dhpv->dthpv_provname);
2096*1031c584SApple OSS Distributions 		return (NULL);
2097*1031c584SApple OSS Distributions 	}
2098*1031c584SApple OSS Distributions 
2099*1031c584SApple OSS Distributions 	/*
2100*1031c584SApple OSS Distributions 	 * Don't let folks spoof the true pid provider.
2101*1031c584SApple OSS Distributions 	 */
2102*1031c584SApple OSS Distributions 	if (strncmp(dhpv->dthpv_provname, FASTTRAP_PID_NAME, sizeof(FASTTRAP_PID_NAME)) == 0) {
2103*1031c584SApple OSS Distributions 		cmn_err(CE_WARN, "failed to instantiate provider %s: "
2104*1031c584SApple OSS Distributions 		    "%s is an invalid name", dhpv->dthpv_provname,
2105*1031c584SApple OSS Distributions 		    FASTTRAP_PID_NAME);
2106*1031c584SApple OSS Distributions 		return (NULL);
2107*1031c584SApple OSS Distributions 	}
2108*1031c584SApple OSS Distributions 
2109*1031c584SApple OSS Distributions 	/*
2110*1031c584SApple OSS Distributions 	 * APPLE NOTE: We also need to check the objc and oneshot pid provider types
2111*1031c584SApple OSS Distributions 	 */
2112*1031c584SApple OSS Distributions 	if (strncmp(dhpv->dthpv_provname, FASTTRAP_OBJC_NAME, sizeof(FASTTRAP_OBJC_NAME)) == 0) {
2113*1031c584SApple OSS Distributions 		cmn_err(CE_WARN, "failed to instantiate provider %s: "
2114*1031c584SApple OSS Distributions 		    "%s is an invalid name", dhpv->dthpv_provname,
2115*1031c584SApple OSS Distributions 		    FASTTRAP_OBJC_NAME);
2116*1031c584SApple OSS Distributions 		return (NULL);
2117*1031c584SApple OSS Distributions 	}
2118*1031c584SApple OSS Distributions 	if (strncmp(dhpv->dthpv_provname, FASTTRAP_ONESHOT_NAME, sizeof(FASTTRAP_ONESHOT_NAME)) == 0) {
2119*1031c584SApple OSS Distributions 		cmn_err(CE_WARN, "failed to instantiate provider %s: "
2120*1031c584SApple OSS Distributions 		    "%s is an invalid name", dhpv->dthpv_provname,
2121*1031c584SApple OSS Distributions 		    FASTTRAP_ONESHOT_NAME);
2122*1031c584SApple OSS Distributions 		return (NULL);
2123*1031c584SApple OSS Distributions 	}
2124*1031c584SApple OSS Distributions 
2125*1031c584SApple OSS Distributions 	/*
2126*1031c584SApple OSS Distributions 	 * The highest stability class that fasttrap supports is ISA; cap
2127*1031c584SApple OSS Distributions 	 * the stability of the new provider accordingly.
2128*1031c584SApple OSS Distributions 	 */
2129*1031c584SApple OSS Distributions 	if (dhpv->dthpv_pattr.dtpa_provider.dtat_class > DTRACE_CLASS_ISA)
2130*1031c584SApple OSS Distributions 		dhpv->dthpv_pattr.dtpa_provider.dtat_class = DTRACE_CLASS_ISA;
2131*1031c584SApple OSS Distributions 	if (dhpv->dthpv_pattr.dtpa_mod.dtat_class > DTRACE_CLASS_ISA)
2132*1031c584SApple OSS Distributions 		dhpv->dthpv_pattr.dtpa_mod.dtat_class = DTRACE_CLASS_ISA;
2133*1031c584SApple OSS Distributions 	if (dhpv->dthpv_pattr.dtpa_func.dtat_class > DTRACE_CLASS_ISA)
2134*1031c584SApple OSS Distributions 		dhpv->dthpv_pattr.dtpa_func.dtat_class = DTRACE_CLASS_ISA;
2135*1031c584SApple OSS Distributions 	if (dhpv->dthpv_pattr.dtpa_name.dtat_class > DTRACE_CLASS_ISA)
2136*1031c584SApple OSS Distributions 		dhpv->dthpv_pattr.dtpa_name.dtat_class = DTRACE_CLASS_ISA;
2137*1031c584SApple OSS Distributions 	if (dhpv->dthpv_pattr.dtpa_args.dtat_class > DTRACE_CLASS_ISA)
2138*1031c584SApple OSS Distributions 		dhpv->dthpv_pattr.dtpa_args.dtat_class = DTRACE_CLASS_ISA;
2139*1031c584SApple OSS Distributions 
2140*1031c584SApple OSS Distributions 	if ((provider = fasttrap_provider_lookup(p, DTFTP_PROVIDER_USDT, dhpv->dthpv_provname,
2141*1031c584SApple OSS Distributions 	    &dhpv->dthpv_pattr)) == NULL) {
2142*1031c584SApple OSS Distributions 		cmn_err(CE_WARN, "failed to instantiate provider %s for "
2143*1031c584SApple OSS Distributions 		    "process %u",  dhpv->dthpv_provname, (uint_t)proc_getpid(p));
2144*1031c584SApple OSS Distributions 		return (NULL);
2145*1031c584SApple OSS Distributions 	}
2146*1031c584SApple OSS Distributions 
2147*1031c584SApple OSS Distributions 	/*
2148*1031c584SApple OSS Distributions 	 * APPLE NOTE!
2149*1031c584SApple OSS Distributions 	 *
2150*1031c584SApple OSS Distributions 	 * USDT probes (fasttrap meta probes) are very expensive to create.
2151*1031c584SApple OSS Distributions 	 * Profiling has shown that the largest single cost is verifying that
2152*1031c584SApple OSS Distributions 	 * dtrace hasn't already created a given meta_probe. The reason for
2153*1031c584SApple OSS Distributions 	 * this is dtrace_match() often has to strcmp ~100 hashed entries for
2154*1031c584SApple OSS Distributions 	 * each static probe being created. We want to get rid of that check.
2155*1031c584SApple OSS Distributions 	 * The simplest way of eliminating it is to deny the ability to add
2156*1031c584SApple OSS Distributions 	 * probes to an existing provider. If the provider already exists, BZZT!
2157*1031c584SApple OSS Distributions 	 * This still leaves the possibility of intentionally malformed DOF
2158*1031c584SApple OSS Distributions 	 * having duplicate probes. However, duplicate probes are not fatal,
2159*1031c584SApple OSS Distributions 	 * and there is no way to get that by accident, so we will not check
2160*1031c584SApple OSS Distributions 	 * for that case.
2161*1031c584SApple OSS Distributions 	 *
2162*1031c584SApple OSS Distributions 	 * UPDATE: It turns out there are several use cases that require adding
2163*1031c584SApple OSS Distributions 	 * probes to existing providers. Disabling the dtrace_probe_lookup()
2164*1031c584SApple OSS Distributions 	 * optimization for now. See APPLE NOTE in fasttrap_meta_create_probe.
2165*1031c584SApple OSS Distributions 	 */
2166*1031c584SApple OSS Distributions 
2167*1031c584SApple OSS Distributions 	/*
2168*1031c584SApple OSS Distributions 	 * Up the meta provider count so this provider isn't removed until
2169*1031c584SApple OSS Distributions 	 * the meta provider has been told to remove it.
2170*1031c584SApple OSS Distributions 	 */
2171*1031c584SApple OSS Distributions 	provider->ftp_mcount++;
2172*1031c584SApple OSS Distributions 
2173*1031c584SApple OSS Distributions 	lck_mtx_unlock(&provider->ftp_mtx);
2174*1031c584SApple OSS Distributions 
2175*1031c584SApple OSS Distributions 	return (provider);
2176*1031c584SApple OSS Distributions }
2177*1031c584SApple OSS Distributions 
2178*1031c584SApple OSS Distributions /*ARGSUSED*/
2179*1031c584SApple OSS Distributions static void
fasttrap_meta_create_probe(void * arg,void * parg,dtrace_helper_probedesc_t * dhpb)2180*1031c584SApple OSS Distributions fasttrap_meta_create_probe(void *arg, void *parg,
2181*1031c584SApple OSS Distributions     dtrace_helper_probedesc_t *dhpb)
2182*1031c584SApple OSS Distributions {
2183*1031c584SApple OSS Distributions #pragma unused(arg)
2184*1031c584SApple OSS Distributions 	fasttrap_provider_t *provider = parg;
2185*1031c584SApple OSS Distributions 	fasttrap_probe_t *pp;
2186*1031c584SApple OSS Distributions 	fasttrap_tracepoint_t *tp;
2187*1031c584SApple OSS Distributions 	unsigned int i, j;
2188*1031c584SApple OSS Distributions 	uint32_t ntps;
2189*1031c584SApple OSS Distributions 
2190*1031c584SApple OSS Distributions 	/*
2191*1031c584SApple OSS Distributions 	 * Since the meta provider count is non-zero we don't have to worry
2192*1031c584SApple OSS Distributions 	 * about this provider disappearing.
2193*1031c584SApple OSS Distributions 	 */
2194*1031c584SApple OSS Distributions 	ASSERT(provider->ftp_mcount > 0);
2195*1031c584SApple OSS Distributions 
2196*1031c584SApple OSS Distributions 	/*
2197*1031c584SApple OSS Distributions 	 * The offsets must be unique.
2198*1031c584SApple OSS Distributions 	 */
2199*1031c584SApple OSS Distributions 	qsort(dhpb->dthpb_offs, dhpb->dthpb_noffs, sizeof (uint32_t),
2200*1031c584SApple OSS Distributions 		fasttrap_uint32_cmp);
2201*1031c584SApple OSS Distributions 	for (i = 1; i < dhpb->dthpb_noffs; i++) {
2202*1031c584SApple OSS Distributions 		if (dhpb->dthpb_base + dhpb->dthpb_offs[i] <=
2203*1031c584SApple OSS Distributions 			dhpb->dthpb_base + dhpb->dthpb_offs[i - 1])
2204*1031c584SApple OSS Distributions 				return;
2205*1031c584SApple OSS Distributions 	}
2206*1031c584SApple OSS Distributions 
2207*1031c584SApple OSS Distributions 	qsort(dhpb->dthpb_enoffs, dhpb->dthpb_nenoffs, sizeof (uint32_t),
2208*1031c584SApple OSS Distributions 		fasttrap_uint32_cmp);
2209*1031c584SApple OSS Distributions 	for (i = 1; i < dhpb->dthpb_nenoffs; i++) {
2210*1031c584SApple OSS Distributions 		if (dhpb->dthpb_base + dhpb->dthpb_enoffs[i] <=
2211*1031c584SApple OSS Distributions 			dhpb->dthpb_base + dhpb->dthpb_enoffs[i - 1])
2212*1031c584SApple OSS Distributions 				return;
2213*1031c584SApple OSS Distributions 	}
2214*1031c584SApple OSS Distributions 
2215*1031c584SApple OSS Distributions 	/*
2216*1031c584SApple OSS Distributions 	 * Grab the creation lock to ensure consistency between calls to
2217*1031c584SApple OSS Distributions 	 * dtrace_probe_lookup() and dtrace_probe_create() in the face of
2218*1031c584SApple OSS Distributions 	 * other threads creating probes.
2219*1031c584SApple OSS Distributions 	 */
2220*1031c584SApple OSS Distributions 	lck_mtx_lock(&provider->ftp_cmtx);
2221*1031c584SApple OSS Distributions 
2222*1031c584SApple OSS Distributions #if 0
2223*1031c584SApple OSS Distributions 	/*
2224*1031c584SApple OSS Distributions 	 * APPLE NOTE: This is hideously expensive. See note in
2225*1031c584SApple OSS Distributions 	 * fasttrap_meta_provide() for why we can get away without
2226*1031c584SApple OSS Distributions 	 * checking here.
2227*1031c584SApple OSS Distributions 	 */
2228*1031c584SApple OSS Distributions 	if (dtrace_probe_lookup(provider->ftp_provid, dhpb->dthpb_mod,
2229*1031c584SApple OSS Distributions 	    dhpb->dthpb_func, dhpb->dthpb_name) != 0) {
2230*1031c584SApple OSS Distributions 		lck_mtx_unlock(&provider->ftp_cmtx);
2231*1031c584SApple OSS Distributions 		return;
2232*1031c584SApple OSS Distributions 	}
2233*1031c584SApple OSS Distributions #endif
2234*1031c584SApple OSS Distributions 
2235*1031c584SApple OSS Distributions 	ntps = dhpb->dthpb_noffs + dhpb->dthpb_nenoffs;
2236*1031c584SApple OSS Distributions 	ASSERT(ntps > 0);
2237*1031c584SApple OSS Distributions 
2238*1031c584SApple OSS Distributions 	os_atomic_add(&fasttrap_total, ntps, relaxed);
2239*1031c584SApple OSS Distributions 
2240*1031c584SApple OSS Distributions 	if (fasttrap_total > fasttrap_max) {
2241*1031c584SApple OSS Distributions 		os_atomic_sub(&fasttrap_total, ntps, relaxed);
2242*1031c584SApple OSS Distributions 		lck_mtx_unlock(&provider->ftp_cmtx);
2243*1031c584SApple OSS Distributions 		return;
2244*1031c584SApple OSS Distributions 	}
2245*1031c584SApple OSS Distributions 
2246*1031c584SApple OSS Distributions 	provider->ftp_pcount += ntps;
2247*1031c584SApple OSS Distributions 
2248*1031c584SApple OSS Distributions 	if (ntps < FASTTRAP_PROBE_T_ZONE_MAX_TRACEPOINTS) {
2249*1031c584SApple OSS Distributions 		pp = zalloc_flags(fasttrap_probe_t_zones[ntps], Z_WAITOK | Z_ZERO);
2250*1031c584SApple OSS Distributions 	} else {
2251*1031c584SApple OSS Distributions 		pp = kmem_zalloc(offsetof(fasttrap_probe_t, ftp_tps[ntps]), KM_SLEEP);
2252*1031c584SApple OSS Distributions 	}
2253*1031c584SApple OSS Distributions 
2254*1031c584SApple OSS Distributions 	pp->ftp_prov = provider;
2255*1031c584SApple OSS Distributions 	pp->ftp_pid = provider->ftp_pid;
2256*1031c584SApple OSS Distributions 	pp->ftp_ntps = ntps;
2257*1031c584SApple OSS Distributions 	pp->ftp_nargs = dhpb->dthpb_xargc;
2258*1031c584SApple OSS Distributions 	pp->ftp_xtypes = dhpb->dthpb_xtypes;
2259*1031c584SApple OSS Distributions 	pp->ftp_ntypes = dhpb->dthpb_ntypes;
2260*1031c584SApple OSS Distributions 
2261*1031c584SApple OSS Distributions 	/*
2262*1031c584SApple OSS Distributions 	 * First create a tracepoint for each actual point of interest.
2263*1031c584SApple OSS Distributions 	 */
2264*1031c584SApple OSS Distributions 	for (i = 0; i < dhpb->dthpb_noffs; i++) {
2265*1031c584SApple OSS Distributions 		tp = zalloc_flags(fasttrap_tracepoint_t_zone, Z_WAITOK | Z_ZERO);
2266*1031c584SApple OSS Distributions 
2267*1031c584SApple OSS Distributions 		tp->ftt_proc = provider->ftp_proc;
2268*1031c584SApple OSS Distributions 
2269*1031c584SApple OSS Distributions 		/*
2270*1031c584SApple OSS Distributions 		 * APPLE NOTE: We have linker support when creating DOF to handle all relocations for us.
2271*1031c584SApple OSS Distributions 		 * Unfortunately, a side effect of this is that the relocations do not point at exactly
2272*1031c584SApple OSS Distributions 		 * the location we want. We need to fix up the addresses here. The fixups vary by arch and type.
2273*1031c584SApple OSS Distributions 		 */
2274*1031c584SApple OSS Distributions #if defined(__x86_64__)
2275*1031c584SApple OSS Distributions 		/*
2276*1031c584SApple OSS Distributions 		 * Both 32 & 64 bit want to go back one byte, to point at the first NOP
2277*1031c584SApple OSS Distributions 		 */
2278*1031c584SApple OSS Distributions 		tp->ftt_pc = dhpb->dthpb_base + (int64_t)dhpb->dthpb_offs[i] - 1;
2279*1031c584SApple OSS Distributions #elif defined(__arm64__)
2280*1031c584SApple OSS Distributions 		/*
2281*1031c584SApple OSS Distributions 		 * All ARM and ARM64 probes are zero offset. We need to zero out the
2282*1031c584SApple OSS Distributions 		 * thumb bit because we still support 32bit user processes.
2283*1031c584SApple OSS Distributions 		 * On 64bit user processes, bit zero won't be set anyway.
2284*1031c584SApple OSS Distributions 		 */
2285*1031c584SApple OSS Distributions 		tp->ftt_pc = (dhpb->dthpb_base + (int64_t)dhpb->dthpb_offs[i]) & ~0x1UL;
2286*1031c584SApple OSS Distributions 		tp->ftt_fntype = FASTTRAP_FN_USDT;
2287*1031c584SApple OSS Distributions #else
2288*1031c584SApple OSS Distributions #error "Architecture not supported"
2289*1031c584SApple OSS Distributions #endif
2290*1031c584SApple OSS Distributions 
2291*1031c584SApple OSS Distributions 		tp->ftt_pid = provider->ftp_pid;
2292*1031c584SApple OSS Distributions 
2293*1031c584SApple OSS Distributions 		pp->ftp_tps[i].fit_tp = tp;
2294*1031c584SApple OSS Distributions 		pp->ftp_tps[i].fit_id.fti_probe = pp;
2295*1031c584SApple OSS Distributions 		pp->ftp_tps[i].fit_id.fti_ptype = DTFTP_OFFSETS;
2296*1031c584SApple OSS Distributions 	}
2297*1031c584SApple OSS Distributions 
2298*1031c584SApple OSS Distributions 	/*
2299*1031c584SApple OSS Distributions 	 * Then create a tracepoint for each is-enabled point.
2300*1031c584SApple OSS Distributions 	 */
2301*1031c584SApple OSS Distributions 	for (j = 0; i < ntps; i++, j++) {
2302*1031c584SApple OSS Distributions 		tp = zalloc_flags(fasttrap_tracepoint_t_zone, Z_WAITOK | Z_ZERO);
2303*1031c584SApple OSS Distributions 
2304*1031c584SApple OSS Distributions 		tp->ftt_proc = provider->ftp_proc;
2305*1031c584SApple OSS Distributions 
2306*1031c584SApple OSS Distributions 		/*
2307*1031c584SApple OSS Distributions 		 * APPLE NOTE: We have linker support when creating DOF to handle all relocations for us.
2308*1031c584SApple OSS Distributions 		 * Unfortunately, a side effect of this is that the relocations do not point at exactly
2309*1031c584SApple OSS Distributions 		 * the location we want. We need to fix up the addresses here. The fixups vary by arch and type.
2310*1031c584SApple OSS Distributions 		 */
2311*1031c584SApple OSS Distributions #if defined(__x86_64__)
2312*1031c584SApple OSS Distributions 		/*
2313*1031c584SApple OSS Distributions 		 * Both 32 & 64 bit want to go forward two bytes, to point at a single byte nop.
2314*1031c584SApple OSS Distributions 		 */
2315*1031c584SApple OSS Distributions 		tp->ftt_pc = dhpb->dthpb_base + (int64_t)dhpb->dthpb_enoffs[j] + 2;
2316*1031c584SApple OSS Distributions #elif defined(__arm64__)
2317*1031c584SApple OSS Distributions 		/*
2318*1031c584SApple OSS Distributions 		 * All ARM and ARM64 probes are zero offset. We need to zero out the
2319*1031c584SApple OSS Distributions 		 * thumb bit because we still support 32bit user processes.
2320*1031c584SApple OSS Distributions 		 * On 64bit user processes, bit zero won't be set anyway.
2321*1031c584SApple OSS Distributions 		 */
2322*1031c584SApple OSS Distributions 		tp->ftt_pc = (dhpb->dthpb_base + (int64_t)dhpb->dthpb_enoffs[j]) & ~0x1UL;
2323*1031c584SApple OSS Distributions 		tp->ftt_fntype = FASTTRAP_FN_USDT;
2324*1031c584SApple OSS Distributions #else
2325*1031c584SApple OSS Distributions #error "Architecture not supported"
2326*1031c584SApple OSS Distributions #endif
2327*1031c584SApple OSS Distributions 
2328*1031c584SApple OSS Distributions 		tp->ftt_pid = provider->ftp_pid;
2329*1031c584SApple OSS Distributions 
2330*1031c584SApple OSS Distributions 		pp->ftp_tps[i].fit_tp = tp;
2331*1031c584SApple OSS Distributions 		pp->ftp_tps[i].fit_id.fti_probe = pp;
2332*1031c584SApple OSS Distributions 		pp->ftp_tps[i].fit_id.fti_ptype = DTFTP_IS_ENABLED;
2333*1031c584SApple OSS Distributions 	}
2334*1031c584SApple OSS Distributions 
2335*1031c584SApple OSS Distributions 	/*
2336*1031c584SApple OSS Distributions 	 * If the arguments are shuffled around we set the argument remapping
2337*1031c584SApple OSS Distributions 	 * table. Later, when the probe fires, we only remap the arguments
2338*1031c584SApple OSS Distributions 	 * if the table is non-NULL.
2339*1031c584SApple OSS Distributions 	 */
2340*1031c584SApple OSS Distributions 	for (i = 0; i < dhpb->dthpb_xargc; i++) {
2341*1031c584SApple OSS Distributions 		if (dhpb->dthpb_args[i] != i) {
2342*1031c584SApple OSS Distributions 			pp->ftp_argmap = dhpb->dthpb_args;
2343*1031c584SApple OSS Distributions 			break;
2344*1031c584SApple OSS Distributions 		}
2345*1031c584SApple OSS Distributions 	}
2346*1031c584SApple OSS Distributions 
2347*1031c584SApple OSS Distributions 	/*
2348*1031c584SApple OSS Distributions 	 * The probe is fully constructed -- register it with DTrace.
2349*1031c584SApple OSS Distributions 	 */
2350*1031c584SApple OSS Distributions 	pp->ftp_id = dtrace_probe_create(provider->ftp_provid, dhpb->dthpb_mod,
2351*1031c584SApple OSS Distributions 	    dhpb->dthpb_func, dhpb->dthpb_name, FASTTRAP_OFFSET_AFRAMES, pp);
2352*1031c584SApple OSS Distributions 
2353*1031c584SApple OSS Distributions 	lck_mtx_unlock(&provider->ftp_cmtx);
2354*1031c584SApple OSS Distributions }
2355*1031c584SApple OSS Distributions 
2356*1031c584SApple OSS Distributions /*ARGSUSED*/
2357*1031c584SApple OSS Distributions static void
fasttrap_meta_remove(void * arg,dtrace_helper_provdesc_t * dhpv,proc_t * p)2358*1031c584SApple OSS Distributions fasttrap_meta_remove(void *arg, dtrace_helper_provdesc_t *dhpv, proc_t *p)
2359*1031c584SApple OSS Distributions {
2360*1031c584SApple OSS Distributions #pragma unused(arg)
2361*1031c584SApple OSS Distributions 	/*
2362*1031c584SApple OSS Distributions 	 * Clean up the USDT provider. There may be active consumers of the
2363*1031c584SApple OSS Distributions 	 * provider busy adding probes, no damage will actually befall the
2364*1031c584SApple OSS Distributions 	 * provider until that count has dropped to zero. This just puts
2365*1031c584SApple OSS Distributions 	 * the provider on death row.
2366*1031c584SApple OSS Distributions 	 */
2367*1031c584SApple OSS Distributions 	fasttrap_provider_retire(p, dhpv->dthpv_provname, 1);
2368*1031c584SApple OSS Distributions }
2369*1031c584SApple OSS Distributions 
2370*1031c584SApple OSS Distributions static char*
fasttrap_meta_provider_name(void * arg)2371*1031c584SApple OSS Distributions fasttrap_meta_provider_name(void *arg)
2372*1031c584SApple OSS Distributions {
2373*1031c584SApple OSS Distributions 	fasttrap_provider_t *fprovider = arg;
2374*1031c584SApple OSS Distributions 	dtrace_provider_t *provider = (dtrace_provider_t*)(fprovider->ftp_provid);
2375*1031c584SApple OSS Distributions 	return provider->dtpv_name;
2376*1031c584SApple OSS Distributions }
2377*1031c584SApple OSS Distributions 
2378*1031c584SApple OSS Distributions static dtrace_mops_t fasttrap_mops = {
2379*1031c584SApple OSS Distributions 	.dtms_create_probe =	fasttrap_meta_create_probe,
2380*1031c584SApple OSS Distributions 	.dtms_provide_proc =	fasttrap_meta_provide,
2381*1031c584SApple OSS Distributions 	.dtms_remove_proc =	fasttrap_meta_remove,
2382*1031c584SApple OSS Distributions 	.dtms_provider_name =	fasttrap_meta_provider_name
2383*1031c584SApple OSS Distributions };
2384*1031c584SApple OSS Distributions 
2385*1031c584SApple OSS Distributions /*
2386*1031c584SApple OSS Distributions  * Validate a null-terminated string. If str is not null-terminated,
2387*1031c584SApple OSS Distributions  * or not a UTF8 valid string, the function returns -1. Otherwise, 0 is
2388*1031c584SApple OSS Distributions  * returned.
2389*1031c584SApple OSS Distributions  *
2390*1031c584SApple OSS Distributions  * str: string to validate.
2391*1031c584SApple OSS Distributions  * maxlen: maximal length of the string, null-terminated byte included.
2392*1031c584SApple OSS Distributions  */
2393*1031c584SApple OSS Distributions static int
fasttrap_validatestr(char const * str,size_t maxlen)2394*1031c584SApple OSS Distributions fasttrap_validatestr(char const* str, size_t maxlen) {
2395*1031c584SApple OSS Distributions 	size_t len;
2396*1031c584SApple OSS Distributions 
2397*1031c584SApple OSS Distributions 	assert(str);
2398*1031c584SApple OSS Distributions 	assert(maxlen != 0);
2399*1031c584SApple OSS Distributions 
2400*1031c584SApple OSS Distributions 	/* Check if the string is null-terminated. */
2401*1031c584SApple OSS Distributions 	len = strnlen(str, maxlen);
2402*1031c584SApple OSS Distributions 	if (len >= maxlen)
2403*1031c584SApple OSS Distributions 		return -1;
2404*1031c584SApple OSS Distributions 
2405*1031c584SApple OSS Distributions 	/* Finally, check for UTF8 validity. */
2406*1031c584SApple OSS Distributions 	return utf8_validatestr((unsigned const char*) str, len);
2407*1031c584SApple OSS Distributions }
2408*1031c584SApple OSS Distributions 
2409*1031c584SApple OSS Distributions /*
2410*1031c584SApple OSS Distributions  * Checks that provided credentials are allowed to debug target process.
2411*1031c584SApple OSS Distributions  */
2412*1031c584SApple OSS Distributions static int
fasttrap_check_cred_priv(cred_t * cr,proc_t * p)2413*1031c584SApple OSS Distributions fasttrap_check_cred_priv(cred_t *cr, proc_t *p)
2414*1031c584SApple OSS Distributions {
2415*1031c584SApple OSS Distributions 	int err = 0;
2416*1031c584SApple OSS Distributions 
2417*1031c584SApple OSS Distributions 	/* Only root can use DTrace. */
2418*1031c584SApple OSS Distributions 	if (!kauth_cred_issuser(cr)) {
2419*1031c584SApple OSS Distributions 		err = EPERM;
2420*1031c584SApple OSS Distributions 		goto out;
2421*1031c584SApple OSS Distributions 	}
2422*1031c584SApple OSS Distributions 
2423*1031c584SApple OSS Distributions 	/* Process is marked as no attach. */
2424*1031c584SApple OSS Distributions 	if (ISSET(p->p_lflag, P_LNOATTACH)) {
2425*1031c584SApple OSS Distributions 		err = EBUSY;
2426*1031c584SApple OSS Distributions 		goto out;
2427*1031c584SApple OSS Distributions 	}
2428*1031c584SApple OSS Distributions 
2429*1031c584SApple OSS Distributions #if CONFIG_MACF
2430*1031c584SApple OSS Distributions 	/* Check with MAC framework when enabled. */
2431*1031c584SApple OSS Distributions 	struct proc_ident cur_ident = proc_ident(current_proc());
2432*1031c584SApple OSS Distributions 	struct proc_ident p_ident = proc_ident(p);
2433*1031c584SApple OSS Distributions 
2434*1031c584SApple OSS Distributions 	/* Do not hold ref to proc here to avoid deadlock. */
2435*1031c584SApple OSS Distributions 	proc_rele(p);
2436*1031c584SApple OSS Distributions 	err = mac_proc_check_debug(&cur_ident, cr, &p_ident);
2437*1031c584SApple OSS Distributions 
2438*1031c584SApple OSS Distributions 	if (proc_find_ident(&p_ident) == PROC_NULL) {
2439*1031c584SApple OSS Distributions 		err = ESRCH;
2440*1031c584SApple OSS Distributions 		goto out_no_proc;
2441*1031c584SApple OSS Distributions 	}
2442*1031c584SApple OSS Distributions #endif /* CONFIG_MACF */
2443*1031c584SApple OSS Distributions 
2444*1031c584SApple OSS Distributions out:
2445*1031c584SApple OSS Distributions 	proc_rele(p);
2446*1031c584SApple OSS Distributions 
2447*1031c584SApple OSS Distributions out_no_proc:
2448*1031c584SApple OSS Distributions 	return err;
2449*1031c584SApple OSS Distributions }
2450*1031c584SApple OSS Distributions 
2451*1031c584SApple OSS Distributions /*ARGSUSED*/
2452*1031c584SApple OSS Distributions static int
fasttrap_ioctl(dev_t dev,u_long cmd,user_addr_t arg,int md,cred_t * cr,int * rv)2453*1031c584SApple OSS Distributions fasttrap_ioctl(dev_t dev, u_long cmd, user_addr_t arg, int md, cred_t *cr, int *rv)
2454*1031c584SApple OSS Distributions {
2455*1031c584SApple OSS Distributions #pragma unused(dev, md, rv)
2456*1031c584SApple OSS Distributions 	if (!dtrace_attached())
2457*1031c584SApple OSS Distributions 		return (EAGAIN);
2458*1031c584SApple OSS Distributions 
2459*1031c584SApple OSS Distributions 	if (cmd == FASTTRAPIOC_MAKEPROBE) {
2460*1031c584SApple OSS Distributions 		fasttrap_probe_spec_t *probe;
2461*1031c584SApple OSS Distributions 		uint64_t noffs;
2462*1031c584SApple OSS Distributions 		size_t size;
2463*1031c584SApple OSS Distributions 		int ret;
2464*1031c584SApple OSS Distributions 
2465*1031c584SApple OSS Distributions 		if (copyin(arg + __offsetof(fasttrap_probe_spec_t, ftps_noffs), &noffs,
2466*1031c584SApple OSS Distributions 		    sizeof (probe->ftps_noffs)))
2467*1031c584SApple OSS Distributions 			return (EFAULT);
2468*1031c584SApple OSS Distributions 
2469*1031c584SApple OSS Distributions 		/*
2470*1031c584SApple OSS Distributions 		 * Probes must have at least one tracepoint.
2471*1031c584SApple OSS Distributions 		 */
2472*1031c584SApple OSS Distributions 		if (noffs == 0)
2473*1031c584SApple OSS Distributions 			return (EINVAL);
2474*1031c584SApple OSS Distributions 
2475*1031c584SApple OSS Distributions 		/*
2476*1031c584SApple OSS Distributions 		 * We want to check the number of noffs before doing
2477*1031c584SApple OSS Distributions 		 * sizing math, to prevent potential buffer overflows.
2478*1031c584SApple OSS Distributions 		 */
2479*1031c584SApple OSS Distributions 		if (noffs > ((1024 * 1024) - sizeof(fasttrap_probe_spec_t)) / sizeof(probe->ftps_offs[0]))
2480*1031c584SApple OSS Distributions 			return (ENOMEM);
2481*1031c584SApple OSS Distributions 
2482*1031c584SApple OSS Distributions 		size = sizeof (fasttrap_probe_spec_t) +
2483*1031c584SApple OSS Distributions 		    sizeof (probe->ftps_offs[0]) * (noffs - 1);
2484*1031c584SApple OSS Distributions 
2485*1031c584SApple OSS Distributions 		probe = kmem_alloc(size, KM_SLEEP);
2486*1031c584SApple OSS Distributions 
2487*1031c584SApple OSS Distributions 		if (copyin(arg, probe, size) != 0 ||
2488*1031c584SApple OSS Distributions 		    probe->ftps_noffs != noffs) {
2489*1031c584SApple OSS Distributions 			kmem_free(probe, size);
2490*1031c584SApple OSS Distributions 			return (EFAULT);
2491*1031c584SApple OSS Distributions 		}
2492*1031c584SApple OSS Distributions 
2493*1031c584SApple OSS Distributions 		/*
2494*1031c584SApple OSS Distributions 		 * Verify that the function and module strings contain no
2495*1031c584SApple OSS Distributions 		 * funny characters.
2496*1031c584SApple OSS Distributions 		 */
2497*1031c584SApple OSS Distributions 
2498*1031c584SApple OSS Distributions 		if (fasttrap_validatestr(probe->ftps_func, sizeof(probe->ftps_func)) != 0) {
2499*1031c584SApple OSS Distributions 			ret = EINVAL;
2500*1031c584SApple OSS Distributions 			goto err;
2501*1031c584SApple OSS Distributions 		}
2502*1031c584SApple OSS Distributions 
2503*1031c584SApple OSS Distributions 		if (fasttrap_validatestr(probe->ftps_mod, sizeof(probe->ftps_mod)) != 0) {
2504*1031c584SApple OSS Distributions 			ret = EINVAL;
2505*1031c584SApple OSS Distributions 			goto err;
2506*1031c584SApple OSS Distributions 		}
2507*1031c584SApple OSS Distributions 
2508*1031c584SApple OSS Distributions 		if (!PRIV_POLICY_CHOICE(cr, PRIV_ALL, B_FALSE)) {
2509*1031c584SApple OSS Distributions 			proc_t *p;
2510*1031c584SApple OSS Distributions 			pid_t pid = probe->ftps_pid;
2511*1031c584SApple OSS Distributions 
2512*1031c584SApple OSS Distributions 			/*
2513*1031c584SApple OSS Distributions 			 * Report an error if the process doesn't exist
2514*1031c584SApple OSS Distributions 			 * or is actively being birthed.
2515*1031c584SApple OSS Distributions 			 */
2516*1031c584SApple OSS Distributions 			if ((p = proc_find(pid)) == PROC_NULL || p->p_stat == SIDL) {
2517*1031c584SApple OSS Distributions 				if (p != PROC_NULL)
2518*1031c584SApple OSS Distributions 					proc_rele(p);
2519*1031c584SApple OSS Distributions 				ret = ESRCH;
2520*1031c584SApple OSS Distributions 				goto err;
2521*1031c584SApple OSS Distributions 			}
2522*1031c584SApple OSS Distributions 
2523*1031c584SApple OSS Distributions 			ret = fasttrap_check_cred_priv(cr, p);
2524*1031c584SApple OSS Distributions 			if (ret != 0) {
2525*1031c584SApple OSS Distributions 				goto err;
2526*1031c584SApple OSS Distributions 			}
2527*1031c584SApple OSS Distributions 		}
2528*1031c584SApple OSS Distributions 
2529*1031c584SApple OSS Distributions 		ret = fasttrap_add_probe(probe);
2530*1031c584SApple OSS Distributions 
2531*1031c584SApple OSS Distributions err:
2532*1031c584SApple OSS Distributions 		kmem_free(probe, size);
2533*1031c584SApple OSS Distributions 
2534*1031c584SApple OSS Distributions 		return (ret);
2535*1031c584SApple OSS Distributions 
2536*1031c584SApple OSS Distributions 	} else if (cmd == FASTTRAPIOC_GETINSTR) {
2537*1031c584SApple OSS Distributions 		fasttrap_instr_query_t instr;
2538*1031c584SApple OSS Distributions 		fasttrap_tracepoint_t *tp;
2539*1031c584SApple OSS Distributions 		uint_t index;
2540*1031c584SApple OSS Distributions 		int ret;
2541*1031c584SApple OSS Distributions 
2542*1031c584SApple OSS Distributions 		if (copyin(arg, &instr, sizeof (instr)) != 0)
2543*1031c584SApple OSS Distributions 			return (EFAULT);
2544*1031c584SApple OSS Distributions 
2545*1031c584SApple OSS Distributions 		if (!PRIV_POLICY_CHOICE(cr, PRIV_ALL, B_FALSE)) {
2546*1031c584SApple OSS Distributions 			proc_t *p;
2547*1031c584SApple OSS Distributions 			pid_t pid = instr.ftiq_pid;
2548*1031c584SApple OSS Distributions 
2549*1031c584SApple OSS Distributions 			/*
2550*1031c584SApple OSS Distributions 			 * Report an error if the process doesn't exist
2551*1031c584SApple OSS Distributions 			 * or is actively being birthed.
2552*1031c584SApple OSS Distributions 			 */
2553*1031c584SApple OSS Distributions 			if ((p = proc_find(pid)) == NULL || p->p_stat == SIDL) {
2554*1031c584SApple OSS Distributions 				if (p != PROC_NULL)
2555*1031c584SApple OSS Distributions 					proc_rele(p);
2556*1031c584SApple OSS Distributions 				return (ESRCH);
2557*1031c584SApple OSS Distributions 			}
2558*1031c584SApple OSS Distributions 
2559*1031c584SApple OSS Distributions 			ret = fasttrap_check_cred_priv(cr, p);
2560*1031c584SApple OSS Distributions 			if (ret != 0) {
2561*1031c584SApple OSS Distributions 				return (ret);
2562*1031c584SApple OSS Distributions 			}
2563*1031c584SApple OSS Distributions 		}
2564*1031c584SApple OSS Distributions 
2565*1031c584SApple OSS Distributions 		index = FASTTRAP_TPOINTS_INDEX(instr.ftiq_pid, instr.ftiq_pc);
2566*1031c584SApple OSS Distributions 
2567*1031c584SApple OSS Distributions 		lck_mtx_lock(&fasttrap_tpoints.fth_table[index].ftb_mtx);
2568*1031c584SApple OSS Distributions 		tp = fasttrap_tpoints.fth_table[index].ftb_data;
2569*1031c584SApple OSS Distributions 		while (tp != NULL) {
2570*1031c584SApple OSS Distributions 			if (instr.ftiq_pid == tp->ftt_pid &&
2571*1031c584SApple OSS Distributions 			    instr.ftiq_pc == tp->ftt_pc &&
2572*1031c584SApple OSS Distributions 			    tp->ftt_proc->ftpc_acount != 0)
2573*1031c584SApple OSS Distributions 				break;
2574*1031c584SApple OSS Distributions 
2575*1031c584SApple OSS Distributions 			tp = tp->ftt_next;
2576*1031c584SApple OSS Distributions 		}
2577*1031c584SApple OSS Distributions 
2578*1031c584SApple OSS Distributions 		if (tp == NULL) {
2579*1031c584SApple OSS Distributions 			lck_mtx_unlock(&fasttrap_tpoints.fth_table[index].ftb_mtx);
2580*1031c584SApple OSS Distributions 			return (ENOENT);
2581*1031c584SApple OSS Distributions 		}
2582*1031c584SApple OSS Distributions 
2583*1031c584SApple OSS Distributions 		bcopy(&tp->ftt_instr, &instr.ftiq_instr,
2584*1031c584SApple OSS Distributions 		    sizeof (instr.ftiq_instr));
2585*1031c584SApple OSS Distributions 		lck_mtx_unlock(&fasttrap_tpoints.fth_table[index].ftb_mtx);
2586*1031c584SApple OSS Distributions 
2587*1031c584SApple OSS Distributions 		if (copyout(&instr, arg, sizeof (instr)) != 0)
2588*1031c584SApple OSS Distributions 			return (EFAULT);
2589*1031c584SApple OSS Distributions 
2590*1031c584SApple OSS Distributions 		return (0);
2591*1031c584SApple OSS Distributions 	}
2592*1031c584SApple OSS Distributions 
2593*1031c584SApple OSS Distributions 	return (EINVAL);
2594*1031c584SApple OSS Distributions }
2595*1031c584SApple OSS Distributions 
2596*1031c584SApple OSS Distributions static void
fasttrap_attach(void)2597*1031c584SApple OSS Distributions fasttrap_attach(void)
2598*1031c584SApple OSS Distributions {
2599*1031c584SApple OSS Distributions 	ulong_t nent;
2600*1031c584SApple OSS Distributions 	unsigned int i;
2601*1031c584SApple OSS Distributions 
2602*1031c584SApple OSS Distributions 	/*
2603*1031c584SApple OSS Distributions 	 * Install our hooks into fork(2), exec(2), and exit(2).
2604*1031c584SApple OSS Distributions 	 */
2605*1031c584SApple OSS Distributions 	dtrace_fasttrap_fork_ptr = &fasttrap_fork;
2606*1031c584SApple OSS Distributions 	dtrace_fasttrap_exit_ptr = &fasttrap_exec_exit;
2607*1031c584SApple OSS Distributions 	dtrace_fasttrap_exec_ptr = &fasttrap_exec_exit;
2608*1031c584SApple OSS Distributions 
2609*1031c584SApple OSS Distributions 	/*
2610*1031c584SApple OSS Distributions 	 * APPLE NOTE:  We size the maximum number of fasttrap probes
2611*1031c584SApple OSS Distributions 	 * based on system memory. 100k probes per 256M of system memory.
2612*1031c584SApple OSS Distributions 	 * Yes, this is a WAG.
2613*1031c584SApple OSS Distributions 	 */
2614*1031c584SApple OSS Distributions 	fasttrap_max = (sane_size >> 28) * 100000;
2615*1031c584SApple OSS Distributions 
2616*1031c584SApple OSS Distributions 	if (fasttrap_max == 0)
2617*1031c584SApple OSS Distributions 		fasttrap_max = 50000;
2618*1031c584SApple OSS Distributions 
2619*1031c584SApple OSS Distributions 	fasttrap_total = 0;
2620*1031c584SApple OSS Distributions 	fasttrap_retired = 0;
2621*1031c584SApple OSS Distributions 
2622*1031c584SApple OSS Distributions 	/*
2623*1031c584SApple OSS Distributions 	 * Conjure up the tracepoints hashtable...
2624*1031c584SApple OSS Distributions 	 */
2625*1031c584SApple OSS Distributions #ifdef illumos
2626*1031c584SApple OSS Distributions 	nent = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
2627*1031c584SApple OSS Distributions 	    "fasttrap-hash-size", FASTTRAP_TPOINTS_DEFAULT_SIZE);
2628*1031c584SApple OSS Distributions #else
2629*1031c584SApple OSS Distributions 	nent = FASTTRAP_TPOINTS_DEFAULT_SIZE;
2630*1031c584SApple OSS Distributions #endif
2631*1031c584SApple OSS Distributions 
2632*1031c584SApple OSS Distributions 	if (nent <= 0 || nent > 0x1000000)
2633*1031c584SApple OSS Distributions 		nent = FASTTRAP_TPOINTS_DEFAULT_SIZE;
2634*1031c584SApple OSS Distributions 
2635*1031c584SApple OSS Distributions 	if ((nent & (nent - 1)) == 0)
2636*1031c584SApple OSS Distributions 		fasttrap_tpoints.fth_nent = nent;
2637*1031c584SApple OSS Distributions 	else
2638*1031c584SApple OSS Distributions 		fasttrap_tpoints.fth_nent = 1 << fasttrap_highbit(nent);
2639*1031c584SApple OSS Distributions 	ASSERT(fasttrap_tpoints.fth_nent > 0);
2640*1031c584SApple OSS Distributions 	fasttrap_tpoints.fth_mask = fasttrap_tpoints.fth_nent - 1;
2641*1031c584SApple OSS Distributions 	fasttrap_tpoints.fth_table = kmem_zalloc(fasttrap_tpoints.fth_nent *
2642*1031c584SApple OSS Distributions 	    sizeof (fasttrap_bucket_t), KM_SLEEP);
2643*1031c584SApple OSS Distributions 	ASSERT(fasttrap_tpoints.fth_table != NULL);
2644*1031c584SApple OSS Distributions 
2645*1031c584SApple OSS Distributions 	for (i = 0; i < fasttrap_tpoints.fth_nent; i++) {
2646*1031c584SApple OSS Distributions 		lck_mtx_init(&fasttrap_tpoints.fth_table[i].ftb_mtx, &fasttrap_lck_grp,
2647*1031c584SApple OSS Distributions 		    &fasttrap_lck_attr);
2648*1031c584SApple OSS Distributions 	}
2649*1031c584SApple OSS Distributions 
2650*1031c584SApple OSS Distributions 	/*
2651*1031c584SApple OSS Distributions 	 * ... and the providers hash table...
2652*1031c584SApple OSS Distributions 	 */
2653*1031c584SApple OSS Distributions 	nent = FASTTRAP_PROVIDERS_DEFAULT_SIZE;
2654*1031c584SApple OSS Distributions 	if ((nent & (nent - 1)) == 0)
2655*1031c584SApple OSS Distributions 		fasttrap_provs.fth_nent = nent;
2656*1031c584SApple OSS Distributions 	else
2657*1031c584SApple OSS Distributions 		fasttrap_provs.fth_nent = 1 << fasttrap_highbit(nent);
2658*1031c584SApple OSS Distributions 	ASSERT(fasttrap_provs.fth_nent > 0);
2659*1031c584SApple OSS Distributions 	fasttrap_provs.fth_mask = fasttrap_provs.fth_nent - 1;
2660*1031c584SApple OSS Distributions 	fasttrap_provs.fth_table = kmem_zalloc(fasttrap_provs.fth_nent *
2661*1031c584SApple OSS Distributions 	    sizeof (fasttrap_bucket_t), KM_SLEEP);
2662*1031c584SApple OSS Distributions 	ASSERT(fasttrap_provs.fth_table != NULL);
2663*1031c584SApple OSS Distributions 
2664*1031c584SApple OSS Distributions 	for (i = 0; i < fasttrap_provs.fth_nent; i++) {
2665*1031c584SApple OSS Distributions 		lck_mtx_init(&fasttrap_provs.fth_table[i].ftb_mtx, &fasttrap_lck_grp,
2666*1031c584SApple OSS Distributions 		    &fasttrap_lck_attr);
2667*1031c584SApple OSS Distributions 	}
2668*1031c584SApple OSS Distributions 
2669*1031c584SApple OSS Distributions 	/*
2670*1031c584SApple OSS Distributions 	 * ... and the procs hash table.
2671*1031c584SApple OSS Distributions 	 */
2672*1031c584SApple OSS Distributions 	nent = FASTTRAP_PROCS_DEFAULT_SIZE;
2673*1031c584SApple OSS Distributions 	if ((nent & (nent - 1)) == 0)
2674*1031c584SApple OSS Distributions 		fasttrap_procs.fth_nent = nent;
2675*1031c584SApple OSS Distributions 	else
2676*1031c584SApple OSS Distributions 		fasttrap_procs.fth_nent = 1 << fasttrap_highbit(nent);
2677*1031c584SApple OSS Distributions 	ASSERT(fasttrap_procs.fth_nent > 0);
2678*1031c584SApple OSS Distributions 	fasttrap_procs.fth_mask = fasttrap_procs.fth_nent - 1;
2679*1031c584SApple OSS Distributions 	fasttrap_procs.fth_table = kmem_zalloc(fasttrap_procs.fth_nent *
2680*1031c584SApple OSS Distributions 	    sizeof (fasttrap_bucket_t), KM_SLEEP);
2681*1031c584SApple OSS Distributions 	ASSERT(fasttrap_procs.fth_table != NULL);
2682*1031c584SApple OSS Distributions 
2683*1031c584SApple OSS Distributions #ifndef illumos
2684*1031c584SApple OSS Distributions 	for (i = 0; i < fasttrap_procs.fth_nent; i++) {
2685*1031c584SApple OSS Distributions 		lck_mtx_init(&fasttrap_procs.fth_table[i].ftb_mtx, &fasttrap_lck_grp,
2686*1031c584SApple OSS Distributions 		    &fasttrap_lck_attr);
2687*1031c584SApple OSS Distributions 	}
2688*1031c584SApple OSS Distributions #endif
2689*1031c584SApple OSS Distributions 
2690*1031c584SApple OSS Distributions 	(void) dtrace_meta_register("fasttrap", &fasttrap_mops, NULL,
2691*1031c584SApple OSS Distributions 	    &fasttrap_meta_id);
2692*1031c584SApple OSS Distributions }
2693*1031c584SApple OSS Distributions 
2694*1031c584SApple OSS Distributions static int
_fasttrap_open(dev_t dev,int flags,int devtype,struct proc * p)2695*1031c584SApple OSS Distributions _fasttrap_open(dev_t dev, int flags, int devtype, struct proc *p)
2696*1031c584SApple OSS Distributions {
2697*1031c584SApple OSS Distributions #pragma unused(dev, flags, devtype, p)
2698*1031c584SApple OSS Distributions 	return  0;
2699*1031c584SApple OSS Distributions }
2700*1031c584SApple OSS Distributions 
2701*1031c584SApple OSS Distributions static int
_fasttrap_ioctl(dev_t dev,u_long cmd,caddr_t data,int fflag,struct proc * p)2702*1031c584SApple OSS Distributions _fasttrap_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct proc *p)
2703*1031c584SApple OSS Distributions {
2704*1031c584SApple OSS Distributions 	int err, rv = 0;
2705*1031c584SApple OSS Distributions 	user_addr_t uaddrp;
2706*1031c584SApple OSS Distributions 
2707*1031c584SApple OSS Distributions 	if (proc_is64bit(p)) {
2708*1031c584SApple OSS Distributions 		uaddrp = *(user_addr_t *)data;
2709*1031c584SApple OSS Distributions 	} else {
2710*1031c584SApple OSS Distributions 		uaddrp = (user_addr_t) *(uint32_t *)data;
2711*1031c584SApple OSS Distributions 	}
2712*1031c584SApple OSS Distributions 
2713*1031c584SApple OSS Distributions 	err = fasttrap_ioctl(dev, cmd, uaddrp, fflag, CRED(), &rv);
2714*1031c584SApple OSS Distributions 
2715*1031c584SApple OSS Distributions 	/* XXX Darwin's BSD ioctls only return -1 or zero. Overload errno to mimic Solaris. 20 bits suffice. */
2716*1031c584SApple OSS Distributions 	if (err != 0) {
2717*1031c584SApple OSS Distributions 		ASSERT( (err & 0xfffff000) == 0 );
2718*1031c584SApple OSS Distributions 		return (err & 0xfff); /* ioctl returns -1 and errno set to an error code < 4096 */
2719*1031c584SApple OSS Distributions 	} else if (rv != 0) {
2720*1031c584SApple OSS Distributions 		ASSERT( (rv & 0xfff00000) == 0 );
2721*1031c584SApple OSS Distributions 		return (((rv & 0xfffff) << 12)); /* ioctl returns -1 and errno set to a return value >= 4096 */
2722*1031c584SApple OSS Distributions 	} else
2723*1031c584SApple OSS Distributions 		return 0;
2724*1031c584SApple OSS Distributions }
2725*1031c584SApple OSS Distributions 
2726*1031c584SApple OSS Distributions static int fasttrap_inited = 0;
2727*1031c584SApple OSS Distributions 
2728*1031c584SApple OSS Distributions #define FASTTRAP_MAJOR  -24 /* let the kernel pick the device number */
2729*1031c584SApple OSS Distributions 
2730*1031c584SApple OSS Distributions static const struct cdevsw fasttrap_cdevsw =
2731*1031c584SApple OSS Distributions {
2732*1031c584SApple OSS Distributions 	.d_open = _fasttrap_open,
2733*1031c584SApple OSS Distributions 	.d_close = eno_opcl,
2734*1031c584SApple OSS Distributions 	.d_read = eno_rdwrt,
2735*1031c584SApple OSS Distributions 	.d_write = eno_rdwrt,
2736*1031c584SApple OSS Distributions 	.d_ioctl = _fasttrap_ioctl,
2737*1031c584SApple OSS Distributions 	.d_stop = eno_stop,
2738*1031c584SApple OSS Distributions 	.d_reset = eno_reset,
2739*1031c584SApple OSS Distributions 	.d_select = eno_select,
2740*1031c584SApple OSS Distributions 	.d_mmap = eno_mmap,
2741*1031c584SApple OSS Distributions 	.d_strategy = eno_strat,
2742*1031c584SApple OSS Distributions 	.d_reserved_1 = eno_getc,
2743*1031c584SApple OSS Distributions 	.d_reserved_2 = eno_putc,
2744*1031c584SApple OSS Distributions };
2745*1031c584SApple OSS Distributions 
2746*1031c584SApple OSS Distributions void fasttrap_init(void);
2747*1031c584SApple OSS Distributions 
2748*1031c584SApple OSS Distributions void
fasttrap_init(void)2749*1031c584SApple OSS Distributions fasttrap_init( void )
2750*1031c584SApple OSS Distributions {
2751*1031c584SApple OSS Distributions 	/*
2752*1031c584SApple OSS Distributions 	 * This method is now invoked from multiple places. Any open of /dev/dtrace,
2753*1031c584SApple OSS Distributions 	 * also dtrace_init if the dtrace_dof_mode is DTRACE_DOF_MODE_NON_LAZY.
2754*1031c584SApple OSS Distributions 	 *
2755*1031c584SApple OSS Distributions 	 * The reason is to delay allocating the (rather large) resources as late as possible.
2756*1031c584SApple OSS Distributions 	 */
2757*1031c584SApple OSS Distributions 	if (!fasttrap_inited) {
2758*1031c584SApple OSS Distributions 		int majdevno = cdevsw_add(FASTTRAP_MAJOR, &fasttrap_cdevsw);
2759*1031c584SApple OSS Distributions 
2760*1031c584SApple OSS Distributions 		if (majdevno < 0) {
2761*1031c584SApple OSS Distributions 			// FIX ME! What kind of error reporting to do here?
2762*1031c584SApple OSS Distributions 			printf("fasttrap_init: failed to allocate a major number!\n");
2763*1031c584SApple OSS Distributions 			return;
2764*1031c584SApple OSS Distributions 		}
2765*1031c584SApple OSS Distributions 
2766*1031c584SApple OSS Distributions 		dev_t device = makedev( (uint32_t)majdevno, 0 );
2767*1031c584SApple OSS Distributions 		if (NULL == devfs_make_node( device, DEVFS_CHAR, UID_ROOT, GID_WHEEL, 0666, "fasttrap" )) {
2768*1031c584SApple OSS Distributions 			return;
2769*1031c584SApple OSS Distributions 		}
2770*1031c584SApple OSS Distributions 
2771*1031c584SApple OSS Distributions 		/*
2772*1031c584SApple OSS Distributions 		 * fasttrap_probe_t's are variable in size. We use an array of zones to
2773*1031c584SApple OSS Distributions 		 * cover the most common sizes.
2774*1031c584SApple OSS Distributions 		 */
2775*1031c584SApple OSS Distributions 		int i;
2776*1031c584SApple OSS Distributions 		for (i=1; i<FASTTRAP_PROBE_T_ZONE_MAX_TRACEPOINTS; i++) {
2777*1031c584SApple OSS Distributions 			fasttrap_probe_t_zones[i] =
2778*1031c584SApple OSS Distributions 			    zone_create(fasttrap_probe_t_zone_names[i],
2779*1031c584SApple OSS Distributions 				    offsetof(fasttrap_probe_t, ftp_tps[i]), ZC_NONE);
2780*1031c584SApple OSS Distributions 		}
2781*1031c584SApple OSS Distributions 
2782*1031c584SApple OSS Distributions 
2783*1031c584SApple OSS Distributions 		fasttrap_attach();
2784*1031c584SApple OSS Distributions 
2785*1031c584SApple OSS Distributions 		/*
2786*1031c584SApple OSS Distributions 		 * Start the fasttrap cleanup thread
2787*1031c584SApple OSS Distributions 		 */
2788*1031c584SApple OSS Distributions 		kern_return_t res = kernel_thread_start_priority((thread_continue_t)fasttrap_pid_cleanup_cb, NULL, 46 /* BASEPRI_BACKGROUND */, &fasttrap_cleanup_thread);
2789*1031c584SApple OSS Distributions 		if (res != KERN_SUCCESS) {
2790*1031c584SApple OSS Distributions 			panic("Could not create fasttrap_cleanup_thread");
2791*1031c584SApple OSS Distributions 		}
2792*1031c584SApple OSS Distributions 		thread_set_thread_name(fasttrap_cleanup_thread, "dtrace_fasttrap_cleanup_thread");
2793*1031c584SApple OSS Distributions 
2794*1031c584SApple OSS Distributions 		fasttrap_retired_size = DEFAULT_RETIRED_SIZE;
2795*1031c584SApple OSS Distributions 		fasttrap_retired_spec = kmem_zalloc(fasttrap_retired_size * sizeof(*fasttrap_retired_spec),
2796*1031c584SApple OSS Distributions 					KM_SLEEP);
2797*1031c584SApple OSS Distributions 
2798*1031c584SApple OSS Distributions 		fasttrap_inited = 1;
2799*1031c584SApple OSS Distributions 	}
2800*1031c584SApple OSS Distributions }
2801*1031c584SApple OSS Distributions 
2802*1031c584SApple OSS Distributions #undef FASTTRAP_MAJOR
2803