xref: /freebsd/sys/cddl/dev/fbt/arm/fbt_isa.c (revision 734e82fe33aa764367791a7d603b383996c6b40b)
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  *
25  */
26 
27 /*
28  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
29  * Use is subject to license terms.
30  */
31 
32 #include <sys/cdefs.h>
33 #include <sys/param.h>
34 
35 #include <sys/dtrace.h>
36 #include <machine/stack.h>
37 #include <machine/trap.h>
38 
39 #include "fbt.h"
40 
41 #define	FBT_PUSHM		0xe92d0000
42 #define	FBT_POPM		0xe8bd0000
43 #define	FBT_JUMP		0xea000000
44 #define	FBT_SUBSP		0xe24dd000
45 
46 int
47 fbt_invop(uintptr_t addr, struct trapframe *frame, uintptr_t rval)
48 {
49 	solaris_cpu_t *cpu = &solaris_cpu[curcpu];
50 	fbt_probe_t *fbt = fbt_probetab[FBT_ADDR2NDX(addr)];
51 	register_t fifthparam;
52 
53 	for (; fbt != NULL; fbt = fbt->fbtp_hashnext) {
54 		if ((uintptr_t)fbt->fbtp_patchpoint != addr)
55 			continue;
56 
57 		cpu->cpu_dtrace_caller = addr;
58 
59 		if (fbt->fbtp_roffset == 0) {
60 			/* Get 5th parameter from stack */
61 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
62 			fifthparam = *(register_t *)frame->tf_svc_sp;
63 			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT | CPU_DTRACE_BADADDR);
64 
65 			dtrace_probe(fbt->fbtp_id, frame->tf_r0,
66 			    frame->tf_r1, frame->tf_r2,
67 			    frame->tf_r3, fifthparam);
68 		} else {
69 			dtrace_probe(fbt->fbtp_id, fbt->fbtp_roffset, rval,
70 			    0, 0, 0);
71 		}
72 
73 		cpu->cpu_dtrace_caller = 0;
74 		return (fbt->fbtp_rval | (fbt->fbtp_savedval << DTRACE_INVOP_SHIFT));
75 	}
76 
77 	return (0);
78 }
79 
80 void
81 fbt_patch_tracepoint(fbt_probe_t *fbt, fbt_patchval_t val)
82 {
83 
84 	*fbt->fbtp_patchpoint = val;
85 	icache_sync((vm_offset_t)fbt->fbtp_patchpoint, sizeof(val));
86 }
87 
88 int
89 fbt_provide_module_function(linker_file_t lf, int symindx,
90     linker_symval_t *symval, void *opaque)
91 {
92 	char *modname = opaque;
93 	const char *name = symval->name;
94 	fbt_probe_t *fbt, *retfbt;
95 	uint32_t *instr, *limit;
96 	int popm;
97 
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 	/*
105 	 * va_arg functions has first instruction of
106 	 * sub sp, sp, #?
107 	 */
108 	if ((*instr & 0xfffff000) == FBT_SUBSP)
109 		instr++;
110 
111 	/*
112 	 * check if insn is a pushm with LR
113 	 */
114 	if ((*instr & 0xffff0000) != FBT_PUSHM ||
115 	    (*instr & (1 << LR)) == 0)
116 		return (0);
117 
118 	fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO);
119 	fbt->fbtp_name = name;
120 	fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
121 	    name, FBT_ENTRY, 2, fbt);
122 	fbt->fbtp_patchpoint = instr;
123 	fbt->fbtp_ctl = lf;
124 	fbt->fbtp_loadcnt = lf->loadcnt;
125 	fbt->fbtp_savedval = *instr;
126 	fbt->fbtp_patchval = FBT_BREAKPOINT;
127 	fbt->fbtp_rval = DTRACE_INVOP_PUSHM;
128 	fbt->fbtp_symindx = symindx;
129 
130 	fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
131 	fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
132 
133 	lf->fbt_nentries++;
134 
135 	popm = FBT_POPM | ((*instr) & 0x3FFF) | 0x8000;
136 
137 	retfbt = NULL;
138 again:
139 	for (; instr < limit; instr++) {
140 		if (*instr == popm)
141 			break;
142 		else if ((*instr & 0xff000000) == FBT_JUMP) {
143 			uint32_t *target, *start;
144 			int offset;
145 
146 			offset = (*instr & 0xffffff);
147 			offset <<= 8;
148 			offset /= 64;
149 			target = instr + (2 + offset);
150 			start = (uint32_t *)symval->value;
151 			if (target >= limit || target < start)
152 				break;
153 		}
154 	}
155 
156 	if (instr >= limit)
157 		return (0);
158 
159 	/*
160 	 * We have a winner!
161 	 */
162 	fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO);
163 	fbt->fbtp_name = name;
164 	if (retfbt == NULL) {
165 		fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
166 		    name, FBT_RETURN, 2, fbt);
167 	} else {
168 		retfbt->fbtp_probenext = fbt;
169 		fbt->fbtp_id = retfbt->fbtp_id;
170 	}
171 	retfbt = fbt;
172 
173 	fbt->fbtp_patchpoint = instr;
174 	fbt->fbtp_ctl = lf;
175 	fbt->fbtp_loadcnt = lf->loadcnt;
176 	fbt->fbtp_symindx = symindx;
177 	if ((*instr & 0xff000000) == FBT_JUMP)
178 		fbt->fbtp_rval = DTRACE_INVOP_B;
179 	else
180 		fbt->fbtp_rval = DTRACE_INVOP_POPM;
181 	fbt->fbtp_roffset = (uintptr_t)instr - (uintptr_t)symval->value;
182 	fbt->fbtp_savedval = *instr;
183 	fbt->fbtp_patchval = FBT_BREAKPOINT;
184 	fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
185 	fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
186 
187 	lf->fbt_nentries++;
188 
189 	instr++;
190 	goto again;
191 }
192