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