1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 * 21 * Portions Copyright 2006-2008 John Birrell jb@freebsd.org 22 * Portions Copyright 2013 Justin Hibbits jhibbits@freebsd.org 23 * Portions Copyright 2013 Howard Su howardsu@freebsd.org 24 * 25 * $FreeBSD$ 26 * 27 */ 28 29 /* 30 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 31 * Use is subject to license terms. 32 */ 33 34 #include <sys/cdefs.h> 35 #include <sys/param.h> 36 37 #include <sys/dtrace.h> 38 39 #include "fbt.h" 40 41 #define FBT_PATCHVAL 0xe06a0cfe /* illegal instruction */ 42 43 #define FBT_PUSHM 0xe92d0000 44 #define FBT_POPM 0xe8bd0000 45 #define FBT_JUMP 0xea000000 46 47 #define FBT_ENTRY "entry" 48 #define FBT_RETURN "return" 49 50 int 51 fbt_invop(uintptr_t addr, uintptr_t *stack, uintptr_t rval) 52 { 53 struct trapframe *frame = (struct trapframe *)stack; 54 solaris_cpu_t *cpu = &solaris_cpu[curcpu]; 55 fbt_probe_t *fbt = fbt_probetab[FBT_ADDR2NDX(addr)]; 56 57 for (; fbt != NULL; fbt = fbt->fbtp_hashnext) { 58 if ((uintptr_t)fbt->fbtp_patchpoint == addr) { 59 fbt->fbtp_invop_cnt++; 60 cpu->cpu_dtrace_caller = addr; 61 62 /* TODO: Need 5th parameter from stack */ 63 dtrace_probe(fbt->fbtp_id, frame->tf_r0, 64 frame->tf_r1, frame->tf_r2, 65 frame->tf_r3, 0); 66 67 cpu->cpu_dtrace_caller = 0; 68 69 return (fbt->fbtp_rval); 70 } 71 } 72 73 return (0); 74 } 75 76 void 77 fbt_patch_tracepoint(fbt_probe_t *fbt, fbt_patchval_t val) 78 { 79 80 *fbt->fbtp_patchpoint = val; 81 cpu_icache_sync_range((vm_offset_t)fbt->fbtp_patchpoint, 4); 82 } 83 84 int 85 fbt_provide_module_function(linker_file_t lf, int symindx, 86 linker_symval_t *symval, void *opaque) 87 { 88 char *modname = opaque; 89 const char *name = symval->name; 90 fbt_probe_t *fbt, *retfbt; 91 uint32_t *instr, *limit; 92 int popm; 93 94 if (strncmp(name, "dtrace_", 7) == 0 && 95 strncmp(name, "dtrace_safe_", 12) != 0) { 96 /* 97 * Anything beginning with "dtrace_" may be called 98 * from probe context unless it explicitly indicates 99 * that it won't be called from probe context by 100 * using the prefix "dtrace_safe_". 101 */ 102 return (0); 103 } 104 105 if (name[0] == '_' && name[1] == '_') 106 return (0); 107 108 instr = (uint32_t *)symval->value; 109 limit = (uint32_t *)(symval->value + symval->size); 110 111 for (; instr < limit; instr++) 112 if ((*instr & 0xffff0000) == FBT_PUSHM && 113 (*instr & 0x4000) != 0) 114 break; 115 116 if (instr >= limit) 117 return (0); 118 119 fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO); 120 fbt->fbtp_name = name; 121 fbt->fbtp_id = dtrace_probe_create(fbt_id, modname, 122 name, FBT_ENTRY, 3, fbt); 123 fbt->fbtp_patchpoint = instr; 124 fbt->fbtp_ctl = lf; 125 fbt->fbtp_loadcnt = lf->loadcnt; 126 fbt->fbtp_savedval = *instr; 127 fbt->fbtp_patchval = FBT_PATCHVAL; 128 fbt->fbtp_rval = DTRACE_INVOP_PUSHM; 129 fbt->fbtp_symindx = symindx; 130 131 fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)]; 132 fbt_probetab[FBT_ADDR2NDX(instr)] = fbt; 133 134 lf->fbt_nentries++; 135 136 popm = FBT_POPM | ((*instr) & 0x3FFF) | 0x8000; 137 138 retfbt = NULL; 139 again: 140 for (; instr < limit; instr++) { 141 if (*instr == popm) 142 break; 143 else if ((*instr & 0xff000000) == FBT_JUMP) { 144 uint32_t *target, *start; 145 int offset; 146 147 offset = (*instr & 0xffffff); 148 offset <<= 8; 149 offset /= 64; 150 target = instr + (2 + offset); 151 start = (uint32_t *)symval->value; 152 if (target >= limit || target < start) 153 break; 154 instr++; /* skip delay slot */ 155 } 156 } 157 158 if (instr >= limit) 159 return (0); 160 161 /* 162 * We have a winner! 163 */ 164 fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO); 165 fbt->fbtp_name = name; 166 if (retfbt == NULL) { 167 fbt->fbtp_id = dtrace_probe_create(fbt_id, modname, 168 name, FBT_RETURN, 5, fbt); 169 } else { 170 retfbt->fbtp_next = fbt; 171 fbt->fbtp_id = retfbt->fbtp_id; 172 } 173 retfbt = fbt; 174 175 fbt->fbtp_patchpoint = instr; 176 fbt->fbtp_ctl = lf; 177 fbt->fbtp_loadcnt = lf->loadcnt; 178 fbt->fbtp_symindx = symindx; 179 if ((*instr & 0xff000000) == FBT_JUMP) 180 fbt->fbtp_rval = DTRACE_INVOP_B; 181 else 182 fbt->fbtp_rval = DTRACE_INVOP_POPM; 183 fbt->fbtp_savedval = *instr; 184 fbt->fbtp_patchval = FBT_PATCHVAL; 185 fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)]; 186 fbt_probetab[FBT_ADDR2NDX(instr)] = fbt; 187 188 lf->fbt_nentries++; 189 190 instr++; 191 goto again; 192 } 193