xref: /freebsd/sys/cddl/dev/dtrace/i386/dtrace_isa.c (revision 82283cad12a417abfb1469d899b2d7cfb1d38f77)
191eaf3e1SJohn Birrell /*
291eaf3e1SJohn Birrell  * CDDL HEADER START
391eaf3e1SJohn Birrell  *
491eaf3e1SJohn Birrell  * The contents of this file are subject to the terms of the
591eaf3e1SJohn Birrell  * Common Development and Distribution License, Version 1.0 only
691eaf3e1SJohn Birrell  * (the "License").  You may not use this file except in compliance
791eaf3e1SJohn Birrell  * with the License.
891eaf3e1SJohn Birrell  *
991eaf3e1SJohn Birrell  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
1091eaf3e1SJohn Birrell  * or http://www.opensolaris.org/os/licensing.
1191eaf3e1SJohn Birrell  * See the License for the specific language governing permissions
1291eaf3e1SJohn Birrell  * and limitations under the License.
1391eaf3e1SJohn Birrell  *
1491eaf3e1SJohn Birrell  * When distributing Covered Code, include this CDDL HEADER in each
1591eaf3e1SJohn Birrell  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1691eaf3e1SJohn Birrell  * If applicable, add the following below this CDDL HEADER, with the
1791eaf3e1SJohn Birrell  * fields enclosed by brackets "[]" replaced with your own identifying
1891eaf3e1SJohn Birrell  * information: Portions Copyright [yyyy] [name of copyright owner]
1991eaf3e1SJohn Birrell  *
2091eaf3e1SJohn Birrell  * CDDL HEADER END
2191eaf3e1SJohn Birrell  */
2291eaf3e1SJohn Birrell /*
2391eaf3e1SJohn Birrell  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
2491eaf3e1SJohn Birrell  * Use is subject to license terms.
2591eaf3e1SJohn Birrell  */
2691eaf3e1SJohn Birrell #include <sys/cdefs.h>
2791eaf3e1SJohn Birrell 
2891eaf3e1SJohn Birrell #include <sys/param.h>
2991eaf3e1SJohn Birrell #include <sys/systm.h>
30*82283cadSMark Johnston #include <sys/dtrace_impl.h>
3191eaf3e1SJohn Birrell #include <sys/kernel.h>
3291eaf3e1SJohn Birrell #include <sys/stack.h>
3391eaf3e1SJohn Birrell #include <sys/pcpu.h>
3491eaf3e1SJohn Birrell 
35c6f5742fSRui Paulo #include <machine/frame.h>
3691eaf3e1SJohn Birrell #include <machine/md_var.h>
37c6f5742fSRui Paulo #include <machine/pcb.h>
3891eaf3e1SJohn Birrell #include <machine/stack.h>
3991eaf3e1SJohn Birrell 
4091eaf3e1SJohn Birrell #include <vm/vm.h>
4191eaf3e1SJohn Birrell #include <vm/vm_param.h>
4291eaf3e1SJohn Birrell #include <vm/pmap.h>
4391eaf3e1SJohn Birrell 
44c6f5742fSRui Paulo #include "regset.h"
45c6f5742fSRui Paulo 
4691eaf3e1SJohn Birrell extern uintptr_t kernbase;
4791eaf3e1SJohn Birrell uintptr_t kernelbase = (uintptr_t) &kernbase;
4891eaf3e1SJohn Birrell 
4991eaf3e1SJohn Birrell uint8_t dtrace_fuword8_nocheck(void *);
5091eaf3e1SJohn Birrell uint16_t dtrace_fuword16_nocheck(void *);
5191eaf3e1SJohn Birrell uint32_t dtrace_fuword32_nocheck(void *);
5291eaf3e1SJohn Birrell uint64_t dtrace_fuword64_nocheck(void *);
5391eaf3e1SJohn Birrell 
5409a15aa3SMark Johnston int	dtrace_ustackdepth_max = 2048;
5509a15aa3SMark Johnston 
5691eaf3e1SJohn Birrell void
5791eaf3e1SJohn Birrell dtrace_getpcstack(pc_t *pcstack, int pcstack_limit, int aframes,
5891eaf3e1SJohn Birrell     uint32_t *intrpc)
5991eaf3e1SJohn Birrell {
6091eaf3e1SJohn Birrell 	int depth = 0;
6191eaf3e1SJohn Birrell 	register_t ebp;
6291eaf3e1SJohn Birrell 	struct i386_frame *frame;
6391eaf3e1SJohn Birrell 	vm_offset_t callpc;
6491eaf3e1SJohn Birrell 	pc_t caller = (pc_t) solaris_cpu[curcpu].cpu_dtrace_caller;
6591eaf3e1SJohn Birrell 
6691eaf3e1SJohn Birrell 	if (intrpc != 0)
6791eaf3e1SJohn Birrell 		pcstack[depth++] = (pc_t) intrpc;
6891eaf3e1SJohn Birrell 
6991eaf3e1SJohn Birrell 	aframes++;
7091eaf3e1SJohn Birrell 
7191eaf3e1SJohn Birrell 	__asm __volatile("movl %%ebp,%0" : "=r" (ebp));
7291eaf3e1SJohn Birrell 
7391eaf3e1SJohn Birrell 	frame = (struct i386_frame *)ebp;
7491eaf3e1SJohn Birrell 	while (depth < pcstack_limit) {
755941edfcSJohn Baldwin 		if (!kstack_contains(curthread, (vm_offset_t)frame,
76013a1ae6SKonstantin Belousov 		    sizeof(*frame)))
7791eaf3e1SJohn Birrell 			break;
7891eaf3e1SJohn Birrell 
7991eaf3e1SJohn Birrell 		callpc = frame->f_retaddr;
8091eaf3e1SJohn Birrell 
8191eaf3e1SJohn Birrell 		if (!INKERNEL(callpc))
8291eaf3e1SJohn Birrell 			break;
8391eaf3e1SJohn Birrell 
8491eaf3e1SJohn Birrell 		if (aframes > 0) {
8591eaf3e1SJohn Birrell 			aframes--;
8691eaf3e1SJohn Birrell 			if ((aframes == 0) && (caller != 0)) {
8791eaf3e1SJohn Birrell 				pcstack[depth++] = caller;
8891eaf3e1SJohn Birrell 			}
8991eaf3e1SJohn Birrell 		}
9091eaf3e1SJohn Birrell 		else {
9191eaf3e1SJohn Birrell 			pcstack[depth++] = callpc;
9291eaf3e1SJohn Birrell 		}
9391eaf3e1SJohn Birrell 
945941edfcSJohn Baldwin 		if (frame->f_frame <= frame)
9591eaf3e1SJohn Birrell 			break;
9691eaf3e1SJohn Birrell 		frame = frame->f_frame;
9791eaf3e1SJohn Birrell 	}
9891eaf3e1SJohn Birrell 
9991eaf3e1SJohn Birrell 	for (; depth < pcstack_limit; depth++) {
10091eaf3e1SJohn Birrell 		pcstack[depth] = 0;
10191eaf3e1SJohn Birrell 	}
10291eaf3e1SJohn Birrell }
10391eaf3e1SJohn Birrell 
10491eaf3e1SJohn Birrell static int
10591eaf3e1SJohn Birrell dtrace_getustack_common(uint64_t *pcstack, int pcstack_limit, uintptr_t pc,
10691eaf3e1SJohn Birrell     uintptr_t sp)
10791eaf3e1SJohn Birrell {
108c6f5742fSRui Paulo #ifdef notyet
10991eaf3e1SJohn Birrell 	proc_t *p = curproc;
110c6f5742fSRui Paulo 	uintptr_t oldcontext = lwp->lwp_oldcontext; /* XXX signal stack. */
111c6f5742fSRui Paulo 	size_t s1, s2;
112c6f5742fSRui Paulo #endif
11309a15aa3SMark Johnston 	uintptr_t oldsp;
11491eaf3e1SJohn Birrell 	volatile uint16_t *flags =
11591eaf3e1SJohn Birrell 	    (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags;
11691eaf3e1SJohn Birrell 	int ret = 0;
11791eaf3e1SJohn Birrell 
11891eaf3e1SJohn Birrell 	ASSERT(pcstack == NULL || pcstack_limit > 0);
11909a15aa3SMark Johnston 	ASSERT(dtrace_ustackdepth_max > 0);
12091eaf3e1SJohn Birrell 
121c6f5742fSRui Paulo #ifdef notyet /* XXX signal stack. */
12291eaf3e1SJohn Birrell 	if (p->p_model == DATAMODEL_NATIVE) {
12391eaf3e1SJohn Birrell 		s1 = sizeof (struct frame) + 2 * sizeof (long);
12491eaf3e1SJohn Birrell 		s2 = s1 + sizeof (siginfo_t);
12591eaf3e1SJohn Birrell 	} else {
12691eaf3e1SJohn Birrell 		s1 = sizeof (struct frame32) + 3 * sizeof (int);
12791eaf3e1SJohn Birrell 		s2 = s1 + sizeof (siginfo32_t);
12891eaf3e1SJohn Birrell 	}
129c6f5742fSRui Paulo #endif
13091eaf3e1SJohn Birrell 
131c6f5742fSRui Paulo 	while (pc != 0) {
13209a15aa3SMark Johnston 		/*
13309a15aa3SMark Johnston 		 * We limit the number of times we can go around this
13409a15aa3SMark Johnston 		 * loop to account for a circular stack.
13509a15aa3SMark Johnston 		 */
13609a15aa3SMark Johnston 		if (ret++ >= dtrace_ustackdepth_max) {
13709a15aa3SMark Johnston 			*flags |= CPU_DTRACE_BADSTACK;
13809a15aa3SMark Johnston 			cpu_core[curcpu].cpuc_dtrace_illval = sp;
13909a15aa3SMark Johnston 			break;
14009a15aa3SMark Johnston 		}
14109a15aa3SMark Johnston 
14291eaf3e1SJohn Birrell 		if (pcstack != NULL) {
14391eaf3e1SJohn Birrell 			*pcstack++ = (uint64_t)pc;
14491eaf3e1SJohn Birrell 			pcstack_limit--;
14591eaf3e1SJohn Birrell 			if (pcstack_limit <= 0)
14691eaf3e1SJohn Birrell 				break;
14791eaf3e1SJohn Birrell 		}
14891eaf3e1SJohn Birrell 
149c6f5742fSRui Paulo 		if (sp == 0)
150c6f5742fSRui Paulo 			break;
151c6f5742fSRui Paulo 
15209a15aa3SMark Johnston 		oldsp = sp;
15309a15aa3SMark Johnston 
154c6f5742fSRui Paulo #ifdef notyet /* XXX signal stack. */
15591eaf3e1SJohn Birrell 		if (oldcontext == sp + s1 || oldcontext == sp + s2) {
15691eaf3e1SJohn Birrell 			if (p->p_model == DATAMODEL_NATIVE) {
15791eaf3e1SJohn Birrell 				ucontext_t *ucp = (ucontext_t *)oldcontext;
15891eaf3e1SJohn Birrell 				greg_t *gregs = ucp->uc_mcontext.gregs;
15991eaf3e1SJohn Birrell 
16091eaf3e1SJohn Birrell 				sp = dtrace_fulword(&gregs[REG_FP]);
16191eaf3e1SJohn Birrell 				pc = dtrace_fulword(&gregs[REG_PC]);
16291eaf3e1SJohn Birrell 
16391eaf3e1SJohn Birrell 				oldcontext = dtrace_fulword(&ucp->uc_link);
16491eaf3e1SJohn Birrell 			} else {
16591eaf3e1SJohn Birrell 				ucontext32_t *ucp = (ucontext32_t *)oldcontext;
16691eaf3e1SJohn Birrell 				greg32_t *gregs = ucp->uc_mcontext.gregs;
16791eaf3e1SJohn Birrell 
16891eaf3e1SJohn Birrell 				sp = dtrace_fuword32(&gregs[EBP]);
16991eaf3e1SJohn Birrell 				pc = dtrace_fuword32(&gregs[EIP]);
17091eaf3e1SJohn Birrell 
17191eaf3e1SJohn Birrell 				oldcontext = dtrace_fuword32(&ucp->uc_link);
17291eaf3e1SJohn Birrell 			}
17391eaf3e1SJohn Birrell 		} else {
17491eaf3e1SJohn Birrell 			if (p->p_model == DATAMODEL_NATIVE) {
17591eaf3e1SJohn Birrell 				struct frame *fr = (struct frame *)sp;
17691eaf3e1SJohn Birrell 
17791eaf3e1SJohn Birrell 				pc = dtrace_fulword(&fr->fr_savpc);
17891eaf3e1SJohn Birrell 				sp = dtrace_fulword(&fr->fr_savfp);
17991eaf3e1SJohn Birrell 			} else {
18091eaf3e1SJohn Birrell 				struct frame32 *fr = (struct frame32 *)sp;
18191eaf3e1SJohn Birrell 
18291eaf3e1SJohn Birrell 				pc = dtrace_fuword32(&fr->fr_savpc);
18391eaf3e1SJohn Birrell 				sp = dtrace_fuword32(&fr->fr_savfp);
18491eaf3e1SJohn Birrell 			}
18591eaf3e1SJohn Birrell 		}
186c6f5742fSRui Paulo #else
187c6f5742fSRui Paulo 		pc = dtrace_fuword32((void *)(sp +
188c6f5742fSRui Paulo 			offsetof(struct i386_frame, f_retaddr)));
189c6f5742fSRui Paulo 		sp = dtrace_fuword32((void *)sp);
190c6f5742fSRui Paulo #endif /* ! notyet */
19191eaf3e1SJohn Birrell 
19209a15aa3SMark Johnston 		if (sp == oldsp) {
19309a15aa3SMark Johnston 			*flags |= CPU_DTRACE_BADSTACK;
19409a15aa3SMark Johnston 			cpu_core[curcpu].cpuc_dtrace_illval = sp;
19509a15aa3SMark Johnston 			break;
19609a15aa3SMark Johnston 		}
19709a15aa3SMark Johnston 
19891eaf3e1SJohn Birrell 		/*
19991eaf3e1SJohn Birrell 		 * This is totally bogus:  if we faulted, we're going to clear
20091eaf3e1SJohn Birrell 		 * the fault and break.  This is to deal with the apparently
20191eaf3e1SJohn Birrell 		 * broken Java stacks on x86.
20291eaf3e1SJohn Birrell 		 */
20391eaf3e1SJohn Birrell 		if (*flags & CPU_DTRACE_FAULT) {
20491eaf3e1SJohn Birrell 			*flags &= ~CPU_DTRACE_FAULT;
20591eaf3e1SJohn Birrell 			break;
20691eaf3e1SJohn Birrell 		}
20791eaf3e1SJohn Birrell 	}
20891eaf3e1SJohn Birrell 
20991eaf3e1SJohn Birrell 	return (ret);
21091eaf3e1SJohn Birrell }
21191eaf3e1SJohn Birrell 
21291eaf3e1SJohn Birrell void
21391eaf3e1SJohn Birrell dtrace_getupcstack(uint64_t *pcstack, int pcstack_limit)
21491eaf3e1SJohn Birrell {
21591eaf3e1SJohn Birrell 	proc_t *p = curproc;
216c6f5742fSRui Paulo 	struct trapframe *tf;
217c6f5742fSRui Paulo 	uintptr_t pc, sp, fp;
21891eaf3e1SJohn Birrell 	volatile uint16_t *flags =
21991eaf3e1SJohn Birrell 	    (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags;
22091eaf3e1SJohn Birrell 	int n;
22191eaf3e1SJohn Birrell 
22291eaf3e1SJohn Birrell 	if (*flags & CPU_DTRACE_FAULT)
22391eaf3e1SJohn Birrell 		return;
22491eaf3e1SJohn Birrell 
22591eaf3e1SJohn Birrell 	if (pcstack_limit <= 0)
22691eaf3e1SJohn Birrell 		return;
22791eaf3e1SJohn Birrell 
22891eaf3e1SJohn Birrell 	/*
22991eaf3e1SJohn Birrell 	 * If there's no user context we still need to zero the stack.
23091eaf3e1SJohn Birrell 	 */
231c6f5742fSRui Paulo 	if (p == NULL || (tf = curthread->td_frame) == NULL)
23291eaf3e1SJohn Birrell 		goto zero;
23391eaf3e1SJohn Birrell 
23491eaf3e1SJohn Birrell 	*pcstack++ = (uint64_t)p->p_pid;
23591eaf3e1SJohn Birrell 	pcstack_limit--;
23691eaf3e1SJohn Birrell 
23791eaf3e1SJohn Birrell 	if (pcstack_limit <= 0)
23891eaf3e1SJohn Birrell 		return;
23991eaf3e1SJohn Birrell 
240c6f5742fSRui Paulo 	pc = tf->tf_eip;
241c6f5742fSRui Paulo 	fp = tf->tf_ebp;
242c6f5742fSRui Paulo 	sp = tf->tf_esp;
24391eaf3e1SJohn Birrell 
24491eaf3e1SJohn Birrell 	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
245c6f5742fSRui Paulo 		/*
246c6f5742fSRui Paulo 		 * In an entry probe.  The frame pointer has not yet been
247c6f5742fSRui Paulo 		 * pushed (that happens in the function prologue).  The
248c6f5742fSRui Paulo 		 * best approach is to add the current pc as a missing top
249c6f5742fSRui Paulo 		 * of stack and back the pc up to the caller, which is stored
250c6f5742fSRui Paulo 		 * at the current stack pointer address since the call
251c6f5742fSRui Paulo 		 * instruction puts it there right before the branch.
252c6f5742fSRui Paulo 		 */
253c6f5742fSRui Paulo 
25491eaf3e1SJohn Birrell 		*pcstack++ = (uint64_t)pc;
25591eaf3e1SJohn Birrell 		pcstack_limit--;
25691eaf3e1SJohn Birrell 		if (pcstack_limit <= 0)
25791eaf3e1SJohn Birrell 			return;
25891eaf3e1SJohn Birrell 
259c6f5742fSRui Paulo 		pc = dtrace_fuword32((void *) sp);
26091eaf3e1SJohn Birrell 	}
26191eaf3e1SJohn Birrell 
26291eaf3e1SJohn Birrell 	n = dtrace_getustack_common(pcstack, pcstack_limit, pc, sp);
26391eaf3e1SJohn Birrell 	ASSERT(n >= 0);
26491eaf3e1SJohn Birrell 	ASSERT(n <= pcstack_limit);
26591eaf3e1SJohn Birrell 
26691eaf3e1SJohn Birrell 	pcstack += n;
26791eaf3e1SJohn Birrell 	pcstack_limit -= n;
26891eaf3e1SJohn Birrell 
26991eaf3e1SJohn Birrell zero:
27091eaf3e1SJohn Birrell 	while (pcstack_limit-- > 0)
271c6f5742fSRui Paulo 		*pcstack++ = 0;
27291eaf3e1SJohn Birrell }
27391eaf3e1SJohn Birrell 
27491eaf3e1SJohn Birrell int
27591eaf3e1SJohn Birrell dtrace_getustackdepth(void)
27691eaf3e1SJohn Birrell {
277c6f5742fSRui Paulo 	proc_t *p = curproc;
278c6f5742fSRui Paulo 	struct trapframe *tf;
279c6f5742fSRui Paulo 	uintptr_t pc, fp, sp;
280c6f5742fSRui Paulo 	int n = 0;
281c6f5742fSRui Paulo 
282c6f5742fSRui Paulo 	if (p == NULL || (tf = curthread->td_frame) == NULL)
283c6f5742fSRui Paulo 		return (0);
284c6f5742fSRui Paulo 
285c6f5742fSRui Paulo 	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT))
286c6f5742fSRui Paulo 		return (-1);
287c6f5742fSRui Paulo 
288c6f5742fSRui Paulo 	pc = tf->tf_eip;
289c6f5742fSRui Paulo 	fp = tf->tf_ebp;
290c6f5742fSRui Paulo 	sp = tf->tf_esp;
291c6f5742fSRui Paulo 
292c6f5742fSRui Paulo 	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
293c6f5742fSRui Paulo 		/*
294c6f5742fSRui Paulo 		 * In an entry probe.  The frame pointer has not yet been
295c6f5742fSRui Paulo 		 * pushed (that happens in the function prologue).  The
296c6f5742fSRui Paulo 		 * best approach is to add the current pc as a missing top
297c6f5742fSRui Paulo 		 * of stack and back the pc up to the caller, which is stored
298c6f5742fSRui Paulo 		 * at the current stack pointer address since the call
299c6f5742fSRui Paulo 		 * instruction puts it there right before the branch.
300c6f5742fSRui Paulo 		 */
301c6f5742fSRui Paulo 
302c6f5742fSRui Paulo 		pc = dtrace_fuword32((void *) sp);
303c6f5742fSRui Paulo 		n++;
304c6f5742fSRui Paulo 	}
305c6f5742fSRui Paulo 
306c6f5742fSRui Paulo 	n += dtrace_getustack_common(NULL, 0, pc, fp);
307c6f5742fSRui Paulo 
308c6f5742fSRui Paulo 	return (n);
30991eaf3e1SJohn Birrell }
31091eaf3e1SJohn Birrell 
31191eaf3e1SJohn Birrell void
31291eaf3e1SJohn Birrell dtrace_getufpstack(uint64_t *pcstack, uint64_t *fpstack, int pcstack_limit)
31391eaf3e1SJohn Birrell {
31491eaf3e1SJohn Birrell 	proc_t *p = curproc;
315c6f5742fSRui Paulo 	struct trapframe *tf;
316c6f5742fSRui Paulo 	uintptr_t pc, sp, fp;
31791eaf3e1SJohn Birrell 	volatile uint16_t *flags =
31891eaf3e1SJohn Birrell 	    (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags;
319c6f5742fSRui Paulo #ifdef notyet /* XXX signal stack */
320c6f5742fSRui Paulo 	uintptr_t oldcontext;
32191eaf3e1SJohn Birrell 	size_t s1, s2;
322c6f5742fSRui Paulo #endif
32391eaf3e1SJohn Birrell 
32491eaf3e1SJohn Birrell 	if (*flags & CPU_DTRACE_FAULT)
32591eaf3e1SJohn Birrell 		return;
32691eaf3e1SJohn Birrell 
32791eaf3e1SJohn Birrell 	if (pcstack_limit <= 0)
32891eaf3e1SJohn Birrell 		return;
32991eaf3e1SJohn Birrell 
33091eaf3e1SJohn Birrell 	/*
33191eaf3e1SJohn Birrell 	 * If there's no user context we still need to zero the stack.
33291eaf3e1SJohn Birrell 	 */
333c6f5742fSRui Paulo 	if (p == NULL || (tf = curthread->td_frame) == NULL)
33491eaf3e1SJohn Birrell 		goto zero;
33591eaf3e1SJohn Birrell 
33691eaf3e1SJohn Birrell 	*pcstack++ = (uint64_t)p->p_pid;
33791eaf3e1SJohn Birrell 	pcstack_limit--;
33891eaf3e1SJohn Birrell 
33991eaf3e1SJohn Birrell 	if (pcstack_limit <= 0)
34091eaf3e1SJohn Birrell 		return;
34191eaf3e1SJohn Birrell 
342c6f5742fSRui Paulo 	pc = tf->tf_eip;
343c6f5742fSRui Paulo 	fp = tf->tf_ebp;
344c6f5742fSRui Paulo 	sp = tf->tf_esp;
345c6f5742fSRui Paulo 
346c6f5742fSRui Paulo #ifdef notyet /* XXX signal stack */
34791eaf3e1SJohn Birrell 	oldcontext = lwp->lwp_oldcontext;
34891eaf3e1SJohn Birrell 
34991eaf3e1SJohn Birrell 	if (p->p_model == DATAMODEL_NATIVE) {
35091eaf3e1SJohn Birrell 		s1 = sizeof (struct frame) + 2 * sizeof (long);
35191eaf3e1SJohn Birrell 		s2 = s1 + sizeof (siginfo_t);
35291eaf3e1SJohn Birrell 	} else {
35391eaf3e1SJohn Birrell 		s1 = sizeof (struct frame32) + 3 * sizeof (int);
35491eaf3e1SJohn Birrell 		s2 = s1 + sizeof (siginfo32_t);
35591eaf3e1SJohn Birrell 	}
356c6f5742fSRui Paulo #endif
35791eaf3e1SJohn Birrell 
35891eaf3e1SJohn Birrell 	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
35991eaf3e1SJohn Birrell 		*pcstack++ = (uint64_t)pc;
36091eaf3e1SJohn Birrell 		*fpstack++ = 0;
36191eaf3e1SJohn Birrell 		pcstack_limit--;
36291eaf3e1SJohn Birrell 		if (pcstack_limit <= 0)
36391eaf3e1SJohn Birrell 			return;
36491eaf3e1SJohn Birrell 
365c6f5742fSRui Paulo 		pc = dtrace_fuword32((void *)sp);
36691eaf3e1SJohn Birrell 	}
36791eaf3e1SJohn Birrell 
368c6f5742fSRui Paulo 	while (pc != 0) {
36991eaf3e1SJohn Birrell 		*pcstack++ = (uint64_t)pc;
370c6f5742fSRui Paulo 		*fpstack++ = fp;
37191eaf3e1SJohn Birrell 		pcstack_limit--;
37291eaf3e1SJohn Birrell 		if (pcstack_limit <= 0)
37391eaf3e1SJohn Birrell 			break;
37491eaf3e1SJohn Birrell 
375c6f5742fSRui Paulo 		if (fp == 0)
376c6f5742fSRui Paulo 			break;
377c6f5742fSRui Paulo 
378c6f5742fSRui Paulo #ifdef notyet /* XXX signal stack */
37991eaf3e1SJohn Birrell 		if (oldcontext == sp + s1 || oldcontext == sp + s2) {
38091eaf3e1SJohn Birrell 			if (p->p_model == DATAMODEL_NATIVE) {
38191eaf3e1SJohn Birrell 				ucontext_t *ucp = (ucontext_t *)oldcontext;
38291eaf3e1SJohn Birrell 				greg_t *gregs = ucp->uc_mcontext.gregs;
38391eaf3e1SJohn Birrell 
38491eaf3e1SJohn Birrell 				sp = dtrace_fulword(&gregs[REG_FP]);
38591eaf3e1SJohn Birrell 				pc = dtrace_fulword(&gregs[REG_PC]);
38691eaf3e1SJohn Birrell 
38791eaf3e1SJohn Birrell 				oldcontext = dtrace_fulword(&ucp->uc_link);
38891eaf3e1SJohn Birrell 			} else {
38991eaf3e1SJohn Birrell 				ucontext_t *ucp = (ucontext_t *)oldcontext;
39091eaf3e1SJohn Birrell 				greg_t *gregs = ucp->uc_mcontext.gregs;
39191eaf3e1SJohn Birrell 
39291eaf3e1SJohn Birrell 				sp = dtrace_fuword32(&gregs[EBP]);
39391eaf3e1SJohn Birrell 				pc = dtrace_fuword32(&gregs[EIP]);
39491eaf3e1SJohn Birrell 
39591eaf3e1SJohn Birrell 				oldcontext = dtrace_fuword32(&ucp->uc_link);
39691eaf3e1SJohn Birrell 			}
397c6f5742fSRui Paulo 		} else
398c6f5742fSRui Paulo #endif /* XXX */
399c6f5742fSRui Paulo 		{
400c6f5742fSRui Paulo 			pc = dtrace_fuword32((void *)(fp +
401c6f5742fSRui Paulo 				offsetof(struct i386_frame, f_retaddr)));
402c6f5742fSRui Paulo 			fp = dtrace_fuword32((void *)fp);
40391eaf3e1SJohn Birrell 		}
40491eaf3e1SJohn Birrell 
40591eaf3e1SJohn Birrell 		/*
40691eaf3e1SJohn Birrell 		 * This is totally bogus:  if we faulted, we're going to clear
40791eaf3e1SJohn Birrell 		 * the fault and break.  This is to deal with the apparently
40891eaf3e1SJohn Birrell 		 * broken Java stacks on x86.
40991eaf3e1SJohn Birrell 		 */
41091eaf3e1SJohn Birrell 		if (*flags & CPU_DTRACE_FAULT) {
41191eaf3e1SJohn Birrell 			*flags &= ~CPU_DTRACE_FAULT;
41291eaf3e1SJohn Birrell 			break;
41391eaf3e1SJohn Birrell 		}
41491eaf3e1SJohn Birrell 	}
41591eaf3e1SJohn Birrell 
41691eaf3e1SJohn Birrell zero:
41791eaf3e1SJohn Birrell 	while (pcstack_limit-- > 0)
418c6f5742fSRui Paulo 		*pcstack++ = 0;
41991eaf3e1SJohn Birrell }
42091eaf3e1SJohn Birrell 
42191eaf3e1SJohn Birrell uint64_t
42291eaf3e1SJohn Birrell dtrace_getarg(int arg, int aframes)
42391eaf3e1SJohn Birrell {
4246c280659SMark Johnston 	struct trapframe *frame;
42591eaf3e1SJohn Birrell 	struct i386_frame *fp = (struct i386_frame *)dtrace_getfp();
4266c280659SMark Johnston 	uintptr_t *stack, val;
42791eaf3e1SJohn Birrell 	int i;
42891eaf3e1SJohn Birrell 
42991eaf3e1SJohn Birrell 	for (i = 1; i <= aframes; i++) {
43091eaf3e1SJohn Birrell 		fp = fp->f_frame;
43191eaf3e1SJohn Birrell 
432*82283cadSMark Johnston 		if (roundup2(fp->f_retaddr, 4) ==
433efa1aff6SMark Johnston 		    (long)dtrace_invop_callsite) {
43491eaf3e1SJohn Birrell 			/*
43591eaf3e1SJohn Birrell 			 * If we pass through the invalid op handler, we will
4366c280659SMark Johnston 			 * use the trap frame pointer that it pushed on the
4376c280659SMark Johnston 			 * stack as the second argument to dtrace_invop() as
4386c280659SMark Johnston 			 * the pointer to the stack.  When using this stack, we
4396c280659SMark Johnston 			 * must skip the third argument to dtrace_invop(),
4406c280659SMark Johnston 			 * which is included in the i386_frame.
44191eaf3e1SJohn Birrell 			 */
4426c280659SMark Johnston 			frame = (struct trapframe *)(((uintptr_t **)&fp[1])[0]);
4436c280659SMark Johnston 			/*
4446c280659SMark Johnston 			 * Skip the three hardware-saved registers and the
4456c280659SMark Johnston 			 * return address.
4466c280659SMark Johnston 			 */
4476c280659SMark Johnston 			stack = (uintptr_t *)frame->tf_isp + 4;
44891eaf3e1SJohn Birrell 			goto load;
44991eaf3e1SJohn Birrell 		}
45091eaf3e1SJohn Birrell 
45191eaf3e1SJohn Birrell 	}
45291eaf3e1SJohn Birrell 
45391eaf3e1SJohn Birrell 	/*
45491eaf3e1SJohn Birrell 	 * We know that we did not come through a trap to get into
45591eaf3e1SJohn Birrell 	 * dtrace_probe() -- the provider simply called dtrace_probe()
45691eaf3e1SJohn Birrell 	 * directly.  As this is the case, we need to shift the argument
45791eaf3e1SJohn Birrell 	 * that we're looking for:  the probe ID is the first argument to
45891eaf3e1SJohn Birrell 	 * dtrace_probe(), so the argument n will actually be found where
45991eaf3e1SJohn Birrell 	 * one would expect to find argument (n + 1).
46091eaf3e1SJohn Birrell 	 */
46191eaf3e1SJohn Birrell 	arg++;
46291eaf3e1SJohn Birrell 
463efa1aff6SMark Johnston 	stack = (uintptr_t *)fp + 2;
46491eaf3e1SJohn Birrell 
46591eaf3e1SJohn Birrell load:
46691eaf3e1SJohn Birrell 	DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
46791eaf3e1SJohn Birrell 	val = stack[arg];
46891eaf3e1SJohn Birrell 	DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
46991eaf3e1SJohn Birrell 
47091eaf3e1SJohn Birrell 	return (val);
47191eaf3e1SJohn Birrell }
47291eaf3e1SJohn Birrell 
47391eaf3e1SJohn Birrell int
47491eaf3e1SJohn Birrell dtrace_getstackdepth(int aframes)
47591eaf3e1SJohn Birrell {
47691eaf3e1SJohn Birrell 	int depth = 0;
47791eaf3e1SJohn Birrell 	struct i386_frame *frame;
47891eaf3e1SJohn Birrell 	vm_offset_t ebp;
47991eaf3e1SJohn Birrell 
48091eaf3e1SJohn Birrell 	aframes++;
48191eaf3e1SJohn Birrell 	ebp = dtrace_getfp();
48291eaf3e1SJohn Birrell 	frame = (struct i386_frame *)ebp;
48391eaf3e1SJohn Birrell 	depth++;
48491eaf3e1SJohn Birrell 	for(;;) {
485013a1ae6SKonstantin Belousov 		if (!kstack_contains(curthread, (vm_offset_t)frame,
486013a1ae6SKonstantin Belousov 		    sizeof(*frame)))
48791eaf3e1SJohn Birrell 			break;
48891eaf3e1SJohn Birrell 		depth++;
4895941edfcSJohn Baldwin 		if (frame->f_frame <= frame)
49091eaf3e1SJohn Birrell 			break;
49191eaf3e1SJohn Birrell 		frame = frame->f_frame;
49291eaf3e1SJohn Birrell 	}
49391eaf3e1SJohn Birrell 	if (depth < aframes)
49491eaf3e1SJohn Birrell 		return 0;
49591eaf3e1SJohn Birrell 	else
49691eaf3e1SJohn Birrell 		return depth - aframes;
49791eaf3e1SJohn Birrell }
49891eaf3e1SJohn Birrell 
49991eaf3e1SJohn Birrell ulong_t
50098ab9802SChristos Margiolis dtrace_getreg(struct trapframe *frame, uint_t reg)
50191eaf3e1SJohn Birrell {
502c6f5742fSRui Paulo 	struct pcb *pcb;
503c6f5742fSRui Paulo 	int regmap[] = {  /* Order is dependent on reg.d */
504c6f5742fSRui Paulo 		REG_GS,		/* 0  GS */
505c6f5742fSRui Paulo 		REG_FS,		/* 1  FS */
506c6f5742fSRui Paulo 		REG_ES,		/* 2  ES */
507c6f5742fSRui Paulo 		REG_DS,		/* 3  DS */
508c6f5742fSRui Paulo 		REG_RDI,	/* 4  EDI */
509c6f5742fSRui Paulo 		REG_RSI,	/* 5  ESI */
510c6f5742fSRui Paulo 		REG_RBP,	/* 6  EBP, REG_FP */
511c6f5742fSRui Paulo 		REG_RSP,	/* 7  ESP */
512c6f5742fSRui Paulo 		REG_RBX,	/* 8  EBX */
513c6f5742fSRui Paulo 		REG_RDX,	/* 9  EDX, REG_R1 */
514c6f5742fSRui Paulo 		REG_RCX,	/* 10 ECX */
515c6f5742fSRui Paulo 		REG_RAX,	/* 11 EAX, REG_R0 */
516c6f5742fSRui Paulo 		REG_TRAPNO,	/* 12 TRAPNO */
517c6f5742fSRui Paulo 		REG_ERR,	/* 13 ERR */
518c6f5742fSRui Paulo 		REG_RIP,	/* 14 EIP, REG_PC */
519c6f5742fSRui Paulo 		REG_CS,		/* 15 CS */
520c6f5742fSRui Paulo 		REG_RFL,	/* 16 EFL, REG_PS */
521c6f5742fSRui Paulo 		REG_RSP,	/* 17 UESP, REG_SP */
522c6f5742fSRui Paulo 		REG_SS		/* 18 SS */
52391eaf3e1SJohn Birrell 	};
52491eaf3e1SJohn Birrell 
525c6f5742fSRui Paulo 	if (reg > SS) {
526c6f5742fSRui Paulo 		DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
527c6f5742fSRui Paulo 		return (0);
528c6f5742fSRui Paulo 	}
529c6f5742fSRui Paulo 
53091eaf3e1SJohn Birrell 	if (reg >= sizeof (regmap) / sizeof (int)) {
53191eaf3e1SJohn Birrell 		DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
53291eaf3e1SJohn Birrell 		return (0);
53391eaf3e1SJohn Birrell 	}
53491eaf3e1SJohn Birrell 
53591eaf3e1SJohn Birrell 	reg = regmap[reg];
53691eaf3e1SJohn Birrell 
53791eaf3e1SJohn Birrell 	switch(reg) {
53891eaf3e1SJohn Birrell 	case REG_GS:
539c6f5742fSRui Paulo 		if ((pcb = curthread->td_pcb) == NULL) {
540c6f5742fSRui Paulo 			DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
541c6f5742fSRui Paulo 			return (0);
542c6f5742fSRui Paulo 		}
543c6f5742fSRui Paulo 		return (pcb->pcb_gs);
544c6f5742fSRui Paulo 	case REG_FS:
54598ab9802SChristos Margiolis 		return (frame->tf_fs);
546c6f5742fSRui Paulo 	case REG_ES:
54798ab9802SChristos Margiolis 		return (frame->tf_es);
548c6f5742fSRui Paulo 	case REG_DS:
54998ab9802SChristos Margiolis 		return (frame->tf_ds);
550c6f5742fSRui Paulo 	case REG_RDI:
55198ab9802SChristos Margiolis 		return (frame->tf_edi);
552c6f5742fSRui Paulo 	case REG_RSI:
55398ab9802SChristos Margiolis 		return (frame->tf_esi);
554c6f5742fSRui Paulo 	case REG_RBP:
55598ab9802SChristos Margiolis 		return (frame->tf_ebp);
55691eaf3e1SJohn Birrell 	case REG_RSP:
55798ab9802SChristos Margiolis 		return (frame->tf_isp);
558c6f5742fSRui Paulo 	case REG_RBX:
55998ab9802SChristos Margiolis 		return (frame->tf_ebx);
560c6f5742fSRui Paulo 	case REG_RCX:
56198ab9802SChristos Margiolis 		return (frame->tf_ecx);
562c6f5742fSRui Paulo 	case REG_RAX:
56398ab9802SChristos Margiolis 		return (frame->tf_eax);
564c6f5742fSRui Paulo 	case REG_TRAPNO:
56598ab9802SChristos Margiolis 		return (frame->tf_trapno);
566c6f5742fSRui Paulo 	case REG_ERR:
56798ab9802SChristos Margiolis 		return (frame->tf_err);
568c6f5742fSRui Paulo 	case REG_RIP:
56998ab9802SChristos Margiolis 		return (frame->tf_eip);
570c6f5742fSRui Paulo 	case REG_CS:
57198ab9802SChristos Margiolis 		return (frame->tf_cs);
572c6f5742fSRui Paulo 	case REG_RFL:
57398ab9802SChristos Margiolis 		return (frame->tf_eflags);
574c6f5742fSRui Paulo #if 0
575c6f5742fSRui Paulo 	case REG_RSP:
57698ab9802SChristos Margiolis 		return (frame->tf_esp);
577c6f5742fSRui Paulo #endif
578c6f5742fSRui Paulo 	case REG_SS:
57998ab9802SChristos Margiolis 		return (frame->tf_ss);
58091eaf3e1SJohn Birrell 	default:
58191eaf3e1SJohn Birrell 		DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
58291eaf3e1SJohn Birrell 		return (0);
58391eaf3e1SJohn Birrell 	}
58491eaf3e1SJohn Birrell }
58591eaf3e1SJohn Birrell 
58691eaf3e1SJohn Birrell static int
58791eaf3e1SJohn Birrell dtrace_copycheck(uintptr_t uaddr, uintptr_t kaddr, size_t size)
58891eaf3e1SJohn Birrell {
58991eaf3e1SJohn Birrell 	ASSERT(kaddr >= kernelbase && kaddr + size >= kaddr);
59091eaf3e1SJohn Birrell 
59191eaf3e1SJohn Birrell 	if (uaddr + size >= kernelbase || uaddr + size < uaddr) {
59291eaf3e1SJohn Birrell 		DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
59391eaf3e1SJohn Birrell 		cpu_core[curcpu].cpuc_dtrace_illval = uaddr;
59491eaf3e1SJohn Birrell 		return (0);
59591eaf3e1SJohn Birrell 	}
59691eaf3e1SJohn Birrell 
59791eaf3e1SJohn Birrell 	return (1);
59891eaf3e1SJohn Birrell }
59991eaf3e1SJohn Birrell 
60091eaf3e1SJohn Birrell void
60191eaf3e1SJohn Birrell dtrace_copyin(uintptr_t uaddr, uintptr_t kaddr, size_t size,
60291eaf3e1SJohn Birrell     volatile uint16_t *flags)
60391eaf3e1SJohn Birrell {
60491eaf3e1SJohn Birrell 	if (dtrace_copycheck(uaddr, kaddr, size))
60591eaf3e1SJohn Birrell 		dtrace_copy(uaddr, kaddr, size);
60691eaf3e1SJohn Birrell }
60791eaf3e1SJohn Birrell 
60891eaf3e1SJohn Birrell void
60991eaf3e1SJohn Birrell dtrace_copyout(uintptr_t kaddr, uintptr_t uaddr, size_t size,
61091eaf3e1SJohn Birrell     volatile uint16_t *flags)
61191eaf3e1SJohn Birrell {
61291eaf3e1SJohn Birrell 	if (dtrace_copycheck(uaddr, kaddr, size))
61391eaf3e1SJohn Birrell 		dtrace_copy(kaddr, uaddr, size);
61491eaf3e1SJohn Birrell }
61591eaf3e1SJohn Birrell 
61691eaf3e1SJohn Birrell void
61791eaf3e1SJohn Birrell dtrace_copyinstr(uintptr_t uaddr, uintptr_t kaddr, size_t size,
61891eaf3e1SJohn Birrell     volatile uint16_t *flags)
61991eaf3e1SJohn Birrell {
62091eaf3e1SJohn Birrell 	if (dtrace_copycheck(uaddr, kaddr, size))
62191eaf3e1SJohn Birrell 		dtrace_copystr(uaddr, kaddr, size, flags);
62291eaf3e1SJohn Birrell }
62391eaf3e1SJohn Birrell 
62491eaf3e1SJohn Birrell void
62591eaf3e1SJohn Birrell dtrace_copyoutstr(uintptr_t kaddr, uintptr_t uaddr, size_t size,
62691eaf3e1SJohn Birrell     volatile uint16_t *flags)
62791eaf3e1SJohn Birrell {
62891eaf3e1SJohn Birrell 	if (dtrace_copycheck(uaddr, kaddr, size))
62991eaf3e1SJohn Birrell 		dtrace_copystr(kaddr, uaddr, size, flags);
63091eaf3e1SJohn Birrell }
63191eaf3e1SJohn Birrell 
63291eaf3e1SJohn Birrell uint8_t
63391eaf3e1SJohn Birrell dtrace_fuword8(void *uaddr)
63491eaf3e1SJohn Birrell {
63591eaf3e1SJohn Birrell 	if ((uintptr_t)uaddr >= kernelbase) {
63691eaf3e1SJohn Birrell 		DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
63791eaf3e1SJohn Birrell 		cpu_core[curcpu].cpuc_dtrace_illval = (uintptr_t)uaddr;
63891eaf3e1SJohn Birrell 		return (0);
63991eaf3e1SJohn Birrell 	}
64091eaf3e1SJohn Birrell 	return (dtrace_fuword8_nocheck(uaddr));
64191eaf3e1SJohn Birrell }
64291eaf3e1SJohn Birrell 
64391eaf3e1SJohn Birrell uint16_t
64491eaf3e1SJohn Birrell dtrace_fuword16(void *uaddr)
64591eaf3e1SJohn Birrell {
64691eaf3e1SJohn Birrell 	if ((uintptr_t)uaddr >= kernelbase) {
64791eaf3e1SJohn Birrell 		DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
64891eaf3e1SJohn Birrell 		cpu_core[curcpu].cpuc_dtrace_illval = (uintptr_t)uaddr;
64991eaf3e1SJohn Birrell 		return (0);
65091eaf3e1SJohn Birrell 	}
65191eaf3e1SJohn Birrell 	return (dtrace_fuword16_nocheck(uaddr));
65291eaf3e1SJohn Birrell }
65391eaf3e1SJohn Birrell 
65491eaf3e1SJohn Birrell uint32_t
65591eaf3e1SJohn Birrell dtrace_fuword32(void *uaddr)
65691eaf3e1SJohn Birrell {
65791eaf3e1SJohn Birrell 	if ((uintptr_t)uaddr >= kernelbase) {
65891eaf3e1SJohn Birrell 		DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
65991eaf3e1SJohn Birrell 		cpu_core[curcpu].cpuc_dtrace_illval = (uintptr_t)uaddr;
66091eaf3e1SJohn Birrell 		return (0);
66191eaf3e1SJohn Birrell 	}
66291eaf3e1SJohn Birrell 	return (dtrace_fuword32_nocheck(uaddr));
66391eaf3e1SJohn Birrell }
66491eaf3e1SJohn Birrell 
66591eaf3e1SJohn Birrell uint64_t
66691eaf3e1SJohn Birrell dtrace_fuword64(void *uaddr)
66791eaf3e1SJohn Birrell {
66891eaf3e1SJohn Birrell 	if ((uintptr_t)uaddr >= kernelbase) {
66991eaf3e1SJohn Birrell 		DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
67091eaf3e1SJohn Birrell 		cpu_core[curcpu].cpuc_dtrace_illval = (uintptr_t)uaddr;
67191eaf3e1SJohn Birrell 		return (0);
67291eaf3e1SJohn Birrell 	}
67391eaf3e1SJohn Birrell 	return (dtrace_fuword64_nocheck(uaddr));
67491eaf3e1SJohn Birrell }
675