16e19f4deSRui Paulo /*
26e19f4deSRui Paulo  * CDDL HEADER START
36e19f4deSRui Paulo  *
46e19f4deSRui Paulo  * The contents of this file are subject to the terms of the
56e19f4deSRui Paulo  * Common Development and Distribution License (the "License").
66e19f4deSRui Paulo  * You may not use this file except in compliance with the License.
76e19f4deSRui Paulo  *
86e19f4deSRui Paulo  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
96e19f4deSRui Paulo  * or http://www.opensolaris.org/os/licensing.
106e19f4deSRui Paulo  * See the License for the specific language governing permissions
116e19f4deSRui Paulo  * and limitations under the License.
126e19f4deSRui Paulo  *
136e19f4deSRui Paulo  * When distributing Covered Code, include this CDDL HEADER in each
146e19f4deSRui Paulo  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
156e19f4deSRui Paulo  * If applicable, add the following below this CDDL HEADER, with the
166e19f4deSRui Paulo  * fields enclosed by brackets "[]" replaced with your own identifying
176e19f4deSRui Paulo  * information: Portions Copyright [yyyy] [name of copyright owner]
186e19f4deSRui Paulo  *
196e19f4deSRui Paulo  * CDDL HEADER END
206e19f4deSRui Paulo  */
216e19f4deSRui Paulo 
226e19f4deSRui Paulo /*
236e19f4deSRui Paulo  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
246e19f4deSRui Paulo  * Use is subject to license terms.
256e19f4deSRui Paulo  */
266e19f4deSRui Paulo 
276e19f4deSRui Paulo #ifndef	_FASTTRAP_IMPL_H
286e19f4deSRui Paulo #define	_FASTTRAP_IMPL_H
296e19f4deSRui Paulo 
306e19f4deSRui Paulo #pragma ident	"%Z%%M%	%I%	%E% SMI"
316e19f4deSRui Paulo 
326e19f4deSRui Paulo #include <sys/types.h>
336e19f4deSRui Paulo #include <sys/dtrace.h>
346e19f4deSRui Paulo #include <sys/proc.h>
350626f3e4SMark Johnston #include <sys/queue.h>
366e19f4deSRui Paulo #include <sys/fasttrap.h>
376e19f4deSRui Paulo #include <sys/fasttrap_isa.h>
386e19f4deSRui Paulo 
396e19f4deSRui Paulo #ifdef	__cplusplus
406e19f4deSRui Paulo extern "C" {
416e19f4deSRui Paulo #endif
426e19f4deSRui Paulo 
436e19f4deSRui Paulo /*
446e19f4deSRui Paulo  * Fasttrap Providers, Probes and Tracepoints
456e19f4deSRui Paulo  *
466e19f4deSRui Paulo  * Each Solaris process can have multiple providers -- the pid provider as
476e19f4deSRui Paulo  * well as any number of user-level statically defined tracing (USDT)
486e19f4deSRui Paulo  * providers. Those providers are each represented by a fasttrap_provider_t.
496e19f4deSRui Paulo  * All providers for a given process have a pointer to a shared
506e19f4deSRui Paulo  * fasttrap_proc_t. The fasttrap_proc_t has two states: active or defunct.
516e19f4deSRui Paulo  * When the count of active providers goes to zero it becomes defunct; a
526e19f4deSRui Paulo  * provider drops its active count when it is removed individually or as part
536e19f4deSRui Paulo  * of a mass removal when a process exits or performs an exec.
546e19f4deSRui Paulo  *
556e19f4deSRui Paulo  * Each probe is represented by a fasttrap_probe_t which has a pointer to
566e19f4deSRui Paulo  * its associated provider as well as a list of fasttrap_id_tp_t structures
576e19f4deSRui Paulo  * which are tuples combining a fasttrap_id_t and a fasttrap_tracepoint_t.
586e19f4deSRui Paulo  * A fasttrap_tracepoint_t represents the actual point of instrumentation
596e19f4deSRui Paulo  * and it contains two lists of fasttrap_id_t structures (to be fired pre-
606e19f4deSRui Paulo  * and post-instruction emulation) that identify the probes attached to the
616e19f4deSRui Paulo  * tracepoint. Tracepoints also have a pointer to the fasttrap_proc_t for the
626e19f4deSRui Paulo  * process they trace which is used when looking up a tracepoint both when a
636e19f4deSRui Paulo  * probe fires and when enabling and disabling probes.
646e19f4deSRui Paulo  *
656e19f4deSRui Paulo  * It's important to note that probes are preallocated with the necessary
666e19f4deSRui Paulo  * number of tracepoints, but that tracepoints can be shared by probes and
676e19f4deSRui Paulo  * swapped between probes. If a probe's preallocated tracepoint is enabled
686e19f4deSRui Paulo  * (and, therefore, the associated probe is enabled), and that probe is
696e19f4deSRui Paulo  * then disabled, ownership of that tracepoint may be exchanged for an
706e19f4deSRui Paulo  * unused tracepoint belonging to another probe that was attached to the
716e19f4deSRui Paulo  * enabled tracepoint.
720626f3e4SMark Johnston  *
730626f3e4SMark Johnston  * On FreeBSD, fasttrap providers also maintain per-thread scratch space for use
740626f3e4SMark Johnston  * by the ISA-specific fasttrap code. The fasttrap_scrblock_t type stores the
750626f3e4SMark Johnston  * virtual address of a page-sized memory block that is mapped into a process'
760626f3e4SMark Johnston  * address space. Each block is carved up into chunks (fasttrap_scrspace_t) for
770626f3e4SMark Johnston  * use by individual threads, which keep the address of their scratch space
780626f3e4SMark Johnston  * chunk in their struct kdtrace_thread. A thread's scratch space isn't released
790626f3e4SMark Johnston  * until it exits.
806e19f4deSRui Paulo  */
816e19f4deSRui Paulo 
82bc96366cSSteven Hartland #ifndef illumos
830626f3e4SMark Johnston typedef struct fasttrap_scrblock {
840626f3e4SMark Johnston 	vm_offset_t ftsb_addr;			/* address of a scratch block */
850626f3e4SMark Johnston 	LIST_ENTRY(fasttrap_scrblock) ftsb_next;/* next block in list */
860626f3e4SMark Johnston } fasttrap_scrblock_t;
870626f3e4SMark Johnston #define	FASTTRAP_SCRBLOCK_SIZE	PAGE_SIZE
880626f3e4SMark Johnston 
890626f3e4SMark Johnston typedef struct fasttrap_scrspace {
900626f3e4SMark Johnston 	uintptr_t ftss_addr;			/* scratch space address */
910626f3e4SMark Johnston 	LIST_ENTRY(fasttrap_scrspace) ftss_next;/* next in list */
920626f3e4SMark Johnston } fasttrap_scrspace_t;
930626f3e4SMark Johnston #define	FASTTRAP_SCRSPACE_SIZE	64
940626f3e4SMark Johnston #endif
950626f3e4SMark Johnston 
966e19f4deSRui Paulo typedef struct fasttrap_proc {
976e19f4deSRui Paulo 	pid_t ftpc_pid;				/* process ID for this proc */
986e19f4deSRui Paulo 	uint64_t ftpc_acount;			/* count of active providers */
996e19f4deSRui Paulo 	uint64_t ftpc_rcount;			/* count of extant providers */
1006e19f4deSRui Paulo 	kmutex_t ftpc_mtx;			/* lock on all but acount */
1016e19f4deSRui Paulo 	struct fasttrap_proc *ftpc_next;	/* next proc in hash chain */
102bc96366cSSteven Hartland #ifndef illumos
1030626f3e4SMark Johnston 	LIST_HEAD(, fasttrap_scrblock) ftpc_scrblks; /* mapped scratch blocks */
1040626f3e4SMark Johnston 	LIST_HEAD(, fasttrap_scrspace) ftpc_fscr; /* free scratch space */
1050626f3e4SMark Johnston 	LIST_HEAD(, fasttrap_scrspace) ftpc_ascr; /* used scratch space */
1060626f3e4SMark Johnston #endif
1076e19f4deSRui Paulo } fasttrap_proc_t;
1086e19f4deSRui Paulo 
1096e19f4deSRui Paulo typedef struct fasttrap_provider {
1106e19f4deSRui Paulo 	pid_t ftp_pid;				/* process ID for this prov */
1116e19f4deSRui Paulo 	char ftp_name[DTRACE_PROVNAMELEN];	/* prov name (w/o the pid) */
1126e19f4deSRui Paulo 	dtrace_provider_id_t ftp_provid;	/* DTrace provider handle */
1136e19f4deSRui Paulo 	uint_t ftp_marked;			/* mark for possible removal */
1146e19f4deSRui Paulo 	uint_t ftp_retired;			/* mark when retired */
1156e19f4deSRui Paulo 	kmutex_t ftp_mtx;			/* provider lock */
1166e19f4deSRui Paulo 	kmutex_t ftp_cmtx;			/* lock on creating probes */
1176e19f4deSRui Paulo 	uint64_t ftp_rcount;			/* enabled probes ref count */
1186e19f4deSRui Paulo 	uint64_t ftp_ccount;			/* consumers creating probes */
1196e19f4deSRui Paulo 	uint64_t ftp_mcount;			/* meta provider count */
1206e19f4deSRui Paulo 	fasttrap_proc_t *ftp_proc;		/* shared proc for all provs */
1216e19f4deSRui Paulo 	struct fasttrap_provider *ftp_next;	/* next prov in hash chain */
1226e19f4deSRui Paulo } fasttrap_provider_t;
1236e19f4deSRui Paulo 
1246e19f4deSRui Paulo typedef struct fasttrap_id fasttrap_id_t;
1256e19f4deSRui Paulo typedef struct fasttrap_probe fasttrap_probe_t;
1266e19f4deSRui Paulo typedef struct fasttrap_tracepoint fasttrap_tracepoint_t;
1276e19f4deSRui Paulo 
1286e19f4deSRui Paulo struct fasttrap_id {
1296e19f4deSRui Paulo 	fasttrap_probe_t *fti_probe;		/* referrring probe */
1306e19f4deSRui Paulo 	fasttrap_id_t *fti_next;		/* enabled probe list on tp */
1316e19f4deSRui Paulo 	fasttrap_probe_type_t fti_ptype;	/* probe type */
1326e19f4deSRui Paulo };
1336e19f4deSRui Paulo 
1346e19f4deSRui Paulo typedef struct fasttrap_id_tp {
1356e19f4deSRui Paulo 	fasttrap_id_t fit_id;
1366e19f4deSRui Paulo 	fasttrap_tracepoint_t *fit_tp;
1376e19f4deSRui Paulo } fasttrap_id_tp_t;
1386e19f4deSRui Paulo 
1396e19f4deSRui Paulo struct fasttrap_probe {
1406e19f4deSRui Paulo 	dtrace_id_t ftp_id;			/* DTrace probe identifier */
1416e19f4deSRui Paulo 	pid_t ftp_pid;				/* pid for this probe */
1426e19f4deSRui Paulo 	fasttrap_provider_t *ftp_prov;		/* this probe's provider */
1436e19f4deSRui Paulo 	uintptr_t ftp_faddr;			/* associated function's addr */
1446e19f4deSRui Paulo 	size_t ftp_fsize;			/* associated function's size */
1456e19f4deSRui Paulo 	uint64_t ftp_gen;			/* modification generation */
1466e19f4deSRui Paulo 	uint64_t ftp_ntps;			/* number of tracepoints */
1476e19f4deSRui Paulo 	uint8_t *ftp_argmap;			/* native to translated args */
1486e19f4deSRui Paulo 	uint8_t ftp_nargs;			/* translated argument count */
1496e19f4deSRui Paulo 	uint8_t ftp_enabled;			/* is this probe enabled */
1506e19f4deSRui Paulo 	char *ftp_xtypes;			/* translated types index */
1516e19f4deSRui Paulo 	char *ftp_ntypes;			/* native types index */
1526e19f4deSRui Paulo 	fasttrap_id_tp_t ftp_tps[1];		/* flexible array */
1536e19f4deSRui Paulo };
1546e19f4deSRui Paulo 
1556e19f4deSRui Paulo #define	FASTTRAP_ID_INDEX(id)	\
1566e19f4deSRui Paulo ((fasttrap_id_tp_t *)(((char *)(id) - offsetof(fasttrap_id_tp_t, fit_id))) - \
1576e19f4deSRui Paulo &(id)->fti_probe->ftp_tps[0])
1586e19f4deSRui Paulo 
1596e19f4deSRui Paulo struct fasttrap_tracepoint {
1606e19f4deSRui Paulo 	fasttrap_proc_t *ftt_proc;		/* associated process struct */
1616e19f4deSRui Paulo 	uintptr_t ftt_pc;			/* address of tracepoint */
1626e19f4deSRui Paulo 	pid_t ftt_pid;				/* pid of tracepoint */
1636e19f4deSRui Paulo 	fasttrap_machtp_t ftt_mtp;		/* ISA-specific portion */
1646e19f4deSRui Paulo 	fasttrap_id_t *ftt_ids;			/* NULL-terminated list */
1656e19f4deSRui Paulo 	fasttrap_id_t *ftt_retids;		/* NULL-terminated list */
1666e19f4deSRui Paulo 	fasttrap_tracepoint_t *ftt_next;	/* link in global hash */
1676e19f4deSRui Paulo };
1686e19f4deSRui Paulo 
1696e19f4deSRui Paulo typedef struct fasttrap_bucket {
1706e19f4deSRui Paulo 	kmutex_t ftb_mtx;			/* bucket lock */
1716e19f4deSRui Paulo 	void *ftb_data;				/* data payload */
1726e19f4deSRui Paulo 
1736e19f4deSRui Paulo 	uint8_t ftb_pad[64 - sizeof (kmutex_t) - sizeof (void *)];
1746e19f4deSRui Paulo } fasttrap_bucket_t;
1756e19f4deSRui Paulo 
1766e19f4deSRui Paulo typedef struct fasttrap_hash {
1776e19f4deSRui Paulo 	ulong_t fth_nent;			/* power-of-2 num. of entries */
1786e19f4deSRui Paulo 	ulong_t fth_mask;			/* fth_nent - 1 */
1796e19f4deSRui Paulo 	fasttrap_bucket_t *fth_table;		/* array of buckets */
1806e19f4deSRui Paulo } fasttrap_hash_t;
1816e19f4deSRui Paulo 
1826e19f4deSRui Paulo /*
1836e19f4deSRui Paulo  * If at some future point these assembly functions become observable by
1846e19f4deSRui Paulo  * DTrace, then these defines should become separate functions so that the
1856e19f4deSRui Paulo  * fasttrap provider doesn't trigger probes during internal operations.
1866e19f4deSRui Paulo  */
1876e19f4deSRui Paulo #define	fasttrap_copyout	copyout
1886e19f4deSRui Paulo #define	fasttrap_fuword32	fuword32
189a4cbcb12SMark Johnston #define	fasttrap_suword32	suword32
190a4cbcb12SMark Johnston #define	fasttrap_suword64	suword64
1916e19f4deSRui Paulo 
1924e41f353SRui Paulo #ifdef __amd64__
1934e41f353SRui Paulo #define	fasttrap_fulword	fuword64
194a4cbcb12SMark Johnston #define	fasttrap_sulword	suword64
1954e41f353SRui Paulo #else
1964e41f353SRui Paulo #define	fasttrap_fulword	fuword32
197a4cbcb12SMark Johnston #define	fasttrap_sulword	suword32
1984e41f353SRui Paulo #endif
1996e19f4deSRui Paulo 
2006e19f4deSRui Paulo extern void fasttrap_sigtrap(proc_t *, kthread_t *, uintptr_t);
201bc96366cSSteven Hartland #ifndef illumos
2020626f3e4SMark Johnston extern fasttrap_scrspace_t *fasttrap_scraddr(struct thread *,
2030626f3e4SMark Johnston     fasttrap_proc_t *);
2040626f3e4SMark Johnston #endif
2056e19f4deSRui Paulo 
2066e19f4deSRui Paulo extern dtrace_id_t 		fasttrap_probe_id;
2076e19f4deSRui Paulo extern fasttrap_hash_t		fasttrap_tpoints;
2086e19f4deSRui Paulo 
209380344a7SMark Johnston #ifndef illumos
210380344a7SMark Johnston extern struct rmlock		fasttrap_tp_lock;
211380344a7SMark Johnston #endif
212380344a7SMark Johnston 
2136e19f4deSRui Paulo #define	FASTTRAP_TPOINTS_INDEX(pid, pc) \
2146e19f4deSRui Paulo 	(((pc) / sizeof (fasttrap_instr_t) + (pid)) & fasttrap_tpoints.fth_mask)
2156e19f4deSRui Paulo 
2166e19f4deSRui Paulo /*
2176e19f4deSRui Paulo  * Must be implemented by fasttrap_isa.c
2186e19f4deSRui Paulo  */
2196e19f4deSRui Paulo extern int fasttrap_tracepoint_init(proc_t *, fasttrap_tracepoint_t *,
2206e19f4deSRui Paulo     uintptr_t, fasttrap_probe_type_t);
2216e19f4deSRui Paulo extern int fasttrap_tracepoint_install(proc_t *, fasttrap_tracepoint_t *);
2226e19f4deSRui Paulo extern int fasttrap_tracepoint_remove(proc_t *, fasttrap_tracepoint_t *);
2236e19f4deSRui Paulo 
2245bab6234SMark Johnston struct trapframe;
2255bab6234SMark Johnston extern int fasttrap_pid_probe(struct trapframe *);
2265bab6234SMark Johnston extern int fasttrap_return_probe(struct trapframe *);
2276e19f4deSRui Paulo 
2286e19f4deSRui Paulo extern uint64_t fasttrap_pid_getarg(void *, dtrace_id_t, void *, int, int);
2296e19f4deSRui Paulo extern uint64_t fasttrap_usdt_getarg(void *, dtrace_id_t, void *, int, int);
2306e19f4deSRui Paulo 
2316e19f4deSRui Paulo #ifdef	__cplusplus
2326e19f4deSRui Paulo }
2336e19f4deSRui Paulo #endif
2346e19f4deSRui Paulo 
2356e19f4deSRui Paulo #endif	/* _FASTTRAP_IMPL_H */
236