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); 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate if (ELF_ST_TYPE(sym->st_info) != STT_FUNC) 2117c478bd9Sstevel@tonic-gate continue; 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate /* 2147c478bd9Sstevel@tonic-gate * Weak symbols are not candidates. This could be made to 2157c478bd9Sstevel@tonic-gate * work (where weak functions and their underlying function 2167c478bd9Sstevel@tonic-gate * appear as two disjoint probes), but it's not simple. 2177c478bd9Sstevel@tonic-gate */ 2187c478bd9Sstevel@tonic-gate if (ELF_ST_BIND(sym->st_info) == STB_WEAK) 2197c478bd9Sstevel@tonic-gate continue; 2207c478bd9Sstevel@tonic-gate 2217c478bd9Sstevel@tonic-gate name = str + sym->st_name; 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate if (strstr(name, "dtrace_") == name && 2247c478bd9Sstevel@tonic-gate strstr(name, "dtrace_safe_") != name) { 2257c478bd9Sstevel@tonic-gate /* 2267c478bd9Sstevel@tonic-gate * Anything beginning with "dtrace_" may be called 2277c478bd9Sstevel@tonic-gate * from probe context unless it explitly indicates 2287c478bd9Sstevel@tonic-gate * that it won't be called from probe context by 2297c478bd9Sstevel@tonic-gate * using the prefix "dtrace_safe_". 2307c478bd9Sstevel@tonic-gate */ 2317c478bd9Sstevel@tonic-gate continue; 2327c478bd9Sstevel@tonic-gate } 2337c478bd9Sstevel@tonic-gate 234*a1b5e537Sbmc if (strstr(name, "kdi_") == name || 235*a1b5e537Sbmc strstr(name, "_kdi_") != NULL) { 2367c478bd9Sstevel@tonic-gate /* 237*a1b5e537Sbmc * Any function name beginning with "kdi_" or 238*a1b5e537Sbmc * containing the string "_kdi_" is a part of the 2397c478bd9Sstevel@tonic-gate * kernel debugger interface and may be called in 2407c478bd9Sstevel@tonic-gate * arbitrary context -- including probe context. 2417c478bd9Sstevel@tonic-gate */ 2427c478bd9Sstevel@tonic-gate continue; 2437c478bd9Sstevel@tonic-gate } 2447c478bd9Sstevel@tonic-gate 2457c478bd9Sstevel@tonic-gate /* 2467c478bd9Sstevel@tonic-gate * Due to 4524008, _init and _fini may have a bloated st_size. 2477c478bd9Sstevel@tonic-gate * While this bug was fixed quite some time ago, old drivers 2487c478bd9Sstevel@tonic-gate * may be lurking. We need to develop a better solution to 2497c478bd9Sstevel@tonic-gate * this problem, such that correct _init and _fini functions 2507c478bd9Sstevel@tonic-gate * (the vast majority) may be correctly traced. One solution 2517c478bd9Sstevel@tonic-gate * may be to scan through the entire symbol table to see if 2527c478bd9Sstevel@tonic-gate * any symbol overlaps with _init. If none does, set a bit in 2537c478bd9Sstevel@tonic-gate * the module structure that this module has correct _init and 2547c478bd9Sstevel@tonic-gate * _fini sizes. This will cause some pain the first time a 2557c478bd9Sstevel@tonic-gate * module is scanned, but at least it would be O(N) instead of 2567c478bd9Sstevel@tonic-gate * O(N log N)... 2577c478bd9Sstevel@tonic-gate */ 2587c478bd9Sstevel@tonic-gate if (strcmp(name, "_init") == 0) 2597c478bd9Sstevel@tonic-gate continue; 2607c478bd9Sstevel@tonic-gate 2617c478bd9Sstevel@tonic-gate if (strcmp(name, "_fini") == 0) 2627c478bd9Sstevel@tonic-gate continue; 2637c478bd9Sstevel@tonic-gate 2647c478bd9Sstevel@tonic-gate /* 2657c478bd9Sstevel@tonic-gate * In order to be eligible, the function must begin with the 2667c478bd9Sstevel@tonic-gate * following sequence: 2677c478bd9Sstevel@tonic-gate * 2687c478bd9Sstevel@tonic-gate * pushl %esp 2697c478bd9Sstevel@tonic-gate * movl %esp, %ebp 2707c478bd9Sstevel@tonic-gate * 2717c478bd9Sstevel@tonic-gate * Note that there are two variants of encodings that generate 2727c478bd9Sstevel@tonic-gate * the movl; we must check for both. For 64-bit, we would 2737c478bd9Sstevel@tonic-gate * normally insist that a function begin with the following 2747c478bd9Sstevel@tonic-gate * sequence: 2757c478bd9Sstevel@tonic-gate * 2767c478bd9Sstevel@tonic-gate * pushq %rbp 2777c478bd9Sstevel@tonic-gate * movq %rsp, %rbp 2787c478bd9Sstevel@tonic-gate * 2797c478bd9Sstevel@tonic-gate * However, the compiler for 64-bit often splits these two 2807c478bd9Sstevel@tonic-gate * instructions -- and the first instruction in the function 2817c478bd9Sstevel@tonic-gate * is often not the pushq. As a result, on 64-bit we look 2827c478bd9Sstevel@tonic-gate * for any "pushq %rbp" in the function and we instrument 2837c478bd9Sstevel@tonic-gate * this with a breakpoint instruction. 2847c478bd9Sstevel@tonic-gate */ 2857c478bd9Sstevel@tonic-gate instr = (uint8_t *)sym->st_value; 2867c478bd9Sstevel@tonic-gate limit = (uint8_t *)(sym->st_value + sym->st_size); 2877c478bd9Sstevel@tonic-gate 2887c478bd9Sstevel@tonic-gate #ifdef __amd64 2897c478bd9Sstevel@tonic-gate while (instr < limit) { 2907c478bd9Sstevel@tonic-gate if (*instr == FBT_PUSHL_EBP) 2917c478bd9Sstevel@tonic-gate break; 2927c478bd9Sstevel@tonic-gate 2937c478bd9Sstevel@tonic-gate if ((size = dtrace_instr_size(instr)) <= 0) 2947c478bd9Sstevel@tonic-gate break; 2957c478bd9Sstevel@tonic-gate 2967c478bd9Sstevel@tonic-gate instr += size; 2977c478bd9Sstevel@tonic-gate } 2987c478bd9Sstevel@tonic-gate 2997c478bd9Sstevel@tonic-gate if (instr >= limit || *instr != FBT_PUSHL_EBP) { 3007c478bd9Sstevel@tonic-gate /* 3017c478bd9Sstevel@tonic-gate * We either don't save the frame pointer in this 3027c478bd9Sstevel@tonic-gate * function, or we ran into some disassembly 3037c478bd9Sstevel@tonic-gate * screw-up. Either way, we bail. 3047c478bd9Sstevel@tonic-gate */ 3057c478bd9Sstevel@tonic-gate continue; 3067c478bd9Sstevel@tonic-gate } 3077c478bd9Sstevel@tonic-gate #else 3087c478bd9Sstevel@tonic-gate if (instr[0] != FBT_PUSHL_EBP) 3097c478bd9Sstevel@tonic-gate continue; 3107c478bd9Sstevel@tonic-gate 3117c478bd9Sstevel@tonic-gate if (!(instr[1] == FBT_MOVL_ESP_EBP0_V0 && 3127c478bd9Sstevel@tonic-gate instr[2] == FBT_MOVL_ESP_EBP1_V0) && 3137c478bd9Sstevel@tonic-gate !(instr[1] == FBT_MOVL_ESP_EBP0_V1 && 3147c478bd9Sstevel@tonic-gate instr[2] == FBT_MOVL_ESP_EBP1_V1)) 3157c478bd9Sstevel@tonic-gate continue; 3167c478bd9Sstevel@tonic-gate #endif 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate fbt = kmem_zalloc(sizeof (fbt_probe_t), KM_SLEEP); 3197c478bd9Sstevel@tonic-gate fbt->fbtp_name = name; 3207c478bd9Sstevel@tonic-gate fbt->fbtp_id = dtrace_probe_create(fbt_id, modname, 3217c478bd9Sstevel@tonic-gate name, FBT_ENTRY, 3, fbt); 3227c478bd9Sstevel@tonic-gate fbt->fbtp_patchpoint = instr; 3237c478bd9Sstevel@tonic-gate fbt->fbtp_ctl = ctl; 3247c478bd9Sstevel@tonic-gate fbt->fbtp_loadcnt = ctl->mod_loadcnt; 3257c478bd9Sstevel@tonic-gate fbt->fbtp_rval = DTRACE_INVOP_PUSHL_EBP; 3267c478bd9Sstevel@tonic-gate fbt->fbtp_savedval = *instr; 3277c478bd9Sstevel@tonic-gate fbt->fbtp_patchval = FBT_PATCHVAL; 3287c478bd9Sstevel@tonic-gate 3297c478bd9Sstevel@tonic-gate fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)]; 3307c478bd9Sstevel@tonic-gate fbt->fbtp_symndx = i; 3317c478bd9Sstevel@tonic-gate fbt_probetab[FBT_ADDR2NDX(instr)] = fbt; 3327c478bd9Sstevel@tonic-gate 3337c478bd9Sstevel@tonic-gate mp->fbt_nentries++; 3347c478bd9Sstevel@tonic-gate 3357c478bd9Sstevel@tonic-gate retfbt = NULL; 3367c478bd9Sstevel@tonic-gate again: 3377c478bd9Sstevel@tonic-gate if (instr >= limit) 3387c478bd9Sstevel@tonic-gate continue; 3397c478bd9Sstevel@tonic-gate 3407c478bd9Sstevel@tonic-gate /* 3417c478bd9Sstevel@tonic-gate * If this disassembly fails, then we've likely walked off into 3427c478bd9Sstevel@tonic-gate * a jump table or some other unsuitable area. Bail out of the 3437c478bd9Sstevel@tonic-gate * disassembly now. 3447c478bd9Sstevel@tonic-gate */ 3457c478bd9Sstevel@tonic-gate if ((size = dtrace_instr_size(instr)) <= 0) 3467c478bd9Sstevel@tonic-gate continue; 3477c478bd9Sstevel@tonic-gate 3487c478bd9Sstevel@tonic-gate #ifdef __amd64 3497c478bd9Sstevel@tonic-gate /* 3507c478bd9Sstevel@tonic-gate * We only instrument "ret" on amd64 -- we don't yet instrument 3517c478bd9Sstevel@tonic-gate * ret imm16, largely because the compiler doesn't seem to 3527c478bd9Sstevel@tonic-gate * (yet) emit them in the kernel... 3537c478bd9Sstevel@tonic-gate */ 3547c478bd9Sstevel@tonic-gate if (*instr != FBT_RET) { 3557c478bd9Sstevel@tonic-gate instr += size; 3567c478bd9Sstevel@tonic-gate goto again; 3577c478bd9Sstevel@tonic-gate } 3587c478bd9Sstevel@tonic-gate #else 3597c478bd9Sstevel@tonic-gate if (!(size == 1 && 3607c478bd9Sstevel@tonic-gate (*instr == FBT_POPL_EBP || *instr == FBT_LEAVE) && 3617c478bd9Sstevel@tonic-gate (*(instr + 1) == FBT_RET || 3627c478bd9Sstevel@tonic-gate *(instr + 1) == FBT_RET_IMM16))) { 3637c478bd9Sstevel@tonic-gate instr += size; 3647c478bd9Sstevel@tonic-gate goto again; 3657c478bd9Sstevel@tonic-gate } 3667c478bd9Sstevel@tonic-gate #endif 3677c478bd9Sstevel@tonic-gate 3687c478bd9Sstevel@tonic-gate /* 3697c478bd9Sstevel@tonic-gate * We have a winner! 3707c478bd9Sstevel@tonic-gate */ 3717c478bd9Sstevel@tonic-gate fbt = kmem_zalloc(sizeof (fbt_probe_t), KM_SLEEP); 3727c478bd9Sstevel@tonic-gate fbt->fbtp_name = name; 3737c478bd9Sstevel@tonic-gate 3747c478bd9Sstevel@tonic-gate if (retfbt == NULL) { 3757c478bd9Sstevel@tonic-gate fbt->fbtp_id = dtrace_probe_create(fbt_id, modname, 3767c478bd9Sstevel@tonic-gate name, FBT_RETURN, 3, fbt); 3777c478bd9Sstevel@tonic-gate } else { 3787c478bd9Sstevel@tonic-gate retfbt->fbtp_next = fbt; 3797c478bd9Sstevel@tonic-gate fbt->fbtp_id = retfbt->fbtp_id; 3807c478bd9Sstevel@tonic-gate } 3817c478bd9Sstevel@tonic-gate 3827c478bd9Sstevel@tonic-gate retfbt = fbt; 3837c478bd9Sstevel@tonic-gate fbt->fbtp_patchpoint = instr; 3847c478bd9Sstevel@tonic-gate fbt->fbtp_ctl = ctl; 3857c478bd9Sstevel@tonic-gate fbt->fbtp_loadcnt = ctl->mod_loadcnt; 3867c478bd9Sstevel@tonic-gate 3877c478bd9Sstevel@tonic-gate #ifndef __amd64 3887c478bd9Sstevel@tonic-gate if (*instr == FBT_POPL_EBP) { 3897c478bd9Sstevel@tonic-gate fbt->fbtp_rval = DTRACE_INVOP_POPL_EBP; 3907c478bd9Sstevel@tonic-gate } else { 3917c478bd9Sstevel@tonic-gate ASSERT(*instr == FBT_LEAVE); 3927c478bd9Sstevel@tonic-gate fbt->fbtp_rval = DTRACE_INVOP_LEAVE; 3937c478bd9Sstevel@tonic-gate } 3947c478bd9Sstevel@tonic-gate fbt->fbtp_roffset = 3957c478bd9Sstevel@tonic-gate (uintptr_t)(instr - (uint8_t *)sym->st_value) + 1; 3967c478bd9Sstevel@tonic-gate 3977c478bd9Sstevel@tonic-gate #else 3987c478bd9Sstevel@tonic-gate ASSERT(*instr == FBT_RET); 3997c478bd9Sstevel@tonic-gate fbt->fbtp_rval = DTRACE_INVOP_RET; 4007c478bd9Sstevel@tonic-gate fbt->fbtp_roffset = 4017c478bd9Sstevel@tonic-gate (uintptr_t)(instr - (uint8_t *)sym->st_value); 4027c478bd9Sstevel@tonic-gate #endif 4037c478bd9Sstevel@tonic-gate 4047c478bd9Sstevel@tonic-gate fbt->fbtp_savedval = *instr; 4057c478bd9Sstevel@tonic-gate fbt->fbtp_patchval = FBT_PATCHVAL; 4067c478bd9Sstevel@tonic-gate fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)]; 4077c478bd9Sstevel@tonic-gate fbt->fbtp_symndx = i; 4087c478bd9Sstevel@tonic-gate fbt_probetab[FBT_ADDR2NDX(instr)] = fbt; 4097c478bd9Sstevel@tonic-gate 4107c478bd9Sstevel@tonic-gate mp->fbt_nentries++; 4117c478bd9Sstevel@tonic-gate 4127c478bd9Sstevel@tonic-gate instr += size; 4137c478bd9Sstevel@tonic-gate goto again; 4147c478bd9Sstevel@tonic-gate } 4157c478bd9Sstevel@tonic-gate } 4167c478bd9Sstevel@tonic-gate 4177c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 4187c478bd9Sstevel@tonic-gate static void 4197c478bd9Sstevel@tonic-gate fbt_destroy(void *arg, dtrace_id_t id, void *parg) 4207c478bd9Sstevel@tonic-gate { 4217c478bd9Sstevel@tonic-gate fbt_probe_t *fbt = parg, *next, *hash, *last; 4227c478bd9Sstevel@tonic-gate struct modctl *ctl = fbt->fbtp_ctl; 4237c478bd9Sstevel@tonic-gate int ndx; 4247c478bd9Sstevel@tonic-gate 4257c478bd9Sstevel@tonic-gate do { 4267c478bd9Sstevel@tonic-gate if (ctl != NULL && ctl->mod_loadcnt == fbt->fbtp_loadcnt) { 4277c478bd9Sstevel@tonic-gate if ((ctl->mod_loadcnt == fbt->fbtp_loadcnt && 4287c478bd9Sstevel@tonic-gate ctl->mod_loaded)) { 4297c478bd9Sstevel@tonic-gate ((struct module *) 4307c478bd9Sstevel@tonic-gate (ctl->mod_mp))->fbt_nentries--; 4317c478bd9Sstevel@tonic-gate } 4327c478bd9Sstevel@tonic-gate } 4337c478bd9Sstevel@tonic-gate 4347c478bd9Sstevel@tonic-gate /* 4357c478bd9Sstevel@tonic-gate * Now we need to remove this probe from the fbt_probetab. 4367c478bd9Sstevel@tonic-gate */ 4377c478bd9Sstevel@tonic-gate ndx = FBT_ADDR2NDX(fbt->fbtp_patchpoint); 4387c478bd9Sstevel@tonic-gate last = NULL; 4397c478bd9Sstevel@tonic-gate hash = fbt_probetab[ndx]; 4407c478bd9Sstevel@tonic-gate 4417c478bd9Sstevel@tonic-gate while (hash != fbt) { 4427c478bd9Sstevel@tonic-gate ASSERT(hash != NULL); 4437c478bd9Sstevel@tonic-gate last = hash; 4447c478bd9Sstevel@tonic-gate hash = hash->fbtp_hashnext; 4457c478bd9Sstevel@tonic-gate } 4467c478bd9Sstevel@tonic-gate 4477c478bd9Sstevel@tonic-gate if (last != NULL) { 4487c478bd9Sstevel@tonic-gate last->fbtp_hashnext = fbt->fbtp_hashnext; 4497c478bd9Sstevel@tonic-gate } else { 4507c478bd9Sstevel@tonic-gate fbt_probetab[ndx] = fbt->fbtp_hashnext; 4517c478bd9Sstevel@tonic-gate } 4527c478bd9Sstevel@tonic-gate 4537c478bd9Sstevel@tonic-gate next = fbt->fbtp_next; 4547c478bd9Sstevel@tonic-gate kmem_free(fbt, sizeof (fbt_probe_t)); 4557c478bd9Sstevel@tonic-gate 4567c478bd9Sstevel@tonic-gate fbt = next; 4577c478bd9Sstevel@tonic-gate } while (fbt != NULL); 4587c478bd9Sstevel@tonic-gate } 4597c478bd9Sstevel@tonic-gate 4607c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 4617c478bd9Sstevel@tonic-gate static void 4627c478bd9Sstevel@tonic-gate fbt_enable(void *arg, dtrace_id_t id, void *parg) 4637c478bd9Sstevel@tonic-gate { 4647c478bd9Sstevel@tonic-gate fbt_probe_t *fbt = parg; 4657c478bd9Sstevel@tonic-gate struct modctl *ctl = fbt->fbtp_ctl; 4667c478bd9Sstevel@tonic-gate 4677c478bd9Sstevel@tonic-gate ctl->mod_nenabled++; 4687c478bd9Sstevel@tonic-gate 4697c478bd9Sstevel@tonic-gate if (!ctl->mod_loaded) { 4707c478bd9Sstevel@tonic-gate if (fbt_verbose) { 4717c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "fbt is failing for probe %s " 4727c478bd9Sstevel@tonic-gate "(module %s unloaded)", 4737c478bd9Sstevel@tonic-gate fbt->fbtp_name, ctl->mod_modname); 4747c478bd9Sstevel@tonic-gate } 4757c478bd9Sstevel@tonic-gate 4767c478bd9Sstevel@tonic-gate return; 4777c478bd9Sstevel@tonic-gate } 4787c478bd9Sstevel@tonic-gate 4797c478bd9Sstevel@tonic-gate /* 4807c478bd9Sstevel@tonic-gate * Now check that our modctl has the expected load count. If it 4817c478bd9Sstevel@tonic-gate * doesn't, this module must have been unloaded and reloaded -- and 4827c478bd9Sstevel@tonic-gate * we're not going to touch it. 4837c478bd9Sstevel@tonic-gate */ 4847c478bd9Sstevel@tonic-gate if (ctl->mod_loadcnt != fbt->fbtp_loadcnt) { 4857c478bd9Sstevel@tonic-gate if (fbt_verbose) { 4867c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "fbt is failing for probe %s " 4877c478bd9Sstevel@tonic-gate "(module %s reloaded)", 4887c478bd9Sstevel@tonic-gate fbt->fbtp_name, ctl->mod_modname); 4897c478bd9Sstevel@tonic-gate } 4907c478bd9Sstevel@tonic-gate 4917c478bd9Sstevel@tonic-gate return; 4927c478bd9Sstevel@tonic-gate } 4937c478bd9Sstevel@tonic-gate 4947c478bd9Sstevel@tonic-gate for (; fbt != NULL; fbt = fbt->fbtp_next) 4957c478bd9Sstevel@tonic-gate *fbt->fbtp_patchpoint = fbt->fbtp_patchval; 4967c478bd9Sstevel@tonic-gate } 4977c478bd9Sstevel@tonic-gate 4987c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 4997c478bd9Sstevel@tonic-gate static void 5007c478bd9Sstevel@tonic-gate fbt_disable(void *arg, dtrace_id_t id, void *parg) 5017c478bd9Sstevel@tonic-gate { 5027c478bd9Sstevel@tonic-gate fbt_probe_t *fbt = parg; 5037c478bd9Sstevel@tonic-gate struct modctl *ctl = fbt->fbtp_ctl; 5047c478bd9Sstevel@tonic-gate 5057c478bd9Sstevel@tonic-gate ASSERT(ctl->mod_nenabled > 0); 5067c478bd9Sstevel@tonic-gate ctl->mod_nenabled--; 5077c478bd9Sstevel@tonic-gate 5087c478bd9Sstevel@tonic-gate if (!ctl->mod_loaded || (ctl->mod_loadcnt != fbt->fbtp_loadcnt)) 5097c478bd9Sstevel@tonic-gate return; 5107c478bd9Sstevel@tonic-gate 5117c478bd9Sstevel@tonic-gate for (; fbt != NULL; fbt = fbt->fbtp_next) 5127c478bd9Sstevel@tonic-gate *fbt->fbtp_patchpoint = fbt->fbtp_savedval; 5137c478bd9Sstevel@tonic-gate } 5147c478bd9Sstevel@tonic-gate 5157c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 5167c478bd9Sstevel@tonic-gate static void 5177c478bd9Sstevel@tonic-gate fbt_suspend(void *arg, dtrace_id_t id, void *parg) 5187c478bd9Sstevel@tonic-gate { 5197c478bd9Sstevel@tonic-gate fbt_probe_t *fbt = parg; 5207c478bd9Sstevel@tonic-gate struct modctl *ctl = fbt->fbtp_ctl; 5217c478bd9Sstevel@tonic-gate 5227c478bd9Sstevel@tonic-gate ASSERT(ctl->mod_nenabled > 0); 5237c478bd9Sstevel@tonic-gate 5247c478bd9Sstevel@tonic-gate if (!ctl->mod_loaded || (ctl->mod_loadcnt != fbt->fbtp_loadcnt)) 5257c478bd9Sstevel@tonic-gate return; 5267c478bd9Sstevel@tonic-gate 5277c478bd9Sstevel@tonic-gate for (; fbt != NULL; fbt = fbt->fbtp_next) 5287c478bd9Sstevel@tonic-gate *fbt->fbtp_patchpoint = fbt->fbtp_savedval; 5297c478bd9Sstevel@tonic-gate } 5307c478bd9Sstevel@tonic-gate 5317c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 5327c478bd9Sstevel@tonic-gate static void 5337c478bd9Sstevel@tonic-gate fbt_resume(void *arg, dtrace_id_t id, void *parg) 5347c478bd9Sstevel@tonic-gate { 5357c478bd9Sstevel@tonic-gate fbt_probe_t *fbt = parg; 5367c478bd9Sstevel@tonic-gate struct modctl *ctl = fbt->fbtp_ctl; 5377c478bd9Sstevel@tonic-gate 5387c478bd9Sstevel@tonic-gate ASSERT(ctl->mod_nenabled > 0); 5397c478bd9Sstevel@tonic-gate 5407c478bd9Sstevel@tonic-gate if (!ctl->mod_loaded || (ctl->mod_loadcnt != fbt->fbtp_loadcnt)) 5417c478bd9Sstevel@tonic-gate return; 5427c478bd9Sstevel@tonic-gate 5437c478bd9Sstevel@tonic-gate for (; fbt != NULL; fbt = fbt->fbtp_next) 5447c478bd9Sstevel@tonic-gate *fbt->fbtp_patchpoint = fbt->fbtp_patchval; 5457c478bd9Sstevel@tonic-gate } 5467c478bd9Sstevel@tonic-gate 5477c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 5487c478bd9Sstevel@tonic-gate static void 5497c478bd9Sstevel@tonic-gate fbt_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc) 5507c478bd9Sstevel@tonic-gate { 5517c478bd9Sstevel@tonic-gate fbt_probe_t *fbt = parg; 5527c478bd9Sstevel@tonic-gate struct modctl *ctl = fbt->fbtp_ctl; 5537c478bd9Sstevel@tonic-gate struct module *mp = ctl->mod_mp; 5547c478bd9Sstevel@tonic-gate ctf_file_t *fp = NULL, *pfp; 5557c478bd9Sstevel@tonic-gate ctf_funcinfo_t f; 5567c478bd9Sstevel@tonic-gate int error; 5577c478bd9Sstevel@tonic-gate ctf_id_t argv[32], type; 5587c478bd9Sstevel@tonic-gate int argc = sizeof (argv) / sizeof (ctf_id_t); 5597c478bd9Sstevel@tonic-gate const char *parent; 5607c478bd9Sstevel@tonic-gate 5617c478bd9Sstevel@tonic-gate if (!ctl->mod_loaded || (ctl->mod_loadcnt != fbt->fbtp_loadcnt)) 5627c478bd9Sstevel@tonic-gate goto err; 5637c478bd9Sstevel@tonic-gate 5647c478bd9Sstevel@tonic-gate if (fbt->fbtp_roffset != 0 && desc->dtargd_ndx == 0) { 5657c478bd9Sstevel@tonic-gate (void) strcpy(desc->dtargd_native, "int"); 5667c478bd9Sstevel@tonic-gate return; 5677c478bd9Sstevel@tonic-gate } 5687c478bd9Sstevel@tonic-gate 5697c478bd9Sstevel@tonic-gate if ((fp = ctf_modopen(mp, &error)) == NULL) { 5707c478bd9Sstevel@tonic-gate /* 5717c478bd9Sstevel@tonic-gate * We have no CTF information for this module -- and therefore 5727c478bd9Sstevel@tonic-gate * no args[] information. 5737c478bd9Sstevel@tonic-gate */ 5747c478bd9Sstevel@tonic-gate goto err; 5757c478bd9Sstevel@tonic-gate } 5767c478bd9Sstevel@tonic-gate 5777c478bd9Sstevel@tonic-gate /* 5787c478bd9Sstevel@tonic-gate * If we have a parent container, we must manually import it. 5797c478bd9Sstevel@tonic-gate */ 5807c478bd9Sstevel@tonic-gate if ((parent = ctf_parent_name(fp)) != NULL) { 5817c478bd9Sstevel@tonic-gate struct modctl *mod; 5827c478bd9Sstevel@tonic-gate 5837c478bd9Sstevel@tonic-gate /* 5847c478bd9Sstevel@tonic-gate * We must iterate over all modules to find the module that 5857c478bd9Sstevel@tonic-gate * is our parent. 5867c478bd9Sstevel@tonic-gate */ 5877c478bd9Sstevel@tonic-gate for (mod = &modules; mod != NULL; mod = mod->mod_next) { 5887c478bd9Sstevel@tonic-gate if (strcmp(mod->mod_filename, parent) == 0) 5897c478bd9Sstevel@tonic-gate break; 5907c478bd9Sstevel@tonic-gate } 5917c478bd9Sstevel@tonic-gate 5927c478bd9Sstevel@tonic-gate if (mod == NULL) 5937c478bd9Sstevel@tonic-gate goto err; 5947c478bd9Sstevel@tonic-gate 5957c478bd9Sstevel@tonic-gate if ((pfp = ctf_modopen(mod->mod_mp, &error)) == NULL) 5967c478bd9Sstevel@tonic-gate goto err; 5977c478bd9Sstevel@tonic-gate 5987c478bd9Sstevel@tonic-gate if (ctf_import(fp, pfp) != 0) { 5997c478bd9Sstevel@tonic-gate ctf_close(pfp); 6007c478bd9Sstevel@tonic-gate goto err; 6017c478bd9Sstevel@tonic-gate } 6027c478bd9Sstevel@tonic-gate 6037c478bd9Sstevel@tonic-gate ctf_close(pfp); 6047c478bd9Sstevel@tonic-gate } 6057c478bd9Sstevel@tonic-gate 6067c478bd9Sstevel@tonic-gate if (ctf_func_info(fp, fbt->fbtp_symndx, &f) == CTF_ERR) 6077c478bd9Sstevel@tonic-gate goto err; 6087c478bd9Sstevel@tonic-gate 6097c478bd9Sstevel@tonic-gate if (fbt->fbtp_roffset != 0) { 6107c478bd9Sstevel@tonic-gate if (desc->dtargd_ndx > 1) 6117c478bd9Sstevel@tonic-gate goto err; 6127c478bd9Sstevel@tonic-gate 6137c478bd9Sstevel@tonic-gate ASSERT(desc->dtargd_ndx == 1); 6147c478bd9Sstevel@tonic-gate type = f.ctc_return; 6157c478bd9Sstevel@tonic-gate } else { 6167c478bd9Sstevel@tonic-gate if (desc->dtargd_ndx + 1 > f.ctc_argc) 6177c478bd9Sstevel@tonic-gate goto err; 6187c478bd9Sstevel@tonic-gate 6197c478bd9Sstevel@tonic-gate if (ctf_func_args(fp, fbt->fbtp_symndx, argc, argv) == CTF_ERR) 6207c478bd9Sstevel@tonic-gate goto err; 6217c478bd9Sstevel@tonic-gate 6227c478bd9Sstevel@tonic-gate type = argv[desc->dtargd_ndx]; 6237c478bd9Sstevel@tonic-gate } 6247c478bd9Sstevel@tonic-gate 6257c478bd9Sstevel@tonic-gate if (ctf_type_name(fp, type, desc->dtargd_native, 6267c478bd9Sstevel@tonic-gate DTRACE_ARGTYPELEN) != NULL) { 6277c478bd9Sstevel@tonic-gate ctf_close(fp); 6287c478bd9Sstevel@tonic-gate return; 6297c478bd9Sstevel@tonic-gate } 6307c478bd9Sstevel@tonic-gate err: 6317c478bd9Sstevel@tonic-gate if (fp != NULL) 6327c478bd9Sstevel@tonic-gate ctf_close(fp); 6337c478bd9Sstevel@tonic-gate 6347c478bd9Sstevel@tonic-gate desc->dtargd_ndx = DTRACE_ARGNONE; 6357c478bd9Sstevel@tonic-gate } 6367c478bd9Sstevel@tonic-gate 6377c478bd9Sstevel@tonic-gate static dtrace_pattr_t fbt_attr = { 6387c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA }, 6397c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 6407c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 6417c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA }, 6427c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA }, 6437c478bd9Sstevel@tonic-gate }; 6447c478bd9Sstevel@tonic-gate 6457c478bd9Sstevel@tonic-gate static dtrace_pops_t fbt_pops = { 6467c478bd9Sstevel@tonic-gate NULL, 6477c478bd9Sstevel@tonic-gate fbt_provide_module, 6487c478bd9Sstevel@tonic-gate fbt_enable, 6497c478bd9Sstevel@tonic-gate fbt_disable, 6507c478bd9Sstevel@tonic-gate fbt_suspend, 6517c478bd9Sstevel@tonic-gate fbt_resume, 6527c478bd9Sstevel@tonic-gate fbt_getargdesc, 6537c478bd9Sstevel@tonic-gate NULL, 6547c478bd9Sstevel@tonic-gate NULL, 6557c478bd9Sstevel@tonic-gate fbt_destroy 6567c478bd9Sstevel@tonic-gate }; 6577c478bd9Sstevel@tonic-gate 6587c478bd9Sstevel@tonic-gate static void 6597c478bd9Sstevel@tonic-gate fbt_cleanup(dev_info_t *devi) 6607c478bd9Sstevel@tonic-gate { 6617c478bd9Sstevel@tonic-gate dtrace_invop_remove(fbt_invop); 6627c478bd9Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL); 6637c478bd9Sstevel@tonic-gate kmem_free(fbt_probetab, fbt_probetab_size * sizeof (fbt_probe_t *)); 6647c478bd9Sstevel@tonic-gate fbt_probetab = NULL; 6657c478bd9Sstevel@tonic-gate fbt_probetab_mask = 0; 6667c478bd9Sstevel@tonic-gate } 6677c478bd9Sstevel@tonic-gate 6687c478bd9Sstevel@tonic-gate static int 6697c478bd9Sstevel@tonic-gate fbt_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 6707c478bd9Sstevel@tonic-gate { 6717c478bd9Sstevel@tonic-gate switch (cmd) { 6727c478bd9Sstevel@tonic-gate case DDI_ATTACH: 6737c478bd9Sstevel@tonic-gate break; 6747c478bd9Sstevel@tonic-gate case DDI_RESUME: 6757c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 6767c478bd9Sstevel@tonic-gate default: 6777c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 6787c478bd9Sstevel@tonic-gate } 6797c478bd9Sstevel@tonic-gate 6807c478bd9Sstevel@tonic-gate if (fbt_probetab_size == 0) 6817c478bd9Sstevel@tonic-gate fbt_probetab_size = FBT_PROBETAB_SIZE; 6827c478bd9Sstevel@tonic-gate 6837c478bd9Sstevel@tonic-gate fbt_probetab_mask = fbt_probetab_size - 1; 6847c478bd9Sstevel@tonic-gate fbt_probetab = 6857c478bd9Sstevel@tonic-gate kmem_zalloc(fbt_probetab_size * sizeof (fbt_probe_t *), KM_SLEEP); 6867c478bd9Sstevel@tonic-gate 6877c478bd9Sstevel@tonic-gate dtrace_invop_add(fbt_invop); 6887c478bd9Sstevel@tonic-gate 6897c478bd9Sstevel@tonic-gate if (ddi_create_minor_node(devi, "fbt", S_IFCHR, 0, 6907c478bd9Sstevel@tonic-gate DDI_PSEUDO, NULL) == DDI_FAILURE || 6917c478bd9Sstevel@tonic-gate dtrace_register("fbt", &fbt_attr, DTRACE_PRIV_KERNEL, 0, 6927c478bd9Sstevel@tonic-gate &fbt_pops, NULL, &fbt_id) != 0) { 6937c478bd9Sstevel@tonic-gate fbt_cleanup(devi); 6947c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 6957c478bd9Sstevel@tonic-gate } 6967c478bd9Sstevel@tonic-gate 6977c478bd9Sstevel@tonic-gate ddi_report_dev(devi); 6987c478bd9Sstevel@tonic-gate fbt_devi = devi; 6997c478bd9Sstevel@tonic-gate 7007c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 7017c478bd9Sstevel@tonic-gate } 7027c478bd9Sstevel@tonic-gate 7037c478bd9Sstevel@tonic-gate static int 7047c478bd9Sstevel@tonic-gate fbt_detach(dev_info_t *devi, ddi_detach_cmd_t cmd) 7057c478bd9Sstevel@tonic-gate { 7067c478bd9Sstevel@tonic-gate switch (cmd) { 7077c478bd9Sstevel@tonic-gate case DDI_DETACH: 7087c478bd9Sstevel@tonic-gate break; 7097c478bd9Sstevel@tonic-gate case DDI_SUSPEND: 7107c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 7117c478bd9Sstevel@tonic-gate default: 7127c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 7137c478bd9Sstevel@tonic-gate } 7147c478bd9Sstevel@tonic-gate 7157c478bd9Sstevel@tonic-gate if (dtrace_unregister(fbt_id) != 0) 7167c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 7177c478bd9Sstevel@tonic-gate 7187c478bd9Sstevel@tonic-gate fbt_cleanup(devi); 7197c478bd9Sstevel@tonic-gate 7207c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 7217c478bd9Sstevel@tonic-gate } 7227c478bd9Sstevel@tonic-gate 7237c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 7247c478bd9Sstevel@tonic-gate static int 7257c478bd9Sstevel@tonic-gate fbt_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 7267c478bd9Sstevel@tonic-gate { 7277c478bd9Sstevel@tonic-gate int error; 7287c478bd9Sstevel@tonic-gate 7297c478bd9Sstevel@tonic-gate switch (infocmd) { 7307c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 7317c478bd9Sstevel@tonic-gate *result = (void *)fbt_devi; 7327c478bd9Sstevel@tonic-gate error = DDI_SUCCESS; 7337c478bd9Sstevel@tonic-gate break; 7347c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 7357c478bd9Sstevel@tonic-gate *result = (void *)0; 7367c478bd9Sstevel@tonic-gate error = DDI_SUCCESS; 7377c478bd9Sstevel@tonic-gate break; 7387c478bd9Sstevel@tonic-gate default: 7397c478bd9Sstevel@tonic-gate error = DDI_FAILURE; 7407c478bd9Sstevel@tonic-gate } 7417c478bd9Sstevel@tonic-gate return (error); 7427c478bd9Sstevel@tonic-gate } 7437c478bd9Sstevel@tonic-gate 7447c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 7457c478bd9Sstevel@tonic-gate static int 7467c478bd9Sstevel@tonic-gate fbt_open(dev_t *devp, int flag, int otyp, cred_t *cred_p) 7477c478bd9Sstevel@tonic-gate { 7487c478bd9Sstevel@tonic-gate return (0); 7497c478bd9Sstevel@tonic-gate } 7507c478bd9Sstevel@tonic-gate 7517c478bd9Sstevel@tonic-gate static struct cb_ops fbt_cb_ops = { 7527c478bd9Sstevel@tonic-gate fbt_open, /* open */ 7537c478bd9Sstevel@tonic-gate nodev, /* close */ 7547c478bd9Sstevel@tonic-gate nulldev, /* strategy */ 7557c478bd9Sstevel@tonic-gate nulldev, /* print */ 7567c478bd9Sstevel@tonic-gate nodev, /* dump */ 7577c478bd9Sstevel@tonic-gate nodev, /* read */ 7587c478bd9Sstevel@tonic-gate nodev, /* write */ 7597c478bd9Sstevel@tonic-gate nodev, /* ioctl */ 7607c478bd9Sstevel@tonic-gate nodev, /* devmap */ 7617c478bd9Sstevel@tonic-gate nodev, /* mmap */ 7627c478bd9Sstevel@tonic-gate nodev, /* segmap */ 7637c478bd9Sstevel@tonic-gate nochpoll, /* poll */ 7647c478bd9Sstevel@tonic-gate ddi_prop_op, /* cb_prop_op */ 7657c478bd9Sstevel@tonic-gate 0, /* streamtab */ 7667c478bd9Sstevel@tonic-gate D_NEW | D_MP /* Driver compatibility flag */ 7677c478bd9Sstevel@tonic-gate }; 7687c478bd9Sstevel@tonic-gate 7697c478bd9Sstevel@tonic-gate static struct dev_ops fbt_ops = { 7707c478bd9Sstevel@tonic-gate DEVO_REV, /* devo_rev */ 7717c478bd9Sstevel@tonic-gate 0, /* refcnt */ 7727c478bd9Sstevel@tonic-gate fbt_info, /* get_dev_info */ 7737c478bd9Sstevel@tonic-gate nulldev, /* identify */ 7747c478bd9Sstevel@tonic-gate nulldev, /* probe */ 7757c478bd9Sstevel@tonic-gate fbt_attach, /* attach */ 7767c478bd9Sstevel@tonic-gate fbt_detach, /* detach */ 7777c478bd9Sstevel@tonic-gate nodev, /* reset */ 7787c478bd9Sstevel@tonic-gate &fbt_cb_ops, /* driver operations */ 7797c478bd9Sstevel@tonic-gate NULL, /* bus operations */ 7807c478bd9Sstevel@tonic-gate nodev /* dev power */ 7817c478bd9Sstevel@tonic-gate }; 7827c478bd9Sstevel@tonic-gate 7837c478bd9Sstevel@tonic-gate /* 7847c478bd9Sstevel@tonic-gate * Module linkage information for the kernel. 7857c478bd9Sstevel@tonic-gate */ 7867c478bd9Sstevel@tonic-gate static struct modldrv modldrv = { 7877c478bd9Sstevel@tonic-gate &mod_driverops, /* module type (this is a pseudo driver) */ 7887c478bd9Sstevel@tonic-gate "Function Boundary Tracing", /* name of module */ 7897c478bd9Sstevel@tonic-gate &fbt_ops, /* driver ops */ 7907c478bd9Sstevel@tonic-gate }; 7917c478bd9Sstevel@tonic-gate 7927c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = { 7937c478bd9Sstevel@tonic-gate MODREV_1, 7947c478bd9Sstevel@tonic-gate (void *)&modldrv, 7957c478bd9Sstevel@tonic-gate NULL 7967c478bd9Sstevel@tonic-gate }; 7977c478bd9Sstevel@tonic-gate 7987c478bd9Sstevel@tonic-gate int 7997c478bd9Sstevel@tonic-gate _init(void) 8007c478bd9Sstevel@tonic-gate { 8017c478bd9Sstevel@tonic-gate return (mod_install(&modlinkage)); 8027c478bd9Sstevel@tonic-gate } 8037c478bd9Sstevel@tonic-gate 8047c478bd9Sstevel@tonic-gate int 8057c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop) 8067c478bd9Sstevel@tonic-gate { 8077c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 8087c478bd9Sstevel@tonic-gate } 8097c478bd9Sstevel@tonic-gate 8107c478bd9Sstevel@tonic-gate int 8117c478bd9Sstevel@tonic-gate _fini(void) 8127c478bd9Sstevel@tonic-gate { 8137c478bd9Sstevel@tonic-gate return (mod_remove(&modlinkage)); 8147c478bd9Sstevel@tonic-gate } 815