xref: /titanic_44/usr/src/uts/intel/dtrace/fbt.c (revision ad4023c40b055806dce2bde9ee9e87e5016b5135)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*ad4023c4Sdp  * Common Development and Distribution License (the "License").
6*ad4023c4Sdp  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*ad4023c4Sdp  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
297c478bd9Sstevel@tonic-gate #include <sys/dtrace.h>
307c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
317c478bd9Sstevel@tonic-gate #include <sys/stat.h>
327c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
337c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
347c478bd9Sstevel@tonic-gate #include <sys/conf.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #define	FBT_PUSHL_EBP		0x55
377c478bd9Sstevel@tonic-gate #define	FBT_MOVL_ESP_EBP0_V0	0x8b
387c478bd9Sstevel@tonic-gate #define	FBT_MOVL_ESP_EBP1_V0	0xec
397c478bd9Sstevel@tonic-gate #define	FBT_MOVL_ESP_EBP0_V1	0x89
407c478bd9Sstevel@tonic-gate #define	FBT_MOVL_ESP_EBP1_V1	0xe5
417c478bd9Sstevel@tonic-gate #define	FBT_REX_RSP_RBP		0x48
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #define	FBT_POPL_EBP		0x5d
447c478bd9Sstevel@tonic-gate #define	FBT_RET			0xc3
457c478bd9Sstevel@tonic-gate #define	FBT_RET_IMM16		0xc2
467c478bd9Sstevel@tonic-gate #define	FBT_LEAVE		0xc9
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #ifdef __amd64
497c478bd9Sstevel@tonic-gate #define	FBT_PATCHVAL		0xcc
507c478bd9Sstevel@tonic-gate #else
517c478bd9Sstevel@tonic-gate #define	FBT_PATCHVAL		0xf0
527c478bd9Sstevel@tonic-gate #endif
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #define	FBT_ENTRY	"entry"
557c478bd9Sstevel@tonic-gate #define	FBT_RETURN	"return"
567c478bd9Sstevel@tonic-gate #define	FBT_ADDR2NDX(addr)	((((uintptr_t)(addr)) >> 4) & fbt_probetab_mask)
577c478bd9Sstevel@tonic-gate #define	FBT_PROBETAB_SIZE	0x8000		/* 32k entries -- 128K total */
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate typedef struct fbt_probe {
607c478bd9Sstevel@tonic-gate 	struct fbt_probe *fbtp_hashnext;
617c478bd9Sstevel@tonic-gate 	uint8_t		*fbtp_patchpoint;
627c478bd9Sstevel@tonic-gate 	int8_t		fbtp_rval;
637c478bd9Sstevel@tonic-gate 	uint8_t		fbtp_patchval;
647c478bd9Sstevel@tonic-gate 	uint8_t		fbtp_savedval;
657c478bd9Sstevel@tonic-gate 	uintptr_t	fbtp_roffset;
667c478bd9Sstevel@tonic-gate 	dtrace_id_t	fbtp_id;
677c478bd9Sstevel@tonic-gate 	char		*fbtp_name;
687c478bd9Sstevel@tonic-gate 	struct modctl	*fbtp_ctl;
697c478bd9Sstevel@tonic-gate 	int		fbtp_loadcnt;
707c478bd9Sstevel@tonic-gate 	int		fbtp_symndx;
717c478bd9Sstevel@tonic-gate 	int		fbtp_primary;
727c478bd9Sstevel@tonic-gate 	struct fbt_probe *fbtp_next;
737c478bd9Sstevel@tonic-gate } fbt_probe_t;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate static dev_info_t		*fbt_devi;
767c478bd9Sstevel@tonic-gate static dtrace_provider_id_t	fbt_id;
777c478bd9Sstevel@tonic-gate static fbt_probe_t		**fbt_probetab;
787c478bd9Sstevel@tonic-gate static int			fbt_probetab_size;
797c478bd9Sstevel@tonic-gate static int			fbt_probetab_mask;
807c478bd9Sstevel@tonic-gate static int			fbt_verbose = 0;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate static int
837c478bd9Sstevel@tonic-gate fbt_invop(uintptr_t addr, uintptr_t *stack, uintptr_t rval)
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate 	uintptr_t stack0, stack1, stack2, stack3, stack4;
867c478bd9Sstevel@tonic-gate 	fbt_probe_t *fbt = fbt_probetab[FBT_ADDR2NDX(addr)];
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	for (; fbt != NULL; fbt = fbt->fbtp_hashnext) {
897c478bd9Sstevel@tonic-gate 		if ((uintptr_t)fbt->fbtp_patchpoint == addr) {
907c478bd9Sstevel@tonic-gate 			if (fbt->fbtp_roffset == 0) {
917c478bd9Sstevel@tonic-gate 				int i = 0;
927c478bd9Sstevel@tonic-gate 				/*
937c478bd9Sstevel@tonic-gate 				 * When accessing the arguments on the stack,
947c478bd9Sstevel@tonic-gate 				 * we must protect against accessing beyond
957c478bd9Sstevel@tonic-gate 				 * the stack.  We can safely set NOFAULT here
967c478bd9Sstevel@tonic-gate 				 * -- we know that interrupts are already
977c478bd9Sstevel@tonic-gate 				 * disabled.
987c478bd9Sstevel@tonic-gate 				 */
997c478bd9Sstevel@tonic-gate 				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
1007c478bd9Sstevel@tonic-gate 				CPU->cpu_dtrace_caller = stack[i++];
1017c478bd9Sstevel@tonic-gate #ifdef __amd64
1027c478bd9Sstevel@tonic-gate 				/*
1037c478bd9Sstevel@tonic-gate 				 * On amd64, stack[0] contains the dereferenced
1047c478bd9Sstevel@tonic-gate 				 * stack pointer, stack[1] contains savfp,
1057c478bd9Sstevel@tonic-gate 				 * stack[2] contains savpc.  We want to step
1067c478bd9Sstevel@tonic-gate 				 * over these entries.
1077c478bd9Sstevel@tonic-gate 				 */
1087c478bd9Sstevel@tonic-gate 				i += 2;
1097c478bd9Sstevel@tonic-gate #endif
1107c478bd9Sstevel@tonic-gate 				stack0 = stack[i++];
1117c478bd9Sstevel@tonic-gate 				stack1 = stack[i++];
1127c478bd9Sstevel@tonic-gate 				stack2 = stack[i++];
1137c478bd9Sstevel@tonic-gate 				stack3 = stack[i++];
1147c478bd9Sstevel@tonic-gate 				stack4 = stack[i++];
1157c478bd9Sstevel@tonic-gate 				DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT |
1167c478bd9Sstevel@tonic-gate 				    CPU_DTRACE_BADADDR);
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 				dtrace_probe(fbt->fbtp_id, stack0, stack1,
1197c478bd9Sstevel@tonic-gate 				    stack2, stack3, stack4);
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 				CPU->cpu_dtrace_caller = NULL;
1227c478bd9Sstevel@tonic-gate 			} else {
1237c478bd9Sstevel@tonic-gate #ifdef __amd64
1247c478bd9Sstevel@tonic-gate 				/*
1257c478bd9Sstevel@tonic-gate 				 * On amd64, we instrument the ret, not the
1267c478bd9Sstevel@tonic-gate 				 * leave.  We therefore need to set the caller
1277c478bd9Sstevel@tonic-gate 				 * to assure that the top frame of a stack()
1287c478bd9Sstevel@tonic-gate 				 * action is correct.
1297c478bd9Sstevel@tonic-gate 				 */
1307c478bd9Sstevel@tonic-gate 				DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
1317c478bd9Sstevel@tonic-gate 				CPU->cpu_dtrace_caller = stack[0];
1327c478bd9Sstevel@tonic-gate 				DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT |
1337c478bd9Sstevel@tonic-gate 				    CPU_DTRACE_BADADDR);
1347c478bd9Sstevel@tonic-gate #endif
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 				dtrace_probe(fbt->fbtp_id, fbt->fbtp_roffset,
1377c478bd9Sstevel@tonic-gate 				    rval, 0, 0, 0);
1387c478bd9Sstevel@tonic-gate 				CPU->cpu_dtrace_caller = NULL;
1397c478bd9Sstevel@tonic-gate 			}
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 			return (fbt->fbtp_rval);
1427c478bd9Sstevel@tonic-gate 		}
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	return (0);
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1497c478bd9Sstevel@tonic-gate static void
1507c478bd9Sstevel@tonic-gate fbt_provide_module(void *arg, struct modctl *ctl)
1517c478bd9Sstevel@tonic-gate {
1527c478bd9Sstevel@tonic-gate 	struct module *mp = ctl->mod_mp;
1537c478bd9Sstevel@tonic-gate 	char *str = mp->strings;
1547c478bd9Sstevel@tonic-gate 	int nsyms = mp->nsyms;
1557c478bd9Sstevel@tonic-gate 	Shdr *symhdr = mp->symhdr;
1567c478bd9Sstevel@tonic-gate 	char *modname = ctl->mod_modname;
1577c478bd9Sstevel@tonic-gate 	char *name;
1587c478bd9Sstevel@tonic-gate 	fbt_probe_t *fbt, *retfbt;
1597c478bd9Sstevel@tonic-gate 	size_t symsize;
1607c478bd9Sstevel@tonic-gate 	int i, size;
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	/*
1637c478bd9Sstevel@tonic-gate 	 * Employees of dtrace and their families are ineligible.  Void
1647c478bd9Sstevel@tonic-gate 	 * where prohibited.
1657c478bd9Sstevel@tonic-gate 	 */
1667c478bd9Sstevel@tonic-gate 	if (strcmp(modname, "dtrace") == 0)
1677c478bd9Sstevel@tonic-gate 		return;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	if (ctl->mod_requisites != NULL) {
1707c478bd9Sstevel@tonic-gate 		struct modctl_list *list;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 		list = (struct modctl_list *)ctl->mod_requisites;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 		for (; list != NULL; list = list->modl_next) {
1757c478bd9Sstevel@tonic-gate 			if (strcmp(list->modl_modp->mod_modname, "dtrace") == 0)
1767c478bd9Sstevel@tonic-gate 				return;
1777c478bd9Sstevel@tonic-gate 		}
1787c478bd9Sstevel@tonic-gate 	}
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	/*
1817c478bd9Sstevel@tonic-gate 	 * KMDB is ineligible for instrumentation -- it may execute in
1827c478bd9Sstevel@tonic-gate 	 * any context, including probe context.
1837c478bd9Sstevel@tonic-gate 	 */
1847c478bd9Sstevel@tonic-gate 	if (strcmp(modname, "kmdbmod") == 0)
1857c478bd9Sstevel@tonic-gate 		return;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	if (str == NULL || symhdr == NULL || symhdr->sh_addr == NULL) {
1887c478bd9Sstevel@tonic-gate 		/*
1897c478bd9Sstevel@tonic-gate 		 * If this module doesn't (yet) have its string or symbol
1907c478bd9Sstevel@tonic-gate 		 * table allocated, clear out.
1917c478bd9Sstevel@tonic-gate 		 */
1927c478bd9Sstevel@tonic-gate 		return;
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	symsize = symhdr->sh_entsize;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	if (mp->fbt_nentries) {
1987c478bd9Sstevel@tonic-gate 		/*
1997c478bd9Sstevel@tonic-gate 		 * This module has some FBT entries allocated; we're afraid
2007c478bd9Sstevel@tonic-gate 		 * to screw with it.
2017c478bd9Sstevel@tonic-gate 		 */
2027c478bd9Sstevel@tonic-gate 		return;
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	for (i = 1; i < nsyms; i++) {
2067c478bd9Sstevel@tonic-gate 		uint8_t *instr, *limit;
2077c478bd9Sstevel@tonic-gate 		Sym *sym = (Sym *)(symhdr->sh_addr + i * symsize);
208b365acd0Sbmc #ifdef __amd64
209b365acd0Sbmc 		int j;
210b365acd0Sbmc #endif
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 		if (ELF_ST_TYPE(sym->st_info) != STT_FUNC)
2137c478bd9Sstevel@tonic-gate 			continue;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 		/*
2167c478bd9Sstevel@tonic-gate 		 * Weak symbols are not candidates.  This could be made to
2177c478bd9Sstevel@tonic-gate 		 * work (where weak functions and their underlying function
2187c478bd9Sstevel@tonic-gate 		 * appear as two disjoint probes), but it's not simple.
2197c478bd9Sstevel@tonic-gate 		 */
2207c478bd9Sstevel@tonic-gate 		if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
2217c478bd9Sstevel@tonic-gate 			continue;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 		name = str + sym->st_name;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 		if (strstr(name, "dtrace_") == name &&
2267c478bd9Sstevel@tonic-gate 		    strstr(name, "dtrace_safe_") != name) {
2277c478bd9Sstevel@tonic-gate 			/*
2287c478bd9Sstevel@tonic-gate 			 * Anything beginning with "dtrace_" may be called
2297c478bd9Sstevel@tonic-gate 			 * from probe context unless it explitly indicates
2307c478bd9Sstevel@tonic-gate 			 * that it won't be called from probe context by
2317c478bd9Sstevel@tonic-gate 			 * using the prefix "dtrace_safe_".
2327c478bd9Sstevel@tonic-gate 			 */
2337c478bd9Sstevel@tonic-gate 			continue;
2347c478bd9Sstevel@tonic-gate 		}
2357c478bd9Sstevel@tonic-gate 
236a1b5e537Sbmc 		if (strstr(name, "kdi_") == name ||
237a1b5e537Sbmc 		    strstr(name, "_kdi_") != NULL) {
2387c478bd9Sstevel@tonic-gate 			/*
239a1b5e537Sbmc 			 * Any function name beginning with "kdi_" or
240a1b5e537Sbmc 			 * containing the string "_kdi_" is a part of the
2417c478bd9Sstevel@tonic-gate 			 * kernel debugger interface and may be called in
2427c478bd9Sstevel@tonic-gate 			 * arbitrary context -- including probe context.
2437c478bd9Sstevel@tonic-gate 			 */
2447c478bd9Sstevel@tonic-gate 			continue;
2457c478bd9Sstevel@tonic-gate 		}
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 		/*
2487c478bd9Sstevel@tonic-gate 		 * Due to 4524008, _init and _fini may have a bloated st_size.
2497c478bd9Sstevel@tonic-gate 		 * While this bug was fixed quite some time ago, old drivers
2507c478bd9Sstevel@tonic-gate 		 * may be lurking.  We need to develop a better solution to
2517c478bd9Sstevel@tonic-gate 		 * this problem, such that correct _init and _fini functions
2527c478bd9Sstevel@tonic-gate 		 * (the vast majority) may be correctly traced.  One solution
2537c478bd9Sstevel@tonic-gate 		 * may be to scan through the entire symbol table to see if
2547c478bd9Sstevel@tonic-gate 		 * any symbol overlaps with _init.  If none does, set a bit in
2557c478bd9Sstevel@tonic-gate 		 * the module structure that this module has correct _init and
2567c478bd9Sstevel@tonic-gate 		 * _fini sizes.  This will cause some pain the first time a
2577c478bd9Sstevel@tonic-gate 		 * module is scanned, but at least it would be O(N) instead of
2587c478bd9Sstevel@tonic-gate 		 * O(N log N)...
2597c478bd9Sstevel@tonic-gate 		 */
2607c478bd9Sstevel@tonic-gate 		if (strcmp(name, "_init") == 0)
2617c478bd9Sstevel@tonic-gate 			continue;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 		if (strcmp(name, "_fini") == 0)
2647c478bd9Sstevel@tonic-gate 			continue;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 		/*
2677c478bd9Sstevel@tonic-gate 		 * In order to be eligible, the function must begin with the
2687c478bd9Sstevel@tonic-gate 		 * following sequence:
2697c478bd9Sstevel@tonic-gate 		 *
2707c478bd9Sstevel@tonic-gate 		 * 	pushl	%esp
2717c478bd9Sstevel@tonic-gate 		 *	movl	%esp, %ebp
2727c478bd9Sstevel@tonic-gate 		 *
2737c478bd9Sstevel@tonic-gate 		 * Note that there are two variants of encodings that generate
2747c478bd9Sstevel@tonic-gate 		 * the movl; we must check for both.  For 64-bit, we would
2757c478bd9Sstevel@tonic-gate 		 * normally insist that a function begin with the following
2767c478bd9Sstevel@tonic-gate 		 * sequence:
2777c478bd9Sstevel@tonic-gate 		 *
2787c478bd9Sstevel@tonic-gate 		 *	pushq	%rbp
2797c478bd9Sstevel@tonic-gate 		 *	movq	%rsp, %rbp
2807c478bd9Sstevel@tonic-gate 		 *
2817c478bd9Sstevel@tonic-gate 		 * However, the compiler for 64-bit often splits these two
2827c478bd9Sstevel@tonic-gate 		 * instructions -- and the first instruction in the function
2837c478bd9Sstevel@tonic-gate 		 * is often not the pushq.  As a result, on 64-bit we look
2847c478bd9Sstevel@tonic-gate 		 * for any "pushq %rbp" in the function and we instrument
2857c478bd9Sstevel@tonic-gate 		 * this with a breakpoint instruction.
2867c478bd9Sstevel@tonic-gate 		 */
2877c478bd9Sstevel@tonic-gate 		instr = (uint8_t *)sym->st_value;
2887c478bd9Sstevel@tonic-gate 		limit = (uint8_t *)(sym->st_value + sym->st_size);
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate #ifdef __amd64
2917c478bd9Sstevel@tonic-gate 		while (instr < limit) {
2927c478bd9Sstevel@tonic-gate 			if (*instr == FBT_PUSHL_EBP)
2937c478bd9Sstevel@tonic-gate 				break;
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 			if ((size = dtrace_instr_size(instr)) <= 0)
2967c478bd9Sstevel@tonic-gate 				break;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 			instr += size;
2997c478bd9Sstevel@tonic-gate 		}
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 		if (instr >= limit || *instr != FBT_PUSHL_EBP) {
3027c478bd9Sstevel@tonic-gate 			/*
3037c478bd9Sstevel@tonic-gate 			 * We either don't save the frame pointer in this
3047c478bd9Sstevel@tonic-gate 			 * function, or we ran into some disassembly
3057c478bd9Sstevel@tonic-gate 			 * screw-up.  Either way, we bail.
3067c478bd9Sstevel@tonic-gate 			 */
3077c478bd9Sstevel@tonic-gate 			continue;
3087c478bd9Sstevel@tonic-gate 		}
3097c478bd9Sstevel@tonic-gate #else
3107c478bd9Sstevel@tonic-gate 		if (instr[0] != FBT_PUSHL_EBP)
3117c478bd9Sstevel@tonic-gate 			continue;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 		if (!(instr[1] == FBT_MOVL_ESP_EBP0_V0 &&
3147c478bd9Sstevel@tonic-gate 		    instr[2] == FBT_MOVL_ESP_EBP1_V0) &&
3157c478bd9Sstevel@tonic-gate 		    !(instr[1] == FBT_MOVL_ESP_EBP0_V1 &&
3167c478bd9Sstevel@tonic-gate 		    instr[2] == FBT_MOVL_ESP_EBP1_V1))
3177c478bd9Sstevel@tonic-gate 			continue;
3187c478bd9Sstevel@tonic-gate #endif
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 		fbt = kmem_zalloc(sizeof (fbt_probe_t), KM_SLEEP);
3217c478bd9Sstevel@tonic-gate 		fbt->fbtp_name = name;
3227c478bd9Sstevel@tonic-gate 		fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
3237c478bd9Sstevel@tonic-gate 		    name, FBT_ENTRY, 3, fbt);
3247c478bd9Sstevel@tonic-gate 		fbt->fbtp_patchpoint = instr;
3257c478bd9Sstevel@tonic-gate 		fbt->fbtp_ctl = ctl;
3267c478bd9Sstevel@tonic-gate 		fbt->fbtp_loadcnt = ctl->mod_loadcnt;
3277c478bd9Sstevel@tonic-gate 		fbt->fbtp_rval = DTRACE_INVOP_PUSHL_EBP;
3287c478bd9Sstevel@tonic-gate 		fbt->fbtp_savedval = *instr;
3297c478bd9Sstevel@tonic-gate 		fbt->fbtp_patchval = FBT_PATCHVAL;
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 		fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
3327c478bd9Sstevel@tonic-gate 		fbt->fbtp_symndx = i;
3337c478bd9Sstevel@tonic-gate 		fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 		mp->fbt_nentries++;
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 		retfbt = NULL;
3387c478bd9Sstevel@tonic-gate again:
3397c478bd9Sstevel@tonic-gate 		if (instr >= limit)
3407c478bd9Sstevel@tonic-gate 			continue;
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 		/*
3437c478bd9Sstevel@tonic-gate 		 * If this disassembly fails, then we've likely walked off into
3447c478bd9Sstevel@tonic-gate 		 * a jump table or some other unsuitable area.  Bail out of the
3457c478bd9Sstevel@tonic-gate 		 * disassembly now.
3467c478bd9Sstevel@tonic-gate 		 */
3477c478bd9Sstevel@tonic-gate 		if ((size = dtrace_instr_size(instr)) <= 0)
3487c478bd9Sstevel@tonic-gate 			continue;
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate #ifdef __amd64
3517c478bd9Sstevel@tonic-gate 		/*
3527c478bd9Sstevel@tonic-gate 		 * We only instrument "ret" on amd64 -- we don't yet instrument
3537c478bd9Sstevel@tonic-gate 		 * ret imm16, largely because the compiler doesn't seem to
3547c478bd9Sstevel@tonic-gate 		 * (yet) emit them in the kernel...
3557c478bd9Sstevel@tonic-gate 		 */
3567c478bd9Sstevel@tonic-gate 		if (*instr != FBT_RET) {
3577c478bd9Sstevel@tonic-gate 			instr += size;
3587c478bd9Sstevel@tonic-gate 			goto again;
3597c478bd9Sstevel@tonic-gate 		}
360b365acd0Sbmc 
361b365acd0Sbmc 		/*
362b365acd0Sbmc 		 * Because we are only looking for a one-byte marker here,
363b365acd0Sbmc 		 * there is an increased likelihood of erroneously interpreting
364b365acd0Sbmc 		 * a jump table to be an instrumentable instruction.  We
365b365acd0Sbmc 		 * obviously want to avoid that, so we resort to some heuristic
366b365acd0Sbmc 		 * sleeze:  we'll treat this instruction as being contained
367b365acd0Sbmc 		 * within a pointer, and see if that pointer points to within
368b365acd0Sbmc 		 * the body of the function.  If it does, we refuse to
369b365acd0Sbmc 		 * instrument it.
370b365acd0Sbmc 		 */
371b365acd0Sbmc 		for (j = 0; j < sizeof (uintptr_t); j++) {
372b365acd0Sbmc 			uintptr_t check = (uintptr_t)instr - j;
373b365acd0Sbmc 			uint8_t *ptr;
374b365acd0Sbmc 
375b365acd0Sbmc 			if (check < sym->st_value)
376b365acd0Sbmc 				break;
377b365acd0Sbmc 
378b365acd0Sbmc 			if (check + sizeof (uintptr_t) > (uintptr_t)limit)
379b365acd0Sbmc 				continue;
380b365acd0Sbmc 
381b365acd0Sbmc 			ptr = *(uint8_t **)check;
382b365acd0Sbmc 
383b365acd0Sbmc 			if (ptr >= (uint8_t *)sym->st_value && ptr < limit) {
384b365acd0Sbmc 				instr += size;
385b365acd0Sbmc 				goto again;
386b365acd0Sbmc 			}
387b365acd0Sbmc 		}
3887c478bd9Sstevel@tonic-gate #else
3897c478bd9Sstevel@tonic-gate 		if (!(size == 1 &&
3907c478bd9Sstevel@tonic-gate 		    (*instr == FBT_POPL_EBP || *instr == FBT_LEAVE) &&
3917c478bd9Sstevel@tonic-gate 		    (*(instr + 1) == FBT_RET ||
3927c478bd9Sstevel@tonic-gate 		    *(instr + 1) == FBT_RET_IMM16))) {
3937c478bd9Sstevel@tonic-gate 			instr += size;
3947c478bd9Sstevel@tonic-gate 			goto again;
3957c478bd9Sstevel@tonic-gate 		}
3967c478bd9Sstevel@tonic-gate #endif
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 		/*
3997c478bd9Sstevel@tonic-gate 		 * We have a winner!
4007c478bd9Sstevel@tonic-gate 		 */
4017c478bd9Sstevel@tonic-gate 		fbt = kmem_zalloc(sizeof (fbt_probe_t), KM_SLEEP);
4027c478bd9Sstevel@tonic-gate 		fbt->fbtp_name = name;
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 		if (retfbt == NULL) {
4057c478bd9Sstevel@tonic-gate 			fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
4067c478bd9Sstevel@tonic-gate 			    name, FBT_RETURN, 3, fbt);
4077c478bd9Sstevel@tonic-gate 		} else {
4087c478bd9Sstevel@tonic-gate 			retfbt->fbtp_next = fbt;
4097c478bd9Sstevel@tonic-gate 			fbt->fbtp_id = retfbt->fbtp_id;
4107c478bd9Sstevel@tonic-gate 		}
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 		retfbt = fbt;
4137c478bd9Sstevel@tonic-gate 		fbt->fbtp_patchpoint = instr;
4147c478bd9Sstevel@tonic-gate 		fbt->fbtp_ctl = ctl;
4157c478bd9Sstevel@tonic-gate 		fbt->fbtp_loadcnt = ctl->mod_loadcnt;
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate #ifndef __amd64
4187c478bd9Sstevel@tonic-gate 		if (*instr == FBT_POPL_EBP) {
4197c478bd9Sstevel@tonic-gate 			fbt->fbtp_rval = DTRACE_INVOP_POPL_EBP;
4207c478bd9Sstevel@tonic-gate 		} else {
4217c478bd9Sstevel@tonic-gate 			ASSERT(*instr == FBT_LEAVE);
4227c478bd9Sstevel@tonic-gate 			fbt->fbtp_rval = DTRACE_INVOP_LEAVE;
4237c478bd9Sstevel@tonic-gate 		}
4247c478bd9Sstevel@tonic-gate 		fbt->fbtp_roffset =
4257c478bd9Sstevel@tonic-gate 		    (uintptr_t)(instr - (uint8_t *)sym->st_value) + 1;
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate #else
4287c478bd9Sstevel@tonic-gate 		ASSERT(*instr == FBT_RET);
4297c478bd9Sstevel@tonic-gate 		fbt->fbtp_rval = DTRACE_INVOP_RET;
4307c478bd9Sstevel@tonic-gate 		fbt->fbtp_roffset =
4317c478bd9Sstevel@tonic-gate 		    (uintptr_t)(instr - (uint8_t *)sym->st_value);
4327c478bd9Sstevel@tonic-gate #endif
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 		fbt->fbtp_savedval = *instr;
4357c478bd9Sstevel@tonic-gate 		fbt->fbtp_patchval = FBT_PATCHVAL;
4367c478bd9Sstevel@tonic-gate 		fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
4377c478bd9Sstevel@tonic-gate 		fbt->fbtp_symndx = i;
4387c478bd9Sstevel@tonic-gate 		fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 		mp->fbt_nentries++;
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 		instr += size;
4437c478bd9Sstevel@tonic-gate 		goto again;
4447c478bd9Sstevel@tonic-gate 	}
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4487c478bd9Sstevel@tonic-gate static void
4497c478bd9Sstevel@tonic-gate fbt_destroy(void *arg, dtrace_id_t id, void *parg)
4507c478bd9Sstevel@tonic-gate {
4517c478bd9Sstevel@tonic-gate 	fbt_probe_t *fbt = parg, *next, *hash, *last;
4527c478bd9Sstevel@tonic-gate 	struct modctl *ctl = fbt->fbtp_ctl;
4537c478bd9Sstevel@tonic-gate 	int ndx;
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	do {
4567c478bd9Sstevel@tonic-gate 		if (ctl != NULL && ctl->mod_loadcnt == fbt->fbtp_loadcnt) {
4577c478bd9Sstevel@tonic-gate 			if ((ctl->mod_loadcnt == fbt->fbtp_loadcnt &&
4587c478bd9Sstevel@tonic-gate 			    ctl->mod_loaded)) {
4597c478bd9Sstevel@tonic-gate 				((struct module *)
4607c478bd9Sstevel@tonic-gate 				    (ctl->mod_mp))->fbt_nentries--;
4617c478bd9Sstevel@tonic-gate 			}
4627c478bd9Sstevel@tonic-gate 		}
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 		/*
4657c478bd9Sstevel@tonic-gate 		 * Now we need to remove this probe from the fbt_probetab.
4667c478bd9Sstevel@tonic-gate 		 */
4677c478bd9Sstevel@tonic-gate 		ndx = FBT_ADDR2NDX(fbt->fbtp_patchpoint);
4687c478bd9Sstevel@tonic-gate 		last = NULL;
4697c478bd9Sstevel@tonic-gate 		hash = fbt_probetab[ndx];
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 		while (hash != fbt) {
4727c478bd9Sstevel@tonic-gate 			ASSERT(hash != NULL);
4737c478bd9Sstevel@tonic-gate 			last = hash;
4747c478bd9Sstevel@tonic-gate 			hash = hash->fbtp_hashnext;
4757c478bd9Sstevel@tonic-gate 		}
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 		if (last != NULL) {
4787c478bd9Sstevel@tonic-gate 			last->fbtp_hashnext = fbt->fbtp_hashnext;
4797c478bd9Sstevel@tonic-gate 		} else {
4807c478bd9Sstevel@tonic-gate 			fbt_probetab[ndx] = fbt->fbtp_hashnext;
4817c478bd9Sstevel@tonic-gate 		}
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 		next = fbt->fbtp_next;
4847c478bd9Sstevel@tonic-gate 		kmem_free(fbt, sizeof (fbt_probe_t));
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 		fbt = next;
4877c478bd9Sstevel@tonic-gate 	} while (fbt != NULL);
4887c478bd9Sstevel@tonic-gate }
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4917c478bd9Sstevel@tonic-gate static void
4927c478bd9Sstevel@tonic-gate fbt_enable(void *arg, dtrace_id_t id, void *parg)
4937c478bd9Sstevel@tonic-gate {
4947c478bd9Sstevel@tonic-gate 	fbt_probe_t *fbt = parg;
4957c478bd9Sstevel@tonic-gate 	struct modctl *ctl = fbt->fbtp_ctl;
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	ctl->mod_nenabled++;
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	if (!ctl->mod_loaded) {
5007c478bd9Sstevel@tonic-gate 		if (fbt_verbose) {
5017c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "fbt is failing for probe %s "
5027c478bd9Sstevel@tonic-gate 			    "(module %s unloaded)",
5037c478bd9Sstevel@tonic-gate 			    fbt->fbtp_name, ctl->mod_modname);
5047c478bd9Sstevel@tonic-gate 		}
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 		return;
5077c478bd9Sstevel@tonic-gate 	}
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	/*
5107c478bd9Sstevel@tonic-gate 	 * Now check that our modctl has the expected load count.  If it
5117c478bd9Sstevel@tonic-gate 	 * doesn't, this module must have been unloaded and reloaded -- and
5127c478bd9Sstevel@tonic-gate 	 * we're not going to touch it.
5137c478bd9Sstevel@tonic-gate 	 */
5147c478bd9Sstevel@tonic-gate 	if (ctl->mod_loadcnt != fbt->fbtp_loadcnt) {
5157c478bd9Sstevel@tonic-gate 		if (fbt_verbose) {
5167c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "fbt is failing for probe %s "
5177c478bd9Sstevel@tonic-gate 			    "(module %s reloaded)",
5187c478bd9Sstevel@tonic-gate 			    fbt->fbtp_name, ctl->mod_modname);
5197c478bd9Sstevel@tonic-gate 		}
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 		return;
5227c478bd9Sstevel@tonic-gate 	}
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	for (; fbt != NULL; fbt = fbt->fbtp_next)
5257c478bd9Sstevel@tonic-gate 		*fbt->fbtp_patchpoint = fbt->fbtp_patchval;
5267c478bd9Sstevel@tonic-gate }
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5297c478bd9Sstevel@tonic-gate static void
5307c478bd9Sstevel@tonic-gate fbt_disable(void *arg, dtrace_id_t id, void *parg)
5317c478bd9Sstevel@tonic-gate {
5327c478bd9Sstevel@tonic-gate 	fbt_probe_t *fbt = parg;
5337c478bd9Sstevel@tonic-gate 	struct modctl *ctl = fbt->fbtp_ctl;
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	ASSERT(ctl->mod_nenabled > 0);
5367c478bd9Sstevel@tonic-gate 	ctl->mod_nenabled--;
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	if (!ctl->mod_loaded || (ctl->mod_loadcnt != fbt->fbtp_loadcnt))
5397c478bd9Sstevel@tonic-gate 		return;
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	for (; fbt != NULL; fbt = fbt->fbtp_next)
5427c478bd9Sstevel@tonic-gate 		*fbt->fbtp_patchpoint = fbt->fbtp_savedval;
5437c478bd9Sstevel@tonic-gate }
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5467c478bd9Sstevel@tonic-gate static void
5477c478bd9Sstevel@tonic-gate fbt_suspend(void *arg, dtrace_id_t id, void *parg)
5487c478bd9Sstevel@tonic-gate {
5497c478bd9Sstevel@tonic-gate 	fbt_probe_t *fbt = parg;
5507c478bd9Sstevel@tonic-gate 	struct modctl *ctl = fbt->fbtp_ctl;
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	ASSERT(ctl->mod_nenabled > 0);
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	if (!ctl->mod_loaded || (ctl->mod_loadcnt != fbt->fbtp_loadcnt))
5557c478bd9Sstevel@tonic-gate 		return;
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	for (; fbt != NULL; fbt = fbt->fbtp_next)
5587c478bd9Sstevel@tonic-gate 		*fbt->fbtp_patchpoint = fbt->fbtp_savedval;
5597c478bd9Sstevel@tonic-gate }
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5627c478bd9Sstevel@tonic-gate static void
5637c478bd9Sstevel@tonic-gate fbt_resume(void *arg, dtrace_id_t id, void *parg)
5647c478bd9Sstevel@tonic-gate {
5657c478bd9Sstevel@tonic-gate 	fbt_probe_t *fbt = parg;
5667c478bd9Sstevel@tonic-gate 	struct modctl *ctl = fbt->fbtp_ctl;
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	ASSERT(ctl->mod_nenabled > 0);
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	if (!ctl->mod_loaded || (ctl->mod_loadcnt != fbt->fbtp_loadcnt))
5717c478bd9Sstevel@tonic-gate 		return;
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	for (; fbt != NULL; fbt = fbt->fbtp_next)
5747c478bd9Sstevel@tonic-gate 		*fbt->fbtp_patchpoint = fbt->fbtp_patchval;
5757c478bd9Sstevel@tonic-gate }
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5787c478bd9Sstevel@tonic-gate static void
5797c478bd9Sstevel@tonic-gate fbt_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc)
5807c478bd9Sstevel@tonic-gate {
5817c478bd9Sstevel@tonic-gate 	fbt_probe_t *fbt = parg;
5827c478bd9Sstevel@tonic-gate 	struct modctl *ctl = fbt->fbtp_ctl;
5837c478bd9Sstevel@tonic-gate 	struct module *mp = ctl->mod_mp;
5847c478bd9Sstevel@tonic-gate 	ctf_file_t *fp = NULL, *pfp;
5857c478bd9Sstevel@tonic-gate 	ctf_funcinfo_t f;
5867c478bd9Sstevel@tonic-gate 	int error;
5877c478bd9Sstevel@tonic-gate 	ctf_id_t argv[32], type;
5887c478bd9Sstevel@tonic-gate 	int argc = sizeof (argv) / sizeof (ctf_id_t);
5897c478bd9Sstevel@tonic-gate 	const char *parent;
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	if (!ctl->mod_loaded || (ctl->mod_loadcnt != fbt->fbtp_loadcnt))
5927c478bd9Sstevel@tonic-gate 		goto err;
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	if (fbt->fbtp_roffset != 0 && desc->dtargd_ndx == 0) {
5957c478bd9Sstevel@tonic-gate 		(void) strcpy(desc->dtargd_native, "int");
5967c478bd9Sstevel@tonic-gate 		return;
5977c478bd9Sstevel@tonic-gate 	}
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 	if ((fp = ctf_modopen(mp, &error)) == NULL) {
6007c478bd9Sstevel@tonic-gate 		/*
6017c478bd9Sstevel@tonic-gate 		 * We have no CTF information for this module -- and therefore
6027c478bd9Sstevel@tonic-gate 		 * no args[] information.
6037c478bd9Sstevel@tonic-gate 		 */
6047c478bd9Sstevel@tonic-gate 		goto err;
6057c478bd9Sstevel@tonic-gate 	}
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	/*
6087c478bd9Sstevel@tonic-gate 	 * If we have a parent container, we must manually import it.
6097c478bd9Sstevel@tonic-gate 	 */
6107c478bd9Sstevel@tonic-gate 	if ((parent = ctf_parent_name(fp)) != NULL) {
6117c478bd9Sstevel@tonic-gate 		struct modctl *mod;
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 		/*
6147c478bd9Sstevel@tonic-gate 		 * We must iterate over all modules to find the module that
6157c478bd9Sstevel@tonic-gate 		 * is our parent.
6167c478bd9Sstevel@tonic-gate 		 */
6177c478bd9Sstevel@tonic-gate 		for (mod = &modules; mod != NULL; mod = mod->mod_next) {
6187c478bd9Sstevel@tonic-gate 			if (strcmp(mod->mod_filename, parent) == 0)
6197c478bd9Sstevel@tonic-gate 				break;
6207c478bd9Sstevel@tonic-gate 		}
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 		if (mod == NULL)
6237c478bd9Sstevel@tonic-gate 			goto err;
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 		if ((pfp = ctf_modopen(mod->mod_mp, &error)) == NULL)
6267c478bd9Sstevel@tonic-gate 			goto err;
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 		if (ctf_import(fp, pfp) != 0) {
6297c478bd9Sstevel@tonic-gate 			ctf_close(pfp);
6307c478bd9Sstevel@tonic-gate 			goto err;
6317c478bd9Sstevel@tonic-gate 		}
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 		ctf_close(pfp);
6347c478bd9Sstevel@tonic-gate 	}
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	if (ctf_func_info(fp, fbt->fbtp_symndx, &f) == CTF_ERR)
6377c478bd9Sstevel@tonic-gate 		goto err;
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 	if (fbt->fbtp_roffset != 0) {
6407c478bd9Sstevel@tonic-gate 		if (desc->dtargd_ndx > 1)
6417c478bd9Sstevel@tonic-gate 			goto err;
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 		ASSERT(desc->dtargd_ndx == 1);
6447c478bd9Sstevel@tonic-gate 		type = f.ctc_return;
6457c478bd9Sstevel@tonic-gate 	} else {
6467c478bd9Sstevel@tonic-gate 		if (desc->dtargd_ndx + 1 > f.ctc_argc)
6477c478bd9Sstevel@tonic-gate 			goto err;
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 		if (ctf_func_args(fp, fbt->fbtp_symndx, argc, argv) == CTF_ERR)
6507c478bd9Sstevel@tonic-gate 			goto err;
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 		type = argv[desc->dtargd_ndx];
6537c478bd9Sstevel@tonic-gate 	}
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	if (ctf_type_name(fp, type, desc->dtargd_native,
6567c478bd9Sstevel@tonic-gate 	    DTRACE_ARGTYPELEN) != NULL) {
6577c478bd9Sstevel@tonic-gate 		ctf_close(fp);
6587c478bd9Sstevel@tonic-gate 		return;
6597c478bd9Sstevel@tonic-gate 	}
6607c478bd9Sstevel@tonic-gate err:
6617c478bd9Sstevel@tonic-gate 	if (fp != NULL)
6627c478bd9Sstevel@tonic-gate 		ctf_close(fp);
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	desc->dtargd_ndx = DTRACE_ARGNONE;
6657c478bd9Sstevel@tonic-gate }
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate static dtrace_pattr_t fbt_attr = {
6687c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
6697c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
6707c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
6717c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
6727c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
6737c478bd9Sstevel@tonic-gate };
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate static dtrace_pops_t fbt_pops = {
6767c478bd9Sstevel@tonic-gate 	NULL,
6777c478bd9Sstevel@tonic-gate 	fbt_provide_module,
6787c478bd9Sstevel@tonic-gate 	fbt_enable,
6797c478bd9Sstevel@tonic-gate 	fbt_disable,
6807c478bd9Sstevel@tonic-gate 	fbt_suspend,
6817c478bd9Sstevel@tonic-gate 	fbt_resume,
6827c478bd9Sstevel@tonic-gate 	fbt_getargdesc,
6837c478bd9Sstevel@tonic-gate 	NULL,
6847c478bd9Sstevel@tonic-gate 	NULL,
6857c478bd9Sstevel@tonic-gate 	fbt_destroy
6867c478bd9Sstevel@tonic-gate };
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate static void
6897c478bd9Sstevel@tonic-gate fbt_cleanup(dev_info_t *devi)
6907c478bd9Sstevel@tonic-gate {
6917c478bd9Sstevel@tonic-gate 	dtrace_invop_remove(fbt_invop);
6927c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
6937c478bd9Sstevel@tonic-gate 	kmem_free(fbt_probetab, fbt_probetab_size * sizeof (fbt_probe_t *));
6947c478bd9Sstevel@tonic-gate 	fbt_probetab = NULL;
6957c478bd9Sstevel@tonic-gate 	fbt_probetab_mask = 0;
6967c478bd9Sstevel@tonic-gate }
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate static int
6997c478bd9Sstevel@tonic-gate fbt_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
7007c478bd9Sstevel@tonic-gate {
7017c478bd9Sstevel@tonic-gate 	switch (cmd) {
7027c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
7037c478bd9Sstevel@tonic-gate 		break;
7047c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
7057c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
7067c478bd9Sstevel@tonic-gate 	default:
7077c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
7087c478bd9Sstevel@tonic-gate 	}
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	if (fbt_probetab_size == 0)
7117c478bd9Sstevel@tonic-gate 		fbt_probetab_size = FBT_PROBETAB_SIZE;
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	fbt_probetab_mask = fbt_probetab_size - 1;
7147c478bd9Sstevel@tonic-gate 	fbt_probetab =
7157c478bd9Sstevel@tonic-gate 	    kmem_zalloc(fbt_probetab_size * sizeof (fbt_probe_t *), KM_SLEEP);
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 	dtrace_invop_add(fbt_invop);
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "fbt", S_IFCHR, 0,
7207c478bd9Sstevel@tonic-gate 	    DDI_PSEUDO, NULL) == DDI_FAILURE ||
721*ad4023c4Sdp 	    dtrace_register("fbt", &fbt_attr, DTRACE_PRIV_KERNEL, NULL,
7227c478bd9Sstevel@tonic-gate 	    &fbt_pops, NULL, &fbt_id) != 0) {
7237c478bd9Sstevel@tonic-gate 		fbt_cleanup(devi);
7247c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
7257c478bd9Sstevel@tonic-gate 	}
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 	ddi_report_dev(devi);
7287c478bd9Sstevel@tonic-gate 	fbt_devi = devi;
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
7317c478bd9Sstevel@tonic-gate }
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate static int
7347c478bd9Sstevel@tonic-gate fbt_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
7357c478bd9Sstevel@tonic-gate {
7367c478bd9Sstevel@tonic-gate 	switch (cmd) {
7377c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
7387c478bd9Sstevel@tonic-gate 		break;
7397c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
7407c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
7417c478bd9Sstevel@tonic-gate 	default:
7427c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
7437c478bd9Sstevel@tonic-gate 	}
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 	if (dtrace_unregister(fbt_id) != 0)
7467c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	fbt_cleanup(devi);
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
7517c478bd9Sstevel@tonic-gate }
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7547c478bd9Sstevel@tonic-gate static int
7557c478bd9Sstevel@tonic-gate fbt_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
7567c478bd9Sstevel@tonic-gate {
7577c478bd9Sstevel@tonic-gate 	int error;
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	switch (infocmd) {
7607c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
7617c478bd9Sstevel@tonic-gate 		*result = (void *)fbt_devi;
7627c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
7637c478bd9Sstevel@tonic-gate 		break;
7647c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
7657c478bd9Sstevel@tonic-gate 		*result = (void *)0;
7667c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
7677c478bd9Sstevel@tonic-gate 		break;
7687c478bd9Sstevel@tonic-gate 	default:
7697c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
7707c478bd9Sstevel@tonic-gate 	}
7717c478bd9Sstevel@tonic-gate 	return (error);
7727c478bd9Sstevel@tonic-gate }
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7757c478bd9Sstevel@tonic-gate static int
7767c478bd9Sstevel@tonic-gate fbt_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
7777c478bd9Sstevel@tonic-gate {
7787c478bd9Sstevel@tonic-gate 	return (0);
7797c478bd9Sstevel@tonic-gate }
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate static struct cb_ops fbt_cb_ops = {
7827c478bd9Sstevel@tonic-gate 	fbt_open,		/* open */
7837c478bd9Sstevel@tonic-gate 	nodev,			/* close */
7847c478bd9Sstevel@tonic-gate 	nulldev,		/* strategy */
7857c478bd9Sstevel@tonic-gate 	nulldev,		/* print */
7867c478bd9Sstevel@tonic-gate 	nodev,			/* dump */
7877c478bd9Sstevel@tonic-gate 	nodev,			/* read */
7887c478bd9Sstevel@tonic-gate 	nodev,			/* write */
7897c478bd9Sstevel@tonic-gate 	nodev,			/* ioctl */
7907c478bd9Sstevel@tonic-gate 	nodev,			/* devmap */
7917c478bd9Sstevel@tonic-gate 	nodev,			/* mmap */
7927c478bd9Sstevel@tonic-gate 	nodev,			/* segmap */
7937c478bd9Sstevel@tonic-gate 	nochpoll,		/* poll */
7947c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
7957c478bd9Sstevel@tonic-gate 	0,			/* streamtab  */
7967c478bd9Sstevel@tonic-gate 	D_NEW | D_MP		/* Driver compatibility flag */
7977c478bd9Sstevel@tonic-gate };
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate static struct dev_ops fbt_ops = {
8007c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
8017c478bd9Sstevel@tonic-gate 	0,			/* refcnt */
8027c478bd9Sstevel@tonic-gate 	fbt_info,		/* get_dev_info */
8037c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
8047c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
8057c478bd9Sstevel@tonic-gate 	fbt_attach,		/* attach */
8067c478bd9Sstevel@tonic-gate 	fbt_detach,		/* detach */
8077c478bd9Sstevel@tonic-gate 	nodev,			/* reset */
8087c478bd9Sstevel@tonic-gate 	&fbt_cb_ops,		/* driver operations */
8097c478bd9Sstevel@tonic-gate 	NULL,			/* bus operations */
8107c478bd9Sstevel@tonic-gate 	nodev			/* dev power */
8117c478bd9Sstevel@tonic-gate };
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate /*
8147c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
8157c478bd9Sstevel@tonic-gate  */
8167c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
8177c478bd9Sstevel@tonic-gate 	&mod_driverops,		/* module type (this is a pseudo driver) */
8187c478bd9Sstevel@tonic-gate 	"Function Boundary Tracing",	/* name of module */
8197c478bd9Sstevel@tonic-gate 	&fbt_ops,		/* driver ops */
8207c478bd9Sstevel@tonic-gate };
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
8237c478bd9Sstevel@tonic-gate 	MODREV_1,
8247c478bd9Sstevel@tonic-gate 	(void *)&modldrv,
8257c478bd9Sstevel@tonic-gate 	NULL
8267c478bd9Sstevel@tonic-gate };
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate int
8297c478bd9Sstevel@tonic-gate _init(void)
8307c478bd9Sstevel@tonic-gate {
8317c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
8327c478bd9Sstevel@tonic-gate }
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate int
8357c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
8367c478bd9Sstevel@tonic-gate {
8377c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
8387c478bd9Sstevel@tonic-gate }
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate int
8417c478bd9Sstevel@tonic-gate _fini(void)
8427c478bd9Sstevel@tonic-gate {
8437c478bd9Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
8447c478bd9Sstevel@tonic-gate }
845