xref: /freebsd/sys/cddl/dev/fbt/aarch64/fbt_isa.c (revision d411c1d6)
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_AFRAMES	4
46 
47 int
48 fbt_invop(uintptr_t addr, struct trapframe *frame, uintptr_t rval)
49 {
50 	solaris_cpu_t *cpu;
51 	fbt_probe_t *fbt;
52 
53 	cpu = &solaris_cpu[curcpu];
54 	fbt = fbt_probetab[FBT_ADDR2NDX(addr)];
55 
56 	for (; fbt != NULL; fbt = fbt->fbtp_hashnext) {
57 		if ((uintptr_t)fbt->fbtp_patchpoint != addr)
58 			continue;
59 
60 		cpu->cpu_dtrace_caller = addr;
61 
62 		if (fbt->fbtp_roffset == 0) {
63 			dtrace_probe(fbt->fbtp_id, frame->tf_x[0],
64 			    frame->tf_x[1], frame->tf_x[2],
65 			    frame->tf_x[3], frame->tf_x[4]);
66 		} else {
67 			dtrace_probe(fbt->fbtp_id, fbt->fbtp_roffset, rval,
68 			    0, 0, 0);
69 		}
70 		cpu->cpu_dtrace_caller = 0;
71 		return (fbt->fbtp_savedval);
72 	}
73 
74 	return (0);
75 }
76 
77 void
78 fbt_patch_tracepoint(fbt_probe_t *fbt, fbt_patchval_t val)
79 {
80 	vm_offset_t addr;
81 
82 	if (!arm64_get_writable_addr((vm_offset_t)fbt->fbtp_patchpoint, &addr))
83 		panic("%s: Unable to write new instruction", __func__);
84 
85 	*(fbt_patchval_t *)addr = val;
86 	cpu_icache_sync_range((vm_offset_t)fbt->fbtp_patchpoint, 4);
87 }
88 
89 int
90 fbt_provide_module_function(linker_file_t lf, int symindx,
91     linker_symval_t *symval, void *opaque)
92 {
93 	fbt_probe_t *fbt, *retfbt;
94 	uint32_t *target, *start;
95 	uint32_t *instr, *limit;
96 	const char *name;
97 	char *modname;
98 	bool found;
99 	int offs;
100 
101 	modname = opaque;
102 	name = symval->name;
103 
104 	/* Check if function is excluded from instrumentation */
105 	if (fbt_excluded(name))
106 		return (0);
107 
108 	/*
109 	 * Instrumenting certain exception handling functions can lead to FBT
110 	 * recursion, so exclude from instrumentation.
111 	 */
112 	 if (strcmp(name, "handle_el1h_sync") == 0 ||
113 	    strcmp(name, "do_el1h_sync") == 0)
114 		return (1);
115 
116 	instr = (uint32_t *)(symval->value);
117 	limit = (uint32_t *)(symval->value + symval->size);
118 
119 	/*
120 	 * Ignore any bti instruction at the start of the function
121 	 * we need to keep it there for any indirect branches calling
122 	 * the function on Armv8.5+
123 	 */
124 	if ((*instr & BTI_MASK) == BTI_INSTR)
125 		instr++;
126 
127 	/* Look for stp (pre-indexed) operation */
128 	found = false;
129 	/*
130 	 * If the first instruction is a nop it's a specially marked
131 	 * asm function. We only support a nop first as it's not a normal
132 	 * part of the function prologue.
133 	 */
134 	if (*instr == NOP_INSTR)
135 		found = true;
136 	if (!found) {
137 		for (; instr < limit; instr++) {
138 			/*
139 			 * Some functions start with
140 			 * "stp xt1, xt2, [xn, <const>]!"
141 			 */
142 			if ((*instr & LDP_STP_MASK) == STP_64) {
143 				/*
144 				 * Assume any other store of this type means we
145 				 * are past the function prolog.
146 				 */
147 				if (((*instr >> ADDR_SHIFT) & ADDR_MASK) == 31)
148 					found = true;
149 				break;
150 			}
151 
152 			/*
153 			 * Some functions start with a "sub sp, sp, <const>"
154 			 * Sometimes the compiler will have a sub instruction
155 			 * that is not of the above type so don't stop if we
156 			 * see one.
157 			 */
158 			if ((*instr & SUB_MASK) == SUB_INSTR &&
159 			    ((*instr >> SUB_RD_SHIFT) & SUB_R_MASK) == 31 &&
160 			    ((*instr >> SUB_RN_SHIFT) & SUB_R_MASK) == 31) {
161 				found = true;
162 				break;
163 			}
164 		}
165 	}
166 
167 	if (!found)
168 		return (0);
169 
170 	fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO);
171 	fbt->fbtp_name = name;
172 	fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
173 	    name, FBT_ENTRY, FBT_AFRAMES, fbt);
174 	fbt->fbtp_patchpoint = instr;
175 	fbt->fbtp_ctl = lf;
176 	fbt->fbtp_loadcnt = lf->loadcnt;
177 	fbt->fbtp_savedval = *instr;
178 	fbt->fbtp_patchval = FBT_PATCHVAL;
179 	if ((*instr & SUB_MASK) == SUB_INSTR)
180 		fbt->fbtp_rval = DTRACE_INVOP_SUB;
181 	else
182 		fbt->fbtp_rval = DTRACE_INVOP_STP;
183 	fbt->fbtp_symindx = symindx;
184 
185 	fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
186 	fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
187 
188 	lf->fbt_nentries++;
189 
190 	retfbt = NULL;
191 again:
192 	for (; instr < limit; instr++) {
193 		if (*instr == RET_INSTR)
194 			break;
195 		else if ((*instr & B_MASK) == B_INSTR) {
196 			offs = (*instr & B_DATA_MASK);
197 			offs *= 4;
198 			target = (instr + offs);
199 			start = (uint32_t *)symval->value;
200 			if (target >= limit || target < start)
201 				break;
202 		}
203 	}
204 
205 	if (instr >= limit)
206 		return (0);
207 
208 	/*
209 	 * We have a winner!
210 	 */
211 	fbt = malloc(sizeof (fbt_probe_t), M_FBT, M_WAITOK | M_ZERO);
212 	fbt->fbtp_name = name;
213 	if (retfbt == NULL) {
214 		fbt->fbtp_id = dtrace_probe_create(fbt_id, modname,
215 		    name, FBT_RETURN, FBT_AFRAMES, fbt);
216 	} else {
217 		retfbt->fbtp_probenext = fbt;
218 		fbt->fbtp_id = retfbt->fbtp_id;
219 	}
220 	retfbt = fbt;
221 
222 	fbt->fbtp_patchpoint = instr;
223 	fbt->fbtp_ctl = lf;
224 	fbt->fbtp_loadcnt = lf->loadcnt;
225 	fbt->fbtp_symindx = symindx;
226 	if ((*instr & B_MASK) == B_INSTR)
227 		fbt->fbtp_rval = DTRACE_INVOP_B;
228 	else
229 		fbt->fbtp_rval = DTRACE_INVOP_RET;
230 	fbt->fbtp_roffset = (uintptr_t)instr - (uintptr_t)symval->value;
231 	fbt->fbtp_savedval = *instr;
232 	fbt->fbtp_patchval = FBT_PATCHVAL;
233 	fbt->fbtp_hashnext = fbt_probetab[FBT_ADDR2NDX(instr)];
234 	fbt_probetab[FBT_ADDR2NDX(instr)] = fbt;
235 
236 	lf->fbt_nentries++;
237 
238 	instr++;
239 	goto again;
240 }
241