1179193Sjb/*
2179193Sjb * CDDL HEADER START
3179193Sjb *
4179193Sjb * The contents of this file are subject to the terms of the
5179193Sjb * Common Development and Distribution License (the "License").
6179193Sjb * You may not use this file except in compliance with the License.
7179193Sjb *
8179193Sjb * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9179193Sjb * or http://www.opensolaris.org/os/licensing.
10179193Sjb * See the License for the specific language governing permissions
11179193Sjb * and limitations under the License.
12179193Sjb *
13179193Sjb * When distributing Covered Code, include this CDDL HEADER in each
14179193Sjb * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15179193Sjb * If applicable, add the following below this CDDL HEADER, with the
16179193Sjb * fields enclosed by brackets "[]" replaced with your own identifying
17179193Sjb * information: Portions Copyright [yyyy] [name of copyright owner]
18179193Sjb *
19179193Sjb * CDDL HEADER END
20211738Srpaulo *
21211738Srpaulo * Portions Copyright 2010 The FreeBSD Foundation
22211738Srpaulo *
23211738Srpaulo * $FreeBSD$
24179193Sjb */
25179193Sjb
26179193Sjb/*
27179198Sjb * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
28179193Sjb * Use is subject to license terms.
29179193Sjb */
30179193Sjb
31268734Spfg/*
32268734Spfg * Copyright (c) 2013, Joyent, Inc. All rights reserved.
33268734Spfg */
34179193Sjb
35179193Sjb#include <sys/atomic.h>
36179193Sjb#include <sys/errno.h>
37179193Sjb#include <sys/stat.h>
38179193Sjb#include <sys/modctl.h>
39179193Sjb#include <sys/conf.h>
40179193Sjb#include <sys/systm.h>
41297077Smav#ifdef illumos
42179193Sjb#include <sys/ddi.h>
43211738Srpaulo#endif
44179193Sjb#include <sys/sunddi.h>
45179193Sjb#include <sys/cpuvar.h>
46179193Sjb#include <sys/kmem.h>
47297077Smav#ifdef illumos
48179193Sjb#include <sys/strsubr.h>
49211738Srpaulo#endif
50179193Sjb#include <sys/fasttrap.h>
51179193Sjb#include <sys/fasttrap_impl.h>
52179193Sjb#include <sys/fasttrap_isa.h>
53179193Sjb#include <sys/dtrace.h>
54179193Sjb#include <sys/dtrace_impl.h>
55179193Sjb#include <sys/sysmacros.h>
56179193Sjb#include <sys/proc.h>
57179193Sjb#include <sys/policy.h>
58297077Smav#ifdef illumos
59179193Sjb#include <util/qsort.h>
60211738Srpaulo#endif
61211738Srpaulo#include <sys/mutex.h>
62211738Srpaulo#include <sys/kernel.h>
63297077Smav#ifndef illumos
64269342Smarkj#include <sys/dtrace_bsd.h>
65269342Smarkj#include <sys/eventhandler.h>
66299003Smarkj#include <sys/rmlock.h>
67293413Sstas#include <sys/sysctl.h>
68293413Sstas#include <sys/u8_textprep.h>
69211738Srpaulo#include <sys/user.h>
70299003Smarkj
71269342Smarkj#include <vm/vm.h>
72269342Smarkj#include <vm/pmap.h>
73269342Smarkj#include <vm/vm_map.h>
74269342Smarkj#include <vm/vm_param.h>
75299003Smarkj
76211738Srpaulo#include <cddl/dev/dtrace/dtrace_cddl.h>
77211738Srpaulo#endif
78179193Sjb
79179193Sjb/*
80179193Sjb * User-Land Trap-Based Tracing
81179193Sjb * ----------------------------
82179193Sjb *
83179193Sjb * The fasttrap provider allows DTrace consumers to instrument any user-level
84179193Sjb * instruction to gather data; this includes probes with semantic
85179193Sjb * signifigance like entry and return as well as simple offsets into the
86179193Sjb * function. While the specific techniques used are very ISA specific, the
87179193Sjb * methodology is generalizable to any architecture.
88179193Sjb *
89179193Sjb *
90179193Sjb * The General Methodology
91179193Sjb * -----------------------
92179193Sjb *
93179193Sjb * With the primary goal of tracing every user-land instruction and the
94179193Sjb * limitation that we can't trust user space so don't want to rely on much
95179193Sjb * information there, we begin by replacing the instructions we want to trace
96179193Sjb * with trap instructions. Each instruction we overwrite is saved into a hash
97179193Sjb * table keyed by process ID and pc address. When we enter the kernel due to
98179193Sjb * this trap instruction, we need the effects of the replaced instruction to
99179193Sjb * appear to have occurred before we proceed with the user thread's
100179193Sjb * execution.
101179193Sjb *
102179193Sjb * Each user level thread is represented by a ulwp_t structure which is
103179193Sjb * always easily accessible through a register. The most basic way to produce
104179193Sjb * the effects of the instruction we replaced is to copy that instruction out
105179193Sjb * to a bit of scratch space reserved in the user thread's ulwp_t structure
106179193Sjb * (a sort of kernel-private thread local storage), set the PC to that
107179193Sjb * scratch space and single step. When we reenter the kernel after single
108179193Sjb * stepping the instruction we must then adjust the PC to point to what would
109179193Sjb * normally be the next instruction. Of course, special care must be taken
110179193Sjb * for branches and jumps, but these represent such a small fraction of any
111179193Sjb * instruction set that writing the code to emulate these in the kernel is
112179193Sjb * not too difficult.
113179193Sjb *
114179193Sjb * Return probes may require several tracepoints to trace every return site,
115179193Sjb * and, conversely, each tracepoint may activate several probes (the entry
116179193Sjb * and offset 0 probes, for example). To solve this muliplexing problem,
117179193Sjb * tracepoints contain lists of probes to activate and probes contain lists
118179193Sjb * of tracepoints to enable. If a probe is activated, it adds its ID to
119179193Sjb * existing tracepoints or creates new ones as necessary.
120179193Sjb *
121179193Sjb * Most probes are activated _before_ the instruction is executed, but return
122179193Sjb * probes are activated _after_ the effects of the last instruction of the
123179193Sjb * function are visible. Return probes must be fired _after_ we have
124179193Sjb * single-stepped the instruction whereas all other probes are fired
125179193Sjb * beforehand.
126179193Sjb *
127179193Sjb *
128179193Sjb * Lock Ordering
129179193Sjb * -------------
130179193Sjb *
131179193Sjb * The lock ordering below -- both internally and with respect to the DTrace
132179193Sjb * framework -- is a little tricky and bears some explanation. Each provider
133179193Sjb * has a lock (ftp_mtx) that protects its members including reference counts
134179193Sjb * for enabled probes (ftp_rcount), consumers actively creating probes
135179193Sjb * (ftp_ccount) and USDT consumers (ftp_mcount); all three prevent a provider
136179193Sjb * from being freed. A provider is looked up by taking the bucket lock for the
137179193Sjb * provider hash table, and is returned with its lock held. The provider lock
138179193Sjb * may be taken in functions invoked by the DTrace framework, but may not be
139179193Sjb * held while calling functions in the DTrace framework.
140179193Sjb *
141179193Sjb * To ensure consistency over multiple calls to the DTrace framework, the
142179193Sjb * creation lock (ftp_cmtx) should be held. Naturally, the creation lock may
143179193Sjb * not be taken when holding the provider lock as that would create a cyclic
144179193Sjb * lock ordering. In situations where one would naturally take the provider
145179193Sjb * lock and then the creation lock, we instead up a reference count to prevent
146179193Sjb * the provider from disappearing, drop the provider lock, and acquire the
147179193Sjb * creation lock.
148179193Sjb *
149179193Sjb * Briefly:
150179193Sjb * 	bucket lock before provider lock
151179193Sjb *	DTrace before provider lock
152179193Sjb *	creation lock before DTrace
153179193Sjb *	never hold the provider lock and creation lock simultaneously
154179193Sjb */
155179193Sjb
156211738Srpaulostatic d_open_t fasttrap_open;
157211738Srpaulostatic d_ioctl_t fasttrap_ioctl;
158211738Srpaulo
159211738Srpaulostatic struct cdevsw fasttrap_cdevsw = {
160211738Srpaulo	.d_version	= D_VERSION,
161211738Srpaulo	.d_open		= fasttrap_open,
162211738Srpaulo	.d_ioctl	= fasttrap_ioctl,
163211738Srpaulo	.d_name		= "fasttrap",
164211738Srpaulo};
165211738Srpaulostatic struct cdev *fasttrap_cdev;
166179193Sjbstatic dtrace_meta_provider_id_t fasttrap_meta_id;
167179193Sjb
168250953Smarkjstatic struct proc *fasttrap_cleanup_proc;
169211738Srpaulostatic struct mtx fasttrap_cleanup_mtx;
170250953Smarkjstatic uint_t fasttrap_cleanup_work, fasttrap_cleanup_drain, fasttrap_cleanup_cv;
171179193Sjb
172179193Sjb/*
173179193Sjb * Generation count on modifications to the global tracepoint lookup table.
174179193Sjb */
175179193Sjbstatic volatile uint64_t fasttrap_mod_gen;
176179193Sjb
177179193Sjb/*
178179193Sjb * When the fasttrap provider is loaded, fasttrap_max is set to either
179293413Sstas * FASTTRAP_MAX_DEFAULT, or the value for fasttrap-max-probes in the
180293413Sstas * fasttrap.conf file (Illumos), or the value provied in the loader.conf (FreeBSD).
181293413Sstas * Each time a probe is created, fasttrap_total is incremented by the number
182293413Sstas * of tracepoints that may be associated with that probe; fasttrap_total is capped
183293413Sstas * at fasttrap_max.
184179193Sjb */
185179193Sjb#define	FASTTRAP_MAX_DEFAULT		250000
186293413Sstasstatic uint32_t fasttrap_max = FASTTRAP_MAX_DEFAULT;
187179193Sjbstatic uint32_t fasttrap_total;
188179193Sjb
189248983Spfg/*
190248983Spfg * Copyright (c) 2011, Joyent, Inc. All rights reserved.
191248983Spfg */
192179193Sjb
193179193Sjb#define	FASTTRAP_TPOINTS_DEFAULT_SIZE	0x4000
194179193Sjb#define	FASTTRAP_PROVIDERS_DEFAULT_SIZE	0x100
195179193Sjb#define	FASTTRAP_PROCS_DEFAULT_SIZE	0x100
196179193Sjb
197179193Sjb#define	FASTTRAP_PID_NAME		"pid"
198179193Sjb
199179193Sjbfasttrap_hash_t			fasttrap_tpoints;
200179193Sjbstatic fasttrap_hash_t		fasttrap_provs;
201179193Sjbstatic fasttrap_hash_t		fasttrap_procs;
202179193Sjb
203179193Sjbstatic uint64_t			fasttrap_pid_count;	/* pid ref count */
204179193Sjbstatic kmutex_t			fasttrap_count_mtx;	/* lock on ref count */
205179193Sjb
206179193Sjb#define	FASTTRAP_ENABLE_FAIL	1
207179193Sjb#define	FASTTRAP_ENABLE_PARTIAL	2
208179193Sjb
209179193Sjbstatic int fasttrap_tracepoint_enable(proc_t *, fasttrap_probe_t *, uint_t);
210179193Sjbstatic void fasttrap_tracepoint_disable(proc_t *, fasttrap_probe_t *, uint_t);
211179193Sjb
212179193Sjbstatic fasttrap_provider_t *fasttrap_provider_lookup(pid_t, const char *,
213179193Sjb    const dtrace_pattr_t *);
214179193Sjbstatic void fasttrap_provider_retire(pid_t, const char *, int);
215179193Sjbstatic void fasttrap_provider_free(fasttrap_provider_t *);
216179193Sjb
217179193Sjbstatic fasttrap_proc_t *fasttrap_proc_lookup(pid_t);
218179193Sjbstatic void fasttrap_proc_release(fasttrap_proc_t *);
219179193Sjb
220297077Smav#ifndef illumos
221269342Smarkjstatic void fasttrap_thread_dtor(void *, struct thread *);
222269342Smarkj#endif
223269342Smarkj
224179193Sjb#define	FASTTRAP_PROVS_INDEX(pid, name) \
225179193Sjb	((fasttrap_hash_str(name) + (pid)) & fasttrap_provs.fth_mask)
226179193Sjb
227179193Sjb#define	FASTTRAP_PROCS_INDEX(pid) ((pid) & fasttrap_procs.fth_mask)
228179193Sjb
229297077Smav#ifndef illumos
230299003Smarkjstruct rmlock fasttrap_tp_lock;
231269342Smarkjstatic eventhandler_tag fasttrap_thread_dtor_tag;
232211925Srpaulo#endif
233211925Srpaulo
234293413Sstasstatic unsigned long tpoints_hash_size = FASTTRAP_TPOINTS_DEFAULT_SIZE;
235293413Sstas
236293413Sstas#ifdef __FreeBSD__
237293413SstasSYSCTL_DECL(_kern_dtrace);
238293413SstasSYSCTL_NODE(_kern_dtrace, OID_AUTO, fasttrap, CTLFLAG_RD, 0, "DTrace fasttrap parameters");
239293413SstasSYSCTL_UINT(_kern_dtrace_fasttrap, OID_AUTO, max_probes, CTLFLAG_RWTUN, &fasttrap_max,
240293413Sstas    FASTTRAP_MAX_DEFAULT, "Maximum number of fasttrap probes");
241293413SstasSYSCTL_ULONG(_kern_dtrace_fasttrap, OID_AUTO, tpoints_hash_size, CTLFLAG_RDTUN, &tpoints_hash_size,
242293413Sstas    FASTTRAP_TPOINTS_DEFAULT_SIZE, "Size of the tracepoint hash table");
243293413Sstas#endif
244293413Sstas
245179193Sjbstatic int
246179193Sjbfasttrap_highbit(ulong_t i)
247179193Sjb{
248179193Sjb	int h = 1;
249179193Sjb
250179193Sjb	if (i == 0)
251179193Sjb		return (0);
252179193Sjb#ifdef _LP64
253179193Sjb	if (i & 0xffffffff00000000ul) {
254179193Sjb		h += 32; i >>= 32;
255179193Sjb	}
256179193Sjb#endif
257179193Sjb	if (i & 0xffff0000) {
258179193Sjb		h += 16; i >>= 16;
259179193Sjb	}
260179193Sjb	if (i & 0xff00) {
261179193Sjb		h += 8; i >>= 8;
262179193Sjb	}
263179193Sjb	if (i & 0xf0) {
264179193Sjb		h += 4; i >>= 4;
265179193Sjb	}
266179193Sjb	if (i & 0xc) {
267179193Sjb		h += 2; i >>= 2;
268179193Sjb	}
269179193Sjb	if (i & 0x2) {
270179193Sjb		h += 1;
271179193Sjb	}
272179193Sjb	return (h);
273179193Sjb}
274179193Sjb
275179193Sjbstatic uint_t
276179193Sjbfasttrap_hash_str(const char *p)
277179193Sjb{
278179193Sjb	unsigned int g;
279179193Sjb	uint_t hval = 0;
280179193Sjb
281179193Sjb	while (*p) {
282179193Sjb		hval = (hval << 4) + *p++;
283179193Sjb		if ((g = (hval & 0xf0000000)) != 0)
284179193Sjb			hval ^= g >> 24;
285179193Sjb		hval &= ~g;
286179193Sjb	}
287179193Sjb	return (hval);
288179193Sjb}
289179193Sjb
290179193Sjbvoid
291179193Sjbfasttrap_sigtrap(proc_t *p, kthread_t *t, uintptr_t pc)
292179193Sjb{
293297077Smav#ifdef illumos
294179193Sjb	sigqueue_t *sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
295179193Sjb
296179193Sjb	sqp->sq_info.si_signo = SIGTRAP;
297179193Sjb	sqp->sq_info.si_code = TRAP_DTRACE;
298179193Sjb	sqp->sq_info.si_addr = (caddr_t)pc;
299179193Sjb
300179193Sjb	mutex_enter(&p->p_lock);
301179193Sjb	sigaddqa(p, t, sqp);
302179193Sjb	mutex_exit(&p->p_lock);
303179193Sjb
304179193Sjb	if (t != NULL)
305179193Sjb		aston(t);
306211738Srpaulo#else
307211738Srpaulo	ksiginfo_t *ksi = kmem_zalloc(sizeof (ksiginfo_t), KM_SLEEP);
308211738Srpaulo
309211738Srpaulo	ksiginfo_init(ksi);
310211738Srpaulo	ksi->ksi_signo = SIGTRAP;
311211738Srpaulo	ksi->ksi_code = TRAP_DTRACE;
312211738Srpaulo	ksi->ksi_addr = (caddr_t)pc;
313211738Srpaulo	PROC_LOCK(p);
314283677Smarkj	(void) tdsendsignal(p, t, SIGTRAP, ksi);
315211738Srpaulo	PROC_UNLOCK(p);
316211738Srpaulo#endif
317179193Sjb}
318179193Sjb
319297077Smav#ifndef illumos
320179193Sjb/*
321269342Smarkj * Obtain a chunk of scratch space in the address space of the target process.
322269342Smarkj */
323269342Smarkjfasttrap_scrspace_t *
324269342Smarkjfasttrap_scraddr(struct thread *td, fasttrap_proc_t *fprc)
325269342Smarkj{
326269342Smarkj	fasttrap_scrblock_t *scrblk;
327269342Smarkj	fasttrap_scrspace_t *scrspc;
328269342Smarkj	struct proc *p;
329269342Smarkj	vm_offset_t addr;
330269342Smarkj	int error, i;
331269342Smarkj
332269342Smarkj	scrspc = NULL;
333269342Smarkj	if (td->t_dtrace_sscr != NULL) {
334269342Smarkj		/* If the thread already has scratch space, we're done. */
335269342Smarkj		scrspc = (fasttrap_scrspace_t *)td->t_dtrace_sscr;
336269342Smarkj		return (scrspc);
337269342Smarkj	}
338269342Smarkj
339269342Smarkj	p = td->td_proc;
340269342Smarkj
341269342Smarkj	mutex_enter(&fprc->ftpc_mtx);
342269342Smarkj	if (LIST_EMPTY(&fprc->ftpc_fscr)) {
343269342Smarkj		/*
344269342Smarkj		 * No scratch space is available, so we'll map a new scratch
345269342Smarkj		 * space block into the traced process' address space.
346269342Smarkj		 */
347269342Smarkj		addr = 0;
348269342Smarkj		error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0, &addr,
349269342Smarkj		    FASTTRAP_SCRBLOCK_SIZE, 0, VMFS_ANY_SPACE, VM_PROT_ALL,
350269342Smarkj		    VM_PROT_ALL, 0);
351269342Smarkj		if (error != KERN_SUCCESS)
352269342Smarkj			goto done;
353269342Smarkj
354269342Smarkj		scrblk = malloc(sizeof(*scrblk), M_SOLARIS, M_WAITOK);
355269342Smarkj		scrblk->ftsb_addr = addr;
356269342Smarkj		LIST_INSERT_HEAD(&fprc->ftpc_scrblks, scrblk, ftsb_next);
357269342Smarkj
358269342Smarkj		/*
359269342Smarkj		 * Carve the block up into chunks and put them on the free list.
360269342Smarkj		 */
361269342Smarkj		for (i = 0;
362269342Smarkj		    i < FASTTRAP_SCRBLOCK_SIZE / FASTTRAP_SCRSPACE_SIZE; i++) {
363269342Smarkj			scrspc = malloc(sizeof(*scrspc), M_SOLARIS, M_WAITOK);
364269342Smarkj			scrspc->ftss_addr = addr +
365269342Smarkj			    i * FASTTRAP_SCRSPACE_SIZE;
366269342Smarkj			LIST_INSERT_HEAD(&fprc->ftpc_fscr, scrspc,
367269342Smarkj			    ftss_next);
368269342Smarkj		}
369269342Smarkj	}
370269342Smarkj
371269342Smarkj	/*
372269342Smarkj	 * Take the first scratch chunk off the free list, put it on the
373269342Smarkj	 * allocated list, and return its address.
374269342Smarkj	 */
375269342Smarkj	scrspc = LIST_FIRST(&fprc->ftpc_fscr);
376269342Smarkj	LIST_REMOVE(scrspc, ftss_next);
377269342Smarkj	LIST_INSERT_HEAD(&fprc->ftpc_ascr, scrspc, ftss_next);
378269342Smarkj
379269342Smarkj	/*
380269342Smarkj	 * This scratch space is reserved for use by td until the thread exits.
381269342Smarkj	 */
382269342Smarkj	td->t_dtrace_sscr = scrspc;
383269342Smarkj
384269342Smarkjdone:
385269342Smarkj	mutex_exit(&fprc->ftpc_mtx);
386269342Smarkj
387269342Smarkj	return (scrspc);
388269342Smarkj}
389269342Smarkj
390269342Smarkj/*
391269342Smarkj * Return any allocated per-thread scratch space chunks back to the process'
392269342Smarkj * free list.
393269342Smarkj */
394269342Smarkjstatic void
395269342Smarkjfasttrap_thread_dtor(void *arg __unused, struct thread *td)
396269342Smarkj{
397269342Smarkj	fasttrap_bucket_t *bucket;
398269342Smarkj	fasttrap_proc_t *fprc;
399269342Smarkj	fasttrap_scrspace_t *scrspc;
400269342Smarkj	pid_t pid;
401269342Smarkj
402269342Smarkj	if (td->t_dtrace_sscr == NULL)
403269342Smarkj		return;
404269342Smarkj
405269342Smarkj	pid = td->td_proc->p_pid;
406269342Smarkj	bucket = &fasttrap_procs.fth_table[FASTTRAP_PROCS_INDEX(pid)];
407269342Smarkj	fprc = NULL;
408269342Smarkj
409269342Smarkj	/* Look up the fasttrap process handle for this process. */
410269342Smarkj	mutex_enter(&bucket->ftb_mtx);
411269342Smarkj	for (fprc = bucket->ftb_data; fprc != NULL; fprc = fprc->ftpc_next) {
412269342Smarkj		if (fprc->ftpc_pid == pid) {
413269342Smarkj			mutex_enter(&fprc->ftpc_mtx);
414269342Smarkj			mutex_exit(&bucket->ftb_mtx);
415269342Smarkj			break;
416269342Smarkj		}
417269342Smarkj	}
418269342Smarkj	if (fprc == NULL) {
419269342Smarkj		mutex_exit(&bucket->ftb_mtx);
420269342Smarkj		return;
421269342Smarkj	}
422269342Smarkj
423269342Smarkj	scrspc = (fasttrap_scrspace_t *)td->t_dtrace_sscr;
424269342Smarkj	LIST_REMOVE(scrspc, ftss_next);
425269342Smarkj	LIST_INSERT_HEAD(&fprc->ftpc_fscr, scrspc, ftss_next);
426269342Smarkj
427269342Smarkj	mutex_exit(&fprc->ftpc_mtx);
428269342Smarkj}
429269342Smarkj#endif
430269342Smarkj
431269342Smarkj/*
432179193Sjb * This function ensures that no threads are actively using the memory
433179193Sjb * associated with probes that were formerly live.
434179193Sjb */
435179193Sjbstatic void
436179193Sjbfasttrap_mod_barrier(uint64_t gen)
437179193Sjb{
438179193Sjb	int i;
439179193Sjb
440179193Sjb	if (gen < fasttrap_mod_gen)
441179193Sjb		return;
442179193Sjb
443179193Sjb	fasttrap_mod_gen++;
444179193Sjb
445299003Smarkj#ifdef illumos
446211925Srpaulo	CPU_FOREACH(i) {
447211925Srpaulo		mutex_enter(&fasttrap_cpuc_pid_lock[i]);
448211925Srpaulo		mutex_exit(&fasttrap_cpuc_pid_lock[i]);
449179193Sjb	}
450299003Smarkj#else
451299003Smarkj	rm_wlock(&fasttrap_tp_lock);
452299003Smarkj	rm_wunlock(&fasttrap_tp_lock);
453299003Smarkj#endif
454179193Sjb}
455179193Sjb
456179193Sjb/*
457250953Smarkj * This function performs asynchronous cleanup of fasttrap providers. The
458250953Smarkj * Solaris implementation of this mechanism use a timeout that's activated in
459250953Smarkj * fasttrap_pid_cleanup(), but this doesn't work in FreeBSD: one may sleep while
460250953Smarkj * holding the DTrace mutexes, but it is unsafe to sleep in a callout handler.
461250953Smarkj * Thus we use a dedicated process to perform the cleanup when requested.
462179193Sjb */
463179193Sjb/*ARGSUSED*/
464179193Sjbstatic void
465179193Sjbfasttrap_pid_cleanup_cb(void *data)
466179193Sjb{
467179193Sjb	fasttrap_provider_t **fpp, *fp;
468179193Sjb	fasttrap_bucket_t *bucket;
469179193Sjb	dtrace_provider_id_t provid;
470248983Spfg	int i, later = 0, rval;
471179193Sjb
472250953Smarkj	mtx_lock(&fasttrap_cleanup_mtx);
473250953Smarkj	while (!fasttrap_cleanup_drain || later > 0) {
474179193Sjb		fasttrap_cleanup_work = 0;
475211738Srpaulo		mtx_unlock(&fasttrap_cleanup_mtx);
476179193Sjb
477179193Sjb		later = 0;
478179193Sjb
479179193Sjb		/*
480179193Sjb		 * Iterate over all the providers trying to remove the marked
481179193Sjb		 * ones. If a provider is marked but not retired, we just
482179193Sjb		 * have to take a crack at removing it -- it's no big deal if
483179193Sjb		 * we can't.
484179193Sjb		 */
485179193Sjb		for (i = 0; i < fasttrap_provs.fth_nent; i++) {
486179193Sjb			bucket = &fasttrap_provs.fth_table[i];
487179193Sjb			mutex_enter(&bucket->ftb_mtx);
488179193Sjb			fpp = (fasttrap_provider_t **)&bucket->ftb_data;
489179193Sjb
490179193Sjb			while ((fp = *fpp) != NULL) {
491179193Sjb				if (!fp->ftp_marked) {
492179193Sjb					fpp = &fp->ftp_next;
493179193Sjb					continue;
494179193Sjb				}
495179193Sjb
496179193Sjb				mutex_enter(&fp->ftp_mtx);
497179193Sjb
498179193Sjb				/*
499179193Sjb				 * If this provider has consumers actively
500179193Sjb				 * creating probes (ftp_ccount) or is a USDT
501179193Sjb				 * provider (ftp_mcount), we can't unregister
502179193Sjb				 * or even condense.
503179193Sjb				 */
504179193Sjb				if (fp->ftp_ccount != 0 ||
505179193Sjb				    fp->ftp_mcount != 0) {
506179193Sjb					mutex_exit(&fp->ftp_mtx);
507179193Sjb					fp->ftp_marked = 0;
508179193Sjb					continue;
509179193Sjb				}
510179193Sjb
511179193Sjb				if (!fp->ftp_retired || fp->ftp_rcount != 0)
512179193Sjb					fp->ftp_marked = 0;
513179193Sjb
514179193Sjb				mutex_exit(&fp->ftp_mtx);
515179193Sjb
516179193Sjb				/*
517179193Sjb				 * If we successfully unregister this
518179193Sjb				 * provider we can remove it from the hash
519179193Sjb				 * chain and free the memory. If our attempt
520179193Sjb				 * to unregister fails and this is a retired
521179193Sjb				 * provider, increment our flag to try again
522179193Sjb				 * pretty soon. If we've consumed more than
523179193Sjb				 * half of our total permitted number of
524179193Sjb				 * probes call dtrace_condense() to try to
525179193Sjb				 * clean out the unenabled probes.
526179193Sjb				 */
527179193Sjb				provid = fp->ftp_provid;
528248983Spfg				if ((rval = dtrace_unregister(provid)) != 0) {
529179193Sjb					if (fasttrap_total > fasttrap_max / 2)
530179193Sjb						(void) dtrace_condense(provid);
531248983Spfg
532248983Spfg					if (rval == EAGAIN)
533248983Spfg						fp->ftp_marked = 1;
534248983Spfg
535179193Sjb					later += fp->ftp_marked;
536179193Sjb					fpp = &fp->ftp_next;
537179193Sjb				} else {
538179193Sjb					*fpp = fp->ftp_next;
539179193Sjb					fasttrap_provider_free(fp);
540179193Sjb				}
541179193Sjb			}
542179193Sjb			mutex_exit(&bucket->ftb_mtx);
543179193Sjb		}
544250953Smarkj		mtx_lock(&fasttrap_cleanup_mtx);
545179193Sjb
546250953Smarkj		/*
547250953Smarkj		 * If we were unable to retire a provider, try again after a
548250953Smarkj		 * second. This situation can occur in certain circumstances
549250953Smarkj		 * where providers cannot be unregistered even though they have
550250953Smarkj		 * no probes enabled because of an execution of dtrace -l or
551250953Smarkj		 * something similar.
552250953Smarkj		 */
553250953Smarkj		if (later > 0 || fasttrap_cleanup_work ||
554250953Smarkj		    fasttrap_cleanup_drain) {
555250953Smarkj			mtx_unlock(&fasttrap_cleanup_mtx);
556250953Smarkj			pause("ftclean", hz);
557250953Smarkj			mtx_lock(&fasttrap_cleanup_mtx);
558250953Smarkj		} else
559250953Smarkj			mtx_sleep(&fasttrap_cleanup_cv, &fasttrap_cleanup_mtx,
560250953Smarkj			    0, "ftcl", 0);
561179193Sjb	}
562179193Sjb
563179193Sjb	/*
564250953Smarkj	 * Wake up the thread in fasttrap_unload() now that we're done.
565179193Sjb	 */
566250953Smarkj	wakeup(&fasttrap_cleanup_drain);
567250953Smarkj	mtx_unlock(&fasttrap_cleanup_mtx);
568179193Sjb
569250953Smarkj	kthread_exit();
570179193Sjb}
571179193Sjb
572179193Sjb/*
573179193Sjb * Activates the asynchronous cleanup mechanism.
574179193Sjb */
575179193Sjbstatic void
576179193Sjbfasttrap_pid_cleanup(void)
577179193Sjb{
578211738Srpaulo
579211738Srpaulo	mtx_lock(&fasttrap_cleanup_mtx);
580250953Smarkj	if (!fasttrap_cleanup_work) {
581250953Smarkj		fasttrap_cleanup_work = 1;
582250953Smarkj		wakeup(&fasttrap_cleanup_cv);
583250953Smarkj	}
584211738Srpaulo	mtx_unlock(&fasttrap_cleanup_mtx);
585179193Sjb}
586179193Sjb
587179193Sjb/*
588179193Sjb * This is called from cfork() via dtrace_fasttrap_fork(). The child
589179198Sjb * process's address space is (roughly) a copy of the parent process's so
590179193Sjb * we have to remove all the instrumentation we had previously enabled in the
591179193Sjb * parent.
592179193Sjb */
593179193Sjbstatic void
594179193Sjbfasttrap_fork(proc_t *p, proc_t *cp)
595179193Sjb{
596297077Smav#ifndef illumos
597269342Smarkj	fasttrap_scrblock_t *scrblk;
598269342Smarkj	fasttrap_proc_t *fprc = NULL;
599269342Smarkj#endif
600179193Sjb	pid_t ppid = p->p_pid;
601179193Sjb	int i;
602179193Sjb
603297077Smav#ifdef illumos
604179193Sjb	ASSERT(curproc == p);
605179193Sjb	ASSERT(p->p_proc_flag & P_PR_LOCK);
606211738Srpaulo#else
607211738Srpaulo	PROC_LOCK_ASSERT(p, MA_OWNED);
608211738Srpaulo#endif
609297077Smav#ifdef illumos
610179193Sjb	ASSERT(p->p_dtrace_count > 0);
611211738Srpaulo#else
612212357Srpaulo	if (p->p_dtrace_helpers) {
613212357Srpaulo		/*
614212357Srpaulo		 * dtrace_helpers_duplicate() allocates memory.
615212357Srpaulo		 */
616212494Srpaulo		_PHOLD(cp);
617212357Srpaulo		PROC_UNLOCK(p);
618212357Srpaulo		PROC_UNLOCK(cp);
619212357Srpaulo		dtrace_helpers_duplicate(p, cp);
620212357Srpaulo		PROC_LOCK(cp);
621212357Srpaulo		PROC_LOCK(p);
622212494Srpaulo		_PRELE(cp);
623212357Srpaulo	}
624211738Srpaulo	/*
625211738Srpaulo	 * This check is purposely here instead of in kern_fork.c because,
626211738Srpaulo	 * for legal resons, we cannot include the dtrace_cddl.h header
627211738Srpaulo	 * inside kern_fork.c and insert if-clause there.
628211738Srpaulo	 */
629211738Srpaulo	if (p->p_dtrace_count == 0)
630211738Srpaulo		return;
631211738Srpaulo#endif
632179193Sjb	ASSERT(cp->p_dtrace_count == 0);
633179193Sjb
634179193Sjb	/*
635179193Sjb	 * This would be simpler and faster if we maintained per-process
636179193Sjb	 * hash tables of enabled tracepoints. It could, however, potentially
637179193Sjb	 * slow down execution of a tracepoint since we'd need to go
638179193Sjb	 * through two levels of indirection. In the future, we should
639179193Sjb	 * consider either maintaining per-process ancillary lists of
640179193Sjb	 * enabled tracepoints or hanging a pointer to a per-process hash
641179193Sjb	 * table of enabled tracepoints off the proc structure.
642179193Sjb	 */
643179193Sjb
644179193Sjb	/*
645179193Sjb	 * We don't have to worry about the child process disappearing
646179193Sjb	 * because we're in fork().
647179193Sjb	 */
648297077Smav#ifdef illumos
649211738Srpaulo	mtx_lock_spin(&cp->p_slock);
650179193Sjb	sprlock_proc(cp);
651211738Srpaulo	mtx_unlock_spin(&cp->p_slock);
652212494Srpaulo#else
653254198Srpaulo	/*
654254198Srpaulo	 * fasttrap_tracepoint_remove() expects the child process to be
655254198Srpaulo	 * unlocked and the VM then expects curproc to be unlocked.
656254198Srpaulo	 */
657212494Srpaulo	_PHOLD(cp);
658254198Srpaulo	PROC_UNLOCK(cp);
659254198Srpaulo	PROC_UNLOCK(p);
660211738Srpaulo#endif
661179193Sjb
662179193Sjb	/*
663179193Sjb	 * Iterate over every tracepoint looking for ones that belong to the
664179193Sjb	 * parent process, and remove each from the child process.
665179193Sjb	 */
666179193Sjb	for (i = 0; i < fasttrap_tpoints.fth_nent; i++) {
667179193Sjb		fasttrap_tracepoint_t *tp;
668179193Sjb		fasttrap_bucket_t *bucket = &fasttrap_tpoints.fth_table[i];
669179193Sjb
670179193Sjb		mutex_enter(&bucket->ftb_mtx);
671179193Sjb		for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
672179193Sjb			if (tp->ftt_pid == ppid &&
673179193Sjb			    tp->ftt_proc->ftpc_acount != 0) {
674179193Sjb				int ret = fasttrap_tracepoint_remove(cp, tp);
675179193Sjb				ASSERT(ret == 0);
676179198Sjb
677179198Sjb				/*
678179198Sjb				 * The count of active providers can only be
679179198Sjb				 * decremented (i.e. to zero) during exec,
680179198Sjb				 * exit, and removal of a meta provider so it
681179198Sjb				 * should be impossible to drop the count
682179198Sjb				 * mid-fork.
683179198Sjb				 */
684179198Sjb				ASSERT(tp->ftt_proc->ftpc_acount != 0);
685297077Smav#ifndef illumos
686269342Smarkj				fprc = tp->ftt_proc;
687269342Smarkj#endif
688179193Sjb			}
689179193Sjb		}
690179193Sjb		mutex_exit(&bucket->ftb_mtx);
691269342Smarkj
692297077Smav#ifndef illumos
693269342Smarkj		/*
694269342Smarkj		 * Unmap any scratch space inherited from the parent's address
695269342Smarkj		 * space.
696269342Smarkj		 */
697269342Smarkj		if (fprc != NULL) {
698269342Smarkj			mutex_enter(&fprc->ftpc_mtx);
699269342Smarkj			LIST_FOREACH(scrblk, &fprc->ftpc_scrblks, ftsb_next) {
700269342Smarkj				vm_map_remove(&cp->p_vmspace->vm_map,
701269342Smarkj				    scrblk->ftsb_addr,
702269342Smarkj				    scrblk->ftsb_addr + FASTTRAP_SCRBLOCK_SIZE);
703269342Smarkj			}
704269342Smarkj			mutex_exit(&fprc->ftpc_mtx);
705269342Smarkj		}
706269342Smarkj#endif
707179193Sjb	}
708179193Sjb
709297077Smav#ifdef illumos
710179193Sjb	mutex_enter(&cp->p_lock);
711179193Sjb	sprunlock(cp);
712212494Srpaulo#else
713254198Srpaulo	PROC_LOCK(p);
714254198Srpaulo	PROC_LOCK(cp);
715212494Srpaulo	_PRELE(cp);
716211738Srpaulo#endif
717179193Sjb}
718179193Sjb
719179193Sjb/*
720179193Sjb * This is called from proc_exit() or from exec_common() if p_dtrace_probes
721179193Sjb * is set on the proc structure to indicate that there is a pid provider
722179193Sjb * associated with this process.
723179193Sjb */
724179193Sjbstatic void
725179193Sjbfasttrap_exec_exit(proc_t *p)
726179193Sjb{
727297077Smav#ifndef illumos
728269342Smarkj	struct thread *td;
729269342Smarkj#endif
730269342Smarkj
731297077Smav#ifdef illumos
732179193Sjb	ASSERT(p == curproc);
733269342Smarkj#else
734211738Srpaulo	PROC_LOCK_ASSERT(p, MA_OWNED);
735212494Srpaulo	_PHOLD(p);
736269342Smarkj	/*
737269342Smarkj	 * Since struct threads may be recycled, we cannot rely on t_dtrace_sscr
738269342Smarkj	 * fields to be zeroed by kdtrace_thread_ctor. Thus we must zero it
739269342Smarkj	 * ourselves when a process exits.
740269342Smarkj	 */
741269342Smarkj	FOREACH_THREAD_IN_PROC(p, td)
742269342Smarkj		td->t_dtrace_sscr = NULL;
743211738Srpaulo	PROC_UNLOCK(p);
744269342Smarkj#endif
745179193Sjb
746179193Sjb	/*
747179193Sjb	 * We clean up the pid provider for this process here; user-land
748179193Sjb	 * static probes are handled by the meta-provider remove entry point.
749179193Sjb	 */
750179193Sjb	fasttrap_provider_retire(p->p_pid, FASTTRAP_PID_NAME, 0);
751297077Smav#ifndef illumos
752212357Srpaulo	if (p->p_dtrace_helpers)
753212357Srpaulo		dtrace_helpers_destroy(p);
754211738Srpaulo	PROC_LOCK(p);
755212494Srpaulo	_PRELE(p);
756269342Smarkj#endif
757179193Sjb}
758179193Sjb
759179193Sjb
760179193Sjb/*ARGSUSED*/
761179193Sjbstatic void
762211738Srpaulofasttrap_pid_provide(void *arg, dtrace_probedesc_t *desc)
763179193Sjb{
764179193Sjb	/*
765179193Sjb	 * There are no "default" pid probes.
766179193Sjb	 */
767179193Sjb}
768179193Sjb
769179193Sjbstatic int
770179193Sjbfasttrap_tracepoint_enable(proc_t *p, fasttrap_probe_t *probe, uint_t index)
771179193Sjb{
772179193Sjb	fasttrap_tracepoint_t *tp, *new_tp = NULL;
773179193Sjb	fasttrap_bucket_t *bucket;
774179193Sjb	fasttrap_id_t *id;
775179193Sjb	pid_t pid;
776179193Sjb	uintptr_t pc;
777179193Sjb
778179193Sjb	ASSERT(index < probe->ftp_ntps);
779179193Sjb
780179193Sjb	pid = probe->ftp_pid;
781179193Sjb	pc = probe->ftp_tps[index].fit_tp->ftt_pc;
782179193Sjb	id = &probe->ftp_tps[index].fit_id;
783179193Sjb
784179193Sjb	ASSERT(probe->ftp_tps[index].fit_tp->ftt_pid == pid);
785179193Sjb
786297077Smav#ifdef illumos
787179193Sjb	ASSERT(!(p->p_flag & SVFORK));
788211738Srpaulo#endif
789179193Sjb
790179193Sjb	/*
791179193Sjb	 * Before we make any modifications, make sure we've imposed a barrier
792179193Sjb	 * on the generation in which this probe was last modified.
793179193Sjb	 */
794179193Sjb	fasttrap_mod_barrier(probe->ftp_gen);
795179193Sjb
796179193Sjb	bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
797179193Sjb
798179193Sjb	/*
799179193Sjb	 * If the tracepoint has already been enabled, just add our id to the
800179193Sjb	 * list of interested probes. This may be our second time through
801179193Sjb	 * this path in which case we'll have constructed the tracepoint we'd
802179193Sjb	 * like to install. If we can't find a match, and have an allocated
803179193Sjb	 * tracepoint ready to go, enable that one now.
804179193Sjb	 *
805179193Sjb	 * A tracepoint whose process is defunct is also considered defunct.
806179193Sjb	 */
807179193Sjbagain:
808179193Sjb	mutex_enter(&bucket->ftb_mtx);
809179193Sjb	for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
810179198Sjb		/*
811179198Sjb		 * Note that it's safe to access the active count on the
812179198Sjb		 * associated proc structure because we know that at least one
813179198Sjb		 * provider (this one) will still be around throughout this
814179198Sjb		 * operation.
815179198Sjb		 */
816179193Sjb		if (tp->ftt_pid != pid || tp->ftt_pc != pc ||
817179193Sjb		    tp->ftt_proc->ftpc_acount == 0)
818179193Sjb			continue;
819179193Sjb
820179193Sjb		/*
821179193Sjb		 * Now that we've found a matching tracepoint, it would be
822179193Sjb		 * a decent idea to confirm that the tracepoint is still
823179193Sjb		 * enabled and the trap instruction hasn't been overwritten.
824179193Sjb		 * Since this is a little hairy, we'll punt for now.
825179193Sjb		 */
826179193Sjb
827179193Sjb		/*
828179193Sjb		 * This can't be the first interested probe. We don't have
829179193Sjb		 * to worry about another thread being in the midst of
830179193Sjb		 * deleting this tracepoint (which would be the only valid
831179193Sjb		 * reason for a tracepoint to have no interested probes)
832179193Sjb		 * since we're holding P_PR_LOCK for this process.
833179193Sjb		 */
834179193Sjb		ASSERT(tp->ftt_ids != NULL || tp->ftt_retids != NULL);
835179193Sjb
836179193Sjb		switch (id->fti_ptype) {
837179193Sjb		case DTFTP_ENTRY:
838179193Sjb		case DTFTP_OFFSETS:
839179193Sjb		case DTFTP_IS_ENABLED:
840179193Sjb			id->fti_next = tp->ftt_ids;
841179193Sjb			membar_producer();
842179193Sjb			tp->ftt_ids = id;
843179193Sjb			membar_producer();
844179193Sjb			break;
845179193Sjb
846179193Sjb		case DTFTP_RETURN:
847179193Sjb		case DTFTP_POST_OFFSETS:
848179193Sjb			id->fti_next = tp->ftt_retids;
849179193Sjb			membar_producer();
850179193Sjb			tp->ftt_retids = id;
851179193Sjb			membar_producer();
852179193Sjb			break;
853179193Sjb
854179193Sjb		default:
855179193Sjb			ASSERT(0);
856179193Sjb		}
857179193Sjb
858179193Sjb		mutex_exit(&bucket->ftb_mtx);
859179193Sjb
860179193Sjb		if (new_tp != NULL) {
861179193Sjb			new_tp->ftt_ids = NULL;
862179193Sjb			new_tp->ftt_retids = NULL;
863179193Sjb		}
864179193Sjb
865179193Sjb		return (0);
866179193Sjb	}
867179193Sjb
868179193Sjb	/*
869179193Sjb	 * If we have a good tracepoint ready to go, install it now while
870179193Sjb	 * we have the lock held and no one can screw with us.
871179193Sjb	 */
872179193Sjb	if (new_tp != NULL) {
873179193Sjb		int rc = 0;
874179193Sjb
875179193Sjb		new_tp->ftt_next = bucket->ftb_data;
876179193Sjb		membar_producer();
877179193Sjb		bucket->ftb_data = new_tp;
878179193Sjb		membar_producer();
879179193Sjb		mutex_exit(&bucket->ftb_mtx);
880179193Sjb
881179193Sjb		/*
882179193Sjb		 * Activate the tracepoint in the ISA-specific manner.
883179193Sjb		 * If this fails, we need to report the failure, but
884179193Sjb		 * indicate that this tracepoint must still be disabled
885179193Sjb		 * by calling fasttrap_tracepoint_disable().
886179193Sjb		 */
887179193Sjb		if (fasttrap_tracepoint_install(p, new_tp) != 0)
888179193Sjb			rc = FASTTRAP_ENABLE_PARTIAL;
889179193Sjb
890179193Sjb		/*
891179193Sjb		 * Increment the count of the number of tracepoints active in
892179193Sjb		 * the victim process.
893179193Sjb		 */
894297077Smav#ifdef illumos
895179193Sjb		ASSERT(p->p_proc_flag & P_PR_LOCK);
896211738Srpaulo#endif
897179193Sjb		p->p_dtrace_count++;
898179193Sjb
899179193Sjb		return (rc);
900179193Sjb	}
901179193Sjb
902179193Sjb	mutex_exit(&bucket->ftb_mtx);
903179193Sjb
904179193Sjb	/*
905179193Sjb	 * Initialize the tracepoint that's been preallocated with the probe.
906179193Sjb	 */
907179193Sjb	new_tp = probe->ftp_tps[index].fit_tp;
908179193Sjb
909179193Sjb	ASSERT(new_tp->ftt_pid == pid);
910179193Sjb	ASSERT(new_tp->ftt_pc == pc);
911179193Sjb	ASSERT(new_tp->ftt_proc == probe->ftp_prov->ftp_proc);
912179193Sjb	ASSERT(new_tp->ftt_ids == NULL);
913179193Sjb	ASSERT(new_tp->ftt_retids == NULL);
914179193Sjb
915179193Sjb	switch (id->fti_ptype) {
916179193Sjb	case DTFTP_ENTRY:
917179193Sjb	case DTFTP_OFFSETS:
918179193Sjb	case DTFTP_IS_ENABLED:
919179193Sjb		id->fti_next = NULL;
920179193Sjb		new_tp->ftt_ids = id;
921179193Sjb		break;
922179193Sjb
923179193Sjb	case DTFTP_RETURN:
924179193Sjb	case DTFTP_POST_OFFSETS:
925179193Sjb		id->fti_next = NULL;
926179193Sjb		new_tp->ftt_retids = id;
927179193Sjb		break;
928179193Sjb
929179193Sjb	default:
930179193Sjb		ASSERT(0);
931179193Sjb	}
932179193Sjb
933179193Sjb	/*
934179193Sjb	 * If the ISA-dependent initialization goes to plan, go back to the
935179193Sjb	 * beginning and try to install this freshly made tracepoint.
936179193Sjb	 */
937179193Sjb	if (fasttrap_tracepoint_init(p, new_tp, pc, id->fti_ptype) == 0)
938179193Sjb		goto again;
939179193Sjb
940179193Sjb	new_tp->ftt_ids = NULL;
941179193Sjb	new_tp->ftt_retids = NULL;
942179193Sjb
943179193Sjb	return (FASTTRAP_ENABLE_FAIL);
944179193Sjb}
945179193Sjb
946179193Sjbstatic void
947179193Sjbfasttrap_tracepoint_disable(proc_t *p, fasttrap_probe_t *probe, uint_t index)
948179193Sjb{
949179193Sjb	fasttrap_bucket_t *bucket;
950179193Sjb	fasttrap_provider_t *provider = probe->ftp_prov;
951179193Sjb	fasttrap_tracepoint_t **pp, *tp;
952211738Srpaulo	fasttrap_id_t *id, **idp = NULL;
953179193Sjb	pid_t pid;
954179193Sjb	uintptr_t pc;
955179193Sjb
956179193Sjb	ASSERT(index < probe->ftp_ntps);
957179193Sjb
958179193Sjb	pid = probe->ftp_pid;
959179193Sjb	pc = probe->ftp_tps[index].fit_tp->ftt_pc;
960179193Sjb	id = &probe->ftp_tps[index].fit_id;
961179193Sjb
962179193Sjb	ASSERT(probe->ftp_tps[index].fit_tp->ftt_pid == pid);
963179193Sjb
964179193Sjb	/*
965179193Sjb	 * Find the tracepoint and make sure that our id is one of the
966179193Sjb	 * ones registered with it.
967179193Sjb	 */
968179193Sjb	bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
969179193Sjb	mutex_enter(&bucket->ftb_mtx);
970179193Sjb	for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
971179193Sjb		if (tp->ftt_pid == pid && tp->ftt_pc == pc &&
972179193Sjb		    tp->ftt_proc == provider->ftp_proc)
973179193Sjb			break;
974179193Sjb	}
975179193Sjb
976179193Sjb	/*
977179193Sjb	 * If we somehow lost this tracepoint, we're in a world of hurt.
978179193Sjb	 */
979179193Sjb	ASSERT(tp != NULL);
980179193Sjb
981179193Sjb	switch (id->fti_ptype) {
982179193Sjb	case DTFTP_ENTRY:
983179193Sjb	case DTFTP_OFFSETS:
984179193Sjb	case DTFTP_IS_ENABLED:
985179193Sjb		ASSERT(tp->ftt_ids != NULL);
986179193Sjb		idp = &tp->ftt_ids;
987179193Sjb		break;
988179193Sjb
989179193Sjb	case DTFTP_RETURN:
990179193Sjb	case DTFTP_POST_OFFSETS:
991179193Sjb		ASSERT(tp->ftt_retids != NULL);
992179193Sjb		idp = &tp->ftt_retids;
993179193Sjb		break;
994179193Sjb
995179193Sjb	default:
996179193Sjb		ASSERT(0);
997179193Sjb	}
998179193Sjb
999179193Sjb	while ((*idp)->fti_probe != probe) {
1000179193Sjb		idp = &(*idp)->fti_next;
1001179193Sjb		ASSERT(*idp != NULL);
1002179193Sjb	}
1003179193Sjb
1004179193Sjb	id = *idp;
1005179193Sjb	*idp = id->fti_next;
1006179193Sjb	membar_producer();
1007179193Sjb
1008179193Sjb	ASSERT(id->fti_probe == probe);
1009179193Sjb
1010179193Sjb	/*
1011179193Sjb	 * If there are other registered enablings of this tracepoint, we're
1012179193Sjb	 * all done, but if this was the last probe assocated with this
1013179193Sjb	 * this tracepoint, we need to remove and free it.
1014179193Sjb	 */
1015179193Sjb	if (tp->ftt_ids != NULL || tp->ftt_retids != NULL) {
1016179193Sjb
1017179193Sjb		/*
1018179193Sjb		 * If the current probe's tracepoint is in use, swap it
1019179193Sjb		 * for an unused tracepoint.
1020179193Sjb		 */
1021179193Sjb		if (tp == probe->ftp_tps[index].fit_tp) {
1022179193Sjb			fasttrap_probe_t *tmp_probe;
1023179193Sjb			fasttrap_tracepoint_t **tmp_tp;
1024179193Sjb			uint_t tmp_index;
1025179193Sjb
1026179193Sjb			if (tp->ftt_ids != NULL) {
1027179193Sjb				tmp_probe = tp->ftt_ids->fti_probe;
1028179193Sjb				/* LINTED - alignment */
1029179193Sjb				tmp_index = FASTTRAP_ID_INDEX(tp->ftt_ids);
1030179193Sjb				tmp_tp = &tmp_probe->ftp_tps[tmp_index].fit_tp;
1031179193Sjb			} else {
1032179193Sjb				tmp_probe = tp->ftt_retids->fti_probe;
1033179193Sjb				/* LINTED - alignment */
1034179193Sjb				tmp_index = FASTTRAP_ID_INDEX(tp->ftt_retids);
1035179193Sjb				tmp_tp = &tmp_probe->ftp_tps[tmp_index].fit_tp;
1036179193Sjb			}
1037179193Sjb
1038179193Sjb			ASSERT(*tmp_tp != NULL);
1039179193Sjb			ASSERT(*tmp_tp != probe->ftp_tps[index].fit_tp);
1040179193Sjb			ASSERT((*tmp_tp)->ftt_ids == NULL);
1041179193Sjb			ASSERT((*tmp_tp)->ftt_retids == NULL);
1042179193Sjb
1043179193Sjb			probe->ftp_tps[index].fit_tp = *tmp_tp;
1044179193Sjb			*tmp_tp = tp;
1045179193Sjb		}
1046179193Sjb
1047179193Sjb		mutex_exit(&bucket->ftb_mtx);
1048179193Sjb
1049179193Sjb		/*
1050179193Sjb		 * Tag the modified probe with the generation in which it was
1051179193Sjb		 * changed.
1052179193Sjb		 */
1053179193Sjb		probe->ftp_gen = fasttrap_mod_gen;
1054179193Sjb		return;
1055179193Sjb	}
1056179193Sjb
1057179193Sjb	mutex_exit(&bucket->ftb_mtx);
1058179193Sjb
1059179193Sjb	/*
1060179193Sjb	 * We can't safely remove the tracepoint from the set of active
1061179193Sjb	 * tracepoints until we've actually removed the fasttrap instruction
1062179193Sjb	 * from the process's text. We can, however, operate on this
1063179193Sjb	 * tracepoint secure in the knowledge that no other thread is going to
1064179193Sjb	 * be looking at it since we hold P_PR_LOCK on the process if it's
1065179193Sjb	 * live or we hold the provider lock on the process if it's dead and
1066179193Sjb	 * gone.
1067179193Sjb	 */
1068179193Sjb
1069179193Sjb	/*
1070179193Sjb	 * We only need to remove the actual instruction if we're looking
1071179193Sjb	 * at an existing process
1072179193Sjb	 */
1073179193Sjb	if (p != NULL) {
1074179193Sjb		/*
1075179193Sjb		 * If we fail to restore the instruction we need to kill
1076179193Sjb		 * this process since it's in a completely unrecoverable
1077179193Sjb		 * state.
1078179193Sjb		 */
1079179193Sjb		if (fasttrap_tracepoint_remove(p, tp) != 0)
1080179193Sjb			fasttrap_sigtrap(p, NULL, pc);
1081179193Sjb
1082179193Sjb		/*
1083179193Sjb		 * Decrement the count of the number of tracepoints active
1084179193Sjb		 * in the victim process.
1085179193Sjb		 */
1086297077Smav#ifdef illumos
1087179193Sjb		ASSERT(p->p_proc_flag & P_PR_LOCK);
1088211738Srpaulo#endif
1089179193Sjb		p->p_dtrace_count--;
1090179193Sjb	}
1091179193Sjb
1092179193Sjb	/*
1093179193Sjb	 * Remove the probe from the hash table of active tracepoints.
1094179193Sjb	 */
1095179193Sjb	mutex_enter(&bucket->ftb_mtx);
1096179193Sjb	pp = (fasttrap_tracepoint_t **)&bucket->ftb_data;
1097179193Sjb	ASSERT(*pp != NULL);
1098179193Sjb	while (*pp != tp) {
1099179193Sjb		pp = &(*pp)->ftt_next;
1100179193Sjb		ASSERT(*pp != NULL);
1101179193Sjb	}
1102179193Sjb
1103179193Sjb	*pp = tp->ftt_next;
1104179193Sjb	membar_producer();
1105179193Sjb
1106179193Sjb	mutex_exit(&bucket->ftb_mtx);
1107179193Sjb
1108179193Sjb	/*
1109179193Sjb	 * Tag the modified probe with the generation in which it was changed.
1110179193Sjb	 */
1111179193Sjb	probe->ftp_gen = fasttrap_mod_gen;
1112179193Sjb}
1113179193Sjb
1114179193Sjbstatic void
1115179193Sjbfasttrap_enable_callbacks(void)
1116179193Sjb{
1117179193Sjb	/*
1118179193Sjb	 * We don't have to play the rw lock game here because we're
1119179193Sjb	 * providing something rather than taking something away --
1120179193Sjb	 * we can be sure that no threads have tried to follow this
1121179193Sjb	 * function pointer yet.
1122179193Sjb	 */
1123179193Sjb	mutex_enter(&fasttrap_count_mtx);
1124179193Sjb	if (fasttrap_pid_count == 0) {
1125179193Sjb		ASSERT(dtrace_pid_probe_ptr == NULL);
1126179193Sjb		ASSERT(dtrace_return_probe_ptr == NULL);
1127179193Sjb		dtrace_pid_probe_ptr = &fasttrap_pid_probe;
1128179193Sjb		dtrace_return_probe_ptr = &fasttrap_return_probe;
1129179193Sjb	}
1130179193Sjb	ASSERT(dtrace_pid_probe_ptr == &fasttrap_pid_probe);
1131179193Sjb	ASSERT(dtrace_return_probe_ptr == &fasttrap_return_probe);
1132179193Sjb	fasttrap_pid_count++;
1133179193Sjb	mutex_exit(&fasttrap_count_mtx);
1134179193Sjb}
1135179193Sjb
1136179193Sjbstatic void
1137179193Sjbfasttrap_disable_callbacks(void)
1138179193Sjb{
1139297077Smav#ifdef illumos
1140179193Sjb	ASSERT(MUTEX_HELD(&cpu_lock));
1141211738Srpaulo#endif
1142179193Sjb
1143211738Srpaulo
1144179193Sjb	mutex_enter(&fasttrap_count_mtx);
1145179193Sjb	ASSERT(fasttrap_pid_count > 0);
1146179193Sjb	fasttrap_pid_count--;
1147179193Sjb	if (fasttrap_pid_count == 0) {
1148297077Smav#ifdef illumos
1149179193Sjb		cpu_t *cur, *cpu = CPU;
1150179193Sjb
1151179193Sjb		for (cur = cpu->cpu_next_onln; cur != cpu;
1152179193Sjb		    cur = cur->cpu_next_onln) {
1153179193Sjb			rw_enter(&cur->cpu_ft_lock, RW_WRITER);
1154179193Sjb		}
1155211738Srpaulo#endif
1156179193Sjb		dtrace_pid_probe_ptr = NULL;
1157179193Sjb		dtrace_return_probe_ptr = NULL;
1158297077Smav#ifdef illumos
1159179193Sjb		for (cur = cpu->cpu_next_onln; cur != cpu;
1160179193Sjb		    cur = cur->cpu_next_onln) {
1161179193Sjb			rw_exit(&cur->cpu_ft_lock);
1162179193Sjb		}
1163211738Srpaulo#endif
1164179193Sjb	}
1165179193Sjb	mutex_exit(&fasttrap_count_mtx);
1166179193Sjb}
1167179193Sjb
1168179193Sjb/*ARGSUSED*/
1169179193Sjbstatic void
1170179193Sjbfasttrap_pid_enable(void *arg, dtrace_id_t id, void *parg)
1171179193Sjb{
1172179193Sjb	fasttrap_probe_t *probe = parg;
1173211738Srpaulo	proc_t *p = NULL;
1174179193Sjb	int i, rc;
1175179193Sjb
1176179193Sjb	ASSERT(probe != NULL);
1177179193Sjb	ASSERT(!probe->ftp_enabled);
1178179193Sjb	ASSERT(id == probe->ftp_id);
1179297077Smav#ifdef illumos
1180179193Sjb	ASSERT(MUTEX_HELD(&cpu_lock));
1181211738Srpaulo#endif
1182179193Sjb
1183179193Sjb	/*
1184179193Sjb	 * Increment the count of enabled probes on this probe's provider;
1185179193Sjb	 * the provider can't go away while the probe still exists. We
1186179193Sjb	 * must increment this even if we aren't able to properly enable
1187179193Sjb	 * this probe.
1188179193Sjb	 */
1189179193Sjb	mutex_enter(&probe->ftp_prov->ftp_mtx);
1190179193Sjb	probe->ftp_prov->ftp_rcount++;
1191179193Sjb	mutex_exit(&probe->ftp_prov->ftp_mtx);
1192179193Sjb
1193179193Sjb	/*
1194179193Sjb	 * If this probe's provider is retired (meaning it was valid in a
1195179193Sjb	 * previously exec'ed incarnation of this address space), bail out. The
1196179193Sjb	 * provider can't go away while we're in this code path.
1197179193Sjb	 */
1198179193Sjb	if (probe->ftp_prov->ftp_retired)
1199179193Sjb		return;
1200179193Sjb
1201179193Sjb	/*
1202179193Sjb	 * If we can't find the process, it may be that we're in the context of
1203179193Sjb	 * a fork in which the traced process is being born and we're copying
1204179193Sjb	 * USDT probes. Otherwise, the process is gone so bail.
1205179193Sjb	 */
1206297077Smav#ifdef illumos
1207179193Sjb	if ((p = sprlock(probe->ftp_pid)) == NULL) {
1208179193Sjb		if ((curproc->p_flag & SFORKING) == 0)
1209179193Sjb			return;
1210179193Sjb
1211179193Sjb		mutex_enter(&pidlock);
1212179193Sjb		p = prfind(probe->ftp_pid);
1213179193Sjb
1214179193Sjb		/*
1215179193Sjb		 * Confirm that curproc is indeed forking the process in which
1216179193Sjb		 * we're trying to enable probes.
1217179193Sjb		 */
1218179193Sjb		ASSERT(p != NULL);
1219179193Sjb		ASSERT(p->p_parent == curproc);
1220179193Sjb		ASSERT(p->p_stat == SIDL);
1221179193Sjb
1222179193Sjb		mutex_enter(&p->p_lock);
1223179193Sjb		mutex_exit(&pidlock);
1224179193Sjb
1225179193Sjb		sprlock_proc(p);
1226179193Sjb	}
1227179193Sjb
1228179193Sjb	ASSERT(!(p->p_flag & SVFORK));
1229179193Sjb	mutex_exit(&p->p_lock);
1230211738Srpaulo#else
1231211738Srpaulo	if ((p = pfind(probe->ftp_pid)) == NULL)
1232211738Srpaulo		return;
1233211738Srpaulo#endif
1234179193Sjb
1235179193Sjb	/*
1236179193Sjb	 * We have to enable the trap entry point before any user threads have
1237179193Sjb	 * the chance to execute the trap instruction we're about to place
1238179193Sjb	 * in their process's text.
1239179193Sjb	 */
1240212494Srpaulo#ifdef __FreeBSD__
1241212494Srpaulo	/*
1242212494Srpaulo	 * pfind() returns a locked process.
1243212494Srpaulo	 */
1244212494Srpaulo	_PHOLD(p);
1245211738Srpaulo	PROC_UNLOCK(p);
1246212494Srpaulo#endif
1247179193Sjb	fasttrap_enable_callbacks();
1248179193Sjb
1249179193Sjb	/*
1250179193Sjb	 * Enable all the tracepoints and add this probe's id to each
1251179193Sjb	 * tracepoint's list of active probes.
1252179193Sjb	 */
1253179193Sjb	for (i = 0; i < probe->ftp_ntps; i++) {
1254179193Sjb		if ((rc = fasttrap_tracepoint_enable(p, probe, i)) != 0) {
1255179193Sjb			/*
1256179193Sjb			 * If enabling the tracepoint failed completely,
1257179193Sjb			 * we don't have to disable it; if the failure
1258179193Sjb			 * was only partial we must disable it.
1259179193Sjb			 */
1260179193Sjb			if (rc == FASTTRAP_ENABLE_FAIL)
1261179193Sjb				i--;
1262179193Sjb			else
1263179193Sjb				ASSERT(rc == FASTTRAP_ENABLE_PARTIAL);
1264179193Sjb
1265179193Sjb			/*
1266179193Sjb			 * Back up and pull out all the tracepoints we've
1267179193Sjb			 * created so far for this probe.
1268179193Sjb			 */
1269179193Sjb			while (i >= 0) {
1270179193Sjb				fasttrap_tracepoint_disable(p, probe, i);
1271179193Sjb				i--;
1272179193Sjb			}
1273179193Sjb
1274297077Smav#ifdef illumos
1275179193Sjb			mutex_enter(&p->p_lock);
1276179193Sjb			sprunlock(p);
1277211738Srpaulo#else
1278212494Srpaulo			PRELE(p);
1279211738Srpaulo#endif
1280179193Sjb
1281179193Sjb			/*
1282179193Sjb			 * Since we're not actually enabling this probe,
1283179193Sjb			 * drop our reference on the trap table entry.
1284179193Sjb			 */
1285179193Sjb			fasttrap_disable_callbacks();
1286179193Sjb			return;
1287179193Sjb		}
1288179193Sjb	}
1289297077Smav#ifdef illumos
1290179193Sjb	mutex_enter(&p->p_lock);
1291179193Sjb	sprunlock(p);
1292211738Srpaulo#else
1293212494Srpaulo	PRELE(p);
1294211738Srpaulo#endif
1295179193Sjb
1296179193Sjb	probe->ftp_enabled = 1;
1297179193Sjb}
1298179193Sjb
1299179193Sjb/*ARGSUSED*/
1300179193Sjbstatic void
1301179193Sjbfasttrap_pid_disable(void *arg, dtrace_id_t id, void *parg)
1302179193Sjb{
1303179193Sjb	fasttrap_probe_t *probe = parg;
1304179193Sjb	fasttrap_provider_t *provider = probe->ftp_prov;
1305179193Sjb	proc_t *p;
1306179193Sjb	int i, whack = 0;
1307179193Sjb
1308179193Sjb	ASSERT(id == probe->ftp_id);
1309179193Sjb
1310211738Srpaulo	mutex_enter(&provider->ftp_mtx);
1311211738Srpaulo
1312179193Sjb	/*
1313179193Sjb	 * We won't be able to acquire a /proc-esque lock on the process
1314179193Sjb	 * iff the process is dead and gone. In this case, we rely on the
1315179193Sjb	 * provider lock as a point of mutual exclusion to prevent other
1316179193Sjb	 * DTrace consumers from disabling this probe.
1317179193Sjb	 */
1318247049Sgibbs	if ((p = pfind(probe->ftp_pid)) != NULL) {
1319212494Srpaulo#ifdef __FreeBSD__
1320283677Smarkj		if (p->p_flag & P_WEXIT) {
1321283677Smarkj			PROC_UNLOCK(p);
1322283677Smarkj			p = NULL;
1323283677Smarkj		} else {
1324283677Smarkj			_PHOLD(p);
1325283677Smarkj			PROC_UNLOCK(p);
1326283677Smarkj		}
1327212494Srpaulo#endif
1328247049Sgibbs	}
1329179193Sjb
1330179193Sjb	/*
1331179193Sjb	 * Disable all the associated tracepoints (for fully enabled probes).
1332179193Sjb	 */
1333179193Sjb	if (probe->ftp_enabled) {
1334179193Sjb		for (i = 0; i < probe->ftp_ntps; i++) {
1335179193Sjb			fasttrap_tracepoint_disable(p, probe, i);
1336179193Sjb		}
1337179193Sjb	}
1338179193Sjb
1339179193Sjb	ASSERT(provider->ftp_rcount > 0);
1340179193Sjb	provider->ftp_rcount--;
1341179193Sjb
1342179193Sjb	if (p != NULL) {
1343179193Sjb		/*
1344179193Sjb		 * Even though we may not be able to remove it entirely, we
1345179193Sjb		 * mark this retired provider to get a chance to remove some
1346179193Sjb		 * of the associated probes.
1347179193Sjb		 */
1348179193Sjb		if (provider->ftp_retired && !provider->ftp_marked)
1349179193Sjb			whack = provider->ftp_marked = 1;
1350179193Sjb		mutex_exit(&provider->ftp_mtx);
1351179193Sjb	} else {
1352179193Sjb		/*
1353179193Sjb		 * If the process is dead, we're just waiting for the
1354179193Sjb		 * last probe to be disabled to be able to free it.
1355179193Sjb		 */
1356179193Sjb		if (provider->ftp_rcount == 0 && !provider->ftp_marked)
1357179193Sjb			whack = provider->ftp_marked = 1;
1358179193Sjb		mutex_exit(&provider->ftp_mtx);
1359179193Sjb	}
1360179193Sjb
1361179193Sjb	if (whack)
1362179193Sjb		fasttrap_pid_cleanup();
1363179193Sjb
1364212494Srpaulo#ifdef __FreeBSD__
1365247049Sgibbs	if (p != NULL)
1366247049Sgibbs		PRELE(p);
1367212494Srpaulo#endif
1368179193Sjb	if (!probe->ftp_enabled)
1369179193Sjb		return;
1370179193Sjb
1371179193Sjb	probe->ftp_enabled = 0;
1372179193Sjb
1373297077Smav#ifdef illumos
1374179193Sjb	ASSERT(MUTEX_HELD(&cpu_lock));
1375211738Srpaulo#endif
1376179193Sjb	fasttrap_disable_callbacks();
1377179193Sjb}
1378179193Sjb
1379179193Sjb/*ARGSUSED*/
1380179193Sjbstatic void
1381179193Sjbfasttrap_pid_getargdesc(void *arg, dtrace_id_t id, void *parg,
1382179193Sjb    dtrace_argdesc_t *desc)
1383179193Sjb{
1384179193Sjb	fasttrap_probe_t *probe = parg;
1385179193Sjb	char *str;
1386179193Sjb	int i, ndx;
1387179193Sjb
1388179193Sjb	desc->dtargd_native[0] = '\0';
1389179193Sjb	desc->dtargd_xlate[0] = '\0';
1390179193Sjb
1391179193Sjb	if (probe->ftp_prov->ftp_retired != 0 ||
1392179193Sjb	    desc->dtargd_ndx >= probe->ftp_nargs) {
1393179193Sjb		desc->dtargd_ndx = DTRACE_ARGNONE;
1394179193Sjb		return;
1395179193Sjb	}
1396179193Sjb
1397179193Sjb	ndx = (probe->ftp_argmap != NULL) ?
1398179193Sjb	    probe->ftp_argmap[desc->dtargd_ndx] : desc->dtargd_ndx;
1399179193Sjb
1400179193Sjb	str = probe->ftp_ntypes;
1401179193Sjb	for (i = 0; i < ndx; i++) {
1402179193Sjb		str += strlen(str) + 1;
1403179193Sjb	}
1404179193Sjb
1405179193Sjb	ASSERT(strlen(str + 1) < sizeof (desc->dtargd_native));
1406179193Sjb	(void) strcpy(desc->dtargd_native, str);
1407179193Sjb
1408179193Sjb	if (probe->ftp_xtypes == NULL)
1409179193Sjb		return;
1410179193Sjb
1411179193Sjb	str = probe->ftp_xtypes;
1412179193Sjb	for (i = 0; i < desc->dtargd_ndx; i++) {
1413179193Sjb		str += strlen(str) + 1;
1414179193Sjb	}
1415179193Sjb
1416179193Sjb	ASSERT(strlen(str + 1) < sizeof (desc->dtargd_xlate));
1417179193Sjb	(void) strcpy(desc->dtargd_xlate, str);
1418179193Sjb}
1419179193Sjb
1420179193Sjb/*ARGSUSED*/
1421179193Sjbstatic void
1422179193Sjbfasttrap_pid_destroy(void *arg, dtrace_id_t id, void *parg)
1423179193Sjb{
1424179193Sjb	fasttrap_probe_t *probe = parg;
1425179193Sjb	int i;
1426179193Sjb	size_t size;
1427179193Sjb
1428179193Sjb	ASSERT(probe != NULL);
1429179193Sjb	ASSERT(!probe->ftp_enabled);
1430179193Sjb	ASSERT(fasttrap_total >= probe->ftp_ntps);
1431179193Sjb
1432179193Sjb	atomic_add_32(&fasttrap_total, -probe->ftp_ntps);
1433179193Sjb	size = offsetof(fasttrap_probe_t, ftp_tps[probe->ftp_ntps]);
1434179193Sjb
1435179193Sjb	if (probe->ftp_gen + 1 >= fasttrap_mod_gen)
1436179193Sjb		fasttrap_mod_barrier(probe->ftp_gen);
1437179193Sjb
1438179193Sjb	for (i = 0; i < probe->ftp_ntps; i++) {
1439179193Sjb		kmem_free(probe->ftp_tps[i].fit_tp,
1440179193Sjb		    sizeof (fasttrap_tracepoint_t));
1441179193Sjb	}
1442179193Sjb
1443179193Sjb	kmem_free(probe, size);
1444179193Sjb}
1445179193Sjb
1446179193Sjb
1447179193Sjbstatic const dtrace_pattr_t pid_attr = {
1448179193Sjb{ DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
1449179193Sjb{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
1450179193Sjb{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
1451179193Sjb{ DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
1452179193Sjb{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
1453179193Sjb};
1454179193Sjb
1455179193Sjbstatic dtrace_pops_t pid_pops = {
1456179193Sjb	fasttrap_pid_provide,
1457179193Sjb	NULL,
1458179193Sjb	fasttrap_pid_enable,
1459179193Sjb	fasttrap_pid_disable,
1460179193Sjb	NULL,
1461179193Sjb	NULL,
1462179193Sjb	fasttrap_pid_getargdesc,
1463179193Sjb	fasttrap_pid_getarg,
1464179193Sjb	NULL,
1465179193Sjb	fasttrap_pid_destroy
1466179193Sjb};
1467179193Sjb
1468179193Sjbstatic dtrace_pops_t usdt_pops = {
1469179193Sjb	fasttrap_pid_provide,
1470179193Sjb	NULL,
1471179193Sjb	fasttrap_pid_enable,
1472179193Sjb	fasttrap_pid_disable,
1473179193Sjb	NULL,
1474179193Sjb	NULL,
1475179193Sjb	fasttrap_pid_getargdesc,
1476179193Sjb	fasttrap_usdt_getarg,
1477179193Sjb	NULL,
1478179193Sjb	fasttrap_pid_destroy
1479179193Sjb};
1480179193Sjb
1481179193Sjbstatic fasttrap_proc_t *
1482179193Sjbfasttrap_proc_lookup(pid_t pid)
1483179193Sjb{
1484179193Sjb	fasttrap_bucket_t *bucket;
1485179193Sjb	fasttrap_proc_t *fprc, *new_fprc;
1486179193Sjb
1487211738Srpaulo
1488179193Sjb	bucket = &fasttrap_procs.fth_table[FASTTRAP_PROCS_INDEX(pid)];
1489179193Sjb	mutex_enter(&bucket->ftb_mtx);
1490179193Sjb
1491179193Sjb	for (fprc = bucket->ftb_data; fprc != NULL; fprc = fprc->ftpc_next) {
1492179193Sjb		if (fprc->ftpc_pid == pid && fprc->ftpc_acount != 0) {
1493179193Sjb			mutex_enter(&fprc->ftpc_mtx);
1494179193Sjb			mutex_exit(&bucket->ftb_mtx);
1495179193Sjb			fprc->ftpc_rcount++;
1496271001Sdelphij			atomic_inc_64(&fprc->ftpc_acount);
1497179198Sjb			ASSERT(fprc->ftpc_acount <= fprc->ftpc_rcount);
1498179193Sjb			mutex_exit(&fprc->ftpc_mtx);
1499179193Sjb
1500179193Sjb			return (fprc);
1501179193Sjb		}
1502179193Sjb	}
1503179193Sjb
1504179193Sjb	/*
1505179193Sjb	 * Drop the bucket lock so we don't try to perform a sleeping
1506179193Sjb	 * allocation under it.
1507179193Sjb	 */
1508179193Sjb	mutex_exit(&bucket->ftb_mtx);
1509179193Sjb
1510179193Sjb	new_fprc = kmem_zalloc(sizeof (fasttrap_proc_t), KM_SLEEP);
1511179193Sjb	new_fprc->ftpc_pid = pid;
1512179193Sjb	new_fprc->ftpc_rcount = 1;
1513179193Sjb	new_fprc->ftpc_acount = 1;
1514297077Smav#ifndef illumos
1515211738Srpaulo	mutex_init(&new_fprc->ftpc_mtx, "fasttrap proc mtx", MUTEX_DEFAULT,
1516211738Srpaulo	    NULL);
1517211738Srpaulo#endif
1518179193Sjb
1519179193Sjb	mutex_enter(&bucket->ftb_mtx);
1520179193Sjb
1521179193Sjb	/*
1522179193Sjb	 * Take another lap through the list to make sure a proc hasn't
1523179193Sjb	 * been created for this pid while we weren't under the bucket lock.
1524179193Sjb	 */
1525179193Sjb	for (fprc = bucket->ftb_data; fprc != NULL; fprc = fprc->ftpc_next) {
1526179193Sjb		if (fprc->ftpc_pid == pid && fprc->ftpc_acount != 0) {
1527179193Sjb			mutex_enter(&fprc->ftpc_mtx);
1528179193Sjb			mutex_exit(&bucket->ftb_mtx);
1529179193Sjb			fprc->ftpc_rcount++;
1530271001Sdelphij			atomic_inc_64(&fprc->ftpc_acount);
1531179198Sjb			ASSERT(fprc->ftpc_acount <= fprc->ftpc_rcount);
1532179193Sjb			mutex_exit(&fprc->ftpc_mtx);
1533179193Sjb
1534179193Sjb			kmem_free(new_fprc, sizeof (fasttrap_proc_t));
1535179193Sjb
1536179193Sjb			return (fprc);
1537179193Sjb		}
1538179193Sjb	}
1539179193Sjb
1540179193Sjb	new_fprc->ftpc_next = bucket->ftb_data;
1541179193Sjb	bucket->ftb_data = new_fprc;
1542179193Sjb
1543179193Sjb	mutex_exit(&bucket->ftb_mtx);
1544179193Sjb
1545179193Sjb	return (new_fprc);
1546179193Sjb}
1547179193Sjb
1548179193Sjbstatic void
1549179193Sjbfasttrap_proc_release(fasttrap_proc_t *proc)
1550179193Sjb{
1551179193Sjb	fasttrap_bucket_t *bucket;
1552179193Sjb	fasttrap_proc_t *fprc, **fprcp;
1553179193Sjb	pid_t pid = proc->ftpc_pid;
1554297077Smav#ifndef illumos
1555269342Smarkj	fasttrap_scrblock_t *scrblk, *scrblktmp;
1556269342Smarkj	fasttrap_scrspace_t *scrspc, *scrspctmp;
1557269342Smarkj	struct proc *p;
1558269342Smarkj	struct thread *td;
1559269342Smarkj#endif
1560179193Sjb
1561179193Sjb	mutex_enter(&proc->ftpc_mtx);
1562179193Sjb
1563179193Sjb	ASSERT(proc->ftpc_rcount != 0);
1564179198Sjb	ASSERT(proc->ftpc_acount <= proc->ftpc_rcount);
1565179193Sjb
1566179193Sjb	if (--proc->ftpc_rcount != 0) {
1567179193Sjb		mutex_exit(&proc->ftpc_mtx);
1568179193Sjb		return;
1569179193Sjb	}
1570179193Sjb
1571297077Smav#ifndef illumos
1572269342Smarkj	/*
1573269342Smarkj	 * Free all structures used to manage per-thread scratch space.
1574269342Smarkj	 */
1575269342Smarkj	LIST_FOREACH_SAFE(scrblk, &proc->ftpc_scrblks, ftsb_next,
1576269342Smarkj	    scrblktmp) {
1577269342Smarkj		LIST_REMOVE(scrblk, ftsb_next);
1578269342Smarkj		free(scrblk, M_SOLARIS);
1579269342Smarkj	}
1580269342Smarkj	LIST_FOREACH_SAFE(scrspc, &proc->ftpc_fscr, ftss_next, scrspctmp) {
1581269342Smarkj		LIST_REMOVE(scrspc, ftss_next);
1582269342Smarkj		free(scrspc, M_SOLARIS);
1583269342Smarkj	}
1584269342Smarkj	LIST_FOREACH_SAFE(scrspc, &proc->ftpc_ascr, ftss_next, scrspctmp) {
1585269342Smarkj		LIST_REMOVE(scrspc, ftss_next);
1586269342Smarkj		free(scrspc, M_SOLARIS);
1587269342Smarkj	}
1588269342Smarkj
1589269342Smarkj	if ((p = pfind(pid)) != NULL) {
1590269342Smarkj		FOREACH_THREAD_IN_PROC(p, td)
1591269342Smarkj			td->t_dtrace_sscr = NULL;
1592269342Smarkj		PROC_UNLOCK(p);
1593269342Smarkj	}
1594269342Smarkj#endif
1595269342Smarkj
1596179193Sjb	mutex_exit(&proc->ftpc_mtx);
1597179193Sjb
1598179193Sjb	/*
1599179193Sjb	 * There should definitely be no live providers associated with this
1600179193Sjb	 * process at this point.
1601179193Sjb	 */
1602179193Sjb	ASSERT(proc->ftpc_acount == 0);
1603179193Sjb
1604179193Sjb	bucket = &fasttrap_procs.fth_table[FASTTRAP_PROCS_INDEX(pid)];
1605179193Sjb	mutex_enter(&bucket->ftb_mtx);
1606179193Sjb
1607179193Sjb	fprcp = (fasttrap_proc_t **)&bucket->ftb_data;
1608179193Sjb	while ((fprc = *fprcp) != NULL) {
1609179193Sjb		if (fprc == proc)
1610179193Sjb			break;
1611179193Sjb
1612179193Sjb		fprcp = &fprc->ftpc_next;
1613179193Sjb	}
1614179193Sjb
1615179193Sjb	/*
1616179193Sjb	 * Something strange has happened if we can't find the proc.
1617179193Sjb	 */
1618179193Sjb	ASSERT(fprc != NULL);
1619179193Sjb
1620179193Sjb	*fprcp = fprc->ftpc_next;
1621179193Sjb
1622179193Sjb	mutex_exit(&bucket->ftb_mtx);
1623179193Sjb
1624179193Sjb	kmem_free(fprc, sizeof (fasttrap_proc_t));
1625179193Sjb}
1626179193Sjb
1627179193Sjb/*
1628179193Sjb * Lookup a fasttrap-managed provider based on its name and associated pid.
1629179193Sjb * If the pattr argument is non-NULL, this function instantiates the provider
1630179193Sjb * if it doesn't exist otherwise it returns NULL. The provider is returned
1631179193Sjb * with its lock held.
1632179193Sjb */
1633179193Sjbstatic fasttrap_provider_t *
1634179193Sjbfasttrap_provider_lookup(pid_t pid, const char *name,
1635179193Sjb    const dtrace_pattr_t *pattr)
1636179193Sjb{
1637179193Sjb	fasttrap_provider_t *fp, *new_fp = NULL;
1638179193Sjb	fasttrap_bucket_t *bucket;
1639179193Sjb	char provname[DTRACE_PROVNAMELEN];
1640179193Sjb	proc_t *p;
1641179193Sjb	cred_t *cred;
1642179193Sjb
1643179193Sjb	ASSERT(strlen(name) < sizeof (fp->ftp_name));
1644179193Sjb	ASSERT(pattr != NULL);
1645179193Sjb
1646179193Sjb	bucket = &fasttrap_provs.fth_table[FASTTRAP_PROVS_INDEX(pid, name)];
1647179193Sjb	mutex_enter(&bucket->ftb_mtx);
1648179193Sjb
1649179193Sjb	/*
1650179193Sjb	 * Take a lap through the list and return the match if we find it.
1651179193Sjb	 */
1652179193Sjb	for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) {
1653179193Sjb		if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 &&
1654179193Sjb		    !fp->ftp_retired) {
1655179193Sjb			mutex_enter(&fp->ftp_mtx);
1656179193Sjb			mutex_exit(&bucket->ftb_mtx);
1657179193Sjb			return (fp);
1658179193Sjb		}
1659179193Sjb	}
1660179193Sjb
1661179193Sjb	/*
1662179193Sjb	 * Drop the bucket lock so we don't try to perform a sleeping
1663179193Sjb	 * allocation under it.
1664179193Sjb	 */
1665179193Sjb	mutex_exit(&bucket->ftb_mtx);
1666179193Sjb
1667179193Sjb	/*
1668179193Sjb	 * Make sure the process exists, isn't a child created as the result
1669179193Sjb	 * of a vfork(2), and isn't a zombie (but may be in fork).
1670179193Sjb	 */
1671211738Srpaulo	if ((p = pfind(pid)) == NULL)
1672179193Sjb		return (NULL);
1673179193Sjb
1674179193Sjb	/*
1675179193Sjb	 * Increment p_dtrace_probes so that the process knows to inform us
1676179193Sjb	 * when it exits or execs. fasttrap_provider_free() decrements this
1677179193Sjb	 * when we're done with this provider.
1678179193Sjb	 */
1679179193Sjb	p->p_dtrace_probes++;
1680179193Sjb
1681179193Sjb	/*
1682179193Sjb	 * Grab the credentials for this process so we have
1683179193Sjb	 * something to pass to dtrace_register().
1684179193Sjb	 */
1685211738Srpaulo	PROC_LOCK_ASSERT(p, MA_OWNED);
1686211738Srpaulo	crhold(p->p_ucred);
1687211738Srpaulo	cred = p->p_ucred;
1688211738Srpaulo	PROC_UNLOCK(p);
1689179193Sjb
1690179193Sjb	new_fp = kmem_zalloc(sizeof (fasttrap_provider_t), KM_SLEEP);
1691179193Sjb	new_fp->ftp_pid = pid;
1692179193Sjb	new_fp->ftp_proc = fasttrap_proc_lookup(pid);
1693297077Smav#ifndef illumos
1694211738Srpaulo	mutex_init(&new_fp->ftp_mtx, "provider mtx", MUTEX_DEFAULT, NULL);
1695211738Srpaulo	mutex_init(&new_fp->ftp_cmtx, "lock on creating", MUTEX_DEFAULT, NULL);
1696211738Srpaulo#endif
1697179193Sjb
1698179193Sjb	ASSERT(new_fp->ftp_proc != NULL);
1699179193Sjb
1700179193Sjb	mutex_enter(&bucket->ftb_mtx);
1701179193Sjb
1702179193Sjb	/*
1703179193Sjb	 * Take another lap through the list to make sure a provider hasn't
1704179193Sjb	 * been created for this pid while we weren't under the bucket lock.
1705179193Sjb	 */
1706179193Sjb	for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) {
1707179193Sjb		if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 &&
1708179193Sjb		    !fp->ftp_retired) {
1709179193Sjb			mutex_enter(&fp->ftp_mtx);
1710179193Sjb			mutex_exit(&bucket->ftb_mtx);
1711179193Sjb			fasttrap_provider_free(new_fp);
1712179193Sjb			crfree(cred);
1713179193Sjb			return (fp);
1714179193Sjb		}
1715179193Sjb	}
1716179193Sjb
1717179193Sjb	(void) strcpy(new_fp->ftp_name, name);
1718179193Sjb
1719179193Sjb	/*
1720179193Sjb	 * Fail and return NULL if either the provider name is too long
1721179193Sjb	 * or we fail to register this new provider with the DTrace
1722179193Sjb	 * framework. Note that this is the only place we ever construct
1723179193Sjb	 * the full provider name -- we keep it in pieces in the provider
1724179193Sjb	 * structure.
1725179193Sjb	 */
1726179193Sjb	if (snprintf(provname, sizeof (provname), "%s%u", name, (uint_t)pid) >=
1727179193Sjb	    sizeof (provname) ||
1728179193Sjb	    dtrace_register(provname, pattr,
1729179193Sjb	    DTRACE_PRIV_PROC | DTRACE_PRIV_OWNER | DTRACE_PRIV_ZONEOWNER, cred,
1730179193Sjb	    pattr == &pid_attr ? &pid_pops : &usdt_pops, new_fp,
1731179193Sjb	    &new_fp->ftp_provid) != 0) {
1732179193Sjb		mutex_exit(&bucket->ftb_mtx);
1733179193Sjb		fasttrap_provider_free(new_fp);
1734179193Sjb		crfree(cred);
1735179193Sjb		return (NULL);
1736179193Sjb	}
1737179193Sjb
1738179193Sjb	new_fp->ftp_next = bucket->ftb_data;
1739179193Sjb	bucket->ftb_data = new_fp;
1740179193Sjb
1741179193Sjb	mutex_enter(&new_fp->ftp_mtx);
1742179193Sjb	mutex_exit(&bucket->ftb_mtx);
1743179193Sjb
1744179193Sjb	crfree(cred);
1745179193Sjb	return (new_fp);
1746179193Sjb}
1747179193Sjb
1748179193Sjbstatic void
1749179193Sjbfasttrap_provider_free(fasttrap_provider_t *provider)
1750179193Sjb{
1751179193Sjb	pid_t pid = provider->ftp_pid;
1752179193Sjb	proc_t *p;
1753179193Sjb
1754179193Sjb	/*
1755179193Sjb	 * There need to be no associated enabled probes, no consumers
1756179193Sjb	 * creating probes, and no meta providers referencing this provider.
1757179193Sjb	 */
1758179193Sjb	ASSERT(provider->ftp_rcount == 0);
1759179193Sjb	ASSERT(provider->ftp_ccount == 0);
1760179193Sjb	ASSERT(provider->ftp_mcount == 0);
1761179193Sjb
1762179198Sjb	/*
1763179198Sjb	 * If this provider hasn't been retired, we need to explicitly drop the
1764179198Sjb	 * count of active providers on the associated process structure.
1765179198Sjb	 */
1766179198Sjb	if (!provider->ftp_retired) {
1767271001Sdelphij		atomic_dec_64(&provider->ftp_proc->ftpc_acount);
1768179198Sjb		ASSERT(provider->ftp_proc->ftpc_acount <
1769179198Sjb		    provider->ftp_proc->ftpc_rcount);
1770179198Sjb	}
1771179198Sjb
1772179193Sjb	fasttrap_proc_release(provider->ftp_proc);
1773179193Sjb
1774297077Smav#ifndef illumos
1775211738Srpaulo	mutex_destroy(&provider->ftp_mtx);
1776211738Srpaulo	mutex_destroy(&provider->ftp_cmtx);
1777211738Srpaulo#endif
1778179193Sjb	kmem_free(provider, sizeof (fasttrap_provider_t));
1779179193Sjb
1780179193Sjb	/*
1781179193Sjb	 * Decrement p_dtrace_probes on the process whose provider we're
1782179193Sjb	 * freeing. We don't have to worry about clobbering somone else's
1783179193Sjb	 * modifications to it because we have locked the bucket that
1784179193Sjb	 * corresponds to this process's hash chain in the provider hash
1785179193Sjb	 * table. Don't sweat it if we can't find the process.
1786179193Sjb	 */
1787211738Srpaulo	if ((p = pfind(pid)) == NULL) {
1788179193Sjb		return;
1789179193Sjb	}
1790179193Sjb
1791179193Sjb	p->p_dtrace_probes--;
1792297077Smav#ifndef illumos
1793211738Srpaulo	PROC_UNLOCK(p);
1794211738Srpaulo#endif
1795179193Sjb}
1796179193Sjb
1797179193Sjbstatic void
1798179193Sjbfasttrap_provider_retire(pid_t pid, const char *name, int mprov)
1799179193Sjb{
1800179193Sjb	fasttrap_provider_t *fp;
1801179193Sjb	fasttrap_bucket_t *bucket;
1802179193Sjb	dtrace_provider_id_t provid;
1803179193Sjb
1804179193Sjb	ASSERT(strlen(name) < sizeof (fp->ftp_name));
1805179193Sjb
1806179193Sjb	bucket = &fasttrap_provs.fth_table[FASTTRAP_PROVS_INDEX(pid, name)];
1807179193Sjb	mutex_enter(&bucket->ftb_mtx);
1808179193Sjb
1809179193Sjb	for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) {
1810179193Sjb		if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 &&
1811179193Sjb		    !fp->ftp_retired)
1812179193Sjb			break;
1813179193Sjb	}
1814179193Sjb
1815179193Sjb	if (fp == NULL) {
1816179193Sjb		mutex_exit(&bucket->ftb_mtx);
1817179193Sjb		return;
1818179193Sjb	}
1819179193Sjb
1820179193Sjb	mutex_enter(&fp->ftp_mtx);
1821179193Sjb	ASSERT(!mprov || fp->ftp_mcount > 0);
1822179193Sjb	if (mprov && --fp->ftp_mcount != 0)  {
1823179193Sjb		mutex_exit(&fp->ftp_mtx);
1824179193Sjb		mutex_exit(&bucket->ftb_mtx);
1825179193Sjb		return;
1826179193Sjb	}
1827179193Sjb
1828179193Sjb	/*
1829179193Sjb	 * Mark the provider to be removed in our post-processing step, mark it
1830179193Sjb	 * retired, and drop the active count on its proc. Marking it indicates
1831179193Sjb	 * that we should try to remove it; setting the retired flag indicates
1832179193Sjb	 * that we're done with this provider; dropping the active the proc
1833179193Sjb	 * releases our hold, and when this reaches zero (as it will during
1834179193Sjb	 * exit or exec) the proc and associated providers become defunct.
1835179193Sjb	 *
1836179193Sjb	 * We obviously need to take the bucket lock before the provider lock
1837179193Sjb	 * to perform the lookup, but we need to drop the provider lock
1838179193Sjb	 * before calling into the DTrace framework since we acquire the
1839179193Sjb	 * provider lock in callbacks invoked from the DTrace framework. The
1840179193Sjb	 * bucket lock therefore protects the integrity of the provider hash
1841179193Sjb	 * table.
1842179193Sjb	 */
1843271001Sdelphij	atomic_dec_64(&fp->ftp_proc->ftpc_acount);
1844179198Sjb	ASSERT(fp->ftp_proc->ftpc_acount < fp->ftp_proc->ftpc_rcount);
1845179198Sjb
1846179193Sjb	fp->ftp_retired = 1;
1847179193Sjb	fp->ftp_marked = 1;
1848179193Sjb	provid = fp->ftp_provid;
1849179193Sjb	mutex_exit(&fp->ftp_mtx);
1850179193Sjb
1851179193Sjb	/*
1852179193Sjb	 * We don't have to worry about invalidating the same provider twice
1853179193Sjb	 * since fasttrap_provider_lookup() will ignore provider that have
1854179193Sjb	 * been marked as retired.
1855179193Sjb	 */
1856179193Sjb	dtrace_invalidate(provid);
1857179193Sjb
1858179193Sjb	mutex_exit(&bucket->ftb_mtx);
1859179193Sjb
1860179193Sjb	fasttrap_pid_cleanup();
1861179193Sjb}
1862179193Sjb
1863179193Sjbstatic int
1864179193Sjbfasttrap_uint32_cmp(const void *ap, const void *bp)
1865179193Sjb{
1866179193Sjb	return (*(const uint32_t *)ap - *(const uint32_t *)bp);
1867179193Sjb}
1868179193Sjb
1869179193Sjbstatic int
1870179193Sjbfasttrap_uint64_cmp(const void *ap, const void *bp)
1871179193Sjb{
1872179193Sjb	return (*(const uint64_t *)ap - *(const uint64_t *)bp);
1873179193Sjb}
1874179193Sjb
1875179193Sjbstatic int
1876179193Sjbfasttrap_add_probe(fasttrap_probe_spec_t *pdata)
1877179193Sjb{
1878179193Sjb	fasttrap_provider_t *provider;
1879179193Sjb	fasttrap_probe_t *pp;
1880179193Sjb	fasttrap_tracepoint_t *tp;
1881179193Sjb	char *name;
1882211738Srpaulo	int i, aframes = 0, whack;
1883179193Sjb
1884179193Sjb	/*
1885179193Sjb	 * There needs to be at least one desired trace point.
1886179193Sjb	 */
1887179193Sjb	if (pdata->ftps_noffs == 0)
1888179193Sjb		return (EINVAL);
1889179193Sjb
1890179193Sjb	switch (pdata->ftps_type) {
1891179193Sjb	case DTFTP_ENTRY:
1892179193Sjb		name = "entry";
1893179193Sjb		aframes = FASTTRAP_ENTRY_AFRAMES;
1894179193Sjb		break;
1895179193Sjb	case DTFTP_RETURN:
1896179193Sjb		name = "return";
1897179193Sjb		aframes = FASTTRAP_RETURN_AFRAMES;
1898179193Sjb		break;
1899179193Sjb	case DTFTP_OFFSETS:
1900179193Sjb		name = NULL;
1901179193Sjb		break;
1902179193Sjb	default:
1903179193Sjb		return (EINVAL);
1904179193Sjb	}
1905179193Sjb
1906179193Sjb	if ((provider = fasttrap_provider_lookup(pdata->ftps_pid,
1907179193Sjb	    FASTTRAP_PID_NAME, &pid_attr)) == NULL)
1908179193Sjb		return (ESRCH);
1909179193Sjb
1910179193Sjb	/*
1911179193Sjb	 * Increment this reference count to indicate that a consumer is
1912179193Sjb	 * actively adding a new probe associated with this provider. This
1913179193Sjb	 * prevents the provider from being deleted -- we'll need to check
1914179193Sjb	 * for pending deletions when we drop this reference count.
1915179193Sjb	 */
1916179193Sjb	provider->ftp_ccount++;
1917179193Sjb	mutex_exit(&provider->ftp_mtx);
1918179193Sjb
1919179193Sjb	/*
1920179193Sjb	 * Grab the creation lock to ensure consistency between calls to
1921179193Sjb	 * dtrace_probe_lookup() and dtrace_probe_create() in the face of
1922179193Sjb	 * other threads creating probes. We must drop the provider lock
1923179193Sjb	 * before taking this lock to avoid a three-way deadlock with the
1924179193Sjb	 * DTrace framework.
1925179193Sjb	 */
1926179193Sjb	mutex_enter(&provider->ftp_cmtx);
1927179193Sjb
1928179193Sjb	if (name == NULL) {
1929179193Sjb		for (i = 0; i < pdata->ftps_noffs; i++) {
1930179193Sjb			char name_str[17];
1931179193Sjb
1932179193Sjb			(void) sprintf(name_str, "%llx",
1933179193Sjb			    (unsigned long long)pdata->ftps_offs[i]);
1934179193Sjb
1935179193Sjb			if (dtrace_probe_lookup(provider->ftp_provid,
1936179193Sjb			    pdata->ftps_mod, pdata->ftps_func, name_str) != 0)
1937179193Sjb				continue;
1938179193Sjb
1939271001Sdelphij			atomic_inc_32(&fasttrap_total);
1940179193Sjb
1941179193Sjb			if (fasttrap_total > fasttrap_max) {
1942271001Sdelphij				atomic_dec_32(&fasttrap_total);
1943179193Sjb				goto no_mem;
1944179193Sjb			}
1945179193Sjb
1946179193Sjb			pp = kmem_zalloc(sizeof (fasttrap_probe_t), KM_SLEEP);
1947179193Sjb
1948179193Sjb			pp->ftp_prov = provider;
1949179193Sjb			pp->ftp_faddr = pdata->ftps_pc;
1950179193Sjb			pp->ftp_fsize = pdata->ftps_size;
1951179193Sjb			pp->ftp_pid = pdata->ftps_pid;
1952179193Sjb			pp->ftp_ntps = 1;
1953179193Sjb
1954179193Sjb			tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t),
1955179193Sjb			    KM_SLEEP);
1956179193Sjb
1957179193Sjb			tp->ftt_proc = provider->ftp_proc;
1958179193Sjb			tp->ftt_pc = pdata->ftps_offs[i] + pdata->ftps_pc;
1959179193Sjb			tp->ftt_pid = pdata->ftps_pid;
1960179193Sjb
1961179193Sjb			pp->ftp_tps[0].fit_tp = tp;
1962179193Sjb			pp->ftp_tps[0].fit_id.fti_probe = pp;
1963179193Sjb			pp->ftp_tps[0].fit_id.fti_ptype = pdata->ftps_type;
1964179193Sjb
1965179193Sjb			pp->ftp_id = dtrace_probe_create(provider->ftp_provid,
1966179193Sjb			    pdata->ftps_mod, pdata->ftps_func, name_str,
1967179193Sjb			    FASTTRAP_OFFSET_AFRAMES, pp);
1968179193Sjb		}
1969179193Sjb
1970179193Sjb	} else if (dtrace_probe_lookup(provider->ftp_provid, pdata->ftps_mod,
1971179193Sjb	    pdata->ftps_func, name) == 0) {
1972179193Sjb		atomic_add_32(&fasttrap_total, pdata->ftps_noffs);
1973179193Sjb
1974179193Sjb		if (fasttrap_total > fasttrap_max) {
1975179193Sjb			atomic_add_32(&fasttrap_total, -pdata->ftps_noffs);
1976179193Sjb			goto no_mem;
1977179193Sjb		}
1978179193Sjb
1979179193Sjb		/*
1980179193Sjb		 * Make sure all tracepoint program counter values are unique.
1981179193Sjb		 * We later assume that each probe has exactly one tracepoint
1982179193Sjb		 * for a given pc.
1983179193Sjb		 */
1984179193Sjb		qsort(pdata->ftps_offs, pdata->ftps_noffs,
1985179193Sjb		    sizeof (uint64_t), fasttrap_uint64_cmp);
1986179193Sjb		for (i = 1; i < pdata->ftps_noffs; i++) {
1987179193Sjb			if (pdata->ftps_offs[i] > pdata->ftps_offs[i - 1])
1988179193Sjb				continue;
1989179193Sjb
1990179193Sjb			atomic_add_32(&fasttrap_total, -pdata->ftps_noffs);
1991179193Sjb			goto no_mem;
1992179193Sjb		}
1993179193Sjb
1994179193Sjb		ASSERT(pdata->ftps_noffs > 0);
1995179193Sjb		pp = kmem_zalloc(offsetof(fasttrap_probe_t,
1996179193Sjb		    ftp_tps[pdata->ftps_noffs]), KM_SLEEP);
1997179193Sjb
1998179193Sjb		pp->ftp_prov = provider;
1999179193Sjb		pp->ftp_faddr = pdata->ftps_pc;
2000179193Sjb		pp->ftp_fsize = pdata->ftps_size;
2001179193Sjb		pp->ftp_pid = pdata->ftps_pid;
2002179193Sjb		pp->ftp_ntps = pdata->ftps_noffs;
2003179193Sjb
2004179193Sjb		for (i = 0; i < pdata->ftps_noffs; i++) {
2005179193Sjb			tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t),
2006179193Sjb			    KM_SLEEP);
2007179193Sjb
2008179193Sjb			tp->ftt_proc = provider->ftp_proc;
2009179193Sjb			tp->ftt_pc = pdata->ftps_offs[i] + pdata->ftps_pc;
2010179193Sjb			tp->ftt_pid = pdata->ftps_pid;
2011179193Sjb
2012179193Sjb			pp->ftp_tps[i].fit_tp = tp;
2013179193Sjb			pp->ftp_tps[i].fit_id.fti_probe = pp;
2014179193Sjb			pp->ftp_tps[i].fit_id.fti_ptype = pdata->ftps_type;
2015179193Sjb		}
2016179193Sjb
2017179193Sjb		pp->ftp_id = dtrace_probe_create(provider->ftp_provid,
2018179193Sjb		    pdata->ftps_mod, pdata->ftps_func, name, aframes, pp);
2019179193Sjb	}
2020179193Sjb
2021179193Sjb	mutex_exit(&provider->ftp_cmtx);
2022179193Sjb
2023179193Sjb	/*
2024179193Sjb	 * We know that the provider is still valid since we incremented the
2025179193Sjb	 * creation reference count. If someone tried to clean up this provider
2026179193Sjb	 * while we were using it (e.g. because the process called exec(2) or
2027179193Sjb	 * exit(2)), take note of that and try to clean it up now.
2028179193Sjb	 */
2029179193Sjb	mutex_enter(&provider->ftp_mtx);
2030179193Sjb	provider->ftp_ccount--;
2031179193Sjb	whack = provider->ftp_retired;
2032179193Sjb	mutex_exit(&provider->ftp_mtx);
2033179193Sjb
2034179193Sjb	if (whack)
2035179193Sjb		fasttrap_pid_cleanup();
2036179193Sjb
2037179193Sjb	return (0);
2038179193Sjb
2039179193Sjbno_mem:
2040179193Sjb	/*
2041179193Sjb	 * If we've exhausted the allowable resources, we'll try to remove
2042179193Sjb	 * this provider to free some up. This is to cover the case where
2043179193Sjb	 * the user has accidentally created many more probes than was
2044179193Sjb	 * intended (e.g. pid123:::).
2045179193Sjb	 */
2046179193Sjb	mutex_exit(&provider->ftp_cmtx);
2047179193Sjb	mutex_enter(&provider->ftp_mtx);
2048179193Sjb	provider->ftp_ccount--;
2049179193Sjb	provider->ftp_marked = 1;
2050179193Sjb	mutex_exit(&provider->ftp_mtx);
2051179193Sjb
2052179193Sjb	fasttrap_pid_cleanup();
2053179193Sjb
2054179193Sjb	return (ENOMEM);
2055179193Sjb}
2056179193Sjb
2057179193Sjb/*ARGSUSED*/
2058179193Sjbstatic void *
2059179193Sjbfasttrap_meta_provide(void *arg, dtrace_helper_provdesc_t *dhpv, pid_t pid)
2060179193Sjb{
2061179193Sjb	fasttrap_provider_t *provider;
2062179193Sjb
2063179193Sjb	/*
2064179193Sjb	 * A 32-bit unsigned integer (like a pid for example) can be
2065179193Sjb	 * expressed in 10 or fewer decimal digits. Make sure that we'll
2066179193Sjb	 * have enough space for the provider name.
2067179193Sjb	 */
2068179193Sjb	if (strlen(dhpv->dthpv_provname) + 10 >=
2069179193Sjb	    sizeof (provider->ftp_name)) {
2070211738Srpaulo		printf("failed to instantiate provider %s: "
2071179193Sjb		    "name too long to accomodate pid", dhpv->dthpv_provname);
2072179193Sjb		return (NULL);
2073179193Sjb	}
2074179193Sjb
2075179193Sjb	/*
2076179193Sjb	 * Don't let folks spoof the true pid provider.
2077179193Sjb	 */
2078179193Sjb	if (strcmp(dhpv->dthpv_provname, FASTTRAP_PID_NAME) == 0) {
2079211738Srpaulo		printf("failed to instantiate provider %s: "
2080179193Sjb		    "%s is an invalid name", dhpv->dthpv_provname,
2081179193Sjb		    FASTTRAP_PID_NAME);
2082179193Sjb		return (NULL);
2083179193Sjb	}
2084179193Sjb
2085179193Sjb	/*
2086179193Sjb	 * The highest stability class that fasttrap supports is ISA; cap
2087179193Sjb	 * the stability of the new provider accordingly.
2088179193Sjb	 */
2089179193Sjb	if (dhpv->dthpv_pattr.dtpa_provider.dtat_class > DTRACE_CLASS_ISA)
2090179193Sjb		dhpv->dthpv_pattr.dtpa_provider.dtat_class = DTRACE_CLASS_ISA;
2091179193Sjb	if (dhpv->dthpv_pattr.dtpa_mod.dtat_class > DTRACE_CLASS_ISA)
2092179193Sjb		dhpv->dthpv_pattr.dtpa_mod.dtat_class = DTRACE_CLASS_ISA;
2093179193Sjb	if (dhpv->dthpv_pattr.dtpa_func.dtat_class > DTRACE_CLASS_ISA)
2094179193Sjb		dhpv->dthpv_pattr.dtpa_func.dtat_class = DTRACE_CLASS_ISA;
2095179193Sjb	if (dhpv->dthpv_pattr.dtpa_name.dtat_class > DTRACE_CLASS_ISA)
2096179193Sjb		dhpv->dthpv_pattr.dtpa_name.dtat_class = DTRACE_CLASS_ISA;
2097179193Sjb	if (dhpv->dthpv_pattr.dtpa_args.dtat_class > DTRACE_CLASS_ISA)
2098179193Sjb		dhpv->dthpv_pattr.dtpa_args.dtat_class = DTRACE_CLASS_ISA;
2099179193Sjb
2100179193Sjb	if ((provider = fasttrap_provider_lookup(pid, dhpv->dthpv_provname,
2101179193Sjb	    &dhpv->dthpv_pattr)) == NULL) {
2102211738Srpaulo		printf("failed to instantiate provider %s for "
2103179193Sjb		    "process %u",  dhpv->dthpv_provname, (uint_t)pid);
2104179193Sjb		return (NULL);
2105179193Sjb	}
2106179193Sjb
2107179193Sjb	/*
2108179193Sjb	 * Up the meta provider count so this provider isn't removed until
2109179193Sjb	 * the meta provider has been told to remove it.
2110179193Sjb	 */
2111179193Sjb	provider->ftp_mcount++;
2112179193Sjb
2113179193Sjb	mutex_exit(&provider->ftp_mtx);
2114179193Sjb
2115179193Sjb	return (provider);
2116179193Sjb}
2117179193Sjb
2118179193Sjb/*ARGSUSED*/
2119179193Sjbstatic void
2120179193Sjbfasttrap_meta_create_probe(void *arg, void *parg,
2121179193Sjb    dtrace_helper_probedesc_t *dhpb)
2122179193Sjb{
2123179193Sjb	fasttrap_provider_t *provider = parg;
2124179193Sjb	fasttrap_probe_t *pp;
2125179193Sjb	fasttrap_tracepoint_t *tp;
2126179193Sjb	int i, j;
2127179193Sjb	uint32_t ntps;
2128179193Sjb
2129179193Sjb	/*
2130179193Sjb	 * Since the meta provider count is non-zero we don't have to worry
2131179193Sjb	 * about this provider disappearing.
2132179193Sjb	 */
2133179193Sjb	ASSERT(provider->ftp_mcount > 0);
2134179193Sjb
2135179193Sjb	/*
2136179193Sjb	 * The offsets must be unique.
2137179193Sjb	 */
2138179193Sjb	qsort(dhpb->dthpb_offs, dhpb->dthpb_noffs, sizeof (uint32_t),
2139179193Sjb	    fasttrap_uint32_cmp);
2140179193Sjb	for (i = 1; i < dhpb->dthpb_noffs; i++) {
2141179193Sjb		if (dhpb->dthpb_base + dhpb->dthpb_offs[i] <=
2142179193Sjb		    dhpb->dthpb_base + dhpb->dthpb_offs[i - 1])
2143179193Sjb			return;
2144179193Sjb	}
2145179193Sjb
2146179193Sjb	qsort(dhpb->dthpb_enoffs, dhpb->dthpb_nenoffs, sizeof (uint32_t),
2147179193Sjb	    fasttrap_uint32_cmp);
2148179193Sjb	for (i = 1; i < dhpb->dthpb_nenoffs; i++) {
2149179193Sjb		if (dhpb->dthpb_base + dhpb->dthpb_enoffs[i] <=
2150179193Sjb		    dhpb->dthpb_base + dhpb->dthpb_enoffs[i - 1])
2151179193Sjb			return;
2152179193Sjb	}
2153179193Sjb
2154179193Sjb	/*
2155179193Sjb	 * Grab the creation lock to ensure consistency between calls to
2156179193Sjb	 * dtrace_probe_lookup() and dtrace_probe_create() in the face of
2157179193Sjb	 * other threads creating probes.
2158179193Sjb	 */
2159179193Sjb	mutex_enter(&provider->ftp_cmtx);
2160179193Sjb
2161179193Sjb	if (dtrace_probe_lookup(provider->ftp_provid, dhpb->dthpb_mod,
2162179193Sjb	    dhpb->dthpb_func, dhpb->dthpb_name) != 0) {
2163179193Sjb		mutex_exit(&provider->ftp_cmtx);
2164179193Sjb		return;
2165179193Sjb	}
2166179193Sjb
2167179193Sjb	ntps = dhpb->dthpb_noffs + dhpb->dthpb_nenoffs;
2168179193Sjb	ASSERT(ntps > 0);
2169179193Sjb
2170179193Sjb	atomic_add_32(&fasttrap_total, ntps);
2171179193Sjb
2172179193Sjb	if (fasttrap_total > fasttrap_max) {
2173179193Sjb		atomic_add_32(&fasttrap_total, -ntps);
2174179193Sjb		mutex_exit(&provider->ftp_cmtx);
2175179193Sjb		return;
2176179193Sjb	}
2177179193Sjb
2178179193Sjb	pp = kmem_zalloc(offsetof(fasttrap_probe_t, ftp_tps[ntps]), KM_SLEEP);
2179179193Sjb
2180179193Sjb	pp->ftp_prov = provider;
2181179193Sjb	pp->ftp_pid = provider->ftp_pid;
2182179193Sjb	pp->ftp_ntps = ntps;
2183179193Sjb	pp->ftp_nargs = dhpb->dthpb_xargc;
2184179193Sjb	pp->ftp_xtypes = dhpb->dthpb_xtypes;
2185179193Sjb	pp->ftp_ntypes = dhpb->dthpb_ntypes;
2186179193Sjb
2187179193Sjb	/*
2188179193Sjb	 * First create a tracepoint for each actual point of interest.
2189179193Sjb	 */
2190179193Sjb	for (i = 0; i < dhpb->dthpb_noffs; i++) {
2191179193Sjb		tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), KM_SLEEP);
2192179193Sjb
2193179193Sjb		tp->ftt_proc = provider->ftp_proc;
2194179193Sjb		tp->ftt_pc = dhpb->dthpb_base + dhpb->dthpb_offs[i];
2195179193Sjb		tp->ftt_pid = provider->ftp_pid;
2196179193Sjb
2197179193Sjb		pp->ftp_tps[i].fit_tp = tp;
2198179193Sjb		pp->ftp_tps[i].fit_id.fti_probe = pp;
2199179193Sjb#ifdef __sparc
2200179193Sjb		pp->ftp_tps[i].fit_id.fti_ptype = DTFTP_POST_OFFSETS;
2201179193Sjb#else
2202179193Sjb		pp->ftp_tps[i].fit_id.fti_ptype = DTFTP_OFFSETS;
2203179193Sjb#endif
2204179193Sjb	}
2205179193Sjb
2206179193Sjb	/*
2207179193Sjb	 * Then create a tracepoint for each is-enabled point.
2208179193Sjb	 */
2209179193Sjb	for (j = 0; i < ntps; i++, j++) {
2210179193Sjb		tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), KM_SLEEP);
2211179193Sjb
2212179193Sjb		tp->ftt_proc = provider->ftp_proc;
2213179193Sjb		tp->ftt_pc = dhpb->dthpb_base + dhpb->dthpb_enoffs[j];
2214179193Sjb		tp->ftt_pid = provider->ftp_pid;
2215179193Sjb
2216179193Sjb		pp->ftp_tps[i].fit_tp = tp;
2217179193Sjb		pp->ftp_tps[i].fit_id.fti_probe = pp;
2218179193Sjb		pp->ftp_tps[i].fit_id.fti_ptype = DTFTP_IS_ENABLED;
2219179193Sjb	}
2220179193Sjb
2221179193Sjb	/*
2222179193Sjb	 * If the arguments are shuffled around we set the argument remapping
2223179193Sjb	 * table. Later, when the probe fires, we only remap the arguments
2224179193Sjb	 * if the table is non-NULL.
2225179193Sjb	 */
2226179193Sjb	for (i = 0; i < dhpb->dthpb_xargc; i++) {
2227179193Sjb		if (dhpb->dthpb_args[i] != i) {
2228179193Sjb			pp->ftp_argmap = dhpb->dthpb_args;
2229179193Sjb			break;
2230179193Sjb		}
2231179193Sjb	}
2232179193Sjb
2233179193Sjb	/*
2234179193Sjb	 * The probe is fully constructed -- register it with DTrace.
2235179193Sjb	 */
2236179193Sjb	pp->ftp_id = dtrace_probe_create(provider->ftp_provid, dhpb->dthpb_mod,
2237179193Sjb	    dhpb->dthpb_func, dhpb->dthpb_name, FASTTRAP_OFFSET_AFRAMES, pp);
2238179193Sjb
2239179193Sjb	mutex_exit(&provider->ftp_cmtx);
2240179193Sjb}
2241179193Sjb
2242179193Sjb/*ARGSUSED*/
2243179193Sjbstatic void
2244179193Sjbfasttrap_meta_remove(void *arg, dtrace_helper_provdesc_t *dhpv, pid_t pid)
2245179193Sjb{
2246179193Sjb	/*
2247179193Sjb	 * Clean up the USDT provider. There may be active consumers of the
2248179193Sjb	 * provider busy adding probes, no damage will actually befall the
2249179193Sjb	 * provider until that count has dropped to zero. This just puts
2250179193Sjb	 * the provider on death row.
2251179193Sjb	 */
2252179193Sjb	fasttrap_provider_retire(pid, dhpv->dthpv_provname, 1);
2253179193Sjb}
2254179193Sjb
2255179193Sjbstatic dtrace_mops_t fasttrap_mops = {
2256179193Sjb	fasttrap_meta_create_probe,
2257179193Sjb	fasttrap_meta_provide,
2258179193Sjb	fasttrap_meta_remove
2259179193Sjb};
2260179193Sjb
2261179193Sjb/*ARGSUSED*/
2262179193Sjbstatic int
2263211738Srpaulofasttrap_open(struct cdev *dev __unused, int oflags __unused,
2264211738Srpaulo    int devtype __unused, struct thread *td __unused)
2265179193Sjb{
2266179193Sjb	return (0);
2267179193Sjb}
2268179193Sjb
2269179193Sjb/*ARGSUSED*/
2270179193Sjbstatic int
2271211738Srpaulofasttrap_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int fflag,
2272211738Srpaulo    struct thread *td)
2273179193Sjb{
2274211738Srpaulo#ifdef notyet
2275211738Srpaulo	struct kinfo_proc kp;
2276211738Srpaulo	const cred_t *cr = td->td_ucred;
2277211738Srpaulo#endif
2278179193Sjb	if (!dtrace_attached())
2279179193Sjb		return (EAGAIN);
2280179193Sjb
2281179193Sjb	if (cmd == FASTTRAPIOC_MAKEPROBE) {
2282262048Savg		fasttrap_probe_spec_t *uprobe = *(fasttrap_probe_spec_t **)arg;
2283179193Sjb		fasttrap_probe_spec_t *probe;
2284179193Sjb		uint64_t noffs;
2285179193Sjb		size_t size;
2286268734Spfg		int ret, err;
2287179193Sjb
2288179193Sjb		if (copyin(&uprobe->ftps_noffs, &noffs,
2289179193Sjb		    sizeof (uprobe->ftps_noffs)))
2290179193Sjb			return (EFAULT);
2291179193Sjb
2292179193Sjb		/*
2293179193Sjb		 * Probes must have at least one tracepoint.
2294179193Sjb		 */
2295179193Sjb		if (noffs == 0)
2296179193Sjb			return (EINVAL);
2297179193Sjb
2298179193Sjb		size = sizeof (fasttrap_probe_spec_t) +
2299179193Sjb		    sizeof (probe->ftps_offs[0]) * (noffs - 1);
2300179193Sjb
2301179193Sjb		if (size > 1024 * 1024)
2302179193Sjb			return (ENOMEM);
2303179193Sjb
2304179193Sjb		probe = kmem_alloc(size, KM_SLEEP);
2305179193Sjb
2306268572Spfg		if (copyin(uprobe, probe, size) != 0 ||
2307268572Spfg		    probe->ftps_noffs != noffs) {
2308179193Sjb			kmem_free(probe, size);
2309179193Sjb			return (EFAULT);
2310179193Sjb		}
2311179193Sjb
2312179193Sjb		/*
2313179193Sjb		 * Verify that the function and module strings contain no
2314179193Sjb		 * funny characters.
2315179193Sjb		 */
2316268734Spfg		if (u8_validate(probe->ftps_func, strlen(probe->ftps_func),
2317268734Spfg		    NULL, U8_VALIDATE_ENTIRE, &err) < 0) {
2318268734Spfg			ret = EINVAL;
2319268734Spfg			goto err;
2320179193Sjb		}
2321179193Sjb
2322268734Spfg		if (u8_validate(probe->ftps_mod, strlen(probe->ftps_mod),
2323268734Spfg		    NULL, U8_VALIDATE_ENTIRE, &err) < 0) {
2324268734Spfg			ret = EINVAL;
2325268734Spfg			goto err;
2326179193Sjb		}
2327179193Sjb
2328211738Srpaulo#ifdef notyet
2329179193Sjb		if (!PRIV_POLICY_CHOICE(cr, PRIV_ALL, B_FALSE)) {
2330179193Sjb			proc_t *p;
2331179193Sjb			pid_t pid = probe->ftps_pid;
2332179193Sjb
2333297077Smav#ifdef illumos
2334179193Sjb			mutex_enter(&pidlock);
2335211738Srpaulo#endif
2336179193Sjb			/*
2337179193Sjb			 * Report an error if the process doesn't exist
2338179193Sjb			 * or is actively being birthed.
2339179193Sjb			 */
2340211738Srpaulo			p = pfind(pid);
2341211738Srpaulo			if (p)
2342211738Srpaulo				fill_kinfo_proc(p, &kp);
2343211738Srpaulo			if (p == NULL || kp.ki_stat == SIDL) {
2344297077Smav#ifdef illumos
2345179193Sjb				mutex_exit(&pidlock);
2346211738Srpaulo#endif
2347179193Sjb				return (ESRCH);
2348179193Sjb			}
2349297077Smav#ifdef illumos
2350179193Sjb			mutex_enter(&p->p_lock);
2351179193Sjb			mutex_exit(&pidlock);
2352211738Srpaulo#else
2353211738Srpaulo			PROC_LOCK_ASSERT(p, MA_OWNED);
2354211738Srpaulo#endif
2355179193Sjb
2356211738Srpaulo#ifdef notyet
2357179193Sjb			if ((ret = priv_proc_cred_perm(cr, p, NULL,
2358179193Sjb			    VREAD | VWRITE)) != 0) {
2359297077Smav#ifdef illumos
2360179193Sjb				mutex_exit(&p->p_lock);
2361211738Srpaulo#else
2362211738Srpaulo				PROC_UNLOCK(p);
2363211738Srpaulo#endif
2364179193Sjb				return (ret);
2365179193Sjb			}
2366211738Srpaulo#endif /* notyet */
2367297077Smav#ifdef illumos
2368179193Sjb			mutex_exit(&p->p_lock);
2369211738Srpaulo#else
2370211738Srpaulo			PROC_UNLOCK(p);
2371211738Srpaulo#endif
2372179193Sjb		}
2373211738Srpaulo#endif /* notyet */
2374179193Sjb
2375179193Sjb		ret = fasttrap_add_probe(probe);
2376179193Sjberr:
2377179193Sjb		kmem_free(probe, size);
2378179193Sjb
2379179193Sjb		return (ret);
2380179193Sjb
2381179193Sjb	} else if (cmd == FASTTRAPIOC_GETINSTR) {
2382179193Sjb		fasttrap_instr_query_t instr;
2383179193Sjb		fasttrap_tracepoint_t *tp;
2384179193Sjb		uint_t index;
2385297077Smav#ifdef illumos
2386179193Sjb		int ret;
2387211738Srpaulo#endif
2388179193Sjb
2389297077Smav#ifdef illumos
2390179193Sjb		if (copyin((void *)arg, &instr, sizeof (instr)) != 0)
2391179193Sjb			return (EFAULT);
2392211738Srpaulo#endif
2393179193Sjb
2394211738Srpaulo#ifdef notyet
2395179193Sjb		if (!PRIV_POLICY_CHOICE(cr, PRIV_ALL, B_FALSE)) {
2396179193Sjb			proc_t *p;
2397179193Sjb			pid_t pid = instr.ftiq_pid;
2398179193Sjb
2399297077Smav#ifdef illumos
2400179193Sjb			mutex_enter(&pidlock);
2401211738Srpaulo#endif
2402179193Sjb			/*
2403179193Sjb			 * Report an error if the process doesn't exist
2404179193Sjb			 * or is actively being birthed.
2405179193Sjb			 */
2406211738Srpaulo			p = pfind(pid);
2407211738Srpaulo			if (p)
2408211738Srpaulo				fill_kinfo_proc(p, &kp);
2409211738Srpaulo			if (p == NULL || kp.ki_stat == SIDL) {
2410297077Smav#ifdef illumos
2411179193Sjb				mutex_exit(&pidlock);
2412211738Srpaulo#endif
2413179193Sjb				return (ESRCH);
2414179193Sjb			}
2415297077Smav#ifdef illumos
2416179193Sjb			mutex_enter(&p->p_lock);
2417179193Sjb			mutex_exit(&pidlock);
2418211738Srpaulo#else
2419211738Srpaulo			PROC_LOCK_ASSERT(p, MA_OWNED);
2420211738Srpaulo#endif
2421179193Sjb
2422211738Srpaulo#ifdef notyet
2423179193Sjb			if ((ret = priv_proc_cred_perm(cr, p, NULL,
2424179193Sjb			    VREAD)) != 0) {
2425297077Smav#ifdef illumos
2426179193Sjb				mutex_exit(&p->p_lock);
2427211738Srpaulo#else
2428211738Srpaulo				PROC_UNLOCK(p);
2429211738Srpaulo#endif
2430179193Sjb				return (ret);
2431179193Sjb			}
2432211738Srpaulo#endif /* notyet */
2433179193Sjb
2434297077Smav#ifdef illumos
2435179193Sjb			mutex_exit(&p->p_lock);
2436211738Srpaulo#else
2437211738Srpaulo			PROC_UNLOCK(p);
2438211738Srpaulo#endif
2439179193Sjb		}
2440211738Srpaulo#endif /* notyet */
2441179193Sjb
2442179193Sjb		index = FASTTRAP_TPOINTS_INDEX(instr.ftiq_pid, instr.ftiq_pc);
2443179193Sjb
2444179193Sjb		mutex_enter(&fasttrap_tpoints.fth_table[index].ftb_mtx);
2445179193Sjb		tp = fasttrap_tpoints.fth_table[index].ftb_data;
2446179193Sjb		while (tp != NULL) {
2447179193Sjb			if (instr.ftiq_pid == tp->ftt_pid &&
2448179193Sjb			    instr.ftiq_pc == tp->ftt_pc &&
2449179193Sjb			    tp->ftt_proc->ftpc_acount != 0)
2450179193Sjb				break;
2451179193Sjb
2452179193Sjb			tp = tp->ftt_next;
2453179193Sjb		}
2454179193Sjb
2455179193Sjb		if (tp == NULL) {
2456179193Sjb			mutex_exit(&fasttrap_tpoints.fth_table[index].ftb_mtx);
2457179193Sjb			return (ENOENT);
2458179193Sjb		}
2459179193Sjb
2460179193Sjb		bcopy(&tp->ftt_instr, &instr.ftiq_instr,
2461179193Sjb		    sizeof (instr.ftiq_instr));
2462179193Sjb		mutex_exit(&fasttrap_tpoints.fth_table[index].ftb_mtx);
2463179193Sjb
2464179193Sjb		if (copyout(&instr, (void *)arg, sizeof (instr)) != 0)
2465179193Sjb			return (EFAULT);
2466179193Sjb
2467179193Sjb		return (0);
2468179193Sjb	}
2469179193Sjb
2470179193Sjb	return (EINVAL);
2471179193Sjb}
2472179193Sjb
2473179193Sjbstatic int
2474211738Srpaulofasttrap_load(void)
2475179193Sjb{
2476179193Sjb	ulong_t nent;
2477250953Smarkj	int i, ret;
2478179193Sjb
2479211738Srpaulo        /* Create the /dev/dtrace/fasttrap entry. */
2480211738Srpaulo        fasttrap_cdev = make_dev(&fasttrap_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
2481211738Srpaulo            "dtrace/fasttrap");
2482179193Sjb
2483211738Srpaulo	mtx_init(&fasttrap_cleanup_mtx, "fasttrap clean", "dtrace", MTX_DEF);
2484211738Srpaulo	mutex_init(&fasttrap_count_mtx, "fasttrap count mtx", MUTEX_DEFAULT,
2485211738Srpaulo	    NULL);
2486179193Sjb
2487297077Smav#ifdef illumos
2488179193Sjb	fasttrap_max = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
2489179193Sjb	    "fasttrap-max-probes", FASTTRAP_MAX_DEFAULT);
2490211738Srpaulo#endif
2491179193Sjb	fasttrap_total = 0;
2492179193Sjb
2493179193Sjb	/*
2494179193Sjb	 * Conjure up the tracepoints hashtable...
2495179193Sjb	 */
2496297077Smav#ifdef illumos
2497179193Sjb	nent = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
2498179193Sjb	    "fasttrap-hash-size", FASTTRAP_TPOINTS_DEFAULT_SIZE);
2499211738Srpaulo#else
2500293413Sstas	nent = tpoints_hash_size;
2501211738Srpaulo#endif
2502179193Sjb
2503179193Sjb	if (nent == 0 || nent > 0x1000000)
2504179193Sjb		nent = FASTTRAP_TPOINTS_DEFAULT_SIZE;
2505179193Sjb
2506293413Sstas	tpoints_hash_size = nent;
2507293413Sstas
2508277547Sdelphij	if (ISP2(nent))
2509179193Sjb		fasttrap_tpoints.fth_nent = nent;
2510179193Sjb	else
2511179193Sjb		fasttrap_tpoints.fth_nent = 1 << fasttrap_highbit(nent);
2512179193Sjb	ASSERT(fasttrap_tpoints.fth_nent > 0);
2513179193Sjb	fasttrap_tpoints.fth_mask = fasttrap_tpoints.fth_nent - 1;
2514179193Sjb	fasttrap_tpoints.fth_table = kmem_zalloc(fasttrap_tpoints.fth_nent *
2515179193Sjb	    sizeof (fasttrap_bucket_t), KM_SLEEP);
2516297077Smav#ifndef illumos
2517211738Srpaulo	for (i = 0; i < fasttrap_tpoints.fth_nent; i++)
2518211738Srpaulo		mutex_init(&fasttrap_tpoints.fth_table[i].ftb_mtx,
2519211738Srpaulo		    "tracepoints bucket mtx", MUTEX_DEFAULT, NULL);
2520211738Srpaulo#endif
2521179193Sjb
2522179193Sjb	/*
2523179193Sjb	 * ... and the providers hash table...
2524179193Sjb	 */
2525179193Sjb	nent = FASTTRAP_PROVIDERS_DEFAULT_SIZE;
2526277547Sdelphij	if (ISP2(nent))
2527179193Sjb		fasttrap_provs.fth_nent = nent;
2528179193Sjb	else
2529179193Sjb		fasttrap_provs.fth_nent = 1 << fasttrap_highbit(nent);
2530179193Sjb	ASSERT(fasttrap_provs.fth_nent > 0);
2531179193Sjb	fasttrap_provs.fth_mask = fasttrap_provs.fth_nent - 1;
2532179193Sjb	fasttrap_provs.fth_table = kmem_zalloc(fasttrap_provs.fth_nent *
2533179193Sjb	    sizeof (fasttrap_bucket_t), KM_SLEEP);
2534297077Smav#ifndef illumos
2535211738Srpaulo	for (i = 0; i < fasttrap_provs.fth_nent; i++)
2536211738Srpaulo		mutex_init(&fasttrap_provs.fth_table[i].ftb_mtx,
2537211738Srpaulo		    "providers bucket mtx", MUTEX_DEFAULT, NULL);
2538211738Srpaulo#endif
2539179193Sjb
2540259483Sasomers	ret = kproc_create(fasttrap_pid_cleanup_cb, NULL,
2541259483Sasomers	    &fasttrap_cleanup_proc, 0, 0, "ftcleanup");
2542259483Sasomers	if (ret != 0) {
2543259483Sasomers		destroy_dev(fasttrap_cdev);
2544297077Smav#ifndef illumos
2545259483Sasomers		for (i = 0; i < fasttrap_provs.fth_nent; i++)
2546259483Sasomers			mutex_destroy(&fasttrap_provs.fth_table[i].ftb_mtx);
2547259483Sasomers		for (i = 0; i < fasttrap_tpoints.fth_nent; i++)
2548259483Sasomers			mutex_destroy(&fasttrap_tpoints.fth_table[i].ftb_mtx);
2549259483Sasomers#endif
2550259483Sasomers		kmem_free(fasttrap_provs.fth_table, fasttrap_provs.fth_nent *
2551259483Sasomers		    sizeof (fasttrap_bucket_t));
2552259483Sasomers		mtx_destroy(&fasttrap_cleanup_mtx);
2553259483Sasomers		mutex_destroy(&fasttrap_count_mtx);
2554259483Sasomers		return (ret);
2555259483Sasomers	}
2556259483Sasomers
2557259483Sasomers
2558179193Sjb	/*
2559179193Sjb	 * ... and the procs hash table.
2560179193Sjb	 */
2561179193Sjb	nent = FASTTRAP_PROCS_DEFAULT_SIZE;
2562277547Sdelphij	if (ISP2(nent))
2563179193Sjb		fasttrap_procs.fth_nent = nent;
2564179193Sjb	else
2565179193Sjb		fasttrap_procs.fth_nent = 1 << fasttrap_highbit(nent);
2566179193Sjb	ASSERT(fasttrap_procs.fth_nent > 0);
2567179193Sjb	fasttrap_procs.fth_mask = fasttrap_procs.fth_nent - 1;
2568179193Sjb	fasttrap_procs.fth_table = kmem_zalloc(fasttrap_procs.fth_nent *
2569179193Sjb	    sizeof (fasttrap_bucket_t), KM_SLEEP);
2570297077Smav#ifndef illumos
2571211738Srpaulo	for (i = 0; i < fasttrap_procs.fth_nent; i++)
2572211738Srpaulo		mutex_init(&fasttrap_procs.fth_table[i].ftb_mtx,
2573211738Srpaulo		    "processes bucket mtx", MUTEX_DEFAULT, NULL);
2574211925Srpaulo
2575299003Smarkj	rm_init(&fasttrap_tp_lock, "fasttrap tracepoint");
2576269342Smarkj
2577269342Smarkj	/*
2578269342Smarkj	 * This event handler must run before kdtrace_thread_dtor() since it
2579269342Smarkj	 * accesses the thread's struct kdtrace_thread.
2580269342Smarkj	 */
2581269342Smarkj	fasttrap_thread_dtor_tag = EVENTHANDLER_REGISTER(thread_dtor,
2582269342Smarkj	    fasttrap_thread_dtor, NULL, EVENTHANDLER_PRI_FIRST);
2583211738Srpaulo#endif
2584179193Sjb
2585253079Savg	/*
2586253079Savg	 * Install our hooks into fork(2), exec(2), and exit(2).
2587253079Savg	 */
2588253079Savg	dtrace_fasttrap_fork = &fasttrap_fork;
2589253079Savg	dtrace_fasttrap_exit = &fasttrap_exec_exit;
2590253079Savg	dtrace_fasttrap_exec = &fasttrap_exec_exit;
2591253079Savg
2592179193Sjb	(void) dtrace_meta_register("fasttrap", &fasttrap_mops, NULL,
2593179193Sjb	    &fasttrap_meta_id);
2594179193Sjb
2595211738Srpaulo	return (0);
2596179193Sjb}
2597179193Sjb
2598179193Sjbstatic int
2599211738Srpaulofasttrap_unload(void)
2600179193Sjb{
2601179193Sjb	int i, fail = 0;
2602179193Sjb
2603179193Sjb	/*
2604179193Sjb	 * Unregister the meta-provider to make sure no new fasttrap-
2605179193Sjb	 * managed providers come along while we're trying to close up
2606179193Sjb	 * shop. If we fail to detach, we'll need to re-register as a
2607179193Sjb	 * meta-provider. We can fail to unregister as a meta-provider
2608179193Sjb	 * if providers we manage still exist.
2609179193Sjb	 */
2610179193Sjb	if (fasttrap_meta_id != DTRACE_METAPROVNONE &&
2611179193Sjb	    dtrace_meta_unregister(fasttrap_meta_id) != 0)
2612211738Srpaulo		return (-1);
2613179193Sjb
2614179193Sjb	/*
2615179193Sjb	 * Iterate over all of our providers. If there's still a process
2616179193Sjb	 * that corresponds to that pid, fail to detach.
2617179193Sjb	 */
2618179193Sjb	for (i = 0; i < fasttrap_provs.fth_nent; i++) {
2619179193Sjb		fasttrap_provider_t **fpp, *fp;
2620179193Sjb		fasttrap_bucket_t *bucket = &fasttrap_provs.fth_table[i];
2621179193Sjb
2622179193Sjb		mutex_enter(&bucket->ftb_mtx);
2623179193Sjb		fpp = (fasttrap_provider_t **)&bucket->ftb_data;
2624179193Sjb		while ((fp = *fpp) != NULL) {
2625179193Sjb			/*
2626179193Sjb			 * Acquire and release the lock as a simple way of
2627179193Sjb			 * waiting for any other consumer to finish with
2628179193Sjb			 * this provider. A thread must first acquire the
2629179193Sjb			 * bucket lock so there's no chance of another thread
2630179193Sjb			 * blocking on the provider's lock.
2631179193Sjb			 */
2632179193Sjb			mutex_enter(&fp->ftp_mtx);
2633179193Sjb			mutex_exit(&fp->ftp_mtx);
2634179193Sjb
2635179193Sjb			if (dtrace_unregister(fp->ftp_provid) != 0) {
2636179193Sjb				fail = 1;
2637179193Sjb				fpp = &fp->ftp_next;
2638179193Sjb			} else {
2639179193Sjb				*fpp = fp->ftp_next;
2640179193Sjb				fasttrap_provider_free(fp);
2641179193Sjb			}
2642179193Sjb		}
2643179193Sjb
2644179193Sjb		mutex_exit(&bucket->ftb_mtx);
2645179193Sjb	}
2646179193Sjb
2647179193Sjb	if (fail) {
2648179193Sjb		(void) dtrace_meta_register("fasttrap", &fasttrap_mops, NULL,
2649179193Sjb		    &fasttrap_meta_id);
2650179193Sjb
2651211738Srpaulo		return (-1);
2652179193Sjb	}
2653179193Sjb
2654259483Sasomers	/*
2655259483Sasomers	 * Stop new processes from entering these hooks now, before the
2656259483Sasomers	 * fasttrap_cleanup thread runs.  That way all processes will hopefully
2657259483Sasomers	 * be out of these hooks before we free fasttrap_provs.fth_table
2658259483Sasomers	 */
2659259483Sasomers	ASSERT(dtrace_fasttrap_fork == &fasttrap_fork);
2660259483Sasomers	dtrace_fasttrap_fork = NULL;
2661259483Sasomers
2662259483Sasomers	ASSERT(dtrace_fasttrap_exec == &fasttrap_exec_exit);
2663259483Sasomers	dtrace_fasttrap_exec = NULL;
2664259483Sasomers
2665259483Sasomers	ASSERT(dtrace_fasttrap_exit == &fasttrap_exec_exit);
2666259483Sasomers	dtrace_fasttrap_exit = NULL;
2667259483Sasomers
2668250953Smarkj	mtx_lock(&fasttrap_cleanup_mtx);
2669250953Smarkj	fasttrap_cleanup_drain = 1;
2670250953Smarkj	/* Wait for the cleanup thread to finish up and signal us. */
2671250953Smarkj	wakeup(&fasttrap_cleanup_cv);
2672250953Smarkj	mtx_sleep(&fasttrap_cleanup_drain, &fasttrap_cleanup_mtx, 0, "ftcld",
2673250953Smarkj	    0);
2674250953Smarkj	fasttrap_cleanup_proc = NULL;
2675252493Smarkj	mtx_destroy(&fasttrap_cleanup_mtx);
2676250953Smarkj
2677179193Sjb#ifdef DEBUG
2678179193Sjb	mutex_enter(&fasttrap_count_mtx);
2679179193Sjb	ASSERT(fasttrap_pid_count == 0);
2680179193Sjb	mutex_exit(&fasttrap_count_mtx);
2681179193Sjb#endif
2682179193Sjb
2683297077Smav#ifndef illumos
2684269342Smarkj	EVENTHANDLER_DEREGISTER(thread_dtor, fasttrap_thread_dtor_tag);
2685269342Smarkj
2686259483Sasomers	for (i = 0; i < fasttrap_tpoints.fth_nent; i++)
2687259483Sasomers		mutex_destroy(&fasttrap_tpoints.fth_table[i].ftb_mtx);
2688259483Sasomers	for (i = 0; i < fasttrap_provs.fth_nent; i++)
2689259483Sasomers		mutex_destroy(&fasttrap_provs.fth_table[i].ftb_mtx);
2690259483Sasomers	for (i = 0; i < fasttrap_procs.fth_nent; i++)
2691259483Sasomers		mutex_destroy(&fasttrap_procs.fth_table[i].ftb_mtx);
2692259483Sasomers#endif
2693179193Sjb	kmem_free(fasttrap_tpoints.fth_table,
2694179193Sjb	    fasttrap_tpoints.fth_nent * sizeof (fasttrap_bucket_t));
2695179193Sjb	fasttrap_tpoints.fth_nent = 0;
2696179193Sjb
2697179193Sjb	kmem_free(fasttrap_provs.fth_table,
2698179193Sjb	    fasttrap_provs.fth_nent * sizeof (fasttrap_bucket_t));
2699179193Sjb	fasttrap_provs.fth_nent = 0;
2700179193Sjb
2701179193Sjb	kmem_free(fasttrap_procs.fth_table,
2702179193Sjb	    fasttrap_procs.fth_nent * sizeof (fasttrap_bucket_t));
2703179193Sjb	fasttrap_procs.fth_nent = 0;
2704179193Sjb
2705297077Smav#ifndef illumos
2706211738Srpaulo	destroy_dev(fasttrap_cdev);
2707211738Srpaulo	mutex_destroy(&fasttrap_count_mtx);
2708299003Smarkj	rm_destroy(&fasttrap_tp_lock);
2709211738Srpaulo#endif
2710179193Sjb
2711211738Srpaulo	return (0);
2712179193Sjb}
2713179193Sjb
2714211738Srpaulo/* ARGSUSED */
2715211738Srpaulostatic int
2716211738Srpaulofasttrap_modevent(module_t mod __unused, int type, void *data __unused)
2717211738Srpaulo{
2718211738Srpaulo	int error = 0;
2719179193Sjb
2720211738Srpaulo	switch (type) {
2721211738Srpaulo	case MOD_LOAD:
2722211738Srpaulo		break;
2723179193Sjb
2724211738Srpaulo	case MOD_UNLOAD:
2725211738Srpaulo		break;
2726179193Sjb
2727211738Srpaulo	case MOD_SHUTDOWN:
2728211738Srpaulo		break;
2729179193Sjb
2730211738Srpaulo	default:
2731211738Srpaulo		error = EOPNOTSUPP;
2732211738Srpaulo		break;
2733211738Srpaulo	}
2734211738Srpaulo	return (error);
2735179193Sjb}
2736179193Sjb
2737211738SrpauloSYSINIT(fasttrap_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, fasttrap_load,
2738211738Srpaulo    NULL);
2739211738SrpauloSYSUNINIT(fasttrap_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY,
2740211738Srpaulo    fasttrap_unload, NULL);
2741211738Srpaulo
2742211738SrpauloDEV_MODULE(fasttrap, fasttrap_modevent, NULL);
2743211738SrpauloMODULE_VERSION(fasttrap, 1);
2744211738SrpauloMODULE_DEPEND(fasttrap, dtrace, 1, 1, 1);
2745211738SrpauloMODULE_DEPEND(fasttrap, opensolaris, 1, 1, 1);
2746