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