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