xref: /freebsd/sys/cddl/dev/fbt/aarch64/fbt_isa.c (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
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  * Portions Copyright 2015 Ruslan Bukin <br@bsdpad.com>
25  *
26  * $FreeBSD$
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 
39 #include "fbt.h"
40 
41 #define	AARCH64_BRK		0xd4200000
42 #define	AARCH64_BRK_IMM16_SHIFT	5
43 #define	AARCH64_BRK_IMM16_VAL	(0x40d << AARCH64_BRK_IMM16_SHIFT)
44 #define	FBT_PATCHVAL		(AARCH64_BRK | AARCH64_BRK_IMM16_VAL)
45 #define	FBT_ENTRY	"entry"
46 #define	FBT_RETURN	"return"
47 
48 int
49 fbt_invop(uintptr_t addr, struct trapframe *frame, uintptr_t rval)
50 {
51 	solaris_cpu_t *cpu;
52 	fbt_probe_t *fbt;
53 
54 	cpu = &solaris_cpu[curcpu];
55 	fbt = fbt_probetab[FBT_ADDR2NDX(addr)];
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 			dtrace_probe(fbt->fbtp_id, frame->tf_x[0],
62 			    frame->tf_x[1], frame->tf_x[2],
63 			    frame->tf_x[3], frame->tf_x[4]);
64 
65 			cpu->cpu_dtrace_caller = 0;
66 			return (fbt->fbtp_savedval);
67 		}
68 	}
69 
70 	return (0);
71 }
72 
73 void
74 fbt_patch_tracepoint(fbt_probe_t *fbt, fbt_patchval_t val)
75 {
76 
77 	*fbt->fbtp_patchpoint = val;
78 	cpu_icache_sync_range((vm_offset_t)fbt->fbtp_patchpoint, 4);
79 }
80 
81 int
82 fbt_provide_module_function(linker_file_t lf, int symindx,
83     linker_symval_t *symval, void *opaque)
84 {
85 	fbt_probe_t *fbt, *retfbt;
86 	uint32_t *target, *start;
87 	uint32_t *instr, *limit;
88 	const char *name;
89 	char *modname;
90 	bool found;
91 	int offs;
92 
93 	modname = opaque;
94 	name = symval->name;
95 
96 	/* Check if function is excluded from instrumentation */
97 	if (fbt_excluded(name))
98 		return (0);
99 
100 	/*
101 	 * Instrumenting certain exception handling functions can lead to FBT
102 	 * recursion, so exclude from instrumentation.
103 	 */
104 	 if (strcmp(name, "handle_el1h_sync") == 0 ||
105 	    strcmp(name, "do_el1h_sync") == 0)
106 		return (1);
107 
108 	instr = (uint32_t *)(symval->value);
109 	limit = (uint32_t *)(symval->value + symval->size);
110 
111 	/* Look for stp (pre-indexed) operation */
112 	found = false;
113 	for (; instr < limit; instr++) {
114 		/* Some functions start with "stp xt1, xt2, [xn, <const>]!" */
115 		if ((*instr & LDP_STP_MASK) == STP_64) {
116 			/*
117 			 * Assume any other store of this type means we
118 			 * are past the function prolog.
119 			 */
120 			if (((*instr >> ADDR_SHIFT) & ADDR_MASK) == 31)
121 				found = true;
122 			break;
123 		}
124 
125 		/*
126 		 * Some functions start with a "sub sp, sp, <const>"
127 		 * Sometimes the compiler will have a sub instruction that
128 		 * is not of the above type so don't stop if we see one.
129 		 */
130 		if ((*instr & SUB_MASK) == SUB_INSTR &&
131 		    ((*instr >> SUB_RD_SHIFT) & SUB_R_MASK) == 31 &&
132 		    ((*instr >> SUB_RN_SHIFT) & SUB_R_MASK) == 31) {
133 			found = true;
134 			break;
135 		}
136 	}
137 
138 	if (!found)
139 		return (0);
140 
141 	fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO);
142 	fbt->fbtp_name = name;
143 	fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
144 	    name, FBT_ENTRY, 3, fbt);
145 	fbt->fbtp_patchpoint = instr;
146 	fbt->fbtp_ctl = lf;
147 	fbt->fbtp_loadcnt = lf->loadcnt;
148 	fbt->fbtp_savedval = *instr;
149 	fbt->fbtp_patchval = FBT_PATCHVAL;
150 	if ((*instr & SUB_MASK) == SUB_INSTR)
151 		fbt->fbtp_rval = DTRACE_INVOP_SUB;
152 	else
153 		fbt->fbtp_rval = DTRACE_INVOP_STP;
154 	fbt->fbtp_symindx = symindx;
155 
156 	fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
157 	fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
158 
159 	lf->fbt_nentries++;
160 
161 	retfbt = NULL;
162 again:
163 	for (; instr < limit; instr++) {
164 		if (*instr == RET_INSTR)
165 			break;
166 		else if ((*instr & B_MASK) == B_INSTR) {
167 			offs = (*instr & B_DATA_MASK);
168 			offs *= 4;
169 			target = (instr + offs);
170 			start = (uint32_t *)symval->value;
171 			if (target >= limit || target < start)
172 				break;
173 		}
174 	}
175 
176 	if (instr >= limit)
177 		return (0);
178 
179 	/*
180 	 * We have a winner!
181 	 */
182 	fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO);
183 	fbt->fbtp_name = name;
184 	if (retfbt == NULL) {
185 		fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
186 		    name, FBT_RETURN, 3, fbt);
187 	} else {
188 		retfbt->fbtp_probenext = fbt;
189 		fbt->fbtp_id = retfbt->fbtp_id;
190 	}
191 	retfbt = fbt;
192 
193 	fbt->fbtp_patchpoint = instr;
194 	fbt->fbtp_ctl = lf;
195 	fbt->fbtp_loadcnt = lf->loadcnt;
196 	fbt->fbtp_symindx = symindx;
197 	if ((*instr & B_MASK) == B_INSTR)
198 		fbt->fbtp_rval = DTRACE_INVOP_B;
199 	else
200 		fbt->fbtp_rval = DTRACE_INVOP_RET;
201 	fbt->fbtp_savedval = *instr;
202 	fbt->fbtp_patchval = FBT_PATCHVAL;
203 	fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
204 	fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
205 
206 	lf->fbt_nentries++;
207 
208 	instr++;
209 	goto again;
210 }
211