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