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