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 * Portions Copyright 2015 Ruslan Bukin <br@bsdpad.com> 25 * 26 * $FreeBSD$ 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 AARCH64_BRK 0xd4200000 42 #define AARCH64_BRK_IMM16_SHIFT 5 43 #define AARCH64_BRK_IMM16_VAL (0x40d << AARCH64_BRK_IMM16_SHIFT) 44 #define FBT_PATCHVAL (AARCH64_BRK | AARCH64_BRK_IMM16_VAL) 45 #define FBT_ENTRY "entry" 46 #define FBT_RETURN "return" 47 #define FBT_AFRAMES 4 48 49 int 50 fbt_invop(uintptr_t addr, struct trapframe *frame, uintptr_t rval) 51 { 52 solaris_cpu_t *cpu; 53 fbt_probe_t *fbt; 54 55 cpu = &solaris_cpu[curcpu]; 56 fbt = fbt_probetab[FBT_ADDR2NDX(addr)]; 57 58 for (; fbt != NULL; fbt = fbt->fbtp_hashnext) { 59 if ((uintptr_t)fbt->fbtp_patchpoint != addr) 60 continue; 61 62 cpu->cpu_dtrace_caller = addr; 63 64 if (fbt->fbtp_roffset == 0) { 65 dtrace_probe(fbt->fbtp_id, frame->tf_x[0], 66 frame->tf_x[1], frame->tf_x[2], 67 frame->tf_x[3], frame->tf_x[4]); 68 } else { 69 dtrace_probe(fbt->fbtp_id, fbt->fbtp_roffset, rval, 70 0, 0, 0); 71 } 72 cpu->cpu_dtrace_caller = 0; 73 return (fbt->fbtp_savedval); 74 } 75 76 return (0); 77 } 78 79 void 80 fbt_patch_tracepoint(fbt_probe_t *fbt, fbt_patchval_t val) 81 { 82 vm_offset_t addr; 83 84 if (!arm64_get_writable_addr((vm_offset_t)fbt->fbtp_patchpoint, &addr)) 85 panic("%s: Unable to write new instruction", __func__); 86 87 *(fbt_patchval_t *)addr = val; 88 cpu_icache_sync_range((vm_offset_t)fbt->fbtp_patchpoint, 4); 89 } 90 91 int 92 fbt_provide_module_function(linker_file_t lf, int symindx, 93 linker_symval_t *symval, void *opaque) 94 { 95 fbt_probe_t *fbt, *retfbt; 96 uint32_t *target, *start; 97 uint32_t *instr, *limit; 98 const char *name; 99 char *modname; 100 bool found; 101 int offs; 102 103 modname = opaque; 104 name = symval->name; 105 106 /* Check if function is excluded from instrumentation */ 107 if (fbt_excluded(name)) 108 return (0); 109 110 /* 111 * Instrumenting certain exception handling functions can lead to FBT 112 * recursion, so exclude from instrumentation. 113 */ 114 if (strcmp(name, "handle_el1h_sync") == 0 || 115 strcmp(name, "do_el1h_sync") == 0) 116 return (1); 117 118 instr = (uint32_t *)(symval->value); 119 limit = (uint32_t *)(symval->value + symval->size); 120 121 /* Look for stp (pre-indexed) operation */ 122 found = false; 123 /* 124 * If the first instruction is a nop it's a specially marked 125 * asm function. We only support a nop first as it's not a normal 126 * part of the function prologue. 127 */ 128 if (*instr == NOP_INSTR) 129 found = true; 130 if (!found) { 131 for (; instr < limit; instr++) { 132 /* 133 * Some functions start with 134 * "stp xt1, xt2, [xn, <const>]!" 135 */ 136 if ((*instr & LDP_STP_MASK) == STP_64) { 137 /* 138 * Assume any other store of this type means we 139 * are past the function prolog. 140 */ 141 if (((*instr >> ADDR_SHIFT) & ADDR_MASK) == 31) 142 found = true; 143 break; 144 } 145 146 /* 147 * Some functions start with a "sub sp, sp, <const>" 148 * Sometimes the compiler will have a sub instruction 149 * that is not of the above type so don't stop if we 150 * see one. 151 */ 152 if ((*instr & SUB_MASK) == SUB_INSTR && 153 ((*instr >> SUB_RD_SHIFT) & SUB_R_MASK) == 31 && 154 ((*instr >> SUB_RN_SHIFT) & SUB_R_MASK) == 31) { 155 found = true; 156 break; 157 } 158 } 159 } 160 161 if (!found) 162 return (0); 163 164 fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO); 165 fbt->fbtp_name = name; 166 fbt->fbtp_id = dtrace_probe_create(fbt_id, modname, 167 name, FBT_ENTRY, FBT_AFRAMES, fbt); 168 fbt->fbtp_patchpoint = instr; 169 fbt->fbtp_ctl = lf; 170 fbt->fbtp_loadcnt = lf->loadcnt; 171 fbt->fbtp_savedval = *instr; 172 fbt->fbtp_patchval = FBT_PATCHVAL; 173 if ((*instr & SUB_MASK) == SUB_INSTR) 174 fbt->fbtp_rval = DTRACE_INVOP_SUB; 175 else 176 fbt->fbtp_rval = DTRACE_INVOP_STP; 177 fbt->fbtp_symindx = symindx; 178 179 fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)]; 180 fbt_probetab[FBT_ADDR2NDX(instr)] = fbt; 181 182 lf->fbt_nentries++; 183 184 retfbt = NULL; 185 again: 186 for (; instr < limit; instr++) { 187 if (*instr == RET_INSTR) 188 break; 189 else if ((*instr & B_MASK) == B_INSTR) { 190 offs = (*instr & B_DATA_MASK); 191 offs *= 4; 192 target = (instr + offs); 193 start = (uint32_t *)symval->value; 194 if (target >= limit || target < start) 195 break; 196 } 197 } 198 199 if (instr >= limit) 200 return (0); 201 202 /* 203 * We have a winner! 204 */ 205 fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO); 206 fbt->fbtp_name = name; 207 if (retfbt == NULL) { 208 fbt->fbtp_id = dtrace_probe_create(fbt_id, modname, 209 name, FBT_RETURN, FBT_AFRAMES, fbt); 210 } else { 211 retfbt->fbtp_probenext = fbt; 212 fbt->fbtp_id = retfbt->fbtp_id; 213 } 214 retfbt = fbt; 215 216 fbt->fbtp_patchpoint = instr; 217 fbt->fbtp_ctl = lf; 218 fbt->fbtp_loadcnt = lf->loadcnt; 219 fbt->fbtp_symindx = symindx; 220 if ((*instr & B_MASK) == B_INSTR) 221 fbt->fbtp_rval = DTRACE_INVOP_B; 222 else 223 fbt->fbtp_rval = DTRACE_INVOP_RET; 224 fbt->fbtp_roffset = (uintptr_t)instr - (uintptr_t)symval->value; 225 fbt->fbtp_savedval = *instr; 226 fbt->fbtp_patchval = FBT_PATCHVAL; 227 fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)]; 228 fbt_probetab[FBT_ADDR2NDX(instr)] = fbt; 229 230 lf->fbt_nentries++; 231 232 instr++; 233 goto again; 234 } 235