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