xref: /titanic_54/usr/src/uts/intel/dtrace/fbt.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/dtrace.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/conf.h>
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #define	FBT_PUSHL_EBP		0x55
38*7c478bd9Sstevel@tonic-gate #define	FBT_MOVL_ESP_EBP0_V0	0x8b
39*7c478bd9Sstevel@tonic-gate #define	FBT_MOVL_ESP_EBP1_V0	0xec
40*7c478bd9Sstevel@tonic-gate #define	FBT_MOVL_ESP_EBP0_V1	0x89
41*7c478bd9Sstevel@tonic-gate #define	FBT_MOVL_ESP_EBP1_V1	0xe5
42*7c478bd9Sstevel@tonic-gate #define	FBT_REX_RSP_RBP		0x48
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate #define	FBT_POPL_EBP		0x5d
45*7c478bd9Sstevel@tonic-gate #define	FBT_RET			0xc3
46*7c478bd9Sstevel@tonic-gate #define	FBT_RET_IMM16		0xc2
47*7c478bd9Sstevel@tonic-gate #define	FBT_LEAVE		0xc9
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate #ifdef __amd64
50*7c478bd9Sstevel@tonic-gate #define	FBT_PATCHVAL		0xcc
51*7c478bd9Sstevel@tonic-gate #else
52*7c478bd9Sstevel@tonic-gate #define	FBT_PATCHVAL		0xf0
53*7c478bd9Sstevel@tonic-gate #endif
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate #define	FBT_ENTRY	"entry"
56*7c478bd9Sstevel@tonic-gate #define	FBT_RETURN	"return"
57*7c478bd9Sstevel@tonic-gate #define	FBT_ADDR2NDX(addr)	((((uintptr_t)(addr)) >> 4) & fbt_probetab_mask)
58*7c478bd9Sstevel@tonic-gate #define	FBT_PROBETAB_SIZE	0x8000		/* 32k entries -- 128K total */
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate typedef struct fbt_probe {
61*7c478bd9Sstevel@tonic-gate 	struct fbt_probe *fbtp_hashnext;
62*7c478bd9Sstevel@tonic-gate 	uint8_t		*fbtp_patchpoint;
63*7c478bd9Sstevel@tonic-gate 	int8_t		fbtp_rval;
64*7c478bd9Sstevel@tonic-gate 	uint8_t		fbtp_patchval;
65*7c478bd9Sstevel@tonic-gate 	uint8_t		fbtp_savedval;
66*7c478bd9Sstevel@tonic-gate 	uintptr_t	fbtp_roffset;
67*7c478bd9Sstevel@tonic-gate 	dtrace_id_t	fbtp_id;
68*7c478bd9Sstevel@tonic-gate 	char		*fbtp_name;
69*7c478bd9Sstevel@tonic-gate 	struct modctl	*fbtp_ctl;
70*7c478bd9Sstevel@tonic-gate 	int		fbtp_loadcnt;
71*7c478bd9Sstevel@tonic-gate 	int		fbtp_symndx;
72*7c478bd9Sstevel@tonic-gate 	int		fbtp_primary;
73*7c478bd9Sstevel@tonic-gate 	struct fbt_probe *fbtp_next;
74*7c478bd9Sstevel@tonic-gate } fbt_probe_t;
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate static dev_info_t		*fbt_devi;
77*7c478bd9Sstevel@tonic-gate static dtrace_provider_id_t	fbt_id;
78*7c478bd9Sstevel@tonic-gate static fbt_probe_t		**fbt_probetab;
79*7c478bd9Sstevel@tonic-gate static int			fbt_probetab_size;
80*7c478bd9Sstevel@tonic-gate static int			fbt_probetab_mask;
81*7c478bd9Sstevel@tonic-gate static int			fbt_verbose = 0;
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate static int
84*7c478bd9Sstevel@tonic-gate fbt_invop(uintptr_t addr, uintptr_t *stack, uintptr_t rval)
85*7c478bd9Sstevel@tonic-gate {
86*7c478bd9Sstevel@tonic-gate 	uintptr_t stack0, stack1, stack2, stack3, stack4;
87*7c478bd9Sstevel@tonic-gate 	fbt_probe_t *fbt = fbt_probetab[FBT_ADDR2NDX(addr)];
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	for (; fbt != NULL; fbt = fbt->fbtp_hashnext) {
90*7c478bd9Sstevel@tonic-gate 		if ((uintptr_t)fbt->fbtp_patchpoint == addr) {
91*7c478bd9Sstevel@tonic-gate 			if (fbt->fbtp_roffset == 0) {
92*7c478bd9Sstevel@tonic-gate 				int i = 0;
93*7c478bd9Sstevel@tonic-gate 				/*
94*7c478bd9Sstevel@tonic-gate 				 * When accessing the arguments on the stack,
95*7c478bd9Sstevel@tonic-gate 				 * we must protect against accessing beyond
96*7c478bd9Sstevel@tonic-gate 				 * the stack.  We can safely set NOFAULT here
97*7c478bd9Sstevel@tonic-gate 				 * -- we know that interrupts are already
98*7c478bd9Sstevel@tonic-gate 				 * disabled.
99*7c478bd9Sstevel@tonic-gate 				 */
100*7c478bd9Sstevel@tonic-gate 				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
101*7c478bd9Sstevel@tonic-gate 				CPU->cpu_dtrace_caller = stack[i++];
102*7c478bd9Sstevel@tonic-gate #ifdef __amd64
103*7c478bd9Sstevel@tonic-gate 				/*
104*7c478bd9Sstevel@tonic-gate 				 * On amd64, stack[0] contains the dereferenced
105*7c478bd9Sstevel@tonic-gate 				 * stack pointer, stack[1] contains savfp,
106*7c478bd9Sstevel@tonic-gate 				 * stack[2] contains savpc.  We want to step
107*7c478bd9Sstevel@tonic-gate 				 * over these entries.
108*7c478bd9Sstevel@tonic-gate 				 */
109*7c478bd9Sstevel@tonic-gate 				i += 2;
110*7c478bd9Sstevel@tonic-gate #endif
111*7c478bd9Sstevel@tonic-gate 				stack0 = stack[i++];
112*7c478bd9Sstevel@tonic-gate 				stack1 = stack[i++];
113*7c478bd9Sstevel@tonic-gate 				stack2 = stack[i++];
114*7c478bd9Sstevel@tonic-gate 				stack3 = stack[i++];
115*7c478bd9Sstevel@tonic-gate 				stack4 = stack[i++];
116*7c478bd9Sstevel@tonic-gate 				DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT |
117*7c478bd9Sstevel@tonic-gate 				    CPU_DTRACE_BADADDR);
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 				dtrace_probe(fbt->fbtp_id, stack0, stack1,
120*7c478bd9Sstevel@tonic-gate 				    stack2, stack3, stack4);
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate 				CPU->cpu_dtrace_caller = NULL;
123*7c478bd9Sstevel@tonic-gate 			} else {
124*7c478bd9Sstevel@tonic-gate #ifdef __amd64
125*7c478bd9Sstevel@tonic-gate 				/*
126*7c478bd9Sstevel@tonic-gate 				 * On amd64, we instrument the ret, not the
127*7c478bd9Sstevel@tonic-gate 				 * leave.  We therefore need to set the caller
128*7c478bd9Sstevel@tonic-gate 				 * to assure that the top frame of a stack()
129*7c478bd9Sstevel@tonic-gate 				 * action is correct.
130*7c478bd9Sstevel@tonic-gate 				 */
131*7c478bd9Sstevel@tonic-gate 				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
132*7c478bd9Sstevel@tonic-gate 				CPU->cpu_dtrace_caller = stack[0];
133*7c478bd9Sstevel@tonic-gate 				DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT |
134*7c478bd9Sstevel@tonic-gate 				    CPU_DTRACE_BADADDR);
135*7c478bd9Sstevel@tonic-gate #endif
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 				dtrace_probe(fbt->fbtp_id, fbt->fbtp_roffset,
138*7c478bd9Sstevel@tonic-gate 				    rval, 0, 0, 0);
139*7c478bd9Sstevel@tonic-gate 				CPU->cpu_dtrace_caller = NULL;
140*7c478bd9Sstevel@tonic-gate 			}
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 			return (fbt->fbtp_rval);
143*7c478bd9Sstevel@tonic-gate 		}
144*7c478bd9Sstevel@tonic-gate 	}
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate 	return (0);
147*7c478bd9Sstevel@tonic-gate }
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
150*7c478bd9Sstevel@tonic-gate static void
151*7c478bd9Sstevel@tonic-gate fbt_provide_module(void *arg, struct modctl *ctl)
152*7c478bd9Sstevel@tonic-gate {
153*7c478bd9Sstevel@tonic-gate 	struct module *mp = ctl->mod_mp;
154*7c478bd9Sstevel@tonic-gate 	char *str = mp->strings;
155*7c478bd9Sstevel@tonic-gate 	int nsyms = mp->nsyms;
156*7c478bd9Sstevel@tonic-gate 	Shdr *symhdr = mp->symhdr;
157*7c478bd9Sstevel@tonic-gate 	char *modname = ctl->mod_modname;
158*7c478bd9Sstevel@tonic-gate 	char *name;
159*7c478bd9Sstevel@tonic-gate 	fbt_probe_t *fbt, *retfbt;
160*7c478bd9Sstevel@tonic-gate 	size_t symsize;
161*7c478bd9Sstevel@tonic-gate 	int i, size;
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 	/*
164*7c478bd9Sstevel@tonic-gate 	 * Employees of dtrace and their families are ineligible.  Void
165*7c478bd9Sstevel@tonic-gate 	 * where prohibited.
166*7c478bd9Sstevel@tonic-gate 	 */
167*7c478bd9Sstevel@tonic-gate 	if (strcmp(modname, "dtrace") == 0)
168*7c478bd9Sstevel@tonic-gate 		return;
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 	if (ctl->mod_requisites != NULL) {
171*7c478bd9Sstevel@tonic-gate 		struct modctl_list *list;
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 		list = (struct modctl_list *)ctl->mod_requisites;
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate 		for (; list != NULL; list = list->modl_next) {
176*7c478bd9Sstevel@tonic-gate 			if (strcmp(list->modl_modp->mod_modname, "dtrace") == 0)
177*7c478bd9Sstevel@tonic-gate 				return;
178*7c478bd9Sstevel@tonic-gate 		}
179*7c478bd9Sstevel@tonic-gate 	}
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 	/*
182*7c478bd9Sstevel@tonic-gate 	 * KMDB is ineligible for instrumentation -- it may execute in
183*7c478bd9Sstevel@tonic-gate 	 * any context, including probe context.
184*7c478bd9Sstevel@tonic-gate 	 */
185*7c478bd9Sstevel@tonic-gate 	if (strcmp(modname, "kmdbmod") == 0)
186*7c478bd9Sstevel@tonic-gate 		return;
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 	if (str == NULL || symhdr == NULL || symhdr->sh_addr == NULL) {
189*7c478bd9Sstevel@tonic-gate 		/*
190*7c478bd9Sstevel@tonic-gate 		 * If this module doesn't (yet) have its string or symbol
191*7c478bd9Sstevel@tonic-gate 		 * table allocated, clear out.
192*7c478bd9Sstevel@tonic-gate 		 */
193*7c478bd9Sstevel@tonic-gate 		return;
194*7c478bd9Sstevel@tonic-gate 	}
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate 	symsize = symhdr->sh_entsize;
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate 	if (mp->fbt_nentries) {
199*7c478bd9Sstevel@tonic-gate 		/*
200*7c478bd9Sstevel@tonic-gate 		 * This module has some FBT entries allocated; we're afraid
201*7c478bd9Sstevel@tonic-gate 		 * to screw with it.
202*7c478bd9Sstevel@tonic-gate 		 */
203*7c478bd9Sstevel@tonic-gate 		return;
204*7c478bd9Sstevel@tonic-gate 	}
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	for (i = 1; i < nsyms; i++) {
207*7c478bd9Sstevel@tonic-gate 		uint8_t *instr, *limit;
208*7c478bd9Sstevel@tonic-gate 		Sym *sym = (Sym *)(symhdr->sh_addr + i * symsize);
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate 		if (ELF_ST_TYPE(sym->st_info) != STT_FUNC)
211*7c478bd9Sstevel@tonic-gate 			continue;
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate 		/*
214*7c478bd9Sstevel@tonic-gate 		 * Weak symbols are not candidates.  This could be made to
215*7c478bd9Sstevel@tonic-gate 		 * work (where weak functions and their underlying function
216*7c478bd9Sstevel@tonic-gate 		 * appear as two disjoint probes), but it's not simple.
217*7c478bd9Sstevel@tonic-gate 		 */
218*7c478bd9Sstevel@tonic-gate 		if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
219*7c478bd9Sstevel@tonic-gate 			continue;
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate 		name = str + sym->st_name;
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate 		if (strstr(name, "dtrace_") == name &&
224*7c478bd9Sstevel@tonic-gate 		    strstr(name, "dtrace_safe_") != name) {
225*7c478bd9Sstevel@tonic-gate 			/*
226*7c478bd9Sstevel@tonic-gate 			 * Anything beginning with "dtrace_" may be called
227*7c478bd9Sstevel@tonic-gate 			 * from probe context unless it explitly indicates
228*7c478bd9Sstevel@tonic-gate 			 * that it won't be called from probe context by
229*7c478bd9Sstevel@tonic-gate 			 * using the prefix "dtrace_safe_".
230*7c478bd9Sstevel@tonic-gate 			 */
231*7c478bd9Sstevel@tonic-gate 			continue;
232*7c478bd9Sstevel@tonic-gate 		}
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate 		if (strstr(name, "kdi_") == name) {
235*7c478bd9Sstevel@tonic-gate 			/*
236*7c478bd9Sstevel@tonic-gate 			 * Anything beginning with "kdi_" is a part of the
237*7c478bd9Sstevel@tonic-gate 			 * kernel debugger interface and may be called in
238*7c478bd9Sstevel@tonic-gate 			 * arbitrary context -- including probe context.
239*7c478bd9Sstevel@tonic-gate 			 */
240*7c478bd9Sstevel@tonic-gate 			continue;
241*7c478bd9Sstevel@tonic-gate 		}
242*7c478bd9Sstevel@tonic-gate 
243*7c478bd9Sstevel@tonic-gate 		/*
244*7c478bd9Sstevel@tonic-gate 		 * Due to 4524008, _init and _fini may have a bloated st_size.
245*7c478bd9Sstevel@tonic-gate 		 * While this bug was fixed quite some time ago, old drivers
246*7c478bd9Sstevel@tonic-gate 		 * may be lurking.  We need to develop a better solution to
247*7c478bd9Sstevel@tonic-gate 		 * this problem, such that correct _init and _fini functions
248*7c478bd9Sstevel@tonic-gate 		 * (the vast majority) may be correctly traced.  One solution
249*7c478bd9Sstevel@tonic-gate 		 * may be to scan through the entire symbol table to see if
250*7c478bd9Sstevel@tonic-gate 		 * any symbol overlaps with _init.  If none does, set a bit in
251*7c478bd9Sstevel@tonic-gate 		 * the module structure that this module has correct _init and
252*7c478bd9Sstevel@tonic-gate 		 * _fini sizes.  This will cause some pain the first time a
253*7c478bd9Sstevel@tonic-gate 		 * module is scanned, but at least it would be O(N) instead of
254*7c478bd9Sstevel@tonic-gate 		 * O(N log N)...
255*7c478bd9Sstevel@tonic-gate 		 */
256*7c478bd9Sstevel@tonic-gate 		if (strcmp(name, "_init") == 0)
257*7c478bd9Sstevel@tonic-gate 			continue;
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate 		if (strcmp(name, "_fini") == 0)
260*7c478bd9Sstevel@tonic-gate 			continue;
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate 		/*
263*7c478bd9Sstevel@tonic-gate 		 * In order to be eligible, the function must begin with the
264*7c478bd9Sstevel@tonic-gate 		 * following sequence:
265*7c478bd9Sstevel@tonic-gate 		 *
266*7c478bd9Sstevel@tonic-gate 		 * 	pushl	%esp
267*7c478bd9Sstevel@tonic-gate 		 *	movl	%esp, %ebp
268*7c478bd9Sstevel@tonic-gate 		 *
269*7c478bd9Sstevel@tonic-gate 		 * Note that there are two variants of encodings that generate
270*7c478bd9Sstevel@tonic-gate 		 * the movl; we must check for both.  For 64-bit, we would
271*7c478bd9Sstevel@tonic-gate 		 * normally insist that a function begin with the following
272*7c478bd9Sstevel@tonic-gate 		 * sequence:
273*7c478bd9Sstevel@tonic-gate 		 *
274*7c478bd9Sstevel@tonic-gate 		 *	pushq	%rbp
275*7c478bd9Sstevel@tonic-gate 		 *	movq	%rsp, %rbp
276*7c478bd9Sstevel@tonic-gate 		 *
277*7c478bd9Sstevel@tonic-gate 		 * However, the compiler for 64-bit often splits these two
278*7c478bd9Sstevel@tonic-gate 		 * instructions -- and the first instruction in the function
279*7c478bd9Sstevel@tonic-gate 		 * is often not the pushq.  As a result, on 64-bit we look
280*7c478bd9Sstevel@tonic-gate 		 * for any "pushq %rbp" in the function and we instrument
281*7c478bd9Sstevel@tonic-gate 		 * this with a breakpoint instruction.
282*7c478bd9Sstevel@tonic-gate 		 */
283*7c478bd9Sstevel@tonic-gate 		instr = (uint8_t *)sym->st_value;
284*7c478bd9Sstevel@tonic-gate 		limit = (uint8_t *)(sym->st_value + sym->st_size);
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate #ifdef __amd64
287*7c478bd9Sstevel@tonic-gate 		while (instr < limit) {
288*7c478bd9Sstevel@tonic-gate 			if (*instr == FBT_PUSHL_EBP)
289*7c478bd9Sstevel@tonic-gate 				break;
290*7c478bd9Sstevel@tonic-gate 
291*7c478bd9Sstevel@tonic-gate 			if ((size = dtrace_instr_size(instr)) <= 0)
292*7c478bd9Sstevel@tonic-gate 				break;
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate 			instr += size;
295*7c478bd9Sstevel@tonic-gate 		}
296*7c478bd9Sstevel@tonic-gate 
297*7c478bd9Sstevel@tonic-gate 		if (instr >= limit || *instr != FBT_PUSHL_EBP) {
298*7c478bd9Sstevel@tonic-gate 			/*
299*7c478bd9Sstevel@tonic-gate 			 * We either don't save the frame pointer in this
300*7c478bd9Sstevel@tonic-gate 			 * function, or we ran into some disassembly
301*7c478bd9Sstevel@tonic-gate 			 * screw-up.  Either way, we bail.
302*7c478bd9Sstevel@tonic-gate 			 */
303*7c478bd9Sstevel@tonic-gate 			continue;
304*7c478bd9Sstevel@tonic-gate 		}
305*7c478bd9Sstevel@tonic-gate #else
306*7c478bd9Sstevel@tonic-gate 		if (instr[0] != FBT_PUSHL_EBP)
307*7c478bd9Sstevel@tonic-gate 			continue;
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate 		if (!(instr[1] == FBT_MOVL_ESP_EBP0_V0 &&
310*7c478bd9Sstevel@tonic-gate 		    instr[2] == FBT_MOVL_ESP_EBP1_V0) &&
311*7c478bd9Sstevel@tonic-gate 		    !(instr[1] == FBT_MOVL_ESP_EBP0_V1 &&
312*7c478bd9Sstevel@tonic-gate 		    instr[2] == FBT_MOVL_ESP_EBP1_V1))
313*7c478bd9Sstevel@tonic-gate 			continue;
314*7c478bd9Sstevel@tonic-gate #endif
315*7c478bd9Sstevel@tonic-gate 
316*7c478bd9Sstevel@tonic-gate 		fbt = kmem_zalloc(sizeof (fbt_probe_t), KM_SLEEP);
317*7c478bd9Sstevel@tonic-gate 		fbt->fbtp_name = name;
318*7c478bd9Sstevel@tonic-gate 		fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
319*7c478bd9Sstevel@tonic-gate 		    name, FBT_ENTRY, 3, fbt);
320*7c478bd9Sstevel@tonic-gate 		fbt->fbtp_patchpoint = instr;
321*7c478bd9Sstevel@tonic-gate 		fbt->fbtp_ctl = ctl;
322*7c478bd9Sstevel@tonic-gate 		fbt->fbtp_loadcnt = ctl->mod_loadcnt;
323*7c478bd9Sstevel@tonic-gate 		fbt->fbtp_rval = DTRACE_INVOP_PUSHL_EBP;
324*7c478bd9Sstevel@tonic-gate 		fbt->fbtp_savedval = *instr;
325*7c478bd9Sstevel@tonic-gate 		fbt->fbtp_patchval = FBT_PATCHVAL;
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate 		fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
328*7c478bd9Sstevel@tonic-gate 		fbt->fbtp_symndx = i;
329*7c478bd9Sstevel@tonic-gate 		fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
330*7c478bd9Sstevel@tonic-gate 
331*7c478bd9Sstevel@tonic-gate 		mp->fbt_nentries++;
332*7c478bd9Sstevel@tonic-gate 
333*7c478bd9Sstevel@tonic-gate 		retfbt = NULL;
334*7c478bd9Sstevel@tonic-gate again:
335*7c478bd9Sstevel@tonic-gate 		if (instr >= limit)
336*7c478bd9Sstevel@tonic-gate 			continue;
337*7c478bd9Sstevel@tonic-gate 
338*7c478bd9Sstevel@tonic-gate 		/*
339*7c478bd9Sstevel@tonic-gate 		 * If this disassembly fails, then we've likely walked off into
340*7c478bd9Sstevel@tonic-gate 		 * a jump table or some other unsuitable area.  Bail out of the
341*7c478bd9Sstevel@tonic-gate 		 * disassembly now.
342*7c478bd9Sstevel@tonic-gate 		 */
343*7c478bd9Sstevel@tonic-gate 		if ((size = dtrace_instr_size(instr)) <= 0)
344*7c478bd9Sstevel@tonic-gate 			continue;
345*7c478bd9Sstevel@tonic-gate 
346*7c478bd9Sstevel@tonic-gate #ifdef __amd64
347*7c478bd9Sstevel@tonic-gate 		/*
348*7c478bd9Sstevel@tonic-gate 		 * We only instrument "ret" on amd64 -- we don't yet instrument
349*7c478bd9Sstevel@tonic-gate 		 * ret imm16, largely because the compiler doesn't seem to
350*7c478bd9Sstevel@tonic-gate 		 * (yet) emit them in the kernel...
351*7c478bd9Sstevel@tonic-gate 		 */
352*7c478bd9Sstevel@tonic-gate 		if (*instr != FBT_RET) {
353*7c478bd9Sstevel@tonic-gate 			instr += size;
354*7c478bd9Sstevel@tonic-gate 			goto again;
355*7c478bd9Sstevel@tonic-gate 		}
356*7c478bd9Sstevel@tonic-gate #else
357*7c478bd9Sstevel@tonic-gate 		if (!(size == 1 &&
358*7c478bd9Sstevel@tonic-gate 		    (*instr == FBT_POPL_EBP || *instr == FBT_LEAVE) &&
359*7c478bd9Sstevel@tonic-gate 		    (*(instr + 1) == FBT_RET ||
360*7c478bd9Sstevel@tonic-gate 		    *(instr + 1) == FBT_RET_IMM16))) {
361*7c478bd9Sstevel@tonic-gate 			instr += size;
362*7c478bd9Sstevel@tonic-gate 			goto again;
363*7c478bd9Sstevel@tonic-gate 		}
364*7c478bd9Sstevel@tonic-gate #endif
365*7c478bd9Sstevel@tonic-gate 
366*7c478bd9Sstevel@tonic-gate 		/*
367*7c478bd9Sstevel@tonic-gate 		 * We have a winner!
368*7c478bd9Sstevel@tonic-gate 		 */
369*7c478bd9Sstevel@tonic-gate 		fbt = kmem_zalloc(sizeof (fbt_probe_t), KM_SLEEP);
370*7c478bd9Sstevel@tonic-gate 		fbt->fbtp_name = name;
371*7c478bd9Sstevel@tonic-gate 
372*7c478bd9Sstevel@tonic-gate 		if (retfbt == NULL) {
373*7c478bd9Sstevel@tonic-gate 			fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
374*7c478bd9Sstevel@tonic-gate 			    name, FBT_RETURN, 3, fbt);
375*7c478bd9Sstevel@tonic-gate 		} else {
376*7c478bd9Sstevel@tonic-gate 			retfbt->fbtp_next = fbt;
377*7c478bd9Sstevel@tonic-gate 			fbt->fbtp_id = retfbt->fbtp_id;
378*7c478bd9Sstevel@tonic-gate 		}
379*7c478bd9Sstevel@tonic-gate 
380*7c478bd9Sstevel@tonic-gate 		retfbt = fbt;
381*7c478bd9Sstevel@tonic-gate 		fbt->fbtp_patchpoint = instr;
382*7c478bd9Sstevel@tonic-gate 		fbt->fbtp_ctl = ctl;
383*7c478bd9Sstevel@tonic-gate 		fbt->fbtp_loadcnt = ctl->mod_loadcnt;
384*7c478bd9Sstevel@tonic-gate 
385*7c478bd9Sstevel@tonic-gate #ifndef __amd64
386*7c478bd9Sstevel@tonic-gate 		if (*instr == FBT_POPL_EBP) {
387*7c478bd9Sstevel@tonic-gate 			fbt->fbtp_rval = DTRACE_INVOP_POPL_EBP;
388*7c478bd9Sstevel@tonic-gate 		} else {
389*7c478bd9Sstevel@tonic-gate 			ASSERT(*instr == FBT_LEAVE);
390*7c478bd9Sstevel@tonic-gate 			fbt->fbtp_rval = DTRACE_INVOP_LEAVE;
391*7c478bd9Sstevel@tonic-gate 		}
392*7c478bd9Sstevel@tonic-gate 		fbt->fbtp_roffset =
393*7c478bd9Sstevel@tonic-gate 		    (uintptr_t)(instr - (uint8_t *)sym->st_value) + 1;
394*7c478bd9Sstevel@tonic-gate 
395*7c478bd9Sstevel@tonic-gate #else
396*7c478bd9Sstevel@tonic-gate 		ASSERT(*instr == FBT_RET);
397*7c478bd9Sstevel@tonic-gate 		fbt->fbtp_rval = DTRACE_INVOP_RET;
398*7c478bd9Sstevel@tonic-gate 		fbt->fbtp_roffset =
399*7c478bd9Sstevel@tonic-gate 		    (uintptr_t)(instr - (uint8_t *)sym->st_value);
400*7c478bd9Sstevel@tonic-gate #endif
401*7c478bd9Sstevel@tonic-gate 
402*7c478bd9Sstevel@tonic-gate 		fbt->fbtp_savedval = *instr;
403*7c478bd9Sstevel@tonic-gate 		fbt->fbtp_patchval = FBT_PATCHVAL;
404*7c478bd9Sstevel@tonic-gate 		fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
405*7c478bd9Sstevel@tonic-gate 		fbt->fbtp_symndx = i;
406*7c478bd9Sstevel@tonic-gate 		fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
407*7c478bd9Sstevel@tonic-gate 
408*7c478bd9Sstevel@tonic-gate 		mp->fbt_nentries++;
409*7c478bd9Sstevel@tonic-gate 
410*7c478bd9Sstevel@tonic-gate 		instr += size;
411*7c478bd9Sstevel@tonic-gate 		goto again;
412*7c478bd9Sstevel@tonic-gate 	}
413*7c478bd9Sstevel@tonic-gate }
414*7c478bd9Sstevel@tonic-gate 
415*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
416*7c478bd9Sstevel@tonic-gate static void
417*7c478bd9Sstevel@tonic-gate fbt_destroy(void *arg, dtrace_id_t id, void *parg)
418*7c478bd9Sstevel@tonic-gate {
419*7c478bd9Sstevel@tonic-gate 	fbt_probe_t *fbt = parg, *next, *hash, *last;
420*7c478bd9Sstevel@tonic-gate 	struct modctl *ctl = fbt->fbtp_ctl;
421*7c478bd9Sstevel@tonic-gate 	int ndx;
422*7c478bd9Sstevel@tonic-gate 
423*7c478bd9Sstevel@tonic-gate 	do {
424*7c478bd9Sstevel@tonic-gate 		if (ctl != NULL && ctl->mod_loadcnt == fbt->fbtp_loadcnt) {
425*7c478bd9Sstevel@tonic-gate 			if ((ctl->mod_loadcnt == fbt->fbtp_loadcnt &&
426*7c478bd9Sstevel@tonic-gate 			    ctl->mod_loaded)) {
427*7c478bd9Sstevel@tonic-gate 				((struct module *)
428*7c478bd9Sstevel@tonic-gate 				    (ctl->mod_mp))->fbt_nentries--;
429*7c478bd9Sstevel@tonic-gate 			}
430*7c478bd9Sstevel@tonic-gate 		}
431*7c478bd9Sstevel@tonic-gate 
432*7c478bd9Sstevel@tonic-gate 		/*
433*7c478bd9Sstevel@tonic-gate 		 * Now we need to remove this probe from the fbt_probetab.
434*7c478bd9Sstevel@tonic-gate 		 */
435*7c478bd9Sstevel@tonic-gate 		ndx = FBT_ADDR2NDX(fbt->fbtp_patchpoint);
436*7c478bd9Sstevel@tonic-gate 		last = NULL;
437*7c478bd9Sstevel@tonic-gate 		hash = fbt_probetab[ndx];
438*7c478bd9Sstevel@tonic-gate 
439*7c478bd9Sstevel@tonic-gate 		while (hash != fbt) {
440*7c478bd9Sstevel@tonic-gate 			ASSERT(hash != NULL);
441*7c478bd9Sstevel@tonic-gate 			last = hash;
442*7c478bd9Sstevel@tonic-gate 			hash = hash->fbtp_hashnext;
443*7c478bd9Sstevel@tonic-gate 		}
444*7c478bd9Sstevel@tonic-gate 
445*7c478bd9Sstevel@tonic-gate 		if (last != NULL) {
446*7c478bd9Sstevel@tonic-gate 			last->fbtp_hashnext = fbt->fbtp_hashnext;
447*7c478bd9Sstevel@tonic-gate 		} else {
448*7c478bd9Sstevel@tonic-gate 			fbt_probetab[ndx] = fbt->fbtp_hashnext;
449*7c478bd9Sstevel@tonic-gate 		}
450*7c478bd9Sstevel@tonic-gate 
451*7c478bd9Sstevel@tonic-gate 		next = fbt->fbtp_next;
452*7c478bd9Sstevel@tonic-gate 		kmem_free(fbt, sizeof (fbt_probe_t));
453*7c478bd9Sstevel@tonic-gate 
454*7c478bd9Sstevel@tonic-gate 		fbt = next;
455*7c478bd9Sstevel@tonic-gate 	} while (fbt != NULL);
456*7c478bd9Sstevel@tonic-gate }
457*7c478bd9Sstevel@tonic-gate 
458*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
459*7c478bd9Sstevel@tonic-gate static void
460*7c478bd9Sstevel@tonic-gate fbt_enable(void *arg, dtrace_id_t id, void *parg)
461*7c478bd9Sstevel@tonic-gate {
462*7c478bd9Sstevel@tonic-gate 	fbt_probe_t *fbt = parg;
463*7c478bd9Sstevel@tonic-gate 	struct modctl *ctl = fbt->fbtp_ctl;
464*7c478bd9Sstevel@tonic-gate 
465*7c478bd9Sstevel@tonic-gate 	ctl->mod_nenabled++;
466*7c478bd9Sstevel@tonic-gate 
467*7c478bd9Sstevel@tonic-gate 	if (!ctl->mod_loaded) {
468*7c478bd9Sstevel@tonic-gate 		if (fbt_verbose) {
469*7c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "fbt is failing for probe %s "
470*7c478bd9Sstevel@tonic-gate 			    "(module %s unloaded)",
471*7c478bd9Sstevel@tonic-gate 			    fbt->fbtp_name, ctl->mod_modname);
472*7c478bd9Sstevel@tonic-gate 		}
473*7c478bd9Sstevel@tonic-gate 
474*7c478bd9Sstevel@tonic-gate 		return;
475*7c478bd9Sstevel@tonic-gate 	}
476*7c478bd9Sstevel@tonic-gate 
477*7c478bd9Sstevel@tonic-gate 	/*
478*7c478bd9Sstevel@tonic-gate 	 * Now check that our modctl has the expected load count.  If it
479*7c478bd9Sstevel@tonic-gate 	 * doesn't, this module must have been unloaded and reloaded -- and
480*7c478bd9Sstevel@tonic-gate 	 * we're not going to touch it.
481*7c478bd9Sstevel@tonic-gate 	 */
482*7c478bd9Sstevel@tonic-gate 	if (ctl->mod_loadcnt != fbt->fbtp_loadcnt) {
483*7c478bd9Sstevel@tonic-gate 		if (fbt_verbose) {
484*7c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "fbt is failing for probe %s "
485*7c478bd9Sstevel@tonic-gate 			    "(module %s reloaded)",
486*7c478bd9Sstevel@tonic-gate 			    fbt->fbtp_name, ctl->mod_modname);
487*7c478bd9Sstevel@tonic-gate 		}
488*7c478bd9Sstevel@tonic-gate 
489*7c478bd9Sstevel@tonic-gate 		return;
490*7c478bd9Sstevel@tonic-gate 	}
491*7c478bd9Sstevel@tonic-gate 
492*7c478bd9Sstevel@tonic-gate 	for (; fbt != NULL; fbt = fbt->fbtp_next)
493*7c478bd9Sstevel@tonic-gate 		*fbt->fbtp_patchpoint = fbt->fbtp_patchval;
494*7c478bd9Sstevel@tonic-gate }
495*7c478bd9Sstevel@tonic-gate 
496*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
497*7c478bd9Sstevel@tonic-gate static void
498*7c478bd9Sstevel@tonic-gate fbt_disable(void *arg, dtrace_id_t id, void *parg)
499*7c478bd9Sstevel@tonic-gate {
500*7c478bd9Sstevel@tonic-gate 	fbt_probe_t *fbt = parg;
501*7c478bd9Sstevel@tonic-gate 	struct modctl *ctl = fbt->fbtp_ctl;
502*7c478bd9Sstevel@tonic-gate 
503*7c478bd9Sstevel@tonic-gate 	ASSERT(ctl->mod_nenabled > 0);
504*7c478bd9Sstevel@tonic-gate 	ctl->mod_nenabled--;
505*7c478bd9Sstevel@tonic-gate 
506*7c478bd9Sstevel@tonic-gate 	if (!ctl->mod_loaded || (ctl->mod_loadcnt != fbt->fbtp_loadcnt))
507*7c478bd9Sstevel@tonic-gate 		return;
508*7c478bd9Sstevel@tonic-gate 
509*7c478bd9Sstevel@tonic-gate 	for (; fbt != NULL; fbt = fbt->fbtp_next)
510*7c478bd9Sstevel@tonic-gate 		*fbt->fbtp_patchpoint = fbt->fbtp_savedval;
511*7c478bd9Sstevel@tonic-gate }
512*7c478bd9Sstevel@tonic-gate 
513*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
514*7c478bd9Sstevel@tonic-gate static void
515*7c478bd9Sstevel@tonic-gate fbt_suspend(void *arg, dtrace_id_t id, void *parg)
516*7c478bd9Sstevel@tonic-gate {
517*7c478bd9Sstevel@tonic-gate 	fbt_probe_t *fbt = parg;
518*7c478bd9Sstevel@tonic-gate 	struct modctl *ctl = fbt->fbtp_ctl;
519*7c478bd9Sstevel@tonic-gate 
520*7c478bd9Sstevel@tonic-gate 	ASSERT(ctl->mod_nenabled > 0);
521*7c478bd9Sstevel@tonic-gate 
522*7c478bd9Sstevel@tonic-gate 	if (!ctl->mod_loaded || (ctl->mod_loadcnt != fbt->fbtp_loadcnt))
523*7c478bd9Sstevel@tonic-gate 		return;
524*7c478bd9Sstevel@tonic-gate 
525*7c478bd9Sstevel@tonic-gate 	for (; fbt != NULL; fbt = fbt->fbtp_next)
526*7c478bd9Sstevel@tonic-gate 		*fbt->fbtp_patchpoint = fbt->fbtp_savedval;
527*7c478bd9Sstevel@tonic-gate }
528*7c478bd9Sstevel@tonic-gate 
529*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
530*7c478bd9Sstevel@tonic-gate static void
531*7c478bd9Sstevel@tonic-gate fbt_resume(void *arg, dtrace_id_t id, void *parg)
532*7c478bd9Sstevel@tonic-gate {
533*7c478bd9Sstevel@tonic-gate 	fbt_probe_t *fbt = parg;
534*7c478bd9Sstevel@tonic-gate 	struct modctl *ctl = fbt->fbtp_ctl;
535*7c478bd9Sstevel@tonic-gate 
536*7c478bd9Sstevel@tonic-gate 	ASSERT(ctl->mod_nenabled > 0);
537*7c478bd9Sstevel@tonic-gate 
538*7c478bd9Sstevel@tonic-gate 	if (!ctl->mod_loaded || (ctl->mod_loadcnt != fbt->fbtp_loadcnt))
539*7c478bd9Sstevel@tonic-gate 		return;
540*7c478bd9Sstevel@tonic-gate 
541*7c478bd9Sstevel@tonic-gate 	for (; fbt != NULL; fbt = fbt->fbtp_next)
542*7c478bd9Sstevel@tonic-gate 		*fbt->fbtp_patchpoint = fbt->fbtp_patchval;
543*7c478bd9Sstevel@tonic-gate }
544*7c478bd9Sstevel@tonic-gate 
545*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
546*7c478bd9Sstevel@tonic-gate static void
547*7c478bd9Sstevel@tonic-gate fbt_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc)
548*7c478bd9Sstevel@tonic-gate {
549*7c478bd9Sstevel@tonic-gate 	fbt_probe_t *fbt = parg;
550*7c478bd9Sstevel@tonic-gate 	struct modctl *ctl = fbt->fbtp_ctl;
551*7c478bd9Sstevel@tonic-gate 	struct module *mp = ctl->mod_mp;
552*7c478bd9Sstevel@tonic-gate 	ctf_file_t *fp = NULL, *pfp;
553*7c478bd9Sstevel@tonic-gate 	ctf_funcinfo_t f;
554*7c478bd9Sstevel@tonic-gate 	int error;
555*7c478bd9Sstevel@tonic-gate 	ctf_id_t argv[32], type;
556*7c478bd9Sstevel@tonic-gate 	int argc = sizeof (argv) / sizeof (ctf_id_t);
557*7c478bd9Sstevel@tonic-gate 	const char *parent;
558*7c478bd9Sstevel@tonic-gate 
559*7c478bd9Sstevel@tonic-gate 	if (!ctl->mod_loaded || (ctl->mod_loadcnt != fbt->fbtp_loadcnt))
560*7c478bd9Sstevel@tonic-gate 		goto err;
561*7c478bd9Sstevel@tonic-gate 
562*7c478bd9Sstevel@tonic-gate 	if (fbt->fbtp_roffset != 0 && desc->dtargd_ndx == 0) {
563*7c478bd9Sstevel@tonic-gate 		(void) strcpy(desc->dtargd_native, "int");
564*7c478bd9Sstevel@tonic-gate 		return;
565*7c478bd9Sstevel@tonic-gate 	}
566*7c478bd9Sstevel@tonic-gate 
567*7c478bd9Sstevel@tonic-gate 	if ((fp = ctf_modopen(mp, &error)) == NULL) {
568*7c478bd9Sstevel@tonic-gate 		/*
569*7c478bd9Sstevel@tonic-gate 		 * We have no CTF information for this module -- and therefore
570*7c478bd9Sstevel@tonic-gate 		 * no args[] information.
571*7c478bd9Sstevel@tonic-gate 		 */
572*7c478bd9Sstevel@tonic-gate 		goto err;
573*7c478bd9Sstevel@tonic-gate 	}
574*7c478bd9Sstevel@tonic-gate 
575*7c478bd9Sstevel@tonic-gate 	/*
576*7c478bd9Sstevel@tonic-gate 	 * If we have a parent container, we must manually import it.
577*7c478bd9Sstevel@tonic-gate 	 */
578*7c478bd9Sstevel@tonic-gate 	if ((parent = ctf_parent_name(fp)) != NULL) {
579*7c478bd9Sstevel@tonic-gate 		struct modctl *mod;
580*7c478bd9Sstevel@tonic-gate 
581*7c478bd9Sstevel@tonic-gate 		/*
582*7c478bd9Sstevel@tonic-gate 		 * We must iterate over all modules to find the module that
583*7c478bd9Sstevel@tonic-gate 		 * is our parent.
584*7c478bd9Sstevel@tonic-gate 		 */
585*7c478bd9Sstevel@tonic-gate 		for (mod = &modules; mod != NULL; mod = mod->mod_next) {
586*7c478bd9Sstevel@tonic-gate 			if (strcmp(mod->mod_filename, parent) == 0)
587*7c478bd9Sstevel@tonic-gate 				break;
588*7c478bd9Sstevel@tonic-gate 		}
589*7c478bd9Sstevel@tonic-gate 
590*7c478bd9Sstevel@tonic-gate 		if (mod == NULL)
591*7c478bd9Sstevel@tonic-gate 			goto err;
592*7c478bd9Sstevel@tonic-gate 
593*7c478bd9Sstevel@tonic-gate 		if ((pfp = ctf_modopen(mod->mod_mp, &error)) == NULL)
594*7c478bd9Sstevel@tonic-gate 			goto err;
595*7c478bd9Sstevel@tonic-gate 
596*7c478bd9Sstevel@tonic-gate 		if (ctf_import(fp, pfp) != 0) {
597*7c478bd9Sstevel@tonic-gate 			ctf_close(pfp);
598*7c478bd9Sstevel@tonic-gate 			goto err;
599*7c478bd9Sstevel@tonic-gate 		}
600*7c478bd9Sstevel@tonic-gate 
601*7c478bd9Sstevel@tonic-gate 		ctf_close(pfp);
602*7c478bd9Sstevel@tonic-gate 	}
603*7c478bd9Sstevel@tonic-gate 
604*7c478bd9Sstevel@tonic-gate 	if (ctf_func_info(fp, fbt->fbtp_symndx, &f) == CTF_ERR)
605*7c478bd9Sstevel@tonic-gate 		goto err;
606*7c478bd9Sstevel@tonic-gate 
607*7c478bd9Sstevel@tonic-gate 	if (fbt->fbtp_roffset != 0) {
608*7c478bd9Sstevel@tonic-gate 		if (desc->dtargd_ndx > 1)
609*7c478bd9Sstevel@tonic-gate 			goto err;
610*7c478bd9Sstevel@tonic-gate 
611*7c478bd9Sstevel@tonic-gate 		ASSERT(desc->dtargd_ndx == 1);
612*7c478bd9Sstevel@tonic-gate 		type = f.ctc_return;
613*7c478bd9Sstevel@tonic-gate 	} else {
614*7c478bd9Sstevel@tonic-gate 		if (desc->dtargd_ndx + 1 > f.ctc_argc)
615*7c478bd9Sstevel@tonic-gate 			goto err;
616*7c478bd9Sstevel@tonic-gate 
617*7c478bd9Sstevel@tonic-gate 		if (ctf_func_args(fp, fbt->fbtp_symndx, argc, argv) == CTF_ERR)
618*7c478bd9Sstevel@tonic-gate 			goto err;
619*7c478bd9Sstevel@tonic-gate 
620*7c478bd9Sstevel@tonic-gate 		type = argv[desc->dtargd_ndx];
621*7c478bd9Sstevel@tonic-gate 	}
622*7c478bd9Sstevel@tonic-gate 
623*7c478bd9Sstevel@tonic-gate 	if (ctf_type_name(fp, type, desc->dtargd_native,
624*7c478bd9Sstevel@tonic-gate 	    DTRACE_ARGTYPELEN) != NULL) {
625*7c478bd9Sstevel@tonic-gate 		ctf_close(fp);
626*7c478bd9Sstevel@tonic-gate 		return;
627*7c478bd9Sstevel@tonic-gate 	}
628*7c478bd9Sstevel@tonic-gate err:
629*7c478bd9Sstevel@tonic-gate 	if (fp != NULL)
630*7c478bd9Sstevel@tonic-gate 		ctf_close(fp);
631*7c478bd9Sstevel@tonic-gate 
632*7c478bd9Sstevel@tonic-gate 	desc->dtargd_ndx = DTRACE_ARGNONE;
633*7c478bd9Sstevel@tonic-gate }
634*7c478bd9Sstevel@tonic-gate 
635*7c478bd9Sstevel@tonic-gate static dtrace_pattr_t fbt_attr = {
636*7c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
637*7c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
638*7c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
639*7c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
640*7c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
641*7c478bd9Sstevel@tonic-gate };
642*7c478bd9Sstevel@tonic-gate 
643*7c478bd9Sstevel@tonic-gate static dtrace_pops_t fbt_pops = {
644*7c478bd9Sstevel@tonic-gate 	NULL,
645*7c478bd9Sstevel@tonic-gate 	fbt_provide_module,
646*7c478bd9Sstevel@tonic-gate 	fbt_enable,
647*7c478bd9Sstevel@tonic-gate 	fbt_disable,
648*7c478bd9Sstevel@tonic-gate 	fbt_suspend,
649*7c478bd9Sstevel@tonic-gate 	fbt_resume,
650*7c478bd9Sstevel@tonic-gate 	fbt_getargdesc,
651*7c478bd9Sstevel@tonic-gate 	NULL,
652*7c478bd9Sstevel@tonic-gate 	NULL,
653*7c478bd9Sstevel@tonic-gate 	fbt_destroy
654*7c478bd9Sstevel@tonic-gate };
655*7c478bd9Sstevel@tonic-gate 
656*7c478bd9Sstevel@tonic-gate static void
657*7c478bd9Sstevel@tonic-gate fbt_cleanup(dev_info_t *devi)
658*7c478bd9Sstevel@tonic-gate {
659*7c478bd9Sstevel@tonic-gate 	dtrace_invop_remove(fbt_invop);
660*7c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
661*7c478bd9Sstevel@tonic-gate 	kmem_free(fbt_probetab, fbt_probetab_size * sizeof (fbt_probe_t *));
662*7c478bd9Sstevel@tonic-gate 	fbt_probetab = NULL;
663*7c478bd9Sstevel@tonic-gate 	fbt_probetab_mask = 0;
664*7c478bd9Sstevel@tonic-gate }
665*7c478bd9Sstevel@tonic-gate 
666*7c478bd9Sstevel@tonic-gate static int
667*7c478bd9Sstevel@tonic-gate fbt_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
668*7c478bd9Sstevel@tonic-gate {
669*7c478bd9Sstevel@tonic-gate 	switch (cmd) {
670*7c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
671*7c478bd9Sstevel@tonic-gate 		break;
672*7c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
673*7c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
674*7c478bd9Sstevel@tonic-gate 	default:
675*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
676*7c478bd9Sstevel@tonic-gate 	}
677*7c478bd9Sstevel@tonic-gate 
678*7c478bd9Sstevel@tonic-gate 	if (fbt_probetab_size == 0)
679*7c478bd9Sstevel@tonic-gate 		fbt_probetab_size = FBT_PROBETAB_SIZE;
680*7c478bd9Sstevel@tonic-gate 
681*7c478bd9Sstevel@tonic-gate 	fbt_probetab_mask = fbt_probetab_size - 1;
682*7c478bd9Sstevel@tonic-gate 	fbt_probetab =
683*7c478bd9Sstevel@tonic-gate 	    kmem_zalloc(fbt_probetab_size * sizeof (fbt_probe_t *), KM_SLEEP);
684*7c478bd9Sstevel@tonic-gate 
685*7c478bd9Sstevel@tonic-gate 	dtrace_invop_add(fbt_invop);
686*7c478bd9Sstevel@tonic-gate 
687*7c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "fbt", S_IFCHR, 0,
688*7c478bd9Sstevel@tonic-gate 	    DDI_PSEUDO, NULL) == DDI_FAILURE ||
689*7c478bd9Sstevel@tonic-gate 	    dtrace_register("fbt", &fbt_attr, DTRACE_PRIV_KERNEL, 0,
690*7c478bd9Sstevel@tonic-gate 	    &fbt_pops, NULL, &fbt_id) != 0) {
691*7c478bd9Sstevel@tonic-gate 		fbt_cleanup(devi);
692*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
693*7c478bd9Sstevel@tonic-gate 	}
694*7c478bd9Sstevel@tonic-gate 
695*7c478bd9Sstevel@tonic-gate 	ddi_report_dev(devi);
696*7c478bd9Sstevel@tonic-gate 	fbt_devi = devi;
697*7c478bd9Sstevel@tonic-gate 
698*7c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
699*7c478bd9Sstevel@tonic-gate }
700*7c478bd9Sstevel@tonic-gate 
701*7c478bd9Sstevel@tonic-gate static int
702*7c478bd9Sstevel@tonic-gate fbt_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
703*7c478bd9Sstevel@tonic-gate {
704*7c478bd9Sstevel@tonic-gate 	switch (cmd) {
705*7c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
706*7c478bd9Sstevel@tonic-gate 		break;
707*7c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
708*7c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
709*7c478bd9Sstevel@tonic-gate 	default:
710*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
711*7c478bd9Sstevel@tonic-gate 	}
712*7c478bd9Sstevel@tonic-gate 
713*7c478bd9Sstevel@tonic-gate 	if (dtrace_unregister(fbt_id) != 0)
714*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
715*7c478bd9Sstevel@tonic-gate 
716*7c478bd9Sstevel@tonic-gate 	fbt_cleanup(devi);
717*7c478bd9Sstevel@tonic-gate 
718*7c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
719*7c478bd9Sstevel@tonic-gate }
720*7c478bd9Sstevel@tonic-gate 
721*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
722*7c478bd9Sstevel@tonic-gate static int
723*7c478bd9Sstevel@tonic-gate fbt_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
724*7c478bd9Sstevel@tonic-gate {
725*7c478bd9Sstevel@tonic-gate 	int error;
726*7c478bd9Sstevel@tonic-gate 
727*7c478bd9Sstevel@tonic-gate 	switch (infocmd) {
728*7c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
729*7c478bd9Sstevel@tonic-gate 		*result = (void *)fbt_devi;
730*7c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
731*7c478bd9Sstevel@tonic-gate 		break;
732*7c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
733*7c478bd9Sstevel@tonic-gate 		*result = (void *)0;
734*7c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
735*7c478bd9Sstevel@tonic-gate 		break;
736*7c478bd9Sstevel@tonic-gate 	default:
737*7c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
738*7c478bd9Sstevel@tonic-gate 	}
739*7c478bd9Sstevel@tonic-gate 	return (error);
740*7c478bd9Sstevel@tonic-gate }
741*7c478bd9Sstevel@tonic-gate 
742*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
743*7c478bd9Sstevel@tonic-gate static int
744*7c478bd9Sstevel@tonic-gate fbt_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
745*7c478bd9Sstevel@tonic-gate {
746*7c478bd9Sstevel@tonic-gate 	return (0);
747*7c478bd9Sstevel@tonic-gate }
748*7c478bd9Sstevel@tonic-gate 
749*7c478bd9Sstevel@tonic-gate static struct cb_ops fbt_cb_ops = {
750*7c478bd9Sstevel@tonic-gate 	fbt_open,		/* open */
751*7c478bd9Sstevel@tonic-gate 	nodev,			/* close */
752*7c478bd9Sstevel@tonic-gate 	nulldev,		/* strategy */
753*7c478bd9Sstevel@tonic-gate 	nulldev,		/* print */
754*7c478bd9Sstevel@tonic-gate 	nodev,			/* dump */
755*7c478bd9Sstevel@tonic-gate 	nodev,			/* read */
756*7c478bd9Sstevel@tonic-gate 	nodev,			/* write */
757*7c478bd9Sstevel@tonic-gate 	nodev,			/* ioctl */
758*7c478bd9Sstevel@tonic-gate 	nodev,			/* devmap */
759*7c478bd9Sstevel@tonic-gate 	nodev,			/* mmap */
760*7c478bd9Sstevel@tonic-gate 	nodev,			/* segmap */
761*7c478bd9Sstevel@tonic-gate 	nochpoll,		/* poll */
762*7c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
763*7c478bd9Sstevel@tonic-gate 	0,			/* streamtab  */
764*7c478bd9Sstevel@tonic-gate 	D_NEW | D_MP		/* Driver compatibility flag */
765*7c478bd9Sstevel@tonic-gate };
766*7c478bd9Sstevel@tonic-gate 
767*7c478bd9Sstevel@tonic-gate static struct dev_ops fbt_ops = {
768*7c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
769*7c478bd9Sstevel@tonic-gate 	0,			/* refcnt */
770*7c478bd9Sstevel@tonic-gate 	fbt_info,		/* get_dev_info */
771*7c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
772*7c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
773*7c478bd9Sstevel@tonic-gate 	fbt_attach,		/* attach */
774*7c478bd9Sstevel@tonic-gate 	fbt_detach,		/* detach */
775*7c478bd9Sstevel@tonic-gate 	nodev,			/* reset */
776*7c478bd9Sstevel@tonic-gate 	&fbt_cb_ops,		/* driver operations */
777*7c478bd9Sstevel@tonic-gate 	NULL,			/* bus operations */
778*7c478bd9Sstevel@tonic-gate 	nodev			/* dev power */
779*7c478bd9Sstevel@tonic-gate };
780*7c478bd9Sstevel@tonic-gate 
781*7c478bd9Sstevel@tonic-gate /*
782*7c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
783*7c478bd9Sstevel@tonic-gate  */
784*7c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
785*7c478bd9Sstevel@tonic-gate 	&mod_driverops,		/* module type (this is a pseudo driver) */
786*7c478bd9Sstevel@tonic-gate 	"Function Boundary Tracing",	/* name of module */
787*7c478bd9Sstevel@tonic-gate 	&fbt_ops,		/* driver ops */
788*7c478bd9Sstevel@tonic-gate };
789*7c478bd9Sstevel@tonic-gate 
790*7c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
791*7c478bd9Sstevel@tonic-gate 	MODREV_1,
792*7c478bd9Sstevel@tonic-gate 	(void *)&modldrv,
793*7c478bd9Sstevel@tonic-gate 	NULL
794*7c478bd9Sstevel@tonic-gate };
795*7c478bd9Sstevel@tonic-gate 
796*7c478bd9Sstevel@tonic-gate int
797*7c478bd9Sstevel@tonic-gate _init(void)
798*7c478bd9Sstevel@tonic-gate {
799*7c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
800*7c478bd9Sstevel@tonic-gate }
801*7c478bd9Sstevel@tonic-gate 
802*7c478bd9Sstevel@tonic-gate int
803*7c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
804*7c478bd9Sstevel@tonic-gate {
805*7c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
806*7c478bd9Sstevel@tonic-gate }
807*7c478bd9Sstevel@tonic-gate 
808*7c478bd9Sstevel@tonic-gate int
809*7c478bd9Sstevel@tonic-gate _fini(void)
810*7c478bd9Sstevel@tonic-gate {
811*7c478bd9Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
812*7c478bd9Sstevel@tonic-gate }
813