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