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 * 24 * $FreeBSD$ 25 * 26 */ 27 28 /* 29 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 30 * Use is subject to license terms. 31 */ 32 33 #include <sys/cdefs.h> 34 #include <sys/param.h> 35 #include <sys/dtrace.h> 36 #include <machine/md_var.h> 37 38 #include "fbt.h" 39 40 #define FBT_PATCHVAL 0x7c810808 41 #define FBT_MFLR_R0 0x7c0802a6 42 #define FBT_MTLR_R0 0x7c0803a6 43 #define FBT_BLR 0x4e800020 44 #define FBT_BCTR 0x4e800030 45 #define FBT_BRANCH 0x48000000 46 #define FBT_BR_MASK 0x03fffffc 47 #define FBT_IS_JUMP(instr) ((instr & ~FBT_BR_MASK) == FBT_BRANCH) 48 49 #define FBT_ENTRY "entry" 50 #define FBT_RETURN "return" 51 #define FBT_AFRAMES 7 52 53 int 54 fbt_invop(uintptr_t addr, uintptr_t *stack, uintptr_t rval) 55 { 56 struct trapframe *frame = (struct trapframe *)stack; 57 solaris_cpu_t *cpu = &solaris_cpu[curcpu]; 58 fbt_probe_t *fbt = fbt_probetab[FBT_ADDR2NDX(addr)]; 59 uintptr_t tmp; 60 61 for (; fbt != NULL; fbt = fbt->fbtp_hashnext) { 62 if ((uintptr_t)fbt->fbtp_patchpoint == addr) { 63 fbt->fbtp_invop_cnt++; 64 if (fbt->fbtp_roffset == 0) { 65 cpu->cpu_dtrace_caller = addr; 66 67 dtrace_probe(fbt->fbtp_id, frame->fixreg[3], 68 frame->fixreg[4], frame->fixreg[5], 69 frame->fixreg[6], frame->fixreg[7]); 70 71 cpu->cpu_dtrace_caller = 0; 72 } else { 73 74 dtrace_probe(fbt->fbtp_id, fbt->fbtp_roffset, 75 rval, 0, 0, 0); 76 /* 77 * The caller doesn't have the fbt item, so 78 * fixup tail calls here. 79 */ 80 if (fbt->fbtp_rval == DTRACE_INVOP_JUMP) { 81 frame->srr0 = (uintptr_t)fbt->fbtp_patchpoint; 82 tmp = fbt->fbtp_savedval & FBT_BR_MASK; 83 /* Sign extend. */ 84 if (tmp & 0x02000000) 85 #ifdef __powerpc64__ 86 tmp |= 0xfffffffffc000000ULL; 87 #else 88 tmp |= 0xfc000000UL; 89 #endif 90 frame->srr0 += tmp; 91 } 92 cpu->cpu_dtrace_caller = 0; 93 } 94 95 return (fbt->fbtp_rval); 96 } 97 } 98 99 return (0); 100 } 101 102 void 103 fbt_patch_tracepoint(fbt_probe_t *fbt, fbt_patchval_t val) 104 { 105 106 *fbt->fbtp_patchpoint = val; 107 __syncicache(fbt->fbtp_patchpoint, 4); 108 } 109 110 int 111 fbt_provide_module_function(linker_file_t lf, int symindx, 112 linker_symval_t *symval, void *opaque) 113 { 114 char *modname = opaque; 115 const char *name = symval->name; 116 fbt_probe_t *fbt, *retfbt; 117 int j; 118 uint32_t *instr, *limit; 119 120 #ifdef __powerpc64__ 121 /* 122 * PowerPC64 uses '.' prefixes on symbol names, ignore it, but only 123 * allow symbols with the '.' prefix, so that we don't get the function 124 * descriptor instead. 125 */ 126 if (name[0] == '.') 127 name++; 128 else 129 return (0); 130 #endif 131 132 if (strncmp(name, "dtrace_", 7) == 0 && 133 strncmp(name, "dtrace_safe_", 12) != 0) { 134 /* 135 * Anything beginning with "dtrace_" may be called 136 * from probe context unless it explicitly indicates 137 * that it won't be called from probe context by 138 * using the prefix "dtrace_safe_". 139 */ 140 return (0); 141 } 142 143 if (name[0] == '_' && name[1] == '_') 144 return (0); 145 146 instr = (uint32_t *) symval->value; 147 limit = (uint32_t *) (symval->value + symval->size); 148 149 for (; instr < limit; instr++) 150 if (*instr == FBT_MFLR_R0) 151 break; 152 153 if (*instr != FBT_MFLR_R0) 154 return (0); 155 156 fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO); 157 fbt->fbtp_name = name; 158 fbt->fbtp_id = dtrace_probe_create(fbt_id, modname, 159 name, FBT_ENTRY, FBT_AFRAMES, fbt); 160 fbt->fbtp_patchpoint = instr; 161 fbt->fbtp_ctl = lf; 162 fbt->fbtp_loadcnt = lf->loadcnt; 163 fbt->fbtp_savedval = *instr; 164 fbt->fbtp_patchval = FBT_PATCHVAL; 165 fbt->fbtp_rval = DTRACE_INVOP_MFLR_R0; 166 fbt->fbtp_symindx = symindx; 167 168 fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)]; 169 fbt_probetab[FBT_ADDR2NDX(instr)] = fbt; 170 171 lf->fbt_nentries++; 172 173 retfbt = NULL; 174 again: 175 if (instr >= limit) 176 return (0); 177 178 /* 179 * We (desperately) want to avoid erroneously instrumenting a 180 * jump table. To determine if we're looking at a true instruction 181 * sequence or an inline jump table that happens to contain the same 182 * byte sequences, we resort to some heuristic sleeze: we treat this 183 * instruction as being contained within a pointer, and see if that 184 * pointer points to within the body of the function. If it does, we 185 * refuse to instrument it. 186 */ 187 { 188 uint32_t *ptr; 189 190 ptr = *(uint32_t **)instr; 191 192 if (ptr >= (uint32_t *) symval->value && ptr < limit) { 193 instr++; 194 goto again; 195 } 196 } 197 198 if (*instr != FBT_MTLR_R0) { 199 instr++; 200 goto again; 201 } 202 203 instr++; 204 205 for (j = 0; j < 12 && instr < limit; j++, instr++) { 206 if ((*instr == FBT_BCTR) || (*instr == FBT_BLR) || 207 FBT_IS_JUMP(*instr)) 208 break; 209 } 210 211 if (!(*instr == FBT_BCTR || *instr == FBT_BLR || FBT_IS_JUMP(*instr))) 212 goto again; 213 214 /* 215 * We have a winner! 216 */ 217 fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO); 218 fbt->fbtp_name = name; 219 220 if (retfbt == NULL) { 221 fbt->fbtp_id = dtrace_probe_create(fbt_id, modname, 222 name, FBT_RETURN, FBT_AFRAMES, fbt); 223 } else { 224 retfbt->fbtp_next = fbt; 225 fbt->fbtp_id = retfbt->fbtp_id; 226 } 227 228 retfbt = fbt; 229 fbt->fbtp_patchpoint = instr; 230 fbt->fbtp_ctl = lf; 231 fbt->fbtp_loadcnt = lf->loadcnt; 232 fbt->fbtp_symindx = symindx; 233 234 if (*instr == FBT_BCTR) 235 fbt->fbtp_rval = DTRACE_INVOP_BCTR; 236 else if (*instr == FBT_BLR) 237 fbt->fbtp_rval = DTRACE_INVOP_RET; 238 else 239 fbt->fbtp_rval = DTRACE_INVOP_JUMP; 240 241 fbt->fbtp_roffset = 242 (uintptr_t)((uint8_t *)instr - (uint8_t *)symval->value); 243 244 fbt->fbtp_savedval = *instr; 245 fbt->fbtp_patchval = FBT_PATCHVAL; 246 fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)]; 247 fbt_probetab[FBT_ADDR2NDX(instr)] = fbt; 248 249 lf->fbt_nentries++; 250 251 instr += 4; 252 goto again; 253 } 254