xref: /freebsd/sys/cddl/dev/dtrace/i386/dtrace_isa.c (revision 6c2806594b351ec39cca31bf386bdee79571fa36)
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  * $FreeBSD$
2391eaf3e1SJohn Birrell  */
2491eaf3e1SJohn Birrell /*
2591eaf3e1SJohn Birrell  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
2691eaf3e1SJohn Birrell  * Use is subject to license terms.
2791eaf3e1SJohn Birrell  */
2891eaf3e1SJohn Birrell #include <sys/cdefs.h>
2991eaf3e1SJohn Birrell 
3091eaf3e1SJohn Birrell #include <sys/param.h>
3191eaf3e1SJohn Birrell #include <sys/systm.h>
3291eaf3e1SJohn Birrell #include <sys/kernel.h>
3391eaf3e1SJohn Birrell #include <sys/stack.h>
3491eaf3e1SJohn Birrell #include <sys/pcpu.h>
3591eaf3e1SJohn Birrell 
36c6f5742fSRui Paulo #include <machine/frame.h>
3791eaf3e1SJohn Birrell #include <machine/md_var.h>
38c6f5742fSRui Paulo #include <machine/pcb.h>
3991eaf3e1SJohn Birrell #include <machine/stack.h>
4091eaf3e1SJohn Birrell 
4191eaf3e1SJohn Birrell #include <vm/vm.h>
4291eaf3e1SJohn Birrell #include <vm/vm_param.h>
4391eaf3e1SJohn Birrell #include <vm/pmap.h>
4491eaf3e1SJohn Birrell 
45c6f5742fSRui Paulo #include "regset.h"
46c6f5742fSRui Paulo 
4791eaf3e1SJohn Birrell extern uintptr_t kernbase;
4891eaf3e1SJohn Birrell uintptr_t kernelbase = (uintptr_t) &kernbase;
4991eaf3e1SJohn Birrell 
5091eaf3e1SJohn Birrell uint8_t dtrace_fuword8_nocheck(void *);
5191eaf3e1SJohn Birrell uint16_t dtrace_fuword16_nocheck(void *);
5291eaf3e1SJohn Birrell uint32_t dtrace_fuword32_nocheck(void *);
5391eaf3e1SJohn Birrell uint64_t dtrace_fuword64_nocheck(void *);
5491eaf3e1SJohn Birrell 
5509a15aa3SMark Johnston int	dtrace_ustackdepth_max = 2048;
5609a15aa3SMark Johnston 
5791eaf3e1SJohn Birrell void
5891eaf3e1SJohn Birrell dtrace_getpcstack(pc_t *pcstack, int pcstack_limit, int aframes,
5991eaf3e1SJohn Birrell     uint32_t *intrpc)
6091eaf3e1SJohn Birrell {
6191eaf3e1SJohn Birrell 	int depth = 0;
6291eaf3e1SJohn Birrell 	register_t ebp;
6391eaf3e1SJohn Birrell 	struct i386_frame *frame;
6491eaf3e1SJohn Birrell 	vm_offset_t callpc;
6591eaf3e1SJohn Birrell 	pc_t caller = (pc_t) solaris_cpu[curcpu].cpu_dtrace_caller;
6691eaf3e1SJohn Birrell 
6791eaf3e1SJohn Birrell 	if (intrpc != 0)
6891eaf3e1SJohn Birrell 		pcstack[depth++] = (pc_t) intrpc;
6991eaf3e1SJohn Birrell 
7091eaf3e1SJohn Birrell 	aframes++;
7191eaf3e1SJohn Birrell 
7291eaf3e1SJohn Birrell 	__asm __volatile("movl %%ebp,%0" : "=r" (ebp));
7391eaf3e1SJohn Birrell 
7491eaf3e1SJohn Birrell 	frame = (struct i386_frame *)ebp;
7591eaf3e1SJohn Birrell 	while (depth < pcstack_limit) {
7691eaf3e1SJohn Birrell 		if (!INKERNEL(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 
9491eaf3e1SJohn Birrell 		if (frame->f_frame <= frame ||
95888e282aSKonstantin Belousov 		    (vm_offset_t)frame->f_frame >= curthread->td_kstack +
96888e282aSKonstantin Belousov 		    curthread->td_kstack_pages * PAGE_SIZE)
9791eaf3e1SJohn Birrell 			break;
9891eaf3e1SJohn Birrell 		frame = frame->f_frame;
9991eaf3e1SJohn Birrell 	}
10091eaf3e1SJohn Birrell 
10191eaf3e1SJohn Birrell 	for (; depth < pcstack_limit; depth++) {
10291eaf3e1SJohn Birrell 		pcstack[depth] = 0;
10391eaf3e1SJohn Birrell 	}
10491eaf3e1SJohn Birrell }
10591eaf3e1SJohn Birrell 
10691eaf3e1SJohn Birrell static int
10791eaf3e1SJohn Birrell dtrace_getustack_common(uint64_t *pcstack, int pcstack_limit, uintptr_t pc,
10891eaf3e1SJohn Birrell     uintptr_t sp)
10991eaf3e1SJohn Birrell {
110c6f5742fSRui Paulo #ifdef notyet
11191eaf3e1SJohn Birrell 	proc_t *p = curproc;
112c6f5742fSRui Paulo 	uintptr_t oldcontext = lwp->lwp_oldcontext; /* XXX signal stack. */
113c6f5742fSRui Paulo 	size_t s1, s2;
114c6f5742fSRui Paulo #endif
11509a15aa3SMark Johnston 	uintptr_t oldsp;
11691eaf3e1SJohn Birrell 	volatile uint16_t *flags =
11791eaf3e1SJohn Birrell 	    (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags;
11891eaf3e1SJohn Birrell 	int ret = 0;
11991eaf3e1SJohn Birrell 
12091eaf3e1SJohn Birrell 	ASSERT(pcstack == NULL || pcstack_limit > 0);
12109a15aa3SMark Johnston 	ASSERT(dtrace_ustackdepth_max > 0);
12291eaf3e1SJohn Birrell 
123c6f5742fSRui Paulo #ifdef notyet /* XXX signal stack. */
12491eaf3e1SJohn Birrell 	if (p->p_model == DATAMODEL_NATIVE) {
12591eaf3e1SJohn Birrell 		s1 = sizeof (struct frame) + 2 * sizeof (long);
12691eaf3e1SJohn Birrell 		s2 = s1 + sizeof (siginfo_t);
12791eaf3e1SJohn Birrell 	} else {
12891eaf3e1SJohn Birrell 		s1 = sizeof (struct frame32) + 3 * sizeof (int);
12991eaf3e1SJohn Birrell 		s2 = s1 + sizeof (siginfo32_t);
13091eaf3e1SJohn Birrell 	}
131c6f5742fSRui Paulo #endif
13291eaf3e1SJohn Birrell 
133c6f5742fSRui Paulo 	while (pc != 0) {
13409a15aa3SMark Johnston 		/*
13509a15aa3SMark Johnston 		 * We limit the number of times we can go around this
13609a15aa3SMark Johnston 		 * loop to account for a circular stack.
13709a15aa3SMark Johnston 		 */
13809a15aa3SMark Johnston 		if (ret++ >= dtrace_ustackdepth_max) {
13909a15aa3SMark Johnston 			*flags |= CPU_DTRACE_BADSTACK;
14009a15aa3SMark Johnston 			cpu_core[curcpu].cpuc_dtrace_illval = sp;
14109a15aa3SMark Johnston 			break;
14209a15aa3SMark Johnston 		}
14309a15aa3SMark Johnston 
14491eaf3e1SJohn Birrell 		if (pcstack != NULL) {
14591eaf3e1SJohn Birrell 			*pcstack++ = (uint64_t)pc;
14691eaf3e1SJohn Birrell 			pcstack_limit--;
14791eaf3e1SJohn Birrell 			if (pcstack_limit <= 0)
14891eaf3e1SJohn Birrell 				break;
14991eaf3e1SJohn Birrell 		}
15091eaf3e1SJohn Birrell 
151c6f5742fSRui Paulo 		if (sp == 0)
152c6f5742fSRui Paulo 			break;
153c6f5742fSRui Paulo 
15409a15aa3SMark Johnston 		oldsp = sp;
15509a15aa3SMark Johnston 
156c6f5742fSRui Paulo #ifdef notyet /* XXX signal stack. */
15791eaf3e1SJohn Birrell 		if (oldcontext == sp + s1 || oldcontext == sp + s2) {
15891eaf3e1SJohn Birrell 			if (p->p_model == DATAMODEL_NATIVE) {
15991eaf3e1SJohn Birrell 				ucontext_t *ucp = (ucontext_t *)oldcontext;
16091eaf3e1SJohn Birrell 				greg_t *gregs = ucp->uc_mcontext.gregs;
16191eaf3e1SJohn Birrell 
16291eaf3e1SJohn Birrell 				sp = dtrace_fulword(&gregs[REG_FP]);
16391eaf3e1SJohn Birrell 				pc = dtrace_fulword(&gregs[REG_PC]);
16491eaf3e1SJohn Birrell 
16591eaf3e1SJohn Birrell 				oldcontext = dtrace_fulword(&ucp->uc_link);
16691eaf3e1SJohn Birrell 			} else {
16791eaf3e1SJohn Birrell 				ucontext32_t *ucp = (ucontext32_t *)oldcontext;
16891eaf3e1SJohn Birrell 				greg32_t *gregs = ucp->uc_mcontext.gregs;
16991eaf3e1SJohn Birrell 
17091eaf3e1SJohn Birrell 				sp = dtrace_fuword32(&gregs[EBP]);
17191eaf3e1SJohn Birrell 				pc = dtrace_fuword32(&gregs[EIP]);
17291eaf3e1SJohn Birrell 
17391eaf3e1SJohn Birrell 				oldcontext = dtrace_fuword32(&ucp->uc_link);
17491eaf3e1SJohn Birrell 			}
17591eaf3e1SJohn Birrell 		} else {
17691eaf3e1SJohn Birrell 			if (p->p_model == DATAMODEL_NATIVE) {
17791eaf3e1SJohn Birrell 				struct frame *fr = (struct frame *)sp;
17891eaf3e1SJohn Birrell 
17991eaf3e1SJohn Birrell 				pc = dtrace_fulword(&fr->fr_savpc);
18091eaf3e1SJohn Birrell 				sp = dtrace_fulword(&fr->fr_savfp);
18191eaf3e1SJohn Birrell 			} else {
18291eaf3e1SJohn Birrell 				struct frame32 *fr = (struct frame32 *)sp;
18391eaf3e1SJohn Birrell 
18491eaf3e1SJohn Birrell 				pc = dtrace_fuword32(&fr->fr_savpc);
18591eaf3e1SJohn Birrell 				sp = dtrace_fuword32(&fr->fr_savfp);
18691eaf3e1SJohn Birrell 			}
18791eaf3e1SJohn Birrell 		}
188c6f5742fSRui Paulo #else
189c6f5742fSRui Paulo 		pc = dtrace_fuword32((void *)(sp +
190c6f5742fSRui Paulo 			offsetof(struct i386_frame, f_retaddr)));
191c6f5742fSRui Paulo 		sp = dtrace_fuword32((void *)sp);
192c6f5742fSRui Paulo #endif /* ! notyet */
19391eaf3e1SJohn Birrell 
19409a15aa3SMark Johnston 		if (sp == oldsp) {
19509a15aa3SMark Johnston 			*flags |= CPU_DTRACE_BADSTACK;
19609a15aa3SMark Johnston 			cpu_core[curcpu].cpuc_dtrace_illval = sp;
19709a15aa3SMark Johnston 			break;
19809a15aa3SMark Johnston 		}
19909a15aa3SMark Johnston 
20091eaf3e1SJohn Birrell 		/*
20191eaf3e1SJohn Birrell 		 * This is totally bogus:  if we faulted, we're going to clear
20291eaf3e1SJohn Birrell 		 * the fault and break.  This is to deal with the apparently
20391eaf3e1SJohn Birrell 		 * broken Java stacks on x86.
20491eaf3e1SJohn Birrell 		 */
20591eaf3e1SJohn Birrell 		if (*flags & CPU_DTRACE_FAULT) {
20691eaf3e1SJohn Birrell 			*flags &= ~CPU_DTRACE_FAULT;
20791eaf3e1SJohn Birrell 			break;
20891eaf3e1SJohn Birrell 		}
20991eaf3e1SJohn Birrell 	}
21091eaf3e1SJohn Birrell 
21191eaf3e1SJohn Birrell 	return (ret);
21291eaf3e1SJohn Birrell }
21391eaf3e1SJohn Birrell 
21491eaf3e1SJohn Birrell void
21591eaf3e1SJohn Birrell dtrace_getupcstack(uint64_t *pcstack, int pcstack_limit)
21691eaf3e1SJohn Birrell {
21791eaf3e1SJohn Birrell 	proc_t *p = curproc;
218c6f5742fSRui Paulo 	struct trapframe *tf;
219c6f5742fSRui Paulo 	uintptr_t pc, sp, fp;
22091eaf3e1SJohn Birrell 	volatile uint16_t *flags =
22191eaf3e1SJohn Birrell 	    (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags;
22291eaf3e1SJohn Birrell 	int n;
22391eaf3e1SJohn Birrell 
22491eaf3e1SJohn Birrell 	if (*flags & CPU_DTRACE_FAULT)
22591eaf3e1SJohn Birrell 		return;
22691eaf3e1SJohn Birrell 
22791eaf3e1SJohn Birrell 	if (pcstack_limit <= 0)
22891eaf3e1SJohn Birrell 		return;
22991eaf3e1SJohn Birrell 
23091eaf3e1SJohn Birrell 	/*
23191eaf3e1SJohn Birrell 	 * If there's no user context we still need to zero the stack.
23291eaf3e1SJohn Birrell 	 */
233c6f5742fSRui Paulo 	if (p == NULL || (tf = curthread->td_frame) == NULL)
23491eaf3e1SJohn Birrell 		goto zero;
23591eaf3e1SJohn Birrell 
23691eaf3e1SJohn Birrell 	*pcstack++ = (uint64_t)p->p_pid;
23791eaf3e1SJohn Birrell 	pcstack_limit--;
23891eaf3e1SJohn Birrell 
23991eaf3e1SJohn Birrell 	if (pcstack_limit <= 0)
24091eaf3e1SJohn Birrell 		return;
24191eaf3e1SJohn Birrell 
242c6f5742fSRui Paulo 	pc = tf->tf_eip;
243c6f5742fSRui Paulo 	fp = tf->tf_ebp;
244c6f5742fSRui Paulo 	sp = tf->tf_esp;
24591eaf3e1SJohn Birrell 
24691eaf3e1SJohn Birrell 	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
247c6f5742fSRui Paulo 		/*
248c6f5742fSRui Paulo 		 * In an entry probe.  The frame pointer has not yet been
249c6f5742fSRui Paulo 		 * pushed (that happens in the function prologue).  The
250c6f5742fSRui Paulo 		 * best approach is to add the current pc as a missing top
251c6f5742fSRui Paulo 		 * of stack and back the pc up to the caller, which is stored
252c6f5742fSRui Paulo 		 * at the current stack pointer address since the call
253c6f5742fSRui Paulo 		 * instruction puts it there right before the branch.
254c6f5742fSRui Paulo 		 */
255c6f5742fSRui Paulo 
25691eaf3e1SJohn Birrell 		*pcstack++ = (uint64_t)pc;
25791eaf3e1SJohn Birrell 		pcstack_limit--;
25891eaf3e1SJohn Birrell 		if (pcstack_limit <= 0)
25991eaf3e1SJohn Birrell 			return;
26091eaf3e1SJohn Birrell 
261c6f5742fSRui Paulo 		pc = dtrace_fuword32((void *) sp);
26291eaf3e1SJohn Birrell 	}
26391eaf3e1SJohn Birrell 
26491eaf3e1SJohn Birrell 	n = dtrace_getustack_common(pcstack, pcstack_limit, pc, sp);
26591eaf3e1SJohn Birrell 	ASSERT(n >= 0);
26691eaf3e1SJohn Birrell 	ASSERT(n <= pcstack_limit);
26791eaf3e1SJohn Birrell 
26891eaf3e1SJohn Birrell 	pcstack += n;
26991eaf3e1SJohn Birrell 	pcstack_limit -= n;
27091eaf3e1SJohn Birrell 
27191eaf3e1SJohn Birrell zero:
27291eaf3e1SJohn Birrell 	while (pcstack_limit-- > 0)
273c6f5742fSRui Paulo 		*pcstack++ = 0;
27491eaf3e1SJohn Birrell }
27591eaf3e1SJohn Birrell 
27691eaf3e1SJohn Birrell int
27791eaf3e1SJohn Birrell dtrace_getustackdepth(void)
27891eaf3e1SJohn Birrell {
279c6f5742fSRui Paulo 	proc_t *p = curproc;
280c6f5742fSRui Paulo 	struct trapframe *tf;
281c6f5742fSRui Paulo 	uintptr_t pc, fp, sp;
282c6f5742fSRui Paulo 	int n = 0;
283c6f5742fSRui Paulo 
284c6f5742fSRui Paulo 	if (p == NULL || (tf = curthread->td_frame) == NULL)
285c6f5742fSRui Paulo 		return (0);
286c6f5742fSRui Paulo 
287c6f5742fSRui Paulo 	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT))
288c6f5742fSRui Paulo 		return (-1);
289c6f5742fSRui Paulo 
290c6f5742fSRui Paulo 	pc = tf->tf_eip;
291c6f5742fSRui Paulo 	fp = tf->tf_ebp;
292c6f5742fSRui Paulo 	sp = tf->tf_esp;
293c6f5742fSRui Paulo 
294c6f5742fSRui Paulo 	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
295c6f5742fSRui Paulo 		/*
296c6f5742fSRui Paulo 		 * In an entry probe.  The frame pointer has not yet been
297c6f5742fSRui Paulo 		 * pushed (that happens in the function prologue).  The
298c6f5742fSRui Paulo 		 * best approach is to add the current pc as a missing top
299c6f5742fSRui Paulo 		 * of stack and back the pc up to the caller, which is stored
300c6f5742fSRui Paulo 		 * at the current stack pointer address since the call
301c6f5742fSRui Paulo 		 * instruction puts it there right before the branch.
302c6f5742fSRui Paulo 		 */
303c6f5742fSRui Paulo 
304c6f5742fSRui Paulo 		pc = dtrace_fuword32((void *) sp);
305c6f5742fSRui Paulo 		n++;
306c6f5742fSRui Paulo 	}
307c6f5742fSRui Paulo 
308c6f5742fSRui Paulo 	n += dtrace_getustack_common(NULL, 0, pc, fp);
309c6f5742fSRui Paulo 
310c6f5742fSRui Paulo 	return (n);
31191eaf3e1SJohn Birrell }
31291eaf3e1SJohn Birrell 
31391eaf3e1SJohn Birrell void
31491eaf3e1SJohn Birrell dtrace_getufpstack(uint64_t *pcstack, uint64_t *fpstack, int pcstack_limit)
31591eaf3e1SJohn Birrell {
31691eaf3e1SJohn Birrell 	proc_t *p = curproc;
317c6f5742fSRui Paulo 	struct trapframe *tf;
318c6f5742fSRui Paulo 	uintptr_t pc, sp, fp;
31991eaf3e1SJohn Birrell 	volatile uint16_t *flags =
32091eaf3e1SJohn Birrell 	    (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags;
321c6f5742fSRui Paulo #ifdef notyet /* XXX signal stack */
322c6f5742fSRui Paulo 	uintptr_t oldcontext;
32391eaf3e1SJohn Birrell 	size_t s1, s2;
324c6f5742fSRui Paulo #endif
32591eaf3e1SJohn Birrell 
32691eaf3e1SJohn Birrell 	if (*flags & CPU_DTRACE_FAULT)
32791eaf3e1SJohn Birrell 		return;
32891eaf3e1SJohn Birrell 
32991eaf3e1SJohn Birrell 	if (pcstack_limit <= 0)
33091eaf3e1SJohn Birrell 		return;
33191eaf3e1SJohn Birrell 
33291eaf3e1SJohn Birrell 	/*
33391eaf3e1SJohn Birrell 	 * If there's no user context we still need to zero the stack.
33491eaf3e1SJohn Birrell 	 */
335c6f5742fSRui Paulo 	if (p == NULL || (tf = curthread->td_frame) == NULL)
33691eaf3e1SJohn Birrell 		goto zero;
33791eaf3e1SJohn Birrell 
33891eaf3e1SJohn Birrell 	*pcstack++ = (uint64_t)p->p_pid;
33991eaf3e1SJohn Birrell 	pcstack_limit--;
34091eaf3e1SJohn Birrell 
34191eaf3e1SJohn Birrell 	if (pcstack_limit <= 0)
34291eaf3e1SJohn Birrell 		return;
34391eaf3e1SJohn Birrell 
344c6f5742fSRui Paulo 	pc = tf->tf_eip;
345c6f5742fSRui Paulo 	fp = tf->tf_ebp;
346c6f5742fSRui Paulo 	sp = tf->tf_esp;
347c6f5742fSRui Paulo 
348c6f5742fSRui Paulo #ifdef notyet /* XXX signal stack */
34991eaf3e1SJohn Birrell 	oldcontext = lwp->lwp_oldcontext;
35091eaf3e1SJohn Birrell 
35191eaf3e1SJohn Birrell 	if (p->p_model == DATAMODEL_NATIVE) {
35291eaf3e1SJohn Birrell 		s1 = sizeof (struct frame) + 2 * sizeof (long);
35391eaf3e1SJohn Birrell 		s2 = s1 + sizeof (siginfo_t);
35491eaf3e1SJohn Birrell 	} else {
35591eaf3e1SJohn Birrell 		s1 = sizeof (struct frame32) + 3 * sizeof (int);
35691eaf3e1SJohn Birrell 		s2 = s1 + sizeof (siginfo32_t);
35791eaf3e1SJohn Birrell 	}
358c6f5742fSRui Paulo #endif
35991eaf3e1SJohn Birrell 
36091eaf3e1SJohn Birrell 	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
36191eaf3e1SJohn Birrell 		*pcstack++ = (uint64_t)pc;
36291eaf3e1SJohn Birrell 		*fpstack++ = 0;
36391eaf3e1SJohn Birrell 		pcstack_limit--;
36491eaf3e1SJohn Birrell 		if (pcstack_limit <= 0)
36591eaf3e1SJohn Birrell 			return;
36691eaf3e1SJohn Birrell 
367c6f5742fSRui Paulo 		pc = dtrace_fuword32((void *)sp);
36891eaf3e1SJohn Birrell 	}
36991eaf3e1SJohn Birrell 
370c6f5742fSRui Paulo 	while (pc != 0) {
37191eaf3e1SJohn Birrell 		*pcstack++ = (uint64_t)pc;
372c6f5742fSRui Paulo 		*fpstack++ = fp;
37391eaf3e1SJohn Birrell 		pcstack_limit--;
37491eaf3e1SJohn Birrell 		if (pcstack_limit <= 0)
37591eaf3e1SJohn Birrell 			break;
37691eaf3e1SJohn Birrell 
377c6f5742fSRui Paulo 		if (fp == 0)
378c6f5742fSRui Paulo 			break;
379c6f5742fSRui Paulo 
380c6f5742fSRui Paulo #ifdef notyet /* XXX signal stack */
38191eaf3e1SJohn Birrell 		if (oldcontext == sp + s1 || oldcontext == sp + s2) {
38291eaf3e1SJohn Birrell 			if (p->p_model == DATAMODEL_NATIVE) {
38391eaf3e1SJohn Birrell 				ucontext_t *ucp = (ucontext_t *)oldcontext;
38491eaf3e1SJohn Birrell 				greg_t *gregs = ucp->uc_mcontext.gregs;
38591eaf3e1SJohn Birrell 
38691eaf3e1SJohn Birrell 				sp = dtrace_fulword(&gregs[REG_FP]);
38791eaf3e1SJohn Birrell 				pc = dtrace_fulword(&gregs[REG_PC]);
38891eaf3e1SJohn Birrell 
38991eaf3e1SJohn Birrell 				oldcontext = dtrace_fulword(&ucp->uc_link);
39091eaf3e1SJohn Birrell 			} else {
39191eaf3e1SJohn Birrell 				ucontext_t *ucp = (ucontext_t *)oldcontext;
39291eaf3e1SJohn Birrell 				greg_t *gregs = ucp->uc_mcontext.gregs;
39391eaf3e1SJohn Birrell 
39491eaf3e1SJohn Birrell 				sp = dtrace_fuword32(&gregs[EBP]);
39591eaf3e1SJohn Birrell 				pc = dtrace_fuword32(&gregs[EIP]);
39691eaf3e1SJohn Birrell 
39791eaf3e1SJohn Birrell 				oldcontext = dtrace_fuword32(&ucp->uc_link);
39891eaf3e1SJohn Birrell 			}
399c6f5742fSRui Paulo 		} else
400c6f5742fSRui Paulo #endif /* XXX */
401c6f5742fSRui Paulo 		{
402c6f5742fSRui Paulo 			pc = dtrace_fuword32((void *)(fp +
403c6f5742fSRui Paulo 				offsetof(struct i386_frame, f_retaddr)));
404c6f5742fSRui Paulo 			fp = dtrace_fuword32((void *)fp);
40591eaf3e1SJohn Birrell 		}
40691eaf3e1SJohn Birrell 
40791eaf3e1SJohn Birrell 		/*
40891eaf3e1SJohn Birrell 		 * This is totally bogus:  if we faulted, we're going to clear
40991eaf3e1SJohn Birrell 		 * the fault and break.  This is to deal with the apparently
41091eaf3e1SJohn Birrell 		 * broken Java stacks on x86.
41191eaf3e1SJohn Birrell 		 */
41291eaf3e1SJohn Birrell 		if (*flags & CPU_DTRACE_FAULT) {
41391eaf3e1SJohn Birrell 			*flags &= ~CPU_DTRACE_FAULT;
41491eaf3e1SJohn Birrell 			break;
41591eaf3e1SJohn Birrell 		}
41691eaf3e1SJohn Birrell 	}
41791eaf3e1SJohn Birrell 
41891eaf3e1SJohn Birrell zero:
41991eaf3e1SJohn Birrell 	while (pcstack_limit-- > 0)
420c6f5742fSRui Paulo 		*pcstack++ = 0;
42191eaf3e1SJohn Birrell }
42291eaf3e1SJohn Birrell 
42391eaf3e1SJohn Birrell uint64_t
42491eaf3e1SJohn Birrell dtrace_getarg(int arg, int aframes)
42591eaf3e1SJohn Birrell {
426*6c280659SMark Johnston 	struct trapframe *frame;
42791eaf3e1SJohn Birrell 	struct i386_frame *fp = (struct i386_frame *)dtrace_getfp();
428*6c280659SMark Johnston 	uintptr_t *stack, val;
42991eaf3e1SJohn Birrell 	int i;
43091eaf3e1SJohn Birrell 
43191eaf3e1SJohn Birrell 	for (i = 1; i <= aframes; i++) {
43291eaf3e1SJohn Birrell 		fp = fp->f_frame;
43391eaf3e1SJohn Birrell 
434efa1aff6SMark Johnston 		if (P2ROUNDUP(fp->f_retaddr, 4) ==
435efa1aff6SMark Johnston 		    (long)dtrace_invop_callsite) {
43691eaf3e1SJohn Birrell 			/*
43791eaf3e1SJohn Birrell 			 * If we pass through the invalid op handler, we will
438*6c280659SMark Johnston 			 * use the trap frame pointer that it pushed on the
439*6c280659SMark Johnston 			 * stack as the second argument to dtrace_invop() as
440*6c280659SMark Johnston 			 * the pointer to the stack.  When using this stack, we
441*6c280659SMark Johnston 			 * must skip the third argument to dtrace_invop(),
442*6c280659SMark Johnston 			 * which is included in the i386_frame.
44391eaf3e1SJohn Birrell 			 */
444*6c280659SMark Johnston 			frame = (struct trapframe *)(((uintptr_t **)&fp[1])[0]);
445*6c280659SMark Johnston 			/*
446*6c280659SMark Johnston 			 * Skip the three hardware-saved registers and the
447*6c280659SMark Johnston 			 * return address.
448*6c280659SMark Johnston 			 */
449*6c280659SMark Johnston 			stack = (uintptr_t *)frame->tf_isp + 4;
45091eaf3e1SJohn Birrell 			goto load;
45191eaf3e1SJohn Birrell 		}
45291eaf3e1SJohn Birrell 
45391eaf3e1SJohn Birrell 	}
45491eaf3e1SJohn Birrell 
45591eaf3e1SJohn Birrell 	/*
45691eaf3e1SJohn Birrell 	 * We know that we did not come through a trap to get into
45791eaf3e1SJohn Birrell 	 * dtrace_probe() -- the provider simply called dtrace_probe()
45891eaf3e1SJohn Birrell 	 * directly.  As this is the case, we need to shift the argument
45991eaf3e1SJohn Birrell 	 * that we're looking for:  the probe ID is the first argument to
46091eaf3e1SJohn Birrell 	 * dtrace_probe(), so the argument n will actually be found where
46191eaf3e1SJohn Birrell 	 * one would expect to find argument (n + 1).
46291eaf3e1SJohn Birrell 	 */
46391eaf3e1SJohn Birrell 	arg++;
46491eaf3e1SJohn Birrell 
465efa1aff6SMark Johnston 	stack = (uintptr_t *)fp + 2;
46691eaf3e1SJohn Birrell 
46791eaf3e1SJohn Birrell load:
46891eaf3e1SJohn Birrell 	DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
46991eaf3e1SJohn Birrell 	val = stack[arg];
47091eaf3e1SJohn Birrell 	DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
47191eaf3e1SJohn Birrell 
47291eaf3e1SJohn Birrell 	return (val);
47391eaf3e1SJohn Birrell }
47491eaf3e1SJohn Birrell 
47591eaf3e1SJohn Birrell int
47691eaf3e1SJohn Birrell dtrace_getstackdepth(int aframes)
47791eaf3e1SJohn Birrell {
47891eaf3e1SJohn Birrell 	int depth = 0;
47991eaf3e1SJohn Birrell 	struct i386_frame *frame;
48091eaf3e1SJohn Birrell 	vm_offset_t ebp;
48191eaf3e1SJohn Birrell 
48291eaf3e1SJohn Birrell 	aframes++;
48391eaf3e1SJohn Birrell 	ebp = dtrace_getfp();
48491eaf3e1SJohn Birrell 	frame = (struct i386_frame *)ebp;
48591eaf3e1SJohn Birrell 	depth++;
48691eaf3e1SJohn Birrell 	for(;;) {
48791eaf3e1SJohn Birrell 		if (!INKERNEL((long) frame))
48891eaf3e1SJohn Birrell 			break;
48991eaf3e1SJohn Birrell 		if (!INKERNEL((long) frame->f_frame))
49091eaf3e1SJohn Birrell 			break;
49191eaf3e1SJohn Birrell 		depth++;
49291eaf3e1SJohn Birrell 		if (frame->f_frame <= frame ||
493888e282aSKonstantin Belousov 		    (vm_offset_t)frame->f_frame >= curthread->td_kstack +
494888e282aSKonstantin Belousov 		    curthread->td_kstack_pages * PAGE_SIZE)
49591eaf3e1SJohn Birrell 			break;
49691eaf3e1SJohn Birrell 		frame = frame->f_frame;
49791eaf3e1SJohn Birrell 	}
49891eaf3e1SJohn Birrell 	if (depth < aframes)
49991eaf3e1SJohn Birrell 		return 0;
50091eaf3e1SJohn Birrell 	else
50191eaf3e1SJohn Birrell 		return depth - aframes;
50291eaf3e1SJohn Birrell }
50391eaf3e1SJohn Birrell 
50491eaf3e1SJohn Birrell ulong_t
505c6f5742fSRui Paulo dtrace_getreg(struct trapframe *rp, uint_t reg)
50691eaf3e1SJohn Birrell {
507c6f5742fSRui Paulo 	struct pcb *pcb;
508c6f5742fSRui Paulo 	int regmap[] = {  /* Order is dependent on reg.d */
509c6f5742fSRui Paulo 		REG_GS,		/* 0  GS */
510c6f5742fSRui Paulo 		REG_FS,		/* 1  FS */
511c6f5742fSRui Paulo 		REG_ES,		/* 2  ES */
512c6f5742fSRui Paulo 		REG_DS,		/* 3  DS */
513c6f5742fSRui Paulo 		REG_RDI,	/* 4  EDI */
514c6f5742fSRui Paulo 		REG_RSI,	/* 5  ESI */
515c6f5742fSRui Paulo 		REG_RBP,	/* 6  EBP, REG_FP */
516c6f5742fSRui Paulo 		REG_RSP,	/* 7  ESP */
517c6f5742fSRui Paulo 		REG_RBX,	/* 8  EBX */
518c6f5742fSRui Paulo 		REG_RDX,	/* 9  EDX, REG_R1 */
519c6f5742fSRui Paulo 		REG_RCX,	/* 10 ECX */
520c6f5742fSRui Paulo 		REG_RAX,	/* 11 EAX, REG_R0 */
521c6f5742fSRui Paulo 		REG_TRAPNO,	/* 12 TRAPNO */
522c6f5742fSRui Paulo 		REG_ERR,	/* 13 ERR */
523c6f5742fSRui Paulo 		REG_RIP,	/* 14 EIP, REG_PC */
524c6f5742fSRui Paulo 		REG_CS,		/* 15 CS */
525c6f5742fSRui Paulo 		REG_RFL,	/* 16 EFL, REG_PS */
526c6f5742fSRui Paulo 		REG_RSP,	/* 17 UESP, REG_SP */
527c6f5742fSRui Paulo 		REG_SS		/* 18 SS */
52891eaf3e1SJohn Birrell 	};
52991eaf3e1SJohn Birrell 
530c6f5742fSRui Paulo 	if (reg > SS) {
531c6f5742fSRui Paulo 		DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
532c6f5742fSRui Paulo 		return (0);
533c6f5742fSRui Paulo 	}
534c6f5742fSRui Paulo 
53591eaf3e1SJohn Birrell 	if (reg >= sizeof (regmap) / sizeof (int)) {
53691eaf3e1SJohn Birrell 		DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
53791eaf3e1SJohn Birrell 		return (0);
53891eaf3e1SJohn Birrell 	}
53991eaf3e1SJohn Birrell 
54091eaf3e1SJohn Birrell 	reg = regmap[reg];
54191eaf3e1SJohn Birrell 
54291eaf3e1SJohn Birrell 	switch(reg) {
54391eaf3e1SJohn Birrell 	case REG_GS:
544c6f5742fSRui Paulo 		if ((pcb = curthread->td_pcb) == NULL) {
545c6f5742fSRui Paulo 			DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
546c6f5742fSRui Paulo 			return (0);
547c6f5742fSRui Paulo 		}
548c6f5742fSRui Paulo 		return (pcb->pcb_gs);
549c6f5742fSRui Paulo 	case REG_FS:
550c6f5742fSRui Paulo 		return (rp->tf_fs);
551c6f5742fSRui Paulo 	case REG_ES:
552c6f5742fSRui Paulo 		return (rp->tf_es);
553c6f5742fSRui Paulo 	case REG_DS:
554c6f5742fSRui Paulo 		return (rp->tf_ds);
555c6f5742fSRui Paulo 	case REG_RDI:
556c6f5742fSRui Paulo 		return (rp->tf_edi);
557c6f5742fSRui Paulo 	case REG_RSI:
558c6f5742fSRui Paulo 		return (rp->tf_esi);
559c6f5742fSRui Paulo 	case REG_RBP:
560c6f5742fSRui Paulo 		return (rp->tf_ebp);
56191eaf3e1SJohn Birrell 	case REG_RSP:
562c6f5742fSRui Paulo 		return (rp->tf_isp);
563c6f5742fSRui Paulo 	case REG_RBX:
564c6f5742fSRui Paulo 		return (rp->tf_ebx);
565c6f5742fSRui Paulo 	case REG_RCX:
566c6f5742fSRui Paulo 		return (rp->tf_ecx);
567c6f5742fSRui Paulo 	case REG_RAX:
568c6f5742fSRui Paulo 		return (rp->tf_eax);
569c6f5742fSRui Paulo 	case REG_TRAPNO:
570c6f5742fSRui Paulo 		return (rp->tf_trapno);
571c6f5742fSRui Paulo 	case REG_ERR:
572c6f5742fSRui Paulo 		return (rp->tf_err);
573c6f5742fSRui Paulo 	case REG_RIP:
574c6f5742fSRui Paulo 		return (rp->tf_eip);
575c6f5742fSRui Paulo 	case REG_CS:
576c6f5742fSRui Paulo 		return (rp->tf_cs);
577c6f5742fSRui Paulo 	case REG_RFL:
578c6f5742fSRui Paulo 		return (rp->tf_eflags);
579c6f5742fSRui Paulo #if 0
580c6f5742fSRui Paulo 	case REG_RSP:
581c6f5742fSRui Paulo 		return (rp->tf_esp);
582c6f5742fSRui Paulo #endif
583c6f5742fSRui Paulo 	case REG_SS:
584c6f5742fSRui Paulo 		return (rp->tf_ss);
58591eaf3e1SJohn Birrell 	default:
58691eaf3e1SJohn Birrell 		DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
58791eaf3e1SJohn Birrell 		return (0);
58891eaf3e1SJohn Birrell 	}
58991eaf3e1SJohn Birrell }
59091eaf3e1SJohn Birrell 
59191eaf3e1SJohn Birrell static int
59291eaf3e1SJohn Birrell dtrace_copycheck(uintptr_t uaddr, uintptr_t kaddr, size_t size)
59391eaf3e1SJohn Birrell {
59491eaf3e1SJohn Birrell 	ASSERT(kaddr >= kernelbase && kaddr + size >= kaddr);
59591eaf3e1SJohn Birrell 
59691eaf3e1SJohn Birrell 	if (uaddr + size >= kernelbase || uaddr + size < uaddr) {
59791eaf3e1SJohn Birrell 		DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
59891eaf3e1SJohn Birrell 		cpu_core[curcpu].cpuc_dtrace_illval = uaddr;
59991eaf3e1SJohn Birrell 		return (0);
60091eaf3e1SJohn Birrell 	}
60191eaf3e1SJohn Birrell 
60291eaf3e1SJohn Birrell 	return (1);
60391eaf3e1SJohn Birrell }
60491eaf3e1SJohn Birrell 
60591eaf3e1SJohn Birrell void
60691eaf3e1SJohn Birrell dtrace_copyin(uintptr_t uaddr, uintptr_t kaddr, size_t size,
60791eaf3e1SJohn Birrell     volatile uint16_t *flags)
60891eaf3e1SJohn Birrell {
60991eaf3e1SJohn Birrell 	if (dtrace_copycheck(uaddr, kaddr, size))
61091eaf3e1SJohn Birrell 		dtrace_copy(uaddr, kaddr, size);
61191eaf3e1SJohn Birrell }
61291eaf3e1SJohn Birrell 
61391eaf3e1SJohn Birrell void
61491eaf3e1SJohn Birrell dtrace_copyout(uintptr_t kaddr, uintptr_t uaddr, size_t size,
61591eaf3e1SJohn Birrell     volatile uint16_t *flags)
61691eaf3e1SJohn Birrell {
61791eaf3e1SJohn Birrell 	if (dtrace_copycheck(uaddr, kaddr, size))
61891eaf3e1SJohn Birrell 		dtrace_copy(kaddr, uaddr, size);
61991eaf3e1SJohn Birrell }
62091eaf3e1SJohn Birrell 
62191eaf3e1SJohn Birrell void
62291eaf3e1SJohn Birrell dtrace_copyinstr(uintptr_t uaddr, uintptr_t kaddr, size_t size,
62391eaf3e1SJohn Birrell     volatile uint16_t *flags)
62491eaf3e1SJohn Birrell {
62591eaf3e1SJohn Birrell 	if (dtrace_copycheck(uaddr, kaddr, size))
62691eaf3e1SJohn Birrell 		dtrace_copystr(uaddr, kaddr, size, flags);
62791eaf3e1SJohn Birrell }
62891eaf3e1SJohn Birrell 
62991eaf3e1SJohn Birrell void
63091eaf3e1SJohn Birrell dtrace_copyoutstr(uintptr_t kaddr, uintptr_t uaddr, size_t size,
63191eaf3e1SJohn Birrell     volatile uint16_t *flags)
63291eaf3e1SJohn Birrell {
63391eaf3e1SJohn Birrell 	if (dtrace_copycheck(uaddr, kaddr, size))
63491eaf3e1SJohn Birrell 		dtrace_copystr(kaddr, uaddr, size, flags);
63591eaf3e1SJohn Birrell }
63691eaf3e1SJohn Birrell 
63791eaf3e1SJohn Birrell uint8_t
63891eaf3e1SJohn Birrell dtrace_fuword8(void *uaddr)
63991eaf3e1SJohn Birrell {
64091eaf3e1SJohn Birrell 	if ((uintptr_t)uaddr >= kernelbase) {
64191eaf3e1SJohn Birrell 		DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
64291eaf3e1SJohn Birrell 		cpu_core[curcpu].cpuc_dtrace_illval = (uintptr_t)uaddr;
64391eaf3e1SJohn Birrell 		return (0);
64491eaf3e1SJohn Birrell 	}
64591eaf3e1SJohn Birrell 	return (dtrace_fuword8_nocheck(uaddr));
64691eaf3e1SJohn Birrell }
64791eaf3e1SJohn Birrell 
64891eaf3e1SJohn Birrell uint16_t
64991eaf3e1SJohn Birrell dtrace_fuword16(void *uaddr)
65091eaf3e1SJohn Birrell {
65191eaf3e1SJohn Birrell 	if ((uintptr_t)uaddr >= kernelbase) {
65291eaf3e1SJohn Birrell 		DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
65391eaf3e1SJohn Birrell 		cpu_core[curcpu].cpuc_dtrace_illval = (uintptr_t)uaddr;
65491eaf3e1SJohn Birrell 		return (0);
65591eaf3e1SJohn Birrell 	}
65691eaf3e1SJohn Birrell 	return (dtrace_fuword16_nocheck(uaddr));
65791eaf3e1SJohn Birrell }
65891eaf3e1SJohn Birrell 
65991eaf3e1SJohn Birrell uint32_t
66091eaf3e1SJohn Birrell dtrace_fuword32(void *uaddr)
66191eaf3e1SJohn Birrell {
66291eaf3e1SJohn Birrell 	if ((uintptr_t)uaddr >= kernelbase) {
66391eaf3e1SJohn Birrell 		DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
66491eaf3e1SJohn Birrell 		cpu_core[curcpu].cpuc_dtrace_illval = (uintptr_t)uaddr;
66591eaf3e1SJohn Birrell 		return (0);
66691eaf3e1SJohn Birrell 	}
66791eaf3e1SJohn Birrell 	return (dtrace_fuword32_nocheck(uaddr));
66891eaf3e1SJohn Birrell }
66991eaf3e1SJohn Birrell 
67091eaf3e1SJohn Birrell uint64_t
67191eaf3e1SJohn Birrell dtrace_fuword64(void *uaddr)
67291eaf3e1SJohn Birrell {
67391eaf3e1SJohn Birrell 	if ((uintptr_t)uaddr >= kernelbase) {
67491eaf3e1SJohn Birrell 		DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
67591eaf3e1SJohn Birrell 		cpu_core[curcpu].cpuc_dtrace_illval = (uintptr_t)uaddr;
67691eaf3e1SJohn Birrell 		return (0);
67791eaf3e1SJohn Birrell 	}
67891eaf3e1SJohn Birrell 	return (dtrace_fuword64_nocheck(uaddr));
67991eaf3e1SJohn Birrell }
680