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