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