xref: /onnv-gate/usr/src/uts/intel/dtrace/fbt.c (revision 8803:8c01b39012c9)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51677Sdp  * Common Development and Distribution License (the "License").
61677Sdp  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*8803SJonathan.Haslam@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <sys/modctl.h>
280Sstevel@tonic-gate #include <sys/dtrace.h>
290Sstevel@tonic-gate #include <sys/kobj.h>
300Sstevel@tonic-gate #include <sys/stat.h>
310Sstevel@tonic-gate #include <sys/ddi.h>
320Sstevel@tonic-gate #include <sys/sunddi.h>
330Sstevel@tonic-gate #include <sys/conf.h>
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #define	FBT_PUSHL_EBP		0x55
360Sstevel@tonic-gate #define	FBT_MOVL_ESP_EBP0_V0	0x8b
370Sstevel@tonic-gate #define	FBT_MOVL_ESP_EBP1_V0	0xec
380Sstevel@tonic-gate #define	FBT_MOVL_ESP_EBP0_V1	0x89
390Sstevel@tonic-gate #define	FBT_MOVL_ESP_EBP1_V1	0xe5
400Sstevel@tonic-gate #define	FBT_REX_RSP_RBP		0x48
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #define	FBT_POPL_EBP		0x5d
430Sstevel@tonic-gate #define	FBT_RET			0xc3
440Sstevel@tonic-gate #define	FBT_RET_IMM16		0xc2
450Sstevel@tonic-gate #define	FBT_LEAVE		0xc9
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #ifdef __amd64
480Sstevel@tonic-gate #define	FBT_PATCHVAL		0xcc
490Sstevel@tonic-gate #else
500Sstevel@tonic-gate #define	FBT_PATCHVAL		0xf0
510Sstevel@tonic-gate #endif
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #define	FBT_ENTRY	"entry"
540Sstevel@tonic-gate #define	FBT_RETURN	"return"
550Sstevel@tonic-gate #define	FBT_ADDR2NDX(addr)	((((uintptr_t)(addr)) >> 4) & fbt_probetab_mask)
560Sstevel@tonic-gate #define	FBT_PROBETAB_SIZE	0x8000		/* 32k entries -- 128K total */
570Sstevel@tonic-gate 
580Sstevel@tonic-gate typedef struct fbt_probe {
590Sstevel@tonic-gate 	struct fbt_probe *fbtp_hashnext;
600Sstevel@tonic-gate 	uint8_t		*fbtp_patchpoint;
610Sstevel@tonic-gate 	int8_t		fbtp_rval;
620Sstevel@tonic-gate 	uint8_t		fbtp_patchval;
630Sstevel@tonic-gate 	uint8_t		fbtp_savedval;
640Sstevel@tonic-gate 	uintptr_t	fbtp_roffset;
650Sstevel@tonic-gate 	dtrace_id_t	fbtp_id;
660Sstevel@tonic-gate 	char		*fbtp_name;
670Sstevel@tonic-gate 	struct modctl	*fbtp_ctl;
680Sstevel@tonic-gate 	int		fbtp_loadcnt;
690Sstevel@tonic-gate 	int		fbtp_symndx;
700Sstevel@tonic-gate 	int		fbtp_primary;
710Sstevel@tonic-gate 	struct fbt_probe *fbtp_next;
720Sstevel@tonic-gate } fbt_probe_t;
730Sstevel@tonic-gate 
740Sstevel@tonic-gate static dev_info_t		*fbt_devi;
750Sstevel@tonic-gate static dtrace_provider_id_t	fbt_id;
760Sstevel@tonic-gate static fbt_probe_t		**fbt_probetab;
770Sstevel@tonic-gate static int			fbt_probetab_size;
780Sstevel@tonic-gate static int			fbt_probetab_mask;
790Sstevel@tonic-gate static int			fbt_verbose = 0;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate static int
fbt_invop(uintptr_t addr,uintptr_t * stack,uintptr_t rval)820Sstevel@tonic-gate fbt_invop(uintptr_t addr, uintptr_t *stack, uintptr_t rval)
830Sstevel@tonic-gate {
840Sstevel@tonic-gate 	uintptr_t stack0, stack1, stack2, stack3, stack4;
850Sstevel@tonic-gate 	fbt_probe_t *fbt = fbt_probetab[FBT_ADDR2NDX(addr)];
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	for (; fbt != NULL; fbt = fbt->fbtp_hashnext) {
880Sstevel@tonic-gate 		if ((uintptr_t)fbt->fbtp_patchpoint == addr) {
890Sstevel@tonic-gate 			if (fbt->fbtp_roffset == 0) {
900Sstevel@tonic-gate 				int i = 0;
910Sstevel@tonic-gate 				/*
920Sstevel@tonic-gate 				 * When accessing the arguments on the stack,
930Sstevel@tonic-gate 				 * we must protect against accessing beyond
940Sstevel@tonic-gate 				 * the stack.  We can safely set NOFAULT here
950Sstevel@tonic-gate 				 * -- we know that interrupts are already
960Sstevel@tonic-gate 				 * disabled.
970Sstevel@tonic-gate 				 */
980Sstevel@tonic-gate 				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
990Sstevel@tonic-gate 				CPU->cpu_dtrace_caller = stack[i++];
1000Sstevel@tonic-gate #ifdef __amd64
1010Sstevel@tonic-gate 				/*
1020Sstevel@tonic-gate 				 * On amd64, stack[0] contains the dereferenced
1030Sstevel@tonic-gate 				 * stack pointer, stack[1] contains savfp,
1040Sstevel@tonic-gate 				 * stack[2] contains savpc.  We want to step
1050Sstevel@tonic-gate 				 * over these entries.
1060Sstevel@tonic-gate 				 */
1070Sstevel@tonic-gate 				i += 2;
1080Sstevel@tonic-gate #endif
1090Sstevel@tonic-gate 				stack0 = stack[i++];
1100Sstevel@tonic-gate 				stack1 = stack[i++];
1110Sstevel@tonic-gate 				stack2 = stack[i++];
1120Sstevel@tonic-gate 				stack3 = stack[i++];
1130Sstevel@tonic-gate 				stack4 = stack[i++];
1140Sstevel@tonic-gate 				DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT |
1150Sstevel@tonic-gate 				    CPU_DTRACE_BADADDR);
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 				dtrace_probe(fbt->fbtp_id, stack0, stack1,
1180Sstevel@tonic-gate 				    stack2, stack3, stack4);
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 				CPU->cpu_dtrace_caller = NULL;
1210Sstevel@tonic-gate 			} else {
1220Sstevel@tonic-gate #ifdef __amd64
1230Sstevel@tonic-gate 				/*
1240Sstevel@tonic-gate 				 * On amd64, we instrument the ret, not the
1250Sstevel@tonic-gate 				 * leave.  We therefore need to set the caller
1260Sstevel@tonic-gate 				 * to assure that the top frame of a stack()
1270Sstevel@tonic-gate 				 * action is correct.
1280Sstevel@tonic-gate 				 */
1290Sstevel@tonic-gate 				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
1300Sstevel@tonic-gate 				CPU->cpu_dtrace_caller = stack[0];
1310Sstevel@tonic-gate 				DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT |
1320Sstevel@tonic-gate 				    CPU_DTRACE_BADADDR);
1330Sstevel@tonic-gate #endif
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 				dtrace_probe(fbt->fbtp_id, fbt->fbtp_roffset,
1360Sstevel@tonic-gate 				    rval, 0, 0, 0);
1370Sstevel@tonic-gate 				CPU->cpu_dtrace_caller = NULL;
1380Sstevel@tonic-gate 			}
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 			return (fbt->fbtp_rval);
1410Sstevel@tonic-gate 		}
1420Sstevel@tonic-gate 	}
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	return (0);
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate /*ARGSUSED*/
1480Sstevel@tonic-gate static void
fbt_provide_module(void * arg,struct modctl * ctl)1490Sstevel@tonic-gate fbt_provide_module(void *arg, struct modctl *ctl)
1500Sstevel@tonic-gate {
1510Sstevel@tonic-gate 	struct module *mp = ctl->mod_mp;
1520Sstevel@tonic-gate 	char *str = mp->strings;
1530Sstevel@tonic-gate 	int nsyms = mp->nsyms;
1540Sstevel@tonic-gate 	Shdr *symhdr = mp->symhdr;
1550Sstevel@tonic-gate 	char *modname = ctl->mod_modname;
1560Sstevel@tonic-gate 	char *name;
1570Sstevel@tonic-gate 	fbt_probe_t *fbt, *retfbt;
1580Sstevel@tonic-gate 	size_t symsize;
1590Sstevel@tonic-gate 	int i, size;
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	/*
1620Sstevel@tonic-gate 	 * Employees of dtrace and their families are ineligible.  Void
1630Sstevel@tonic-gate 	 * where prohibited.
1640Sstevel@tonic-gate 	 */
1650Sstevel@tonic-gate 	if (strcmp(modname, "dtrace") == 0)
1660Sstevel@tonic-gate 		return;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	if (ctl->mod_requisites != NULL) {
1690Sstevel@tonic-gate 		struct modctl_list *list;
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 		list = (struct modctl_list *)ctl->mod_requisites;
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 		for (; list != NULL; list = list->modl_next) {
1740Sstevel@tonic-gate 			if (strcmp(list->modl_modp->mod_modname, "dtrace") == 0)
1750Sstevel@tonic-gate 				return;
1760Sstevel@tonic-gate 		}
1770Sstevel@tonic-gate 	}
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	/*
1800Sstevel@tonic-gate 	 * KMDB is ineligible for instrumentation -- it may execute in
1810Sstevel@tonic-gate 	 * any context, including probe context.
1820Sstevel@tonic-gate 	 */
1830Sstevel@tonic-gate 	if (strcmp(modname, "kmdbmod") == 0)
1840Sstevel@tonic-gate 		return;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	if (str == NULL || symhdr == NULL || symhdr->sh_addr == NULL) {
1870Sstevel@tonic-gate 		/*
1880Sstevel@tonic-gate 		 * If this module doesn't (yet) have its string or symbol
1890Sstevel@tonic-gate 		 * table allocated, clear out.
1900Sstevel@tonic-gate 		 */
1910Sstevel@tonic-gate 		return;
1920Sstevel@tonic-gate 	}
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	symsize = symhdr->sh_entsize;
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	if (mp->fbt_nentries) {
1970Sstevel@tonic-gate 		/*
1980Sstevel@tonic-gate 		 * This module has some FBT entries allocated; we're afraid
1990Sstevel@tonic-gate 		 * to screw with it.
2000Sstevel@tonic-gate 		 */
2010Sstevel@tonic-gate 		return;
2020Sstevel@tonic-gate 	}
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	for (i = 1; i < nsyms; i++) {
2050Sstevel@tonic-gate 		uint8_t *instr, *limit;
2060Sstevel@tonic-gate 		Sym *sym = (Sym *)(symhdr->sh_addr + i * symsize);
207576Sbmc 		int j;
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 		if (ELF_ST_TYPE(sym->st_info) != STT_FUNC)
2100Sstevel@tonic-gate 			continue;
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 		/*
2130Sstevel@tonic-gate 		 * Weak symbols are not candidates.  This could be made to
2140Sstevel@tonic-gate 		 * work (where weak functions and their underlying function
2150Sstevel@tonic-gate 		 * appear as two disjoint probes), but it's not simple.
2160Sstevel@tonic-gate 		 */
2170Sstevel@tonic-gate 		if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
2180Sstevel@tonic-gate 			continue;
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 		name = str + sym->st_name;
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 		if (strstr(name, "dtrace_") == name &&
2230Sstevel@tonic-gate 		    strstr(name, "dtrace_safe_") != name) {
2240Sstevel@tonic-gate 			/*
2250Sstevel@tonic-gate 			 * Anything beginning with "dtrace_" may be called
2260Sstevel@tonic-gate 			 * from probe context unless it explitly indicates
2270Sstevel@tonic-gate 			 * that it won't be called from probe context by
2280Sstevel@tonic-gate 			 * using the prefix "dtrace_safe_".
2290Sstevel@tonic-gate 			 */
2300Sstevel@tonic-gate 			continue;
2310Sstevel@tonic-gate 		}
2320Sstevel@tonic-gate 
233457Sbmc 		if (strstr(name, "kdi_") == name ||
234457Sbmc 		    strstr(name, "_kdi_") != NULL) {
2350Sstevel@tonic-gate 			/*
236457Sbmc 			 * Any function name beginning with "kdi_" or
237457Sbmc 			 * containing the string "_kdi_" is a part of the
2380Sstevel@tonic-gate 			 * kernel debugger interface and may be called in
2390Sstevel@tonic-gate 			 * arbitrary context -- including probe context.
2400Sstevel@tonic-gate 			 */
2410Sstevel@tonic-gate 			continue;
2420Sstevel@tonic-gate 		}
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 		/*
2450Sstevel@tonic-gate 		 * Due to 4524008, _init and _fini may have a bloated st_size.
2460Sstevel@tonic-gate 		 * While this bug was fixed quite some time ago, old drivers
2470Sstevel@tonic-gate 		 * may be lurking.  We need to develop a better solution to
2480Sstevel@tonic-gate 		 * this problem, such that correct _init and _fini functions
2490Sstevel@tonic-gate 		 * (the vast majority) may be correctly traced.  One solution
2500Sstevel@tonic-gate 		 * may be to scan through the entire symbol table to see if
2510Sstevel@tonic-gate 		 * any symbol overlaps with _init.  If none does, set a bit in
2520Sstevel@tonic-gate 		 * the module structure that this module has correct _init and
2530Sstevel@tonic-gate 		 * _fini sizes.  This will cause some pain the first time a
2540Sstevel@tonic-gate 		 * module is scanned, but at least it would be O(N) instead of
2550Sstevel@tonic-gate 		 * O(N log N)...
2560Sstevel@tonic-gate 		 */
2570Sstevel@tonic-gate 		if (strcmp(name, "_init") == 0)
2580Sstevel@tonic-gate 			continue;
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 		if (strcmp(name, "_fini") == 0)
2610Sstevel@tonic-gate 			continue;
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 		/*
2640Sstevel@tonic-gate 		 * In order to be eligible, the function must begin with the
2650Sstevel@tonic-gate 		 * following sequence:
2660Sstevel@tonic-gate 		 *
2670Sstevel@tonic-gate 		 * 	pushl	%esp
2680Sstevel@tonic-gate 		 *	movl	%esp, %ebp
2690Sstevel@tonic-gate 		 *
2700Sstevel@tonic-gate 		 * Note that there are two variants of encodings that generate
2710Sstevel@tonic-gate 		 * the movl; we must check for both.  For 64-bit, we would
2720Sstevel@tonic-gate 		 * normally insist that a function begin with the following
2730Sstevel@tonic-gate 		 * sequence:
2740Sstevel@tonic-gate 		 *
2750Sstevel@tonic-gate 		 *	pushq	%rbp
2760Sstevel@tonic-gate 		 *	movq	%rsp, %rbp
2770Sstevel@tonic-gate 		 *
2780Sstevel@tonic-gate 		 * However, the compiler for 64-bit often splits these two
2790Sstevel@tonic-gate 		 * instructions -- and the first instruction in the function
2800Sstevel@tonic-gate 		 * is often not the pushq.  As a result, on 64-bit we look
2810Sstevel@tonic-gate 		 * for any "pushq %rbp" in the function and we instrument
2820Sstevel@tonic-gate 		 * this with a breakpoint instruction.
2830Sstevel@tonic-gate 		 */
2840Sstevel@tonic-gate 		instr = (uint8_t *)sym->st_value;
2850Sstevel@tonic-gate 		limit = (uint8_t *)(sym->st_value + sym->st_size);
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate #ifdef __amd64
2880Sstevel@tonic-gate 		while (instr < limit) {
2890Sstevel@tonic-gate 			if (*instr == FBT_PUSHL_EBP)
2900Sstevel@tonic-gate 				break;
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 			if ((size = dtrace_instr_size(instr)) <= 0)
2930Sstevel@tonic-gate 				break;
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 			instr += size;
2960Sstevel@tonic-gate 		}
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 		if (instr >= limit || *instr != FBT_PUSHL_EBP) {
2990Sstevel@tonic-gate 			/*
3000Sstevel@tonic-gate 			 * We either don't save the frame pointer in this
3010Sstevel@tonic-gate 			 * function, or we ran into some disassembly
3020Sstevel@tonic-gate 			 * screw-up.  Either way, we bail.
3030Sstevel@tonic-gate 			 */
3040Sstevel@tonic-gate 			continue;
3050Sstevel@tonic-gate 		}
3060Sstevel@tonic-gate #else
3070Sstevel@tonic-gate 		if (instr[0] != FBT_PUSHL_EBP)
3080Sstevel@tonic-gate 			continue;
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 		if (!(instr[1] == FBT_MOVL_ESP_EBP0_V0 &&
3110Sstevel@tonic-gate 		    instr[2] == FBT_MOVL_ESP_EBP1_V0) &&
3120Sstevel@tonic-gate 		    !(instr[1] == FBT_MOVL_ESP_EBP0_V1 &&
3130Sstevel@tonic-gate 		    instr[2] == FBT_MOVL_ESP_EBP1_V1))
3140Sstevel@tonic-gate 			continue;
3150Sstevel@tonic-gate #endif
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 		fbt = kmem_zalloc(sizeof (fbt_probe_t), KM_SLEEP);
3180Sstevel@tonic-gate 		fbt->fbtp_name = name;
3190Sstevel@tonic-gate 		fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
3200Sstevel@tonic-gate 		    name, FBT_ENTRY, 3, fbt);
3210Sstevel@tonic-gate 		fbt->fbtp_patchpoint = instr;
3220Sstevel@tonic-gate 		fbt->fbtp_ctl = ctl;
3230Sstevel@tonic-gate 		fbt->fbtp_loadcnt = ctl->mod_loadcnt;
3240Sstevel@tonic-gate 		fbt->fbtp_rval = DTRACE_INVOP_PUSHL_EBP;
3250Sstevel@tonic-gate 		fbt->fbtp_savedval = *instr;
3260Sstevel@tonic-gate 		fbt->fbtp_patchval = FBT_PATCHVAL;
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 		fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
3290Sstevel@tonic-gate 		fbt->fbtp_symndx = i;
3300Sstevel@tonic-gate 		fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 		mp->fbt_nentries++;
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 		retfbt = NULL;
3350Sstevel@tonic-gate again:
3360Sstevel@tonic-gate 		if (instr >= limit)
3370Sstevel@tonic-gate 			continue;
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 		/*
3400Sstevel@tonic-gate 		 * If this disassembly fails, then we've likely walked off into
3410Sstevel@tonic-gate 		 * a jump table or some other unsuitable area.  Bail out of the
3420Sstevel@tonic-gate 		 * disassembly now.
3430Sstevel@tonic-gate 		 */
3440Sstevel@tonic-gate 		if ((size = dtrace_instr_size(instr)) <= 0)
3450Sstevel@tonic-gate 			continue;
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate #ifdef __amd64
3480Sstevel@tonic-gate 		/*
3490Sstevel@tonic-gate 		 * We only instrument "ret" on amd64 -- we don't yet instrument
3500Sstevel@tonic-gate 		 * ret imm16, largely because the compiler doesn't seem to
3510Sstevel@tonic-gate 		 * (yet) emit them in the kernel...
3520Sstevel@tonic-gate 		 */
3530Sstevel@tonic-gate 		if (*instr != FBT_RET) {
3540Sstevel@tonic-gate 			instr += size;
3550Sstevel@tonic-gate 			goto again;
3560Sstevel@tonic-gate 		}
3571739Sbmc #else
3581739Sbmc 		if (!(size == 1 &&
3591739Sbmc 		    (*instr == FBT_POPL_EBP || *instr == FBT_LEAVE) &&
3601739Sbmc 		    (*(instr + 1) == FBT_RET ||
3611739Sbmc 		    *(instr + 1) == FBT_RET_IMM16))) {
3621739Sbmc 			instr += size;
3631739Sbmc 			goto again;
3641739Sbmc 		}
3651739Sbmc #endif
366576Sbmc 
367576Sbmc 		/*
3681739Sbmc 		 * We (desperately) want to avoid erroneously instrumenting a
3691739Sbmc 		 * jump table, especially given that our markers are pretty
3701739Sbmc 		 * short:  two bytes on x86, and just one byte on amd64.  To
3711739Sbmc 		 * determine if we're looking at a true instruction sequence
3721739Sbmc 		 * or an inline jump table that happens to contain the same
3731739Sbmc 		 * byte sequences, we resort to some heuristic sleeze:  we
3741739Sbmc 		 * treat this instruction as being contained within a pointer,
3751739Sbmc 		 * and see if that pointer points to within the body of the
3761739Sbmc 		 * function.  If it does, we refuse to instrument it.
377576Sbmc 		 */
378576Sbmc 		for (j = 0; j < sizeof (uintptr_t); j++) {
379576Sbmc 			uintptr_t check = (uintptr_t)instr - j;
380576Sbmc 			uint8_t *ptr;
381576Sbmc 
382576Sbmc 			if (check < sym->st_value)
383576Sbmc 				break;
384576Sbmc 
385576Sbmc 			if (check + sizeof (uintptr_t) > (uintptr_t)limit)
386576Sbmc 				continue;
387576Sbmc 
388576Sbmc 			ptr = *(uint8_t **)check;
389576Sbmc 
390576Sbmc 			if (ptr >= (uint8_t *)sym->st_value && ptr < limit) {
391576Sbmc 				instr += size;
392576Sbmc 				goto again;
393576Sbmc 			}
394576Sbmc 		}
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 		/*
3970Sstevel@tonic-gate 		 * We have a winner!
3980Sstevel@tonic-gate 		 */
3990Sstevel@tonic-gate 		fbt = kmem_zalloc(sizeof (fbt_probe_t), KM_SLEEP);
4000Sstevel@tonic-gate 		fbt->fbtp_name = name;
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 		if (retfbt == NULL) {
4030Sstevel@tonic-gate 			fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
4040Sstevel@tonic-gate 			    name, FBT_RETURN, 3, fbt);
4050Sstevel@tonic-gate 		} else {
4060Sstevel@tonic-gate 			retfbt->fbtp_next = fbt;
4070Sstevel@tonic-gate 			fbt->fbtp_id = retfbt->fbtp_id;
4080Sstevel@tonic-gate 		}
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 		retfbt = fbt;
4110Sstevel@tonic-gate 		fbt->fbtp_patchpoint = instr;
4120Sstevel@tonic-gate 		fbt->fbtp_ctl = ctl;
4130Sstevel@tonic-gate 		fbt->fbtp_loadcnt = ctl->mod_loadcnt;
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate #ifndef __amd64
4160Sstevel@tonic-gate 		if (*instr == FBT_POPL_EBP) {
4170Sstevel@tonic-gate 			fbt->fbtp_rval = DTRACE_INVOP_POPL_EBP;
4180Sstevel@tonic-gate 		} else {
4190Sstevel@tonic-gate 			ASSERT(*instr == FBT_LEAVE);
4200Sstevel@tonic-gate 			fbt->fbtp_rval = DTRACE_INVOP_LEAVE;
4210Sstevel@tonic-gate 		}
4220Sstevel@tonic-gate 		fbt->fbtp_roffset =
4230Sstevel@tonic-gate 		    (uintptr_t)(instr - (uint8_t *)sym->st_value) + 1;
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate #else
4260Sstevel@tonic-gate 		ASSERT(*instr == FBT_RET);
4270Sstevel@tonic-gate 		fbt->fbtp_rval = DTRACE_INVOP_RET;
4280Sstevel@tonic-gate 		fbt->fbtp_roffset =
4290Sstevel@tonic-gate 		    (uintptr_t)(instr - (uint8_t *)sym->st_value);
4300Sstevel@tonic-gate #endif
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 		fbt->fbtp_savedval = *instr;
4330Sstevel@tonic-gate 		fbt->fbtp_patchval = FBT_PATCHVAL;
4340Sstevel@tonic-gate 		fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
4350Sstevel@tonic-gate 		fbt->fbtp_symndx = i;
4360Sstevel@tonic-gate 		fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 		mp->fbt_nentries++;
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 		instr += size;
4410Sstevel@tonic-gate 		goto again;
4420Sstevel@tonic-gate 	}
4430Sstevel@tonic-gate }
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate /*ARGSUSED*/
4460Sstevel@tonic-gate static void
fbt_destroy(void * arg,dtrace_id_t id,void * parg)4470Sstevel@tonic-gate fbt_destroy(void *arg, dtrace_id_t id, void *parg)
4480Sstevel@tonic-gate {
4490Sstevel@tonic-gate 	fbt_probe_t *fbt = parg, *next, *hash, *last;
4500Sstevel@tonic-gate 	struct modctl *ctl = fbt->fbtp_ctl;
4510Sstevel@tonic-gate 	int ndx;
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	do {
4540Sstevel@tonic-gate 		if (ctl != NULL && ctl->mod_loadcnt == fbt->fbtp_loadcnt) {
4550Sstevel@tonic-gate 			if ((ctl->mod_loadcnt == fbt->fbtp_loadcnt &&
4560Sstevel@tonic-gate 			    ctl->mod_loaded)) {
4570Sstevel@tonic-gate 				((struct module *)
4580Sstevel@tonic-gate 				    (ctl->mod_mp))->fbt_nentries--;
4590Sstevel@tonic-gate 			}
4600Sstevel@tonic-gate 		}
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 		/*
4630Sstevel@tonic-gate 		 * Now we need to remove this probe from the fbt_probetab.
4640Sstevel@tonic-gate 		 */
4650Sstevel@tonic-gate 		ndx = FBT_ADDR2NDX(fbt->fbtp_patchpoint);
4660Sstevel@tonic-gate 		last = NULL;
4670Sstevel@tonic-gate 		hash = fbt_probetab[ndx];
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 		while (hash != fbt) {
4700Sstevel@tonic-gate 			ASSERT(hash != NULL);
4710Sstevel@tonic-gate 			last = hash;
4720Sstevel@tonic-gate 			hash = hash->fbtp_hashnext;
4730Sstevel@tonic-gate 		}
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 		if (last != NULL) {
4760Sstevel@tonic-gate 			last->fbtp_hashnext = fbt->fbtp_hashnext;
4770Sstevel@tonic-gate 		} else {
4780Sstevel@tonic-gate 			fbt_probetab[ndx] = fbt->fbtp_hashnext;
4790Sstevel@tonic-gate 		}
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 		next = fbt->fbtp_next;
4820Sstevel@tonic-gate 		kmem_free(fbt, sizeof (fbt_probe_t));
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 		fbt = next;
4850Sstevel@tonic-gate 	} while (fbt != NULL);
4860Sstevel@tonic-gate }
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate /*ARGSUSED*/
489*8803SJonathan.Haslam@Sun.COM static int
fbt_enable(void * arg,dtrace_id_t id,void * parg)4900Sstevel@tonic-gate fbt_enable(void *arg, dtrace_id_t id, void *parg)
4910Sstevel@tonic-gate {
4920Sstevel@tonic-gate 	fbt_probe_t *fbt = parg;
4930Sstevel@tonic-gate 	struct modctl *ctl = fbt->fbtp_ctl;
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	ctl->mod_nenabled++;
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	if (!ctl->mod_loaded) {
4980Sstevel@tonic-gate 		if (fbt_verbose) {
4990Sstevel@tonic-gate 			cmn_err(CE_NOTE, "fbt is failing for probe %s "
5000Sstevel@tonic-gate 			    "(module %s unloaded)",
5010Sstevel@tonic-gate 			    fbt->fbtp_name, ctl->mod_modname);
5020Sstevel@tonic-gate 		}
5030Sstevel@tonic-gate 
504*8803SJonathan.Haslam@Sun.COM 		return (0);
5050Sstevel@tonic-gate 	}
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	/*
5080Sstevel@tonic-gate 	 * Now check that our modctl has the expected load count.  If it
5090Sstevel@tonic-gate 	 * doesn't, this module must have been unloaded and reloaded -- and
5100Sstevel@tonic-gate 	 * we're not going to touch it.
5110Sstevel@tonic-gate 	 */
5120Sstevel@tonic-gate 	if (ctl->mod_loadcnt != fbt->fbtp_loadcnt) {
5130Sstevel@tonic-gate 		if (fbt_verbose) {
5140Sstevel@tonic-gate 			cmn_err(CE_NOTE, "fbt is failing for probe %s "
5150Sstevel@tonic-gate 			    "(module %s reloaded)",
5160Sstevel@tonic-gate 			    fbt->fbtp_name, ctl->mod_modname);
5170Sstevel@tonic-gate 		}
5180Sstevel@tonic-gate 
519*8803SJonathan.Haslam@Sun.COM 		return (0);
5200Sstevel@tonic-gate 	}
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	for (; fbt != NULL; fbt = fbt->fbtp_next)
5230Sstevel@tonic-gate 		*fbt->fbtp_patchpoint = fbt->fbtp_patchval;
524*8803SJonathan.Haslam@Sun.COM 
525*8803SJonathan.Haslam@Sun.COM 	return (0);
5260Sstevel@tonic-gate }
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate /*ARGSUSED*/
5290Sstevel@tonic-gate static void
fbt_disable(void * arg,dtrace_id_t id,void * parg)5300Sstevel@tonic-gate fbt_disable(void *arg, dtrace_id_t id, void *parg)
5310Sstevel@tonic-gate {
5320Sstevel@tonic-gate 	fbt_probe_t *fbt = parg;
5330Sstevel@tonic-gate 	struct modctl *ctl = fbt->fbtp_ctl;
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	ASSERT(ctl->mod_nenabled > 0);
5360Sstevel@tonic-gate 	ctl->mod_nenabled--;
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 	if (!ctl->mod_loaded || (ctl->mod_loadcnt != fbt->fbtp_loadcnt))
5390Sstevel@tonic-gate 		return;
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	for (; fbt != NULL; fbt = fbt->fbtp_next)
5420Sstevel@tonic-gate 		*fbt->fbtp_patchpoint = fbt->fbtp_savedval;
5430Sstevel@tonic-gate }
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate /*ARGSUSED*/
5460Sstevel@tonic-gate static void
fbt_suspend(void * arg,dtrace_id_t id,void * parg)5470Sstevel@tonic-gate fbt_suspend(void *arg, dtrace_id_t id, void *parg)
5480Sstevel@tonic-gate {
5490Sstevel@tonic-gate 	fbt_probe_t *fbt = parg;
5500Sstevel@tonic-gate 	struct modctl *ctl = fbt->fbtp_ctl;
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	ASSERT(ctl->mod_nenabled > 0);
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 	if (!ctl->mod_loaded || (ctl->mod_loadcnt != fbt->fbtp_loadcnt))
5550Sstevel@tonic-gate 		return;
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	for (; fbt != NULL; fbt = fbt->fbtp_next)
5580Sstevel@tonic-gate 		*fbt->fbtp_patchpoint = fbt->fbtp_savedval;
5590Sstevel@tonic-gate }
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate /*ARGSUSED*/
5620Sstevel@tonic-gate static void
fbt_resume(void * arg,dtrace_id_t id,void * parg)5630Sstevel@tonic-gate fbt_resume(void *arg, dtrace_id_t id, void *parg)
5640Sstevel@tonic-gate {
5650Sstevel@tonic-gate 	fbt_probe_t *fbt = parg;
5660Sstevel@tonic-gate 	struct modctl *ctl = fbt->fbtp_ctl;
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	ASSERT(ctl->mod_nenabled > 0);
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 	if (!ctl->mod_loaded || (ctl->mod_loadcnt != fbt->fbtp_loadcnt))
5710Sstevel@tonic-gate 		return;
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	for (; fbt != NULL; fbt = fbt->fbtp_next)
5740Sstevel@tonic-gate 		*fbt->fbtp_patchpoint = fbt->fbtp_patchval;
5750Sstevel@tonic-gate }
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate /*ARGSUSED*/
5780Sstevel@tonic-gate static void
fbt_getargdesc(void * arg,dtrace_id_t id,void * parg,dtrace_argdesc_t * desc)5790Sstevel@tonic-gate fbt_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc)
5800Sstevel@tonic-gate {
5810Sstevel@tonic-gate 	fbt_probe_t *fbt = parg;
5820Sstevel@tonic-gate 	struct modctl *ctl = fbt->fbtp_ctl;
5830Sstevel@tonic-gate 	struct module *mp = ctl->mod_mp;
5840Sstevel@tonic-gate 	ctf_file_t *fp = NULL, *pfp;
5850Sstevel@tonic-gate 	ctf_funcinfo_t f;
5860Sstevel@tonic-gate 	int error;
5870Sstevel@tonic-gate 	ctf_id_t argv[32], type;
5880Sstevel@tonic-gate 	int argc = sizeof (argv) / sizeof (ctf_id_t);
5890Sstevel@tonic-gate 	const char *parent;
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	if (!ctl->mod_loaded || (ctl->mod_loadcnt != fbt->fbtp_loadcnt))
5920Sstevel@tonic-gate 		goto err;
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	if (fbt->fbtp_roffset != 0 && desc->dtargd_ndx == 0) {
5950Sstevel@tonic-gate 		(void) strcpy(desc->dtargd_native, "int");
5960Sstevel@tonic-gate 		return;
5970Sstevel@tonic-gate 	}
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 	if ((fp = ctf_modopen(mp, &error)) == NULL) {
6000Sstevel@tonic-gate 		/*
6010Sstevel@tonic-gate 		 * We have no CTF information for this module -- and therefore
6020Sstevel@tonic-gate 		 * no args[] information.
6030Sstevel@tonic-gate 		 */
6040Sstevel@tonic-gate 		goto err;
6050Sstevel@tonic-gate 	}
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	/*
6080Sstevel@tonic-gate 	 * If we have a parent container, we must manually import it.
6090Sstevel@tonic-gate 	 */
6100Sstevel@tonic-gate 	if ((parent = ctf_parent_name(fp)) != NULL) {
6113446Smrj 		struct modctl *mp = &modules;
6123446Smrj 		struct modctl *mod = NULL;
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 		/*
6150Sstevel@tonic-gate 		 * We must iterate over all modules to find the module that
6160Sstevel@tonic-gate 		 * is our parent.
6170Sstevel@tonic-gate 		 */
6183446Smrj 		do {
6193446Smrj 			if (strcmp(mp->mod_modname, parent) == 0) {
6203446Smrj 				mod = mp;
6210Sstevel@tonic-gate 				break;
6223446Smrj 			}
6233446Smrj 		} while ((mp = mp->mod_next) != &modules);
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 		if (mod == NULL)
6260Sstevel@tonic-gate 			goto err;
6270Sstevel@tonic-gate 
6283446Smrj 		if ((pfp = ctf_modopen(mod->mod_mp, &error)) == NULL) {
6290Sstevel@tonic-gate 			goto err;
6303446Smrj 		}
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 		if (ctf_import(fp, pfp) != 0) {
6330Sstevel@tonic-gate 			ctf_close(pfp);
6340Sstevel@tonic-gate 			goto err;
6350Sstevel@tonic-gate 		}
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 		ctf_close(pfp);
6380Sstevel@tonic-gate 	}
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 	if (ctf_func_info(fp, fbt->fbtp_symndx, &f) == CTF_ERR)
6410Sstevel@tonic-gate 		goto err;
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	if (fbt->fbtp_roffset != 0) {
6440Sstevel@tonic-gate 		if (desc->dtargd_ndx > 1)
6450Sstevel@tonic-gate 			goto err;
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 		ASSERT(desc->dtargd_ndx == 1);
6480Sstevel@tonic-gate 		type = f.ctc_return;
6490Sstevel@tonic-gate 	} else {
6500Sstevel@tonic-gate 		if (desc->dtargd_ndx + 1 > f.ctc_argc)
6510Sstevel@tonic-gate 			goto err;
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 		if (ctf_func_args(fp, fbt->fbtp_symndx, argc, argv) == CTF_ERR)
6540Sstevel@tonic-gate 			goto err;
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 		type = argv[desc->dtargd_ndx];
6570Sstevel@tonic-gate 	}
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 	if (ctf_type_name(fp, type, desc->dtargd_native,
6600Sstevel@tonic-gate 	    DTRACE_ARGTYPELEN) != NULL) {
6610Sstevel@tonic-gate 		ctf_close(fp);
6620Sstevel@tonic-gate 		return;
6630Sstevel@tonic-gate 	}
6640Sstevel@tonic-gate err:
6650Sstevel@tonic-gate 	if (fp != NULL)
6660Sstevel@tonic-gate 		ctf_close(fp);
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 	desc->dtargd_ndx = DTRACE_ARGNONE;
6690Sstevel@tonic-gate }
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate static dtrace_pattr_t fbt_attr = {
6720Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
6730Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
6740Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
6750Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
6760Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
6770Sstevel@tonic-gate };
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate static dtrace_pops_t fbt_pops = {
6800Sstevel@tonic-gate 	NULL,
6810Sstevel@tonic-gate 	fbt_provide_module,
6820Sstevel@tonic-gate 	fbt_enable,
6830Sstevel@tonic-gate 	fbt_disable,
6840Sstevel@tonic-gate 	fbt_suspend,
6850Sstevel@tonic-gate 	fbt_resume,
6860Sstevel@tonic-gate 	fbt_getargdesc,
6870Sstevel@tonic-gate 	NULL,
6880Sstevel@tonic-gate 	NULL,
6890Sstevel@tonic-gate 	fbt_destroy
6900Sstevel@tonic-gate };
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate static void
fbt_cleanup(dev_info_t * devi)6930Sstevel@tonic-gate fbt_cleanup(dev_info_t *devi)
6940Sstevel@tonic-gate {
6950Sstevel@tonic-gate 	dtrace_invop_remove(fbt_invop);
6960Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
6970Sstevel@tonic-gate 	kmem_free(fbt_probetab, fbt_probetab_size * sizeof (fbt_probe_t *));
6980Sstevel@tonic-gate 	fbt_probetab = NULL;
6990Sstevel@tonic-gate 	fbt_probetab_mask = 0;
7000Sstevel@tonic-gate }
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate static int
fbt_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)7030Sstevel@tonic-gate fbt_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
7040Sstevel@tonic-gate {
7050Sstevel@tonic-gate 	switch (cmd) {
7060Sstevel@tonic-gate 	case DDI_ATTACH:
7070Sstevel@tonic-gate 		break;
7080Sstevel@tonic-gate 	case DDI_RESUME:
7090Sstevel@tonic-gate 		return (DDI_SUCCESS);
7100Sstevel@tonic-gate 	default:
7110Sstevel@tonic-gate 		return (DDI_FAILURE);
7120Sstevel@tonic-gate 	}
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 	if (fbt_probetab_size == 0)
7150Sstevel@tonic-gate 		fbt_probetab_size = FBT_PROBETAB_SIZE;
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 	fbt_probetab_mask = fbt_probetab_size - 1;
7180Sstevel@tonic-gate 	fbt_probetab =
7190Sstevel@tonic-gate 	    kmem_zalloc(fbt_probetab_size * sizeof (fbt_probe_t *), KM_SLEEP);
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 	dtrace_invop_add(fbt_invop);
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "fbt", S_IFCHR, 0,
7240Sstevel@tonic-gate 	    DDI_PSEUDO, NULL) == DDI_FAILURE ||
7251677Sdp 	    dtrace_register("fbt", &fbt_attr, DTRACE_PRIV_KERNEL, NULL,
7260Sstevel@tonic-gate 	    &fbt_pops, NULL, &fbt_id) != 0) {
7270Sstevel@tonic-gate 		fbt_cleanup(devi);
7280Sstevel@tonic-gate 		return (DDI_FAILURE);
7290Sstevel@tonic-gate 	}
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 	ddi_report_dev(devi);
7320Sstevel@tonic-gate 	fbt_devi = devi;
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate 	return (DDI_SUCCESS);
7350Sstevel@tonic-gate }
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate static int
fbt_detach(dev_info_t * devi,ddi_detach_cmd_t cmd)7380Sstevel@tonic-gate fbt_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
7390Sstevel@tonic-gate {
7400Sstevel@tonic-gate 	switch (cmd) {
7410Sstevel@tonic-gate 	case DDI_DETACH:
7420Sstevel@tonic-gate 		break;
7430Sstevel@tonic-gate 	case DDI_SUSPEND:
7440Sstevel@tonic-gate 		return (DDI_SUCCESS);
7450Sstevel@tonic-gate 	default:
7460Sstevel@tonic-gate 		return (DDI_FAILURE);
7470Sstevel@tonic-gate 	}
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 	if (dtrace_unregister(fbt_id) != 0)
7500Sstevel@tonic-gate 		return (DDI_FAILURE);
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 	fbt_cleanup(devi);
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 	return (DDI_SUCCESS);
7550Sstevel@tonic-gate }
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate /*ARGSUSED*/
7580Sstevel@tonic-gate static int
fbt_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)7590Sstevel@tonic-gate fbt_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
7600Sstevel@tonic-gate {
7610Sstevel@tonic-gate 	int error;
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	switch (infocmd) {
7640Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
7650Sstevel@tonic-gate 		*result = (void *)fbt_devi;
7660Sstevel@tonic-gate 		error = DDI_SUCCESS;
7670Sstevel@tonic-gate 		break;
7680Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
7690Sstevel@tonic-gate 		*result = (void *)0;
7700Sstevel@tonic-gate 		error = DDI_SUCCESS;
7710Sstevel@tonic-gate 		break;
7720Sstevel@tonic-gate 	default:
7730Sstevel@tonic-gate 		error = DDI_FAILURE;
7740Sstevel@tonic-gate 	}
7750Sstevel@tonic-gate 	return (error);
7760Sstevel@tonic-gate }
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate /*ARGSUSED*/
7790Sstevel@tonic-gate static int
fbt_open(dev_t * devp,int flag,int otyp,cred_t * cred_p)7800Sstevel@tonic-gate fbt_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
7810Sstevel@tonic-gate {
7820Sstevel@tonic-gate 	return (0);
7830Sstevel@tonic-gate }
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate static struct cb_ops fbt_cb_ops = {
7860Sstevel@tonic-gate 	fbt_open,		/* open */
7870Sstevel@tonic-gate 	nodev,			/* close */
7880Sstevel@tonic-gate 	nulldev,		/* strategy */
7890Sstevel@tonic-gate 	nulldev,		/* print */
7900Sstevel@tonic-gate 	nodev,			/* dump */
7910Sstevel@tonic-gate 	nodev,			/* read */
7920Sstevel@tonic-gate 	nodev,			/* write */
7930Sstevel@tonic-gate 	nodev,			/* ioctl */
7940Sstevel@tonic-gate 	nodev,			/* devmap */
7950Sstevel@tonic-gate 	nodev,			/* mmap */
7960Sstevel@tonic-gate 	nodev,			/* segmap */
7970Sstevel@tonic-gate 	nochpoll,		/* poll */
7980Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
7990Sstevel@tonic-gate 	0,			/* streamtab  */
8000Sstevel@tonic-gate 	D_NEW | D_MP		/* Driver compatibility flag */
8010Sstevel@tonic-gate };
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate static struct dev_ops fbt_ops = {
8040Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
8050Sstevel@tonic-gate 	0,			/* refcnt */
8060Sstevel@tonic-gate 	fbt_info,		/* get_dev_info */
8070Sstevel@tonic-gate 	nulldev,		/* identify */
8080Sstevel@tonic-gate 	nulldev,		/* probe */
8090Sstevel@tonic-gate 	fbt_attach,		/* attach */
8100Sstevel@tonic-gate 	fbt_detach,		/* detach */
8110Sstevel@tonic-gate 	nodev,			/* reset */
8120Sstevel@tonic-gate 	&fbt_cb_ops,		/* driver operations */
8130Sstevel@tonic-gate 	NULL,			/* bus operations */
8147656SSherry.Moore@Sun.COM 	nodev,			/* dev power */
8157656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,		/* quiesce */
8160Sstevel@tonic-gate };
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate /*
8190Sstevel@tonic-gate  * Module linkage information for the kernel.
8200Sstevel@tonic-gate  */
8210Sstevel@tonic-gate static struct modldrv modldrv = {
8220Sstevel@tonic-gate 	&mod_driverops,		/* module type (this is a pseudo driver) */
8230Sstevel@tonic-gate 	"Function Boundary Tracing",	/* name of module */
8240Sstevel@tonic-gate 	&fbt_ops,		/* driver ops */
8250Sstevel@tonic-gate };
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate static struct modlinkage modlinkage = {
8280Sstevel@tonic-gate 	MODREV_1,
8290Sstevel@tonic-gate 	(void *)&modldrv,
8300Sstevel@tonic-gate 	NULL
8310Sstevel@tonic-gate };
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate int
_init(void)8340Sstevel@tonic-gate _init(void)
8350Sstevel@tonic-gate {
8360Sstevel@tonic-gate 	return (mod_install(&modlinkage));
8370Sstevel@tonic-gate }
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate int
_info(struct modinfo * modinfop)8400Sstevel@tonic-gate _info(struct modinfo *modinfop)
8410Sstevel@tonic-gate {
8420Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
8430Sstevel@tonic-gate }
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate int
_fini(void)8460Sstevel@tonic-gate _fini(void)
8470Sstevel@tonic-gate {
8480Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
8490Sstevel@tonic-gate }
850