xref: /freebsd/sys/cddl/dev/fbt/arm/fbt_isa.c (revision 4a8169d97b36df6420959c773a502fd5cfb25bfc)
1fcb56067SGeorge V. Neville-Neil /*
2fcb56067SGeorge V. Neville-Neil  * CDDL HEADER START
3fcb56067SGeorge V. Neville-Neil  *
4fcb56067SGeorge V. Neville-Neil  * The contents of this file are subject to the terms of the
5fcb56067SGeorge V. Neville-Neil  * Common Development and Distribution License (the "License").
6fcb56067SGeorge V. Neville-Neil  * You may not use this file except in compliance with the License.
7fcb56067SGeorge V. Neville-Neil  *
8fcb56067SGeorge V. Neville-Neil  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fcb56067SGeorge V. Neville-Neil  * or http://www.opensolaris.org/os/licensing.
10fcb56067SGeorge V. Neville-Neil  * See the License for the specific language governing permissions
11fcb56067SGeorge V. Neville-Neil  * and limitations under the License.
12fcb56067SGeorge V. Neville-Neil  *
13fcb56067SGeorge V. Neville-Neil  * When distributing Covered Code, include this CDDL HEADER in each
14fcb56067SGeorge V. Neville-Neil  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fcb56067SGeorge V. Neville-Neil  * If applicable, add the following below this CDDL HEADER, with the
16fcb56067SGeorge V. Neville-Neil  * fields enclosed by brackets "[]" replaced with your own identifying
17fcb56067SGeorge V. Neville-Neil  * information: Portions Copyright [yyyy] [name of copyright owner]
18fcb56067SGeorge V. Neville-Neil  *
19fcb56067SGeorge V. Neville-Neil  * CDDL HEADER END
20fcb56067SGeorge V. Neville-Neil  *
21fcb56067SGeorge V. Neville-Neil  * Portions Copyright 2006-2008 John Birrell jb@freebsd.org
22fcb56067SGeorge V. Neville-Neil  * Portions Copyright 2013 Justin Hibbits jhibbits@freebsd.org
23fcb56067SGeorge V. Neville-Neil  * Portions Copyright 2013 Howard Su howardsu@freebsd.org
24fcb56067SGeorge V. Neville-Neil  *
25fcb56067SGeorge V. Neville-Neil  * $FreeBSD$
26fcb56067SGeorge V. Neville-Neil  *
27fcb56067SGeorge V. Neville-Neil  */
28fcb56067SGeorge V. Neville-Neil 
29fcb56067SGeorge V. Neville-Neil /*
30fcb56067SGeorge V. Neville-Neil  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
31fcb56067SGeorge V. Neville-Neil  * Use is subject to license terms.
32fcb56067SGeorge V. Neville-Neil  */
33fcb56067SGeorge V. Neville-Neil 
34fcb56067SGeorge V. Neville-Neil #include <sys/cdefs.h>
35fcb56067SGeorge V. Neville-Neil #include <sys/param.h>
36fcb56067SGeorge V. Neville-Neil 
37fcb56067SGeorge V. Neville-Neil #include <sys/dtrace.h>
38fcb56067SGeorge V. Neville-Neil 
39a57dc98eSGeorge V. Neville-Neil #include "fbt.h"
40fcb56067SGeorge V. Neville-Neil 
41*4a8169d9SAndrew Turner #define	FBT_PATCHVAL		0xe7f000f0 /* Specified undefined instruction */
42fcb56067SGeorge V. Neville-Neil 
43fcb56067SGeorge V. Neville-Neil #define	FBT_PUSHM		0xe92d0000
44fcb56067SGeorge V. Neville-Neil #define	FBT_POPM		0xe8bd0000
45fcb56067SGeorge V. Neville-Neil #define	FBT_JUMP		0xea000000
46fcb56067SGeorge V. Neville-Neil 
47fcb56067SGeorge V. Neville-Neil #define	FBT_ENTRY	"entry"
48fcb56067SGeorge V. Neville-Neil #define	FBT_RETURN	"return"
49fcb56067SGeorge V. Neville-Neil 
50a57dc98eSGeorge V. Neville-Neil int
51fcb56067SGeorge V. Neville-Neil fbt_invop(uintptr_t addr, uintptr_t *stack, uintptr_t rval)
52fcb56067SGeorge V. Neville-Neil {
53fcb56067SGeorge V. Neville-Neil 	struct trapframe *frame = (struct trapframe *)stack;
54fcb56067SGeorge V. Neville-Neil 	solaris_cpu_t *cpu = &solaris_cpu[curcpu];
55fcb56067SGeorge V. Neville-Neil 	fbt_probe_t *fbt = fbt_probetab[FBT_ADDR2NDX(addr)];
56fcb56067SGeorge V. Neville-Neil 
57fcb56067SGeorge V. Neville-Neil 	for (; fbt != NULL; fbt = fbt->fbtp_hashnext) {
58fcb56067SGeorge V. Neville-Neil 		if ((uintptr_t)fbt->fbtp_patchpoint == addr) {
59fcb56067SGeorge V. Neville-Neil 			fbt->fbtp_invop_cnt++;
60fcb56067SGeorge V. Neville-Neil 			cpu->cpu_dtrace_caller = addr;
61fcb56067SGeorge V. Neville-Neil 
62a57dc98eSGeorge V. Neville-Neil 			/* TODO: Need 5th parameter from stack */
63fcb56067SGeorge V. Neville-Neil 			dtrace_probe(fbt->fbtp_id, frame->tf_r0,
64fcb56067SGeorge V. Neville-Neil 			    frame->tf_r1, frame->tf_r2,
65a57dc98eSGeorge V. Neville-Neil 			    frame->tf_r3, 0);
66fcb56067SGeorge V. Neville-Neil 
67fcb56067SGeorge V. Neville-Neil 			cpu->cpu_dtrace_caller = 0;
68fcb56067SGeorge V. Neville-Neil 
69*4a8169d9SAndrew Turner 			return (fbt->fbtp_rval | (fbt->fbtp_savedval << DTRACE_INVOP_SHIFT));
70fcb56067SGeorge V. Neville-Neil 		}
71fcb56067SGeorge V. Neville-Neil 	}
72fcb56067SGeorge V. Neville-Neil 
73fcb56067SGeorge V. Neville-Neil 	return (0);
74fcb56067SGeorge V. Neville-Neil }
75fcb56067SGeorge V. Neville-Neil 
76a57dc98eSGeorge V. Neville-Neil void
77a57dc98eSGeorge V. Neville-Neil fbt_patch_tracepoint(fbt_probe_t *fbt, fbt_patchval_t val)
78a57dc98eSGeorge V. Neville-Neil {
79a57dc98eSGeorge V. Neville-Neil 
80a57dc98eSGeorge V. Neville-Neil 	*fbt->fbtp_patchpoint = val;
81a57dc98eSGeorge V. Neville-Neil 	cpu_icache_sync_range((vm_offset_t)fbt->fbtp_patchpoint, 4);
82a57dc98eSGeorge V. Neville-Neil }
83a57dc98eSGeorge V. Neville-Neil 
84a57dc98eSGeorge V. Neville-Neil int
85fcb56067SGeorge V. Neville-Neil fbt_provide_module_function(linker_file_t lf, int symindx,
86fcb56067SGeorge V. Neville-Neil     linker_symval_t *symval, void *opaque)
87fcb56067SGeorge V. Neville-Neil {
88fcb56067SGeorge V. Neville-Neil 	char *modname = opaque;
89fcb56067SGeorge V. Neville-Neil 	const char *name = symval->name;
90fcb56067SGeorge V. Neville-Neil 	fbt_probe_t *fbt, *retfbt;
91a57dc98eSGeorge V. Neville-Neil 	uint32_t *instr, *limit;
92fcb56067SGeorge V. Neville-Neil 	int popm;
93fcb56067SGeorge V. Neville-Neil 
94fcb56067SGeorge V. Neville-Neil 	if (strncmp(name, "dtrace_", 7) == 0 &&
95fcb56067SGeorge V. Neville-Neil 	    strncmp(name, "dtrace_safe_", 12) != 0) {
96fcb56067SGeorge V. Neville-Neil 		/*
97fcb56067SGeorge V. Neville-Neil 		 * Anything beginning with "dtrace_" may be called
98fcb56067SGeorge V. Neville-Neil 		 * from probe context unless it explicitly indicates
99fcb56067SGeorge V. Neville-Neil 		 * that it won't be called from probe context by
100fcb56067SGeorge V. Neville-Neil 		 * using the prefix "dtrace_safe_".
101fcb56067SGeorge V. Neville-Neil 		 */
102fcb56067SGeorge V. Neville-Neil 		return (0);
103fcb56067SGeorge V. Neville-Neil 	}
104fcb56067SGeorge V. Neville-Neil 
105fcb56067SGeorge V. Neville-Neil 	if (name[0] == '_' && name[1] == '_')
106fcb56067SGeorge V. Neville-Neil 		return (0);
107fcb56067SGeorge V. Neville-Neil 
108ca162c08SRobert Watson 	/*
109ca162c08SRobert Watson 	 * Architecture-specific exclusion list, largely to do with FBT trap
110ca162c08SRobert Watson 	 * processing, to prevent reentrance.
111ca162c08SRobert Watson 	 */
112ca162c08SRobert Watson 	if (strcmp(name, "undefinedinstruction") == 0)
113ca162c08SRobert Watson 		return (0);
114ca162c08SRobert Watson 
115a57dc98eSGeorge V. Neville-Neil 	instr = (uint32_t *)symval->value;
116a57dc98eSGeorge V. Neville-Neil 	limit = (uint32_t *)(symval->value + symval->size);
117fcb56067SGeorge V. Neville-Neil 
118fcb56067SGeorge V. Neville-Neil 	for (; instr < limit; instr++)
119a57dc98eSGeorge V. Neville-Neil 		if ((*instr & 0xffff0000) == FBT_PUSHM &&
120a57dc98eSGeorge V. Neville-Neil 		    (*instr & 0x4000) != 0)
121fcb56067SGeorge V. Neville-Neil 			break;
122fcb56067SGeorge V. Neville-Neil 
123fcb56067SGeorge V. Neville-Neil 	if (instr >= limit)
124fcb56067SGeorge V. Neville-Neil 		return (0);
125fcb56067SGeorge V. Neville-Neil 
126fcb56067SGeorge V. Neville-Neil 	fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO);
127fcb56067SGeorge V. Neville-Neil 	fbt->fbtp_name = name;
128fcb56067SGeorge V. Neville-Neil 	fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
129fcb56067SGeorge V. Neville-Neil 	    name, FBT_ENTRY, 3, fbt);
130fcb56067SGeorge V. Neville-Neil 	fbt->fbtp_patchpoint = instr;
131fcb56067SGeorge V. Neville-Neil 	fbt->fbtp_ctl = lf;
132fcb56067SGeorge V. Neville-Neil 	fbt->fbtp_loadcnt = lf->loadcnt;
133fcb56067SGeorge V. Neville-Neil 	fbt->fbtp_savedval = *instr;
134fcb56067SGeorge V. Neville-Neil 	fbt->fbtp_patchval = FBT_PATCHVAL;
135fcb56067SGeorge V. Neville-Neil 	fbt->fbtp_rval = DTRACE_INVOP_PUSHM;
136fcb56067SGeorge V. Neville-Neil 	fbt->fbtp_symindx = symindx;
137fcb56067SGeorge V. Neville-Neil 
138fcb56067SGeorge V. Neville-Neil 	fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
139fcb56067SGeorge V. Neville-Neil 	fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
140fcb56067SGeorge V. Neville-Neil 
141fcb56067SGeorge V. Neville-Neil 	lf->fbt_nentries++;
142fcb56067SGeorge V. Neville-Neil 
143fcb56067SGeorge V. Neville-Neil 	popm = FBT_POPM | ((*instr) & 0x3FFF) | 0x8000;
144fcb56067SGeorge V. Neville-Neil 
145fcb56067SGeorge V. Neville-Neil 	retfbt = NULL;
146fcb56067SGeorge V. Neville-Neil again:
147a57dc98eSGeorge V. Neville-Neil 	for (; instr < limit; instr++) {
148fcb56067SGeorge V. Neville-Neil 		if (*instr == popm)
149fcb56067SGeorge V. Neville-Neil 			break;
150a57dc98eSGeorge V. Neville-Neil 		else if ((*instr & 0xff000000) == FBT_JUMP) {
151a57dc98eSGeorge V. Neville-Neil 			uint32_t *target, *start;
152fcb56067SGeorge V. Neville-Neil 			int offset;
153a57dc98eSGeorge V. Neville-Neil 
154fcb56067SGeorge V. Neville-Neil 			offset = (*instr & 0xffffff);
155fcb56067SGeorge V. Neville-Neil 			offset <<= 8;
156fcb56067SGeorge V. Neville-Neil 			offset /= 64;
157fcb56067SGeorge V. Neville-Neil 			target = instr + (2 + offset);
158a57dc98eSGeorge V. Neville-Neil 			start = (uint32_t *)symval->value;
159fcb56067SGeorge V. Neville-Neil 			if (target >= limit || target < start)
160fcb56067SGeorge V. Neville-Neil 				break;
161a57dc98eSGeorge V. Neville-Neil 			instr++; /* skip delay slot */
162fcb56067SGeorge V. Neville-Neil 		}
163fcb56067SGeorge V. Neville-Neil 	}
164fcb56067SGeorge V. Neville-Neil 
165fcb56067SGeorge V. Neville-Neil 	if (instr >= limit)
166fcb56067SGeorge V. Neville-Neil 		return (0);
167fcb56067SGeorge V. Neville-Neil 
168fcb56067SGeorge V. Neville-Neil 	/*
169fcb56067SGeorge V. Neville-Neil 	 * We have a winner!
170fcb56067SGeorge V. Neville-Neil 	 */
171fcb56067SGeorge V. Neville-Neil 	fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO);
172fcb56067SGeorge V. Neville-Neil 	fbt->fbtp_name = name;
173fcb56067SGeorge V. Neville-Neil 	if (retfbt == NULL) {
174fcb56067SGeorge V. Neville-Neil 		fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
175fcb56067SGeorge V. Neville-Neil 		    name, FBT_RETURN, 5, fbt);
176fcb56067SGeorge V. Neville-Neil 	} else {
177fcb56067SGeorge V. Neville-Neil 		retfbt->fbtp_next = fbt;
178fcb56067SGeorge V. Neville-Neil 		fbt->fbtp_id = retfbt->fbtp_id;
179fcb56067SGeorge V. Neville-Neil 	}
180fcb56067SGeorge V. Neville-Neil 	retfbt = fbt;
181fcb56067SGeorge V. Neville-Neil 
182fcb56067SGeorge V. Neville-Neil 	fbt->fbtp_patchpoint = instr;
183fcb56067SGeorge V. Neville-Neil 	fbt->fbtp_ctl = lf;
184fcb56067SGeorge V. Neville-Neil 	fbt->fbtp_loadcnt = lf->loadcnt;
185fcb56067SGeorge V. Neville-Neil 	fbt->fbtp_symindx = symindx;
186fcb56067SGeorge V. Neville-Neil 	if ((*instr & 0xff000000) == FBT_JUMP)
187fcb56067SGeorge V. Neville-Neil 		fbt->fbtp_rval = DTRACE_INVOP_B;
188fcb56067SGeorge V. Neville-Neil 	else
189fcb56067SGeorge V. Neville-Neil 		fbt->fbtp_rval = DTRACE_INVOP_POPM;
190fcb56067SGeorge V. Neville-Neil 	fbt->fbtp_savedval = *instr;
191fcb56067SGeorge V. Neville-Neil 	fbt->fbtp_patchval = FBT_PATCHVAL;
192fcb56067SGeorge V. Neville-Neil 	fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
193fcb56067SGeorge V. Neville-Neil 	fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
194fcb56067SGeorge V. Neville-Neil 
195fcb56067SGeorge V. Neville-Neil 	lf->fbt_nentries++;
196fcb56067SGeorge V. Neville-Neil 
197fcb56067SGeorge V. Neville-Neil 	instr++;
198fcb56067SGeorge V. Neville-Neil 	goto again;
199fcb56067SGeorge V. Neville-Neil }
200