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