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