xref: /freebsd/sys/cddl/dev/dtrace/aarch64/dtrace_isa.c (revision e9d419a05357036ea2fd37218d853d2c713d55cc)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  *
22  * $FreeBSD$
23  */
24 /*
25  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
26  * Use is subject to license terms.
27  */
28 #include <sys/cdefs.h>
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/stack.h>
34 #include <sys/pcpu.h>
35 
36 #include <machine/frame.h>
37 #include <machine/md_var.h>
38 #include <machine/reg.h>
39 
40 #include <vm/vm.h>
41 #include <vm/vm_param.h>
42 #include <vm/pmap.h>
43 
44 #include <machine/atomic.h>
45 #include <machine/db_machdep.h>
46 #include <machine/md_var.h>
47 #include <machine/stack.h>
48 #include <ddb/db_sym.h>
49 #include <ddb/ddb.h>
50 #include <sys/kdb.h>
51 
52 #include "regset.h"
53 
54 /*
55  * Wee need some reasonable default to prevent backtrace code
56  * from wandering too far
57  */
58 #define	MAX_FUNCTION_SIZE 0x10000
59 #define	MAX_PROLOGUE_SIZE 0x100
60 #define	MAX_USTACK_DEPTH  2048
61 
62 uint8_t dtrace_fuword8_nocheck(void *);
63 uint16_t dtrace_fuword16_nocheck(void *);
64 uint32_t dtrace_fuword32_nocheck(void *);
65 uint64_t dtrace_fuword64_nocheck(void *);
66 
67 void
68 dtrace_getpcstack(pc_t *pcstack, int pcstack_limit, int aframes,
69     uint32_t *intrpc)
70 {
71 	struct unwind_state state;
72 	int scp_offset;
73 	int depth;
74 
75 	depth = 0;
76 
77 	if (intrpc != 0) {
78 		pcstack[depth++] = (pc_t) intrpc;
79 	}
80 
81 	aframes++;
82 
83 	state.fp = (uintptr_t)__builtin_frame_address(0);
84 	state.pc = (uintptr_t)dtrace_getpcstack;
85 
86 	while (depth < pcstack_limit) {
87 		if (!unwind_frame(curthread, &state))
88 			break;
89 		if (!INKERNEL(state.pc))
90 			break;
91 
92 		/*
93 		 * NB: Unlike some other architectures, we don't need to
94 		 * explicitly insert cpu_dtrace_caller as it appears in the
95 		 * normal kernel stack trace rather than a special trap frame.
96 		 */
97 		if (aframes > 0) {
98 			aframes--;
99 		} else {
100 			pcstack[depth++] = state.pc;
101 		}
102 
103 	}
104 
105 	for (; depth < pcstack_limit; depth++) {
106 		pcstack[depth] = 0;
107 	}
108 }
109 
110 static int
111 dtrace_getustack_common(uint64_t *pcstack, int pcstack_limit, uintptr_t pc,
112     uintptr_t fp)
113 {
114 	volatile uint16_t *flags =
115 	    (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags;
116 	int ret = 0;
117 	uintptr_t oldfp = fp;
118 
119 	ASSERT(pcstack == NULL || pcstack_limit > 0);
120 
121 	while (pc != 0) {
122 		/*
123 		 * We limit the number of times we can go around this
124 		 * loop to account for a circular stack.
125 		 */
126 		if (ret++ >= MAX_USTACK_DEPTH) {
127 			*flags |= CPU_DTRACE_BADSTACK;
128 			cpu_core[curcpu].cpuc_dtrace_illval = fp;
129 			break;
130 		}
131 
132 		if (pcstack != NULL) {
133 			*pcstack++ = (uint64_t)pc;
134 			pcstack_limit--;
135 			if (pcstack_limit <= 0)
136 				break;
137 		}
138 
139 		if (fp == 0)
140 			break;
141 
142 		pc = dtrace_fuword64((void *)(fp +
143 		    offsetof(struct arm64_frame, f_retaddr)));
144 		fp = dtrace_fuword64((void *)fp);
145 
146 		if (fp == oldfp) {
147 			*flags |= CPU_DTRACE_BADSTACK;
148 			cpu_core[curcpu].cpuc_dtrace_illval = fp;
149 			break;
150 		}
151 
152 		/*
153 		 * ARM64TODO:
154 		 *     This workaround might not be necessary. It needs to be
155 		 *     revised and removed from all architectures if found
156 		 *     unwanted. Leaving the original x86 comment for reference.
157 		 *
158 		 * This is totally bogus:  if we faulted, we're going to clear
159 		 * the fault and break.  This is to deal with the apparently
160 		 * broken Java stacks on x86.
161 		 */
162 		if (*flags & CPU_DTRACE_FAULT) {
163 			*flags &= ~CPU_DTRACE_FAULT;
164 			break;
165 		}
166 
167 		oldfp = fp;
168 	}
169 
170 	return (ret);
171 }
172 
173 void
174 dtrace_getupcstack(uint64_t *pcstack, int pcstack_limit)
175 {
176 	proc_t *p = curproc;
177 	struct trapframe *tf;
178 	uintptr_t pc, fp;
179 	volatile uint16_t *flags =
180 	    (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags;
181 	int n;
182 
183 	if (*flags & CPU_DTRACE_FAULT)
184 		return;
185 
186 	if (pcstack_limit <= 0)
187 		return;
188 
189 	/*
190 	 * If there's no user context we still need to zero the stack.
191 	 */
192 	if (p == NULL || (tf = curthread->td_frame) == NULL)
193 		goto zero;
194 
195 	*pcstack++ = (uint64_t)p->p_pid;
196 	pcstack_limit--;
197 
198 	if (pcstack_limit <= 0)
199 		return;
200 
201 	pc = tf->tf_elr;
202 	fp = tf->tf_x[29];
203 
204 	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
205 		/*
206 		 * In an entry probe.  The frame pointer has not yet been
207 		 * pushed (that happens in the function prologue).  The
208 		 * best approach is to add the current pc as a missing top
209 		 * of stack and back the pc up to the caller, which is stored
210 		 * at the current stack pointer address since the call
211 		 * instruction puts it there right before the branch.
212 		 */
213 
214 		*pcstack++ = (uint64_t)pc;
215 		pcstack_limit--;
216 		if (pcstack_limit <= 0)
217 			return;
218 
219 		pc = tf->tf_lr;
220 	}
221 
222 	n = dtrace_getustack_common(pcstack, pcstack_limit, pc, fp);
223 	ASSERT(n >= 0);
224 	ASSERT(n <= pcstack_limit);
225 
226 	pcstack += n;
227 	pcstack_limit -= n;
228 
229 zero:
230 	while (pcstack_limit-- > 0)
231 		*pcstack++ = 0;
232 }
233 
234 int
235 dtrace_getustackdepth(void)
236 {
237 
238 	printf("IMPLEMENT ME: %s\n", __func__);
239 
240 	return (0);
241 }
242 
243 void
244 dtrace_getufpstack(uint64_t *pcstack, uint64_t *fpstack, int pcstack_limit)
245 {
246 
247 	printf("IMPLEMENT ME: %s\n", __func__);
248 }
249 
250 /*ARGSUSED*/
251 uint64_t
252 dtrace_getarg(int arg, int aframes)
253 {
254 
255 	printf("IMPLEMENT ME: %s\n", __func__);
256 
257 	return (0);
258 }
259 
260 int
261 dtrace_getstackdepth(int aframes)
262 {
263 	struct unwind_state state;
264 	int scp_offset;
265 	int depth;
266 	bool done;
267 
268 	depth = 1;
269 	done = false;
270 
271 	state.fp = (uintptr_t)__builtin_frame_address(0);
272 	state.pc = (uintptr_t)dtrace_getstackdepth;
273 
274 	do {
275 		done = !unwind_frame(curthread, &state);
276 		if (!INKERNEL(state.pc) || !INKERNEL(state.fp))
277 			break;
278 		depth++;
279 	} while (!done);
280 
281 	if (depth < aframes)
282 		return (0);
283 	else
284 		return (depth - aframes);
285 }
286 
287 ulong_t
288 dtrace_getreg(struct trapframe *rp, uint_t reg)
289 {
290 
291 	printf("IMPLEMENT ME: %s\n", __func__);
292 
293 	return (0);
294 }
295 
296 static int
297 dtrace_copycheck(uintptr_t uaddr, uintptr_t kaddr, size_t size)
298 {
299 
300 	if (uaddr + size > VM_MAXUSER_ADDRESS || uaddr + size < uaddr) {
301 		DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
302 		cpu_core[curcpu].cpuc_dtrace_illval = uaddr;
303 		return (0);
304 	}
305 
306 	return (1);
307 }
308 
309 void
310 dtrace_copyin(uintptr_t uaddr, uintptr_t kaddr, size_t size,
311     volatile uint16_t *flags)
312 {
313 
314 	if (dtrace_copycheck(uaddr, kaddr, size))
315 		dtrace_copy(uaddr, kaddr, size);
316 }
317 
318 void
319 dtrace_copyout(uintptr_t kaddr, uintptr_t uaddr, size_t size,
320     volatile uint16_t *flags)
321 {
322 
323 	if (dtrace_copycheck(uaddr, kaddr, size))
324 		dtrace_copy(kaddr, uaddr, size);
325 }
326 
327 void
328 dtrace_copyinstr(uintptr_t uaddr, uintptr_t kaddr, size_t size,
329     volatile uint16_t *flags)
330 {
331 
332 	if (dtrace_copycheck(uaddr, kaddr, size))
333 		dtrace_copystr(uaddr, kaddr, size, flags);
334 }
335 
336 void
337 dtrace_copyoutstr(uintptr_t kaddr, uintptr_t uaddr, size_t size,
338     volatile uint16_t *flags)
339 {
340 
341 	if (dtrace_copycheck(uaddr, kaddr, size))
342 		dtrace_copystr(kaddr, uaddr, size, flags);
343 }
344 
345 uint8_t
346 dtrace_fuword8(void *uaddr)
347 {
348 
349 	if ((uintptr_t)uaddr > VM_MAXUSER_ADDRESS) {
350 		DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
351 		cpu_core[curcpu].cpuc_dtrace_illval = (uintptr_t)uaddr;
352 		return (0);
353 	}
354 
355 	return (dtrace_fuword8_nocheck(uaddr));
356 }
357 
358 uint16_t
359 dtrace_fuword16(void *uaddr)
360 {
361 
362 	if ((uintptr_t)uaddr > VM_MAXUSER_ADDRESS) {
363 		DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
364 		cpu_core[curcpu].cpuc_dtrace_illval = (uintptr_t)uaddr;
365 		return (0);
366 	}
367 
368 	return (dtrace_fuword16_nocheck(uaddr));
369 }
370 
371 uint32_t
372 dtrace_fuword32(void *uaddr)
373 {
374 
375 	if ((uintptr_t)uaddr > VM_MAXUSER_ADDRESS) {
376 		DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
377 		cpu_core[curcpu].cpuc_dtrace_illval = (uintptr_t)uaddr;
378 		return (0);
379 	}
380 
381 	return (dtrace_fuword32_nocheck(uaddr));
382 }
383 
384 uint64_t
385 dtrace_fuword64(void *uaddr)
386 {
387 
388 	if ((uintptr_t)uaddr > VM_MAXUSER_ADDRESS) {
389 		DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
390 		cpu_core[curcpu].cpuc_dtrace_illval = (uintptr_t)uaddr;
391 		return (0);
392 	}
393 
394 	return (dtrace_fuword64_nocheck(uaddr));
395 }
396