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