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 /*
22*b9e93c10SJonathan 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 #ifdef __amd64
487c478bd9Sstevel@tonic-gate #define FBT_PATCHVAL 0xcc
497c478bd9Sstevel@tonic-gate #else
507c478bd9Sstevel@tonic-gate #define FBT_PATCHVAL 0xf0
517c478bd9Sstevel@tonic-gate #endif
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate #define FBT_ENTRY "entry"
547c478bd9Sstevel@tonic-gate #define FBT_RETURN "return"
557c478bd9Sstevel@tonic-gate #define FBT_ADDR2NDX(addr) ((((uintptr_t)(addr)) >> 4) & fbt_probetab_mask)
567c478bd9Sstevel@tonic-gate #define FBT_PROBETAB_SIZE 0x8000 /* 32k entries -- 128K total */
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate typedef struct fbt_probe {
597c478bd9Sstevel@tonic-gate struct fbt_probe *fbtp_hashnext;
607c478bd9Sstevel@tonic-gate uint8_t *fbtp_patchpoint;
617c478bd9Sstevel@tonic-gate int8_t fbtp_rval;
627c478bd9Sstevel@tonic-gate uint8_t fbtp_patchval;
637c478bd9Sstevel@tonic-gate uint8_t fbtp_savedval;
647c478bd9Sstevel@tonic-gate uintptr_t fbtp_roffset;
657c478bd9Sstevel@tonic-gate dtrace_id_t fbtp_id;
667c478bd9Sstevel@tonic-gate char *fbtp_name;
677c478bd9Sstevel@tonic-gate struct modctl *fbtp_ctl;
687c478bd9Sstevel@tonic-gate int fbtp_loadcnt;
697c478bd9Sstevel@tonic-gate int fbtp_symndx;
707c478bd9Sstevel@tonic-gate int fbtp_primary;
717c478bd9Sstevel@tonic-gate struct fbt_probe *fbtp_next;
727c478bd9Sstevel@tonic-gate } fbt_probe_t;
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate static dev_info_t *fbt_devi;
757c478bd9Sstevel@tonic-gate static dtrace_provider_id_t fbt_id;
767c478bd9Sstevel@tonic-gate static fbt_probe_t **fbt_probetab;
777c478bd9Sstevel@tonic-gate static int fbt_probetab_size;
787c478bd9Sstevel@tonic-gate static int fbt_probetab_mask;
797c478bd9Sstevel@tonic-gate static int fbt_verbose = 0;
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate static int
fbt_invop(uintptr_t addr,uintptr_t * stack,uintptr_t rval)827c478bd9Sstevel@tonic-gate fbt_invop(uintptr_t addr, uintptr_t *stack, uintptr_t rval)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate uintptr_t stack0, stack1, stack2, stack3, stack4;
857c478bd9Sstevel@tonic-gate fbt_probe_t *fbt = fbt_probetab[FBT_ADDR2NDX(addr)];
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate for (; fbt != NULL; fbt = fbt->fbtp_hashnext) {
887c478bd9Sstevel@tonic-gate if ((uintptr_t)fbt->fbtp_patchpoint == addr) {
897c478bd9Sstevel@tonic-gate if (fbt->fbtp_roffset == 0) {
907c478bd9Sstevel@tonic-gate int i = 0;
917c478bd9Sstevel@tonic-gate /*
927c478bd9Sstevel@tonic-gate * When accessing the arguments on the stack,
937c478bd9Sstevel@tonic-gate * we must protect against accessing beyond
947c478bd9Sstevel@tonic-gate * the stack. We can safely set NOFAULT here
957c478bd9Sstevel@tonic-gate * -- we know that interrupts are already
967c478bd9Sstevel@tonic-gate * disabled.
977c478bd9Sstevel@tonic-gate */
987c478bd9Sstevel@tonic-gate DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
997c478bd9Sstevel@tonic-gate CPU->cpu_dtrace_caller = stack[i++];
1007c478bd9Sstevel@tonic-gate #ifdef __amd64
1017c478bd9Sstevel@tonic-gate /*
1027c478bd9Sstevel@tonic-gate * On amd64, stack[0] contains the dereferenced
1037c478bd9Sstevel@tonic-gate * stack pointer, stack[1] contains savfp,
1047c478bd9Sstevel@tonic-gate * stack[2] contains savpc. We want to step
1057c478bd9Sstevel@tonic-gate * over these entries.
1067c478bd9Sstevel@tonic-gate */
1077c478bd9Sstevel@tonic-gate i += 2;
1087c478bd9Sstevel@tonic-gate #endif
1097c478bd9Sstevel@tonic-gate stack0 = stack[i++];
1107c478bd9Sstevel@tonic-gate stack1 = stack[i++];
1117c478bd9Sstevel@tonic-gate stack2 = stack[i++];
1127c478bd9Sstevel@tonic-gate stack3 = stack[i++];
1137c478bd9Sstevel@tonic-gate stack4 = stack[i++];
1147c478bd9Sstevel@tonic-gate DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT |
1157c478bd9Sstevel@tonic-gate CPU_DTRACE_BADADDR);
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate dtrace_probe(fbt->fbtp_id, stack0, stack1,
1187c478bd9Sstevel@tonic-gate stack2, stack3, stack4);
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate CPU->cpu_dtrace_caller = NULL;
1217c478bd9Sstevel@tonic-gate } else {
1227c478bd9Sstevel@tonic-gate #ifdef __amd64
1237c478bd9Sstevel@tonic-gate /*
1247c478bd9Sstevel@tonic-gate * On amd64, we instrument the ret, not the
1257c478bd9Sstevel@tonic-gate * leave. We therefore need to set the caller
1267c478bd9Sstevel@tonic-gate * to assure that the top frame of a stack()
1277c478bd9Sstevel@tonic-gate * action is correct.
1287c478bd9Sstevel@tonic-gate */
1297c478bd9Sstevel@tonic-gate DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
1307c478bd9Sstevel@tonic-gate CPU->cpu_dtrace_caller = stack[0];
1317c478bd9Sstevel@tonic-gate DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT |
1327c478bd9Sstevel@tonic-gate CPU_DTRACE_BADADDR);
1337c478bd9Sstevel@tonic-gate #endif
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate dtrace_probe(fbt->fbtp_id, fbt->fbtp_roffset,
1367c478bd9Sstevel@tonic-gate rval, 0, 0, 0);
1377c478bd9Sstevel@tonic-gate CPU->cpu_dtrace_caller = NULL;
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate return (fbt->fbtp_rval);
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate return (0);
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1487c478bd9Sstevel@tonic-gate static void
fbt_provide_module(void * arg,struct modctl * ctl)1497c478bd9Sstevel@tonic-gate fbt_provide_module(void *arg, struct modctl *ctl)
1507c478bd9Sstevel@tonic-gate {
1517c478bd9Sstevel@tonic-gate struct module *mp = ctl->mod_mp;
1527c478bd9Sstevel@tonic-gate char *str = mp->strings;
1537c478bd9Sstevel@tonic-gate int nsyms = mp->nsyms;
1547c478bd9Sstevel@tonic-gate Shdr *symhdr = mp->symhdr;
1557c478bd9Sstevel@tonic-gate char *modname = ctl->mod_modname;
1567c478bd9Sstevel@tonic-gate char *name;
1577c478bd9Sstevel@tonic-gate fbt_probe_t *fbt, *retfbt;
1587c478bd9Sstevel@tonic-gate size_t symsize;
1597c478bd9Sstevel@tonic-gate int i, size;
1607c478bd9Sstevel@tonic-gate
1617c478bd9Sstevel@tonic-gate /*
1627c478bd9Sstevel@tonic-gate * Employees of dtrace and their families are ineligible. Void
1637c478bd9Sstevel@tonic-gate * where prohibited.
1647c478bd9Sstevel@tonic-gate */
1657c478bd9Sstevel@tonic-gate if (strcmp(modname, "dtrace") == 0)
1667c478bd9Sstevel@tonic-gate return;
1677c478bd9Sstevel@tonic-gate
1687c478bd9Sstevel@tonic-gate if (ctl->mod_requisites != NULL) {
1697c478bd9Sstevel@tonic-gate struct modctl_list *list;
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate list = (struct modctl_list *)ctl->mod_requisites;
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gate for (; list != NULL; list = list->modl_next) {
1747c478bd9Sstevel@tonic-gate if (strcmp(list->modl_modp->mod_modname, "dtrace") == 0)
1757c478bd9Sstevel@tonic-gate return;
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate
1797c478bd9Sstevel@tonic-gate /*
1807c478bd9Sstevel@tonic-gate * KMDB is ineligible for instrumentation -- it may execute in
1817c478bd9Sstevel@tonic-gate * any context, including probe context.
1827c478bd9Sstevel@tonic-gate */
1837c478bd9Sstevel@tonic-gate if (strcmp(modname, "kmdbmod") == 0)
1847c478bd9Sstevel@tonic-gate return;
1857c478bd9Sstevel@tonic-gate
1867c478bd9Sstevel@tonic-gate if (str == NULL || symhdr == NULL || symhdr->sh_addr == NULL) {
1877c478bd9Sstevel@tonic-gate /*
1887c478bd9Sstevel@tonic-gate * If this module doesn't (yet) have its string or symbol
1897c478bd9Sstevel@tonic-gate * table allocated, clear out.
1907c478bd9Sstevel@tonic-gate */
1917c478bd9Sstevel@tonic-gate return;
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate
1947c478bd9Sstevel@tonic-gate symsize = symhdr->sh_entsize;
1957c478bd9Sstevel@tonic-gate
1967c478bd9Sstevel@tonic-gate if (mp->fbt_nentries) {
1977c478bd9Sstevel@tonic-gate /*
1987c478bd9Sstevel@tonic-gate * This module has some FBT entries allocated; we're afraid
1997c478bd9Sstevel@tonic-gate * to screw with it.
2007c478bd9Sstevel@tonic-gate */
2017c478bd9Sstevel@tonic-gate return;
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate
2047c478bd9Sstevel@tonic-gate for (i = 1; i < nsyms; i++) {
2057c478bd9Sstevel@tonic-gate uint8_t *instr, *limit;
2067c478bd9Sstevel@tonic-gate Sym *sym = (Sym *)(symhdr->sh_addr + i * symsize);
207b365acd0Sbmc int j;
2087c478bd9Sstevel@tonic-gate
2097c478bd9Sstevel@tonic-gate if (ELF_ST_TYPE(sym->st_info) != STT_FUNC)
2107c478bd9Sstevel@tonic-gate continue;
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate /*
2137c478bd9Sstevel@tonic-gate * Weak symbols are not candidates. This could be made to
2147c478bd9Sstevel@tonic-gate * work (where weak functions and their underlying function
2157c478bd9Sstevel@tonic-gate * appear as two disjoint probes), but it's not simple.
2167c478bd9Sstevel@tonic-gate */
2177c478bd9Sstevel@tonic-gate if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
2187c478bd9Sstevel@tonic-gate continue;
2197c478bd9Sstevel@tonic-gate
2207c478bd9Sstevel@tonic-gate name = str + sym->st_name;
2217c478bd9Sstevel@tonic-gate
2227c478bd9Sstevel@tonic-gate if (strstr(name, "dtrace_") == name &&
2237c478bd9Sstevel@tonic-gate strstr(name, "dtrace_safe_") != name) {
2247c478bd9Sstevel@tonic-gate /*
2257c478bd9Sstevel@tonic-gate * Anything beginning with "dtrace_" may be called
2267c478bd9Sstevel@tonic-gate * from probe context unless it explitly indicates
2277c478bd9Sstevel@tonic-gate * that it won't be called from probe context by
2287c478bd9Sstevel@tonic-gate * using the prefix "dtrace_safe_".
2297c478bd9Sstevel@tonic-gate */
2307c478bd9Sstevel@tonic-gate continue;
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate
233a1b5e537Sbmc if (strstr(name, "kdi_") == name ||
234a1b5e537Sbmc strstr(name, "_kdi_") != NULL) {
2357c478bd9Sstevel@tonic-gate /*
236a1b5e537Sbmc * Any function name beginning with "kdi_" or
237a1b5e537Sbmc * containing the string "_kdi_" is a part of the
2387c478bd9Sstevel@tonic-gate * kernel debugger interface and may be called in
2397c478bd9Sstevel@tonic-gate * arbitrary context -- including probe context.
2407c478bd9Sstevel@tonic-gate */
2417c478bd9Sstevel@tonic-gate continue;
2427c478bd9Sstevel@tonic-gate }
2437c478bd9Sstevel@tonic-gate
2447c478bd9Sstevel@tonic-gate /*
2457c478bd9Sstevel@tonic-gate * Due to 4524008, _init and _fini may have a bloated st_size.
2467c478bd9Sstevel@tonic-gate * While this bug was fixed quite some time ago, old drivers
2477c478bd9Sstevel@tonic-gate * may be lurking. We need to develop a better solution to
2487c478bd9Sstevel@tonic-gate * this problem, such that correct _init and _fini functions
2497c478bd9Sstevel@tonic-gate * (the vast majority) may be correctly traced. One solution
2507c478bd9Sstevel@tonic-gate * may be to scan through the entire symbol table to see if
2517c478bd9Sstevel@tonic-gate * any symbol overlaps with _init. If none does, set a bit in
2527c478bd9Sstevel@tonic-gate * the module structure that this module has correct _init and
2537c478bd9Sstevel@tonic-gate * _fini sizes. This will cause some pain the first time a
2547c478bd9Sstevel@tonic-gate * module is scanned, but at least it would be O(N) instead of
2557c478bd9Sstevel@tonic-gate * O(N log N)...
2567c478bd9Sstevel@tonic-gate */
2577c478bd9Sstevel@tonic-gate if (strcmp(name, "_init") == 0)
2587c478bd9Sstevel@tonic-gate continue;
2597c478bd9Sstevel@tonic-gate
2607c478bd9Sstevel@tonic-gate if (strcmp(name, "_fini") == 0)
2617c478bd9Sstevel@tonic-gate continue;
2627c478bd9Sstevel@tonic-gate
2637c478bd9Sstevel@tonic-gate /*
2647c478bd9Sstevel@tonic-gate * In order to be eligible, the function must begin with the
2657c478bd9Sstevel@tonic-gate * following sequence:
2667c478bd9Sstevel@tonic-gate *
2677c478bd9Sstevel@tonic-gate * pushl %esp
2687c478bd9Sstevel@tonic-gate * movl %esp, %ebp
2697c478bd9Sstevel@tonic-gate *
2707c478bd9Sstevel@tonic-gate * Note that there are two variants of encodings that generate
2717c478bd9Sstevel@tonic-gate * the movl; we must check for both. For 64-bit, we would
2727c478bd9Sstevel@tonic-gate * normally insist that a function begin with the following
2737c478bd9Sstevel@tonic-gate * sequence:
2747c478bd9Sstevel@tonic-gate *
2757c478bd9Sstevel@tonic-gate * pushq %rbp
2767c478bd9Sstevel@tonic-gate * movq %rsp, %rbp
2777c478bd9Sstevel@tonic-gate *
2787c478bd9Sstevel@tonic-gate * However, the compiler for 64-bit often splits these two
2797c478bd9Sstevel@tonic-gate * instructions -- and the first instruction in the function
2807c478bd9Sstevel@tonic-gate * is often not the pushq. As a result, on 64-bit we look
2817c478bd9Sstevel@tonic-gate * for any "pushq %rbp" in the function and we instrument
2827c478bd9Sstevel@tonic-gate * this with a breakpoint instruction.
2837c478bd9Sstevel@tonic-gate */
2847c478bd9Sstevel@tonic-gate instr = (uint8_t *)sym->st_value;
2857c478bd9Sstevel@tonic-gate limit = (uint8_t *)(sym->st_value + sym->st_size);
2867c478bd9Sstevel@tonic-gate
2877c478bd9Sstevel@tonic-gate #ifdef __amd64
2887c478bd9Sstevel@tonic-gate while (instr < limit) {
2897c478bd9Sstevel@tonic-gate if (*instr == FBT_PUSHL_EBP)
2907c478bd9Sstevel@tonic-gate break;
2917c478bd9Sstevel@tonic-gate
2927c478bd9Sstevel@tonic-gate if ((size = dtrace_instr_size(instr)) <= 0)
2937c478bd9Sstevel@tonic-gate break;
2947c478bd9Sstevel@tonic-gate
2957c478bd9Sstevel@tonic-gate instr += size;
2967c478bd9Sstevel@tonic-gate }
2977c478bd9Sstevel@tonic-gate
2987c478bd9Sstevel@tonic-gate if (instr >= limit || *instr != FBT_PUSHL_EBP) {
2997c478bd9Sstevel@tonic-gate /*
3007c478bd9Sstevel@tonic-gate * We either don't save the frame pointer in this
3017c478bd9Sstevel@tonic-gate * function, or we ran into some disassembly
3027c478bd9Sstevel@tonic-gate * screw-up. Either way, we bail.
3037c478bd9Sstevel@tonic-gate */
3047c478bd9Sstevel@tonic-gate continue;
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate #else
3077c478bd9Sstevel@tonic-gate if (instr[0] != FBT_PUSHL_EBP)
3087c478bd9Sstevel@tonic-gate continue;
3097c478bd9Sstevel@tonic-gate
3107c478bd9Sstevel@tonic-gate if (!(instr[1] == FBT_MOVL_ESP_EBP0_V0 &&
3117c478bd9Sstevel@tonic-gate instr[2] == FBT_MOVL_ESP_EBP1_V0) &&
3127c478bd9Sstevel@tonic-gate !(instr[1] == FBT_MOVL_ESP_EBP0_V1 &&
3137c478bd9Sstevel@tonic-gate instr[2] == FBT_MOVL_ESP_EBP1_V1))
3147c478bd9Sstevel@tonic-gate continue;
3157c478bd9Sstevel@tonic-gate #endif
3167c478bd9Sstevel@tonic-gate
3177c478bd9Sstevel@tonic-gate fbt = kmem_zalloc(sizeof (fbt_probe_t), KM_SLEEP);
3187c478bd9Sstevel@tonic-gate fbt->fbtp_name = name;
3197c478bd9Sstevel@tonic-gate fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
3207c478bd9Sstevel@tonic-gate name, FBT_ENTRY, 3, fbt);
3217c478bd9Sstevel@tonic-gate fbt->fbtp_patchpoint = instr;
3227c478bd9Sstevel@tonic-gate fbt->fbtp_ctl = ctl;
3237c478bd9Sstevel@tonic-gate fbt->fbtp_loadcnt = ctl->mod_loadcnt;
3247c478bd9Sstevel@tonic-gate fbt->fbtp_rval = DTRACE_INVOP_PUSHL_EBP;
3257c478bd9Sstevel@tonic-gate fbt->fbtp_savedval = *instr;
3267c478bd9Sstevel@tonic-gate fbt->fbtp_patchval = FBT_PATCHVAL;
3277c478bd9Sstevel@tonic-gate
3287c478bd9Sstevel@tonic-gate fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
3297c478bd9Sstevel@tonic-gate fbt->fbtp_symndx = i;
3307c478bd9Sstevel@tonic-gate fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
3317c478bd9Sstevel@tonic-gate
3327c478bd9Sstevel@tonic-gate mp->fbt_nentries++;
3337c478bd9Sstevel@tonic-gate
3347c478bd9Sstevel@tonic-gate retfbt = NULL;
3357c478bd9Sstevel@tonic-gate again:
3367c478bd9Sstevel@tonic-gate if (instr >= limit)
3377c478bd9Sstevel@tonic-gate continue;
3387c478bd9Sstevel@tonic-gate
3397c478bd9Sstevel@tonic-gate /*
3407c478bd9Sstevel@tonic-gate * If this disassembly fails, then we've likely walked off into
3417c478bd9Sstevel@tonic-gate * a jump table or some other unsuitable area. Bail out of the
3427c478bd9Sstevel@tonic-gate * disassembly now.
3437c478bd9Sstevel@tonic-gate */
3447c478bd9Sstevel@tonic-gate if ((size = dtrace_instr_size(instr)) <= 0)
3457c478bd9Sstevel@tonic-gate continue;
3467c478bd9Sstevel@tonic-gate
3477c478bd9Sstevel@tonic-gate #ifdef __amd64
3487c478bd9Sstevel@tonic-gate /*
3497c478bd9Sstevel@tonic-gate * We only instrument "ret" on amd64 -- we don't yet instrument
3507c478bd9Sstevel@tonic-gate * ret imm16, largely because the compiler doesn't seem to
3517c478bd9Sstevel@tonic-gate * (yet) emit them in the kernel...
3527c478bd9Sstevel@tonic-gate */
3537c478bd9Sstevel@tonic-gate if (*instr != FBT_RET) {
3547c478bd9Sstevel@tonic-gate instr += size;
3557c478bd9Sstevel@tonic-gate goto again;
3567c478bd9Sstevel@tonic-gate }
357586d07d0Sbmc #else
358586d07d0Sbmc if (!(size == 1 &&
359586d07d0Sbmc (*instr == FBT_POPL_EBP || *instr == FBT_LEAVE) &&
360586d07d0Sbmc (*(instr + 1) == FBT_RET ||
361586d07d0Sbmc *(instr + 1) == FBT_RET_IMM16))) {
362586d07d0Sbmc instr += size;
363586d07d0Sbmc goto again;
364586d07d0Sbmc }
365586d07d0Sbmc #endif
366b365acd0Sbmc
367b365acd0Sbmc /*
368586d07d0Sbmc * We (desperately) want to avoid erroneously instrumenting a
369586d07d0Sbmc * jump table, especially given that our markers are pretty
370586d07d0Sbmc * short: two bytes on x86, and just one byte on amd64. To
371586d07d0Sbmc * determine if we're looking at a true instruction sequence
372586d07d0Sbmc * or an inline jump table that happens to contain the same
373586d07d0Sbmc * byte sequences, we resort to some heuristic sleeze: we
374586d07d0Sbmc * treat this instruction as being contained within a pointer,
375586d07d0Sbmc * and see if that pointer points to within the body of the
376586d07d0Sbmc * function. If it does, we refuse to instrument it.
377b365acd0Sbmc */
378b365acd0Sbmc for (j = 0; j < sizeof (uintptr_t); j++) {
379b365acd0Sbmc uintptr_t check = (uintptr_t)instr - j;
380b365acd0Sbmc uint8_t *ptr;
381b365acd0Sbmc
382b365acd0Sbmc if (check < sym->st_value)
383b365acd0Sbmc break;
384b365acd0Sbmc
385b365acd0Sbmc if (check + sizeof (uintptr_t) > (uintptr_t)limit)
386b365acd0Sbmc continue;
387b365acd0Sbmc
388b365acd0Sbmc ptr = *(uint8_t **)check;
389b365acd0Sbmc
390b365acd0Sbmc if (ptr >= (uint8_t *)sym->st_value && ptr < limit) {
391b365acd0Sbmc instr += size;
392b365acd0Sbmc goto again;
393b365acd0Sbmc }
394b365acd0Sbmc }
3957c478bd9Sstevel@tonic-gate
3967c478bd9Sstevel@tonic-gate /*
3977c478bd9Sstevel@tonic-gate * We have a winner!
3987c478bd9Sstevel@tonic-gate */
3997c478bd9Sstevel@tonic-gate fbt = kmem_zalloc(sizeof (fbt_probe_t), KM_SLEEP);
4007c478bd9Sstevel@tonic-gate fbt->fbtp_name = name;
4017c478bd9Sstevel@tonic-gate
4027c478bd9Sstevel@tonic-gate if (retfbt == NULL) {
4037c478bd9Sstevel@tonic-gate fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
4047c478bd9Sstevel@tonic-gate name, FBT_RETURN, 3, fbt);
4057c478bd9Sstevel@tonic-gate } else {
4067c478bd9Sstevel@tonic-gate retfbt->fbtp_next = fbt;
4077c478bd9Sstevel@tonic-gate fbt->fbtp_id = retfbt->fbtp_id;
4087c478bd9Sstevel@tonic-gate }
4097c478bd9Sstevel@tonic-gate
4107c478bd9Sstevel@tonic-gate retfbt = fbt;
4117c478bd9Sstevel@tonic-gate fbt->fbtp_patchpoint = instr;
4127c478bd9Sstevel@tonic-gate fbt->fbtp_ctl = ctl;
4137c478bd9Sstevel@tonic-gate fbt->fbtp_loadcnt = ctl->mod_loadcnt;
4147c478bd9Sstevel@tonic-gate
4157c478bd9Sstevel@tonic-gate #ifndef __amd64
4167c478bd9Sstevel@tonic-gate if (*instr == FBT_POPL_EBP) {
4177c478bd9Sstevel@tonic-gate fbt->fbtp_rval = DTRACE_INVOP_POPL_EBP;
4187c478bd9Sstevel@tonic-gate } else {
4197c478bd9Sstevel@tonic-gate ASSERT(*instr == FBT_LEAVE);
4207c478bd9Sstevel@tonic-gate fbt->fbtp_rval = DTRACE_INVOP_LEAVE;
4217c478bd9Sstevel@tonic-gate }
4227c478bd9Sstevel@tonic-gate fbt->fbtp_roffset =
4237c478bd9Sstevel@tonic-gate (uintptr_t)(instr - (uint8_t *)sym->st_value) + 1;
4247c478bd9Sstevel@tonic-gate
4257c478bd9Sstevel@tonic-gate #else
4267c478bd9Sstevel@tonic-gate ASSERT(*instr == FBT_RET);
4277c478bd9Sstevel@tonic-gate fbt->fbtp_rval = DTRACE_INVOP_RET;
4287c478bd9Sstevel@tonic-gate fbt->fbtp_roffset =
4297c478bd9Sstevel@tonic-gate (uintptr_t)(instr - (uint8_t *)sym->st_value);
4307c478bd9Sstevel@tonic-gate #endif
4317c478bd9Sstevel@tonic-gate
4327c478bd9Sstevel@tonic-gate fbt->fbtp_savedval = *instr;
4337c478bd9Sstevel@tonic-gate fbt->fbtp_patchval = FBT_PATCHVAL;
4347c478bd9Sstevel@tonic-gate fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
4357c478bd9Sstevel@tonic-gate fbt->fbtp_symndx = i;
4367c478bd9Sstevel@tonic-gate fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
4377c478bd9Sstevel@tonic-gate
4387c478bd9Sstevel@tonic-gate mp->fbt_nentries++;
4397c478bd9Sstevel@tonic-gate
4407c478bd9Sstevel@tonic-gate instr += size;
4417c478bd9Sstevel@tonic-gate goto again;
4427c478bd9Sstevel@tonic-gate }
4437c478bd9Sstevel@tonic-gate }
4447c478bd9Sstevel@tonic-gate
4457c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4467c478bd9Sstevel@tonic-gate static void
fbt_destroy(void * arg,dtrace_id_t id,void * parg)4477c478bd9Sstevel@tonic-gate fbt_destroy(void *arg, dtrace_id_t id, void *parg)
4487c478bd9Sstevel@tonic-gate {
4497c478bd9Sstevel@tonic-gate fbt_probe_t *fbt = parg, *next, *hash, *last;
4507c478bd9Sstevel@tonic-gate struct modctl *ctl = fbt->fbtp_ctl;
4517c478bd9Sstevel@tonic-gate int ndx;
4527c478bd9Sstevel@tonic-gate
4537c478bd9Sstevel@tonic-gate do {
4547c478bd9Sstevel@tonic-gate if (ctl != NULL && ctl->mod_loadcnt == fbt->fbtp_loadcnt) {
4557c478bd9Sstevel@tonic-gate if ((ctl->mod_loadcnt == fbt->fbtp_loadcnt &&
4567c478bd9Sstevel@tonic-gate ctl->mod_loaded)) {
4577c478bd9Sstevel@tonic-gate ((struct module *)
4587c478bd9Sstevel@tonic-gate (ctl->mod_mp))->fbt_nentries--;
4597c478bd9Sstevel@tonic-gate }
4607c478bd9Sstevel@tonic-gate }
4617c478bd9Sstevel@tonic-gate
4627c478bd9Sstevel@tonic-gate /*
4637c478bd9Sstevel@tonic-gate * Now we need to remove this probe from the fbt_probetab.
4647c478bd9Sstevel@tonic-gate */
4657c478bd9Sstevel@tonic-gate ndx = FBT_ADDR2NDX(fbt->fbtp_patchpoint);
4667c478bd9Sstevel@tonic-gate last = NULL;
4677c478bd9Sstevel@tonic-gate hash = fbt_probetab[ndx];
4687c478bd9Sstevel@tonic-gate
4697c478bd9Sstevel@tonic-gate while (hash != fbt) {
4707c478bd9Sstevel@tonic-gate ASSERT(hash != NULL);
4717c478bd9Sstevel@tonic-gate last = hash;
4727c478bd9Sstevel@tonic-gate hash = hash->fbtp_hashnext;
4737c478bd9Sstevel@tonic-gate }
4747c478bd9Sstevel@tonic-gate
4757c478bd9Sstevel@tonic-gate if (last != NULL) {
4767c478bd9Sstevel@tonic-gate last->fbtp_hashnext = fbt->fbtp_hashnext;
4777c478bd9Sstevel@tonic-gate } else {
4787c478bd9Sstevel@tonic-gate fbt_probetab[ndx] = fbt->fbtp_hashnext;
4797c478bd9Sstevel@tonic-gate }
4807c478bd9Sstevel@tonic-gate
4817c478bd9Sstevel@tonic-gate next = fbt->fbtp_next;
4827c478bd9Sstevel@tonic-gate kmem_free(fbt, sizeof (fbt_probe_t));
4837c478bd9Sstevel@tonic-gate
4847c478bd9Sstevel@tonic-gate fbt = next;
4857c478bd9Sstevel@tonic-gate } while (fbt != NULL);
4867c478bd9Sstevel@tonic-gate }
4877c478bd9Sstevel@tonic-gate
4887c478bd9Sstevel@tonic-gate /*ARGSUSED*/
489*b9e93c10SJonathan Haslam static int
fbt_enable(void * arg,dtrace_id_t id,void * parg)4907c478bd9Sstevel@tonic-gate fbt_enable(void *arg, dtrace_id_t id, void *parg)
4917c478bd9Sstevel@tonic-gate {
4927c478bd9Sstevel@tonic-gate fbt_probe_t *fbt = parg;
4937c478bd9Sstevel@tonic-gate struct modctl *ctl = fbt->fbtp_ctl;
4947c478bd9Sstevel@tonic-gate
4957c478bd9Sstevel@tonic-gate ctl->mod_nenabled++;
4967c478bd9Sstevel@tonic-gate
4977c478bd9Sstevel@tonic-gate if (!ctl->mod_loaded) {
4987c478bd9Sstevel@tonic-gate if (fbt_verbose) {
4997c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "fbt is failing for probe %s "
5007c478bd9Sstevel@tonic-gate "(module %s unloaded)",
5017c478bd9Sstevel@tonic-gate fbt->fbtp_name, ctl->mod_modname);
5027c478bd9Sstevel@tonic-gate }
5037c478bd9Sstevel@tonic-gate
504*b9e93c10SJonathan Haslam return (0);
5057c478bd9Sstevel@tonic-gate }
5067c478bd9Sstevel@tonic-gate
5077c478bd9Sstevel@tonic-gate /*
5087c478bd9Sstevel@tonic-gate * Now check that our modctl has the expected load count. If it
5097c478bd9Sstevel@tonic-gate * doesn't, this module must have been unloaded and reloaded -- and
5107c478bd9Sstevel@tonic-gate * we're not going to touch it.
5117c478bd9Sstevel@tonic-gate */
5127c478bd9Sstevel@tonic-gate if (ctl->mod_loadcnt != fbt->fbtp_loadcnt) {
5137c478bd9Sstevel@tonic-gate if (fbt_verbose) {
5147c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "fbt is failing for probe %s "
5157c478bd9Sstevel@tonic-gate "(module %s reloaded)",
5167c478bd9Sstevel@tonic-gate fbt->fbtp_name, ctl->mod_modname);
5177c478bd9Sstevel@tonic-gate }
5187c478bd9Sstevel@tonic-gate
519*b9e93c10SJonathan Haslam return (0);
5207c478bd9Sstevel@tonic-gate }
5217c478bd9Sstevel@tonic-gate
5227c478bd9Sstevel@tonic-gate for (; fbt != NULL; fbt = fbt->fbtp_next)
5237c478bd9Sstevel@tonic-gate *fbt->fbtp_patchpoint = fbt->fbtp_patchval;
524*b9e93c10SJonathan Haslam
525*b9e93c10SJonathan Haslam return (0);
5267c478bd9Sstevel@tonic-gate }
5277c478bd9Sstevel@tonic-gate
5287c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5297c478bd9Sstevel@tonic-gate static void
fbt_disable(void * arg,dtrace_id_t id,void * parg)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
fbt_suspend(void * arg,dtrace_id_t id,void * parg)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
fbt_resume(void * arg,dtrace_id_t id,void * parg)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
fbt_getargdesc(void * arg,dtrace_id_t id,void * parg,dtrace_argdesc_t * desc)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) {
611ae115bc7Smrj struct modctl *mp = &modules;
612ae115bc7Smrj struct modctl *mod = NULL;
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 */
618ae115bc7Smrj do {
619ae115bc7Smrj if (strcmp(mp->mod_modname, parent) == 0) {
620ae115bc7Smrj mod = mp;
6217c478bd9Sstevel@tonic-gate break;
6227c478bd9Sstevel@tonic-gate }
623ae115bc7Smrj } while ((mp = mp->mod_next) != &modules);
6247c478bd9Sstevel@tonic-gate
6257c478bd9Sstevel@tonic-gate if (mod == NULL)
6267c478bd9Sstevel@tonic-gate goto err;
6277c478bd9Sstevel@tonic-gate
628ae115bc7Smrj if ((pfp = ctf_modopen(mod->mod_mp, &error)) == NULL) {
6297c478bd9Sstevel@tonic-gate goto err;
630ae115bc7Smrj }
6317c478bd9Sstevel@tonic-gate
6327c478bd9Sstevel@tonic-gate if (ctf_import(fp, pfp) != 0) {
6337c478bd9Sstevel@tonic-gate ctf_close(pfp);
6347c478bd9Sstevel@tonic-gate goto err;
6357c478bd9Sstevel@tonic-gate }
6367c478bd9Sstevel@tonic-gate
6377c478bd9Sstevel@tonic-gate ctf_close(pfp);
6387c478bd9Sstevel@tonic-gate }
6397c478bd9Sstevel@tonic-gate
6407c478bd9Sstevel@tonic-gate if (ctf_func_info(fp, fbt->fbtp_symndx, &f) == CTF_ERR)
6417c478bd9Sstevel@tonic-gate goto err;
6427c478bd9Sstevel@tonic-gate
6437c478bd9Sstevel@tonic-gate if (fbt->fbtp_roffset != 0) {
6447c478bd9Sstevel@tonic-gate if (desc->dtargd_ndx > 1)
6457c478bd9Sstevel@tonic-gate goto err;
6467c478bd9Sstevel@tonic-gate
6477c478bd9Sstevel@tonic-gate ASSERT(desc->dtargd_ndx == 1);
6487c478bd9Sstevel@tonic-gate type = f.ctc_return;
6497c478bd9Sstevel@tonic-gate } else {
6507c478bd9Sstevel@tonic-gate if (desc->dtargd_ndx + 1 > f.ctc_argc)
6517c478bd9Sstevel@tonic-gate goto err;
6527c478bd9Sstevel@tonic-gate
6537c478bd9Sstevel@tonic-gate if (ctf_func_args(fp, fbt->fbtp_symndx, argc, argv) == CTF_ERR)
6547c478bd9Sstevel@tonic-gate goto err;
6557c478bd9Sstevel@tonic-gate
6567c478bd9Sstevel@tonic-gate type = argv[desc->dtargd_ndx];
6577c478bd9Sstevel@tonic-gate }
6587c478bd9Sstevel@tonic-gate
6597c478bd9Sstevel@tonic-gate if (ctf_type_name(fp, type, desc->dtargd_native,
6607c478bd9Sstevel@tonic-gate DTRACE_ARGTYPELEN) != NULL) {
6617c478bd9Sstevel@tonic-gate ctf_close(fp);
6627c478bd9Sstevel@tonic-gate return;
6637c478bd9Sstevel@tonic-gate }
6647c478bd9Sstevel@tonic-gate err:
6657c478bd9Sstevel@tonic-gate if (fp != NULL)
6667c478bd9Sstevel@tonic-gate ctf_close(fp);
6677c478bd9Sstevel@tonic-gate
6687c478bd9Sstevel@tonic-gate desc->dtargd_ndx = DTRACE_ARGNONE;
6697c478bd9Sstevel@tonic-gate }
6707c478bd9Sstevel@tonic-gate
6717c478bd9Sstevel@tonic-gate static dtrace_pattr_t fbt_attr = {
6727c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
6737c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
6747c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
6757c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
6767c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
6777c478bd9Sstevel@tonic-gate };
6787c478bd9Sstevel@tonic-gate
6797c478bd9Sstevel@tonic-gate static dtrace_pops_t fbt_pops = {
6807c478bd9Sstevel@tonic-gate NULL,
6817c478bd9Sstevel@tonic-gate fbt_provide_module,
6827c478bd9Sstevel@tonic-gate fbt_enable,
6837c478bd9Sstevel@tonic-gate fbt_disable,
6847c478bd9Sstevel@tonic-gate fbt_suspend,
6857c478bd9Sstevel@tonic-gate fbt_resume,
6867c478bd9Sstevel@tonic-gate fbt_getargdesc,
6877c478bd9Sstevel@tonic-gate NULL,
6887c478bd9Sstevel@tonic-gate NULL,
6897c478bd9Sstevel@tonic-gate fbt_destroy
6907c478bd9Sstevel@tonic-gate };
6917c478bd9Sstevel@tonic-gate
6927c478bd9Sstevel@tonic-gate static void
fbt_cleanup(dev_info_t * devi)6937c478bd9Sstevel@tonic-gate fbt_cleanup(dev_info_t *devi)
6947c478bd9Sstevel@tonic-gate {
6957c478bd9Sstevel@tonic-gate dtrace_invop_remove(fbt_invop);
6967c478bd9Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL);
6977c478bd9Sstevel@tonic-gate kmem_free(fbt_probetab, fbt_probetab_size * sizeof (fbt_probe_t *));
6987c478bd9Sstevel@tonic-gate fbt_probetab = NULL;
6997c478bd9Sstevel@tonic-gate fbt_probetab_mask = 0;
7007c478bd9Sstevel@tonic-gate }
7017c478bd9Sstevel@tonic-gate
7027c478bd9Sstevel@tonic-gate static int
fbt_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)7037c478bd9Sstevel@tonic-gate fbt_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
7047c478bd9Sstevel@tonic-gate {
7057c478bd9Sstevel@tonic-gate switch (cmd) {
7067c478bd9Sstevel@tonic-gate case DDI_ATTACH:
7077c478bd9Sstevel@tonic-gate break;
7087c478bd9Sstevel@tonic-gate case DDI_RESUME:
7097c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
7107c478bd9Sstevel@tonic-gate default:
7117c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
7127c478bd9Sstevel@tonic-gate }
7137c478bd9Sstevel@tonic-gate
7147c478bd9Sstevel@tonic-gate if (fbt_probetab_size == 0)
7157c478bd9Sstevel@tonic-gate fbt_probetab_size = FBT_PROBETAB_SIZE;
7167c478bd9Sstevel@tonic-gate
7177c478bd9Sstevel@tonic-gate fbt_probetab_mask = fbt_probetab_size - 1;
7187c478bd9Sstevel@tonic-gate fbt_probetab =
7197c478bd9Sstevel@tonic-gate kmem_zalloc(fbt_probetab_size * sizeof (fbt_probe_t *), KM_SLEEP);
7207c478bd9Sstevel@tonic-gate
7217c478bd9Sstevel@tonic-gate dtrace_invop_add(fbt_invop);
7227c478bd9Sstevel@tonic-gate
7237c478bd9Sstevel@tonic-gate if (ddi_create_minor_node(devi, "fbt", S_IFCHR, 0,
7247c478bd9Sstevel@tonic-gate DDI_PSEUDO, NULL) == DDI_FAILURE ||
725ad4023c4Sdp dtrace_register("fbt", &fbt_attr, DTRACE_PRIV_KERNEL, NULL,
7267c478bd9Sstevel@tonic-gate &fbt_pops, NULL, &fbt_id) != 0) {
7277c478bd9Sstevel@tonic-gate fbt_cleanup(devi);
7287c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
7297c478bd9Sstevel@tonic-gate }
7307c478bd9Sstevel@tonic-gate
7317c478bd9Sstevel@tonic-gate ddi_report_dev(devi);
7327c478bd9Sstevel@tonic-gate fbt_devi = devi;
7337c478bd9Sstevel@tonic-gate
7347c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
7357c478bd9Sstevel@tonic-gate }
7367c478bd9Sstevel@tonic-gate
7377c478bd9Sstevel@tonic-gate static int
fbt_detach(dev_info_t * devi,ddi_detach_cmd_t cmd)7387c478bd9Sstevel@tonic-gate fbt_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
7397c478bd9Sstevel@tonic-gate {
7407c478bd9Sstevel@tonic-gate switch (cmd) {
7417c478bd9Sstevel@tonic-gate case DDI_DETACH:
7427c478bd9Sstevel@tonic-gate break;
7437c478bd9Sstevel@tonic-gate case DDI_SUSPEND:
7447c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
7457c478bd9Sstevel@tonic-gate default:
7467c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
7477c478bd9Sstevel@tonic-gate }
7487c478bd9Sstevel@tonic-gate
7497c478bd9Sstevel@tonic-gate if (dtrace_unregister(fbt_id) != 0)
7507c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
7517c478bd9Sstevel@tonic-gate
7527c478bd9Sstevel@tonic-gate fbt_cleanup(devi);
7537c478bd9Sstevel@tonic-gate
7547c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
7557c478bd9Sstevel@tonic-gate }
7567c478bd9Sstevel@tonic-gate
7577c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7587c478bd9Sstevel@tonic-gate static int
fbt_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)7597c478bd9Sstevel@tonic-gate fbt_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
7607c478bd9Sstevel@tonic-gate {
7617c478bd9Sstevel@tonic-gate int error;
7627c478bd9Sstevel@tonic-gate
7637c478bd9Sstevel@tonic-gate switch (infocmd) {
7647c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO:
7657c478bd9Sstevel@tonic-gate *result = (void *)fbt_devi;
7667c478bd9Sstevel@tonic-gate error = DDI_SUCCESS;
7677c478bd9Sstevel@tonic-gate break;
7687c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE:
7697c478bd9Sstevel@tonic-gate *result = (void *)0;
7707c478bd9Sstevel@tonic-gate error = DDI_SUCCESS;
7717c478bd9Sstevel@tonic-gate break;
7727c478bd9Sstevel@tonic-gate default:
7737c478bd9Sstevel@tonic-gate error = DDI_FAILURE;
7747c478bd9Sstevel@tonic-gate }
7757c478bd9Sstevel@tonic-gate return (error);
7767c478bd9Sstevel@tonic-gate }
7777c478bd9Sstevel@tonic-gate
7787c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7797c478bd9Sstevel@tonic-gate static int
fbt_open(dev_t * devp,int flag,int otyp,cred_t * cred_p)7807c478bd9Sstevel@tonic-gate fbt_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
7817c478bd9Sstevel@tonic-gate {
7827c478bd9Sstevel@tonic-gate return (0);
7837c478bd9Sstevel@tonic-gate }
7847c478bd9Sstevel@tonic-gate
7857c478bd9Sstevel@tonic-gate static struct cb_ops fbt_cb_ops = {
7867c478bd9Sstevel@tonic-gate fbt_open, /* open */
7877c478bd9Sstevel@tonic-gate nodev, /* close */
7887c478bd9Sstevel@tonic-gate nulldev, /* strategy */
7897c478bd9Sstevel@tonic-gate nulldev, /* print */
7907c478bd9Sstevel@tonic-gate nodev, /* dump */
7917c478bd9Sstevel@tonic-gate nodev, /* read */
7927c478bd9Sstevel@tonic-gate nodev, /* write */
7937c478bd9Sstevel@tonic-gate nodev, /* ioctl */
7947c478bd9Sstevel@tonic-gate nodev, /* devmap */
7957c478bd9Sstevel@tonic-gate nodev, /* mmap */
7967c478bd9Sstevel@tonic-gate nodev, /* segmap */
7977c478bd9Sstevel@tonic-gate nochpoll, /* poll */
7987c478bd9Sstevel@tonic-gate ddi_prop_op, /* cb_prop_op */
7997c478bd9Sstevel@tonic-gate 0, /* streamtab */
8007c478bd9Sstevel@tonic-gate D_NEW | D_MP /* Driver compatibility flag */
8017c478bd9Sstevel@tonic-gate };
8027c478bd9Sstevel@tonic-gate
8037c478bd9Sstevel@tonic-gate static struct dev_ops fbt_ops = {
8047c478bd9Sstevel@tonic-gate DEVO_REV, /* devo_rev */
8057c478bd9Sstevel@tonic-gate 0, /* refcnt */
8067c478bd9Sstevel@tonic-gate fbt_info, /* get_dev_info */
8077c478bd9Sstevel@tonic-gate nulldev, /* identify */
8087c478bd9Sstevel@tonic-gate nulldev, /* probe */
8097c478bd9Sstevel@tonic-gate fbt_attach, /* attach */
8107c478bd9Sstevel@tonic-gate fbt_detach, /* detach */
8117c478bd9Sstevel@tonic-gate nodev, /* reset */
8127c478bd9Sstevel@tonic-gate &fbt_cb_ops, /* driver operations */
8137c478bd9Sstevel@tonic-gate NULL, /* bus operations */
81419397407SSherry Moore nodev, /* dev power */
81519397407SSherry Moore ddi_quiesce_not_needed, /* quiesce */
8167c478bd9Sstevel@tonic-gate };
8177c478bd9Sstevel@tonic-gate
8187c478bd9Sstevel@tonic-gate /*
8197c478bd9Sstevel@tonic-gate * Module linkage information for the kernel.
8207c478bd9Sstevel@tonic-gate */
8217c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
8227c478bd9Sstevel@tonic-gate &mod_driverops, /* module type (this is a pseudo driver) */
8237c478bd9Sstevel@tonic-gate "Function Boundary Tracing", /* name of module */
8247c478bd9Sstevel@tonic-gate &fbt_ops, /* driver ops */
8257c478bd9Sstevel@tonic-gate };
8267c478bd9Sstevel@tonic-gate
8277c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
8287c478bd9Sstevel@tonic-gate MODREV_1,
8297c478bd9Sstevel@tonic-gate (void *)&modldrv,
8307c478bd9Sstevel@tonic-gate NULL
8317c478bd9Sstevel@tonic-gate };
8327c478bd9Sstevel@tonic-gate
8337c478bd9Sstevel@tonic-gate int
_init(void)8347c478bd9Sstevel@tonic-gate _init(void)
8357c478bd9Sstevel@tonic-gate {
8367c478bd9Sstevel@tonic-gate return (mod_install(&modlinkage));
8377c478bd9Sstevel@tonic-gate }
8387c478bd9Sstevel@tonic-gate
8397c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)8407c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
8417c478bd9Sstevel@tonic-gate {
8427c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
8437c478bd9Sstevel@tonic-gate }
8447c478bd9Sstevel@tonic-gate
8457c478bd9Sstevel@tonic-gate int
_fini(void)8467c478bd9Sstevel@tonic-gate _fini(void)
8477c478bd9Sstevel@tonic-gate {
8487c478bd9Sstevel@tonic-gate return (mod_remove(&modlinkage));
8497c478bd9Sstevel@tonic-gate }
850