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 48 int 49 fbt_invop(uintptr_t addr, uintptr_t *stack, uintptr_t rval) 50 { 51 struct trapframe *frame; 52 solaris_cpu_t *cpu; 53 fbt_probe_t *fbt; 54 55 frame = (struct trapframe *)stack; 56 cpu = &solaris_cpu[curcpu]; 57 fbt = fbt_probetab[FBT_ADDR2NDX(addr)]; 58 59 for (; fbt != NULL; fbt = fbt->fbtp_hashnext) { 60 if ((uintptr_t)fbt->fbtp_patchpoint == addr) { 61 cpu->cpu_dtrace_caller = addr; 62 63 dtrace_probe(fbt->fbtp_id, frame->tf_x[0], 64 frame->tf_x[1], frame->tf_x[2], 65 frame->tf_x[3], frame->tf_x[4]); 66 67 cpu->cpu_dtrace_caller = 0; 68 return (fbt->fbtp_savedval); 69 } 70 } 71 72 return (0); 73 } 74 75 void 76 fbt_patch_tracepoint(fbt_probe_t *fbt, fbt_patchval_t val) 77 { 78 79 *fbt->fbtp_patchpoint = val; 80 cpu_icache_sync_range((vm_offset_t)fbt->fbtp_patchpoint, 4); 81 } 82 83 int 84 fbt_provide_module_function(linker_file_t lf, int symindx, 85 linker_symval_t *symval, void *opaque) 86 { 87 fbt_probe_t *fbt, *retfbt; 88 uint32_t *target, *start; 89 uint32_t *instr, *limit; 90 const char *name; 91 char *modname; 92 int offs; 93 94 modname = opaque; 95 name = symval->name; 96 97 /* Check if function is excluded from instrumentation */ 98 if (fbt_excluded(name)) 99 return (0); 100 101 instr = (uint32_t *)(symval->value); 102 limit = (uint32_t *)(symval->value + symval->size); 103 104 /* Look for stp (pre-indexed) operation */ 105 for (; instr < limit; instr++) { 106 if ((*instr & LDP_STP_MASK) == STP_64) 107 break; 108 } 109 110 if (instr >= limit) 111 return (0); 112 113 fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO); 114 fbt->fbtp_name = name; 115 fbt->fbtp_id = dtrace_probe_create(fbt_id, modname, 116 name, FBT_ENTRY, 3, fbt); 117 fbt->fbtp_patchpoint = instr; 118 fbt->fbtp_ctl = lf; 119 fbt->fbtp_loadcnt = lf->loadcnt; 120 fbt->fbtp_savedval = *instr; 121 fbt->fbtp_patchval = FBT_PATCHVAL; 122 fbt->fbtp_rval = DTRACE_INVOP_PUSHM; 123 fbt->fbtp_symindx = symindx; 124 125 fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)]; 126 fbt_probetab[FBT_ADDR2NDX(instr)] = fbt; 127 128 lf->fbt_nentries++; 129 130 retfbt = NULL; 131 again: 132 for (; instr < limit; instr++) { 133 if (*instr == RET_INSTR) 134 break; 135 else if ((*instr & B_MASK) == B_INSTR) { 136 offs = (*instr & B_DATA_MASK); 137 offs *= 4; 138 target = (instr + offs); 139 start = (uint32_t *)symval->value; 140 if (target >= limit || target < start) 141 break; 142 } 143 } 144 145 if (instr >= limit) 146 return (0); 147 148 /* 149 * We have a winner! 150 */ 151 fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO); 152 fbt->fbtp_name = name; 153 if (retfbt == NULL) { 154 fbt->fbtp_id = dtrace_probe_create(fbt_id, modname, 155 name, FBT_RETURN, 3, fbt); 156 } else { 157 retfbt->fbtp_next = fbt; 158 fbt->fbtp_id = retfbt->fbtp_id; 159 } 160 retfbt = fbt; 161 162 fbt->fbtp_patchpoint = instr; 163 fbt->fbtp_ctl = lf; 164 fbt->fbtp_loadcnt = lf->loadcnt; 165 fbt->fbtp_symindx = symindx; 166 if ((*instr & B_MASK) == B_INSTR) 167 fbt->fbtp_rval = DTRACE_INVOP_B; 168 else 169 fbt->fbtp_rval = DTRACE_INVOP_RET; 170 fbt->fbtp_savedval = *instr; 171 fbt->fbtp_patchval = FBT_PATCHVAL; 172 fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)]; 173 fbt_probetab[FBT_ADDR2NDX(instr)] = fbt; 174 175 lf->fbt_nentries++; 176 177 instr++; 178 goto again; 179 } 180