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