xref: /freebsd/sys/cddl/dev/dtrace/i386/dtrace_isa.c (revision 91eaf3e1831d805c9cffd85818a70213d9007e07)
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 
3691eaf3e1SJohn Birrell #include <machine/md_var.h>
3791eaf3e1SJohn Birrell #include <machine/stack.h>
3891eaf3e1SJohn Birrell 
3991eaf3e1SJohn Birrell #include <vm/vm.h>
4091eaf3e1SJohn Birrell #include <vm/vm_param.h>
4191eaf3e1SJohn Birrell #include <vm/pmap.h>
4291eaf3e1SJohn Birrell 
4391eaf3e1SJohn Birrell extern uintptr_t kernbase;
4491eaf3e1SJohn Birrell uintptr_t kernelbase = (uintptr_t) &kernbase;
4591eaf3e1SJohn Birrell 
4691eaf3e1SJohn Birrell #define INKERNEL(va) (((vm_offset_t)(va)) >= USRSTACK && \
4791eaf3e1SJohn Birrell 	 ((vm_offset_t)(va)) < VM_MAX_KERNEL_ADDRESS)
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 
5491eaf3e1SJohn Birrell void
5591eaf3e1SJohn Birrell dtrace_getpcstack(pc_t *pcstack, int pcstack_limit, int aframes,
5691eaf3e1SJohn Birrell     uint32_t *intrpc)
5791eaf3e1SJohn Birrell {
5891eaf3e1SJohn Birrell 	int depth = 0;
5991eaf3e1SJohn Birrell 	register_t ebp;
6091eaf3e1SJohn Birrell 	struct i386_frame *frame;
6191eaf3e1SJohn Birrell 	vm_offset_t callpc;
6291eaf3e1SJohn Birrell 	pc_t caller = (pc_t) solaris_cpu[curcpu].cpu_dtrace_caller;
6391eaf3e1SJohn Birrell 
6491eaf3e1SJohn Birrell 	if (intrpc != 0)
6591eaf3e1SJohn Birrell 		pcstack[depth++] = (pc_t) intrpc;
6691eaf3e1SJohn Birrell 
6791eaf3e1SJohn Birrell 	aframes++;
6891eaf3e1SJohn Birrell 
6991eaf3e1SJohn Birrell 	__asm __volatile("movl %%ebp,%0" : "=r" (ebp));
7091eaf3e1SJohn Birrell 
7191eaf3e1SJohn Birrell 	frame = (struct i386_frame *)ebp;
7291eaf3e1SJohn Birrell 	while (depth < pcstack_limit) {
7391eaf3e1SJohn Birrell 		if (!INKERNEL(frame))
7491eaf3e1SJohn Birrell 			break;
7591eaf3e1SJohn Birrell 
7691eaf3e1SJohn Birrell 		callpc = frame->f_retaddr;
7791eaf3e1SJohn Birrell 
7891eaf3e1SJohn Birrell 		if (!INKERNEL(callpc))
7991eaf3e1SJohn Birrell 			break;
8091eaf3e1SJohn Birrell 
8191eaf3e1SJohn Birrell 		if (aframes > 0) {
8291eaf3e1SJohn Birrell 			aframes--;
8391eaf3e1SJohn Birrell 			if ((aframes == 0) && (caller != 0)) {
8491eaf3e1SJohn Birrell 				pcstack[depth++] = caller;
8591eaf3e1SJohn Birrell 			}
8691eaf3e1SJohn Birrell 		}
8791eaf3e1SJohn Birrell 		else {
8891eaf3e1SJohn Birrell 			pcstack[depth++] = callpc;
8991eaf3e1SJohn Birrell 		}
9091eaf3e1SJohn Birrell 
9191eaf3e1SJohn Birrell 		if (frame->f_frame <= frame ||
9291eaf3e1SJohn Birrell 		    (vm_offset_t)frame->f_frame >=
9391eaf3e1SJohn Birrell 		    (vm_offset_t)ebp + KSTACK_PAGES * PAGE_SIZE)
9491eaf3e1SJohn Birrell 			break;
9591eaf3e1SJohn Birrell 		frame = frame->f_frame;
9691eaf3e1SJohn Birrell 	}
9791eaf3e1SJohn Birrell 
9891eaf3e1SJohn Birrell 	for (; depth < pcstack_limit; depth++) {
9991eaf3e1SJohn Birrell 		pcstack[depth] = 0;
10091eaf3e1SJohn Birrell 	}
10191eaf3e1SJohn Birrell }
10291eaf3e1SJohn Birrell 
10391eaf3e1SJohn Birrell #ifdef notyet
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 {
10891eaf3e1SJohn Birrell 	klwp_t *lwp = ttolwp(curthread);
10991eaf3e1SJohn Birrell 	proc_t *p = curproc;
11091eaf3e1SJohn Birrell 	uintptr_t oldcontext = lwp->lwp_oldcontext;
11191eaf3e1SJohn Birrell 	volatile uint16_t *flags =
11291eaf3e1SJohn Birrell 	    (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags;
11391eaf3e1SJohn Birrell 	size_t s1, s2;
11491eaf3e1SJohn Birrell 	int ret = 0;
11591eaf3e1SJohn Birrell 
11691eaf3e1SJohn Birrell 	ASSERT(pcstack == NULL || pcstack_limit > 0);
11791eaf3e1SJohn Birrell 
11891eaf3e1SJohn Birrell 	if (p->p_model == DATAMODEL_NATIVE) {
11991eaf3e1SJohn Birrell 		s1 = sizeof (struct frame) + 2 * sizeof (long);
12091eaf3e1SJohn Birrell 		s2 = s1 + sizeof (siginfo_t);
12191eaf3e1SJohn Birrell 	} else {
12291eaf3e1SJohn Birrell 		s1 = sizeof (struct frame32) + 3 * sizeof (int);
12391eaf3e1SJohn Birrell 		s2 = s1 + sizeof (siginfo32_t);
12491eaf3e1SJohn Birrell 	}
12591eaf3e1SJohn Birrell 
12691eaf3e1SJohn Birrell 	while (pc != 0 && sp != 0) {
12791eaf3e1SJohn Birrell 		ret++;
12891eaf3e1SJohn Birrell 		if (pcstack != NULL) {
12991eaf3e1SJohn Birrell 			*pcstack++ = (uint64_t)pc;
13091eaf3e1SJohn Birrell 			pcstack_limit--;
13191eaf3e1SJohn Birrell 			if (pcstack_limit <= 0)
13291eaf3e1SJohn Birrell 				break;
13391eaf3e1SJohn Birrell 		}
13491eaf3e1SJohn Birrell 
13591eaf3e1SJohn Birrell 		if (oldcontext == sp + s1 || oldcontext == sp + s2) {
13691eaf3e1SJohn Birrell 			if (p->p_model == DATAMODEL_NATIVE) {
13791eaf3e1SJohn Birrell 				ucontext_t *ucp = (ucontext_t *)oldcontext;
13891eaf3e1SJohn Birrell 				greg_t *gregs = ucp->uc_mcontext.gregs;
13991eaf3e1SJohn Birrell 
14091eaf3e1SJohn Birrell 				sp = dtrace_fulword(&gregs[REG_FP]);
14191eaf3e1SJohn Birrell 				pc = dtrace_fulword(&gregs[REG_PC]);
14291eaf3e1SJohn Birrell 
14391eaf3e1SJohn Birrell 				oldcontext = dtrace_fulword(&ucp->uc_link);
14491eaf3e1SJohn Birrell 			} else {
14591eaf3e1SJohn Birrell 				ucontext32_t *ucp = (ucontext32_t *)oldcontext;
14691eaf3e1SJohn Birrell 				greg32_t *gregs = ucp->uc_mcontext.gregs;
14791eaf3e1SJohn Birrell 
14891eaf3e1SJohn Birrell 				sp = dtrace_fuword32(&gregs[EBP]);
14991eaf3e1SJohn Birrell 				pc = dtrace_fuword32(&gregs[EIP]);
15091eaf3e1SJohn Birrell 
15191eaf3e1SJohn Birrell 				oldcontext = dtrace_fuword32(&ucp->uc_link);
15291eaf3e1SJohn Birrell 			}
15391eaf3e1SJohn Birrell 		} else {
15491eaf3e1SJohn Birrell 			if (p->p_model == DATAMODEL_NATIVE) {
15591eaf3e1SJohn Birrell 				struct frame *fr = (struct frame *)sp;
15691eaf3e1SJohn Birrell 
15791eaf3e1SJohn Birrell 				pc = dtrace_fulword(&fr->fr_savpc);
15891eaf3e1SJohn Birrell 				sp = dtrace_fulword(&fr->fr_savfp);
15991eaf3e1SJohn Birrell 			} else {
16091eaf3e1SJohn Birrell 				struct frame32 *fr = (struct frame32 *)sp;
16191eaf3e1SJohn Birrell 
16291eaf3e1SJohn Birrell 				pc = dtrace_fuword32(&fr->fr_savpc);
16391eaf3e1SJohn Birrell 				sp = dtrace_fuword32(&fr->fr_savfp);
16491eaf3e1SJohn Birrell 			}
16591eaf3e1SJohn Birrell 		}
16691eaf3e1SJohn Birrell 
16791eaf3e1SJohn Birrell 		/*
16891eaf3e1SJohn Birrell 		 * This is totally bogus:  if we faulted, we're going to clear
16991eaf3e1SJohn Birrell 		 * the fault and break.  This is to deal with the apparently
17091eaf3e1SJohn Birrell 		 * broken Java stacks on x86.
17191eaf3e1SJohn Birrell 		 */
17291eaf3e1SJohn Birrell 		if (*flags & CPU_DTRACE_FAULT) {
17391eaf3e1SJohn Birrell 			*flags &= ~CPU_DTRACE_FAULT;
17491eaf3e1SJohn Birrell 			break;
17591eaf3e1SJohn Birrell 		}
17691eaf3e1SJohn Birrell 	}
17791eaf3e1SJohn Birrell 
17891eaf3e1SJohn Birrell 	return (ret);
17991eaf3e1SJohn Birrell }
18091eaf3e1SJohn Birrell 
18191eaf3e1SJohn Birrell void
18291eaf3e1SJohn Birrell dtrace_getupcstack(uint64_t *pcstack, int pcstack_limit)
18391eaf3e1SJohn Birrell {
18491eaf3e1SJohn Birrell 	klwp_t *lwp = ttolwp(curthread);
18591eaf3e1SJohn Birrell 	proc_t *p = curproc;
18691eaf3e1SJohn Birrell 	struct regs *rp;
18791eaf3e1SJohn Birrell 	uintptr_t pc, sp;
18891eaf3e1SJohn Birrell 	volatile uint16_t *flags =
18991eaf3e1SJohn Birrell 	    (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags;
19091eaf3e1SJohn Birrell 	int n;
19191eaf3e1SJohn Birrell 
19291eaf3e1SJohn Birrell 	if (*flags & CPU_DTRACE_FAULT)
19391eaf3e1SJohn Birrell 		return;
19491eaf3e1SJohn Birrell 
19591eaf3e1SJohn Birrell 	if (pcstack_limit <= 0)
19691eaf3e1SJohn Birrell 		return;
19791eaf3e1SJohn Birrell 
19891eaf3e1SJohn Birrell 	/*
19991eaf3e1SJohn Birrell 	 * If there's no user context we still need to zero the stack.
20091eaf3e1SJohn Birrell 	 */
20191eaf3e1SJohn Birrell 	if (lwp == NULL || p == NULL || (rp = lwp->lwp_regs) == NULL)
20291eaf3e1SJohn Birrell 		goto zero;
20391eaf3e1SJohn Birrell 
20491eaf3e1SJohn Birrell 	*pcstack++ = (uint64_t)p->p_pid;
20591eaf3e1SJohn Birrell 	pcstack_limit--;
20691eaf3e1SJohn Birrell 
20791eaf3e1SJohn Birrell 	if (pcstack_limit <= 0)
20891eaf3e1SJohn Birrell 		return;
20991eaf3e1SJohn Birrell 
21091eaf3e1SJohn Birrell 	pc = rp->r_pc;
21191eaf3e1SJohn Birrell 	sp = rp->r_fp;
21291eaf3e1SJohn Birrell 
21391eaf3e1SJohn Birrell 	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
21491eaf3e1SJohn Birrell 		*pcstack++ = (uint64_t)pc;
21591eaf3e1SJohn Birrell 		pcstack_limit--;
21691eaf3e1SJohn Birrell 		if (pcstack_limit <= 0)
21791eaf3e1SJohn Birrell 			return;
21891eaf3e1SJohn Birrell 
21991eaf3e1SJohn Birrell 		if (p->p_model == DATAMODEL_NATIVE)
22091eaf3e1SJohn Birrell 			pc = dtrace_fulword((void *)rp->r_sp);
22191eaf3e1SJohn Birrell 		else
22291eaf3e1SJohn Birrell 			pc = dtrace_fuword32((void *)rp->r_sp);
22391eaf3e1SJohn Birrell 	}
22491eaf3e1SJohn Birrell 
22591eaf3e1SJohn Birrell 	n = dtrace_getustack_common(pcstack, pcstack_limit, pc, sp);
22691eaf3e1SJohn Birrell 	ASSERT(n >= 0);
22791eaf3e1SJohn Birrell 	ASSERT(n <= pcstack_limit);
22891eaf3e1SJohn Birrell 
22991eaf3e1SJohn Birrell 	pcstack += n;
23091eaf3e1SJohn Birrell 	pcstack_limit -= n;
23191eaf3e1SJohn Birrell 
23291eaf3e1SJohn Birrell zero:
23391eaf3e1SJohn Birrell 	while (pcstack_limit-- > 0)
23491eaf3e1SJohn Birrell 		*pcstack++ = NULL;
23591eaf3e1SJohn Birrell }
23691eaf3e1SJohn Birrell 
23791eaf3e1SJohn Birrell int
23891eaf3e1SJohn Birrell dtrace_getustackdepth(void)
23991eaf3e1SJohn Birrell {
24091eaf3e1SJohn Birrell }
24191eaf3e1SJohn Birrell 
24291eaf3e1SJohn Birrell void
24391eaf3e1SJohn Birrell dtrace_getufpstack(uint64_t *pcstack, uint64_t *fpstack, int pcstack_limit)
24491eaf3e1SJohn Birrell {
24591eaf3e1SJohn Birrell 	klwp_t *lwp = ttolwp(curthread);
24691eaf3e1SJohn Birrell 	proc_t *p = curproc;
24791eaf3e1SJohn Birrell 	struct regs *rp;
24891eaf3e1SJohn Birrell 	uintptr_t pc, sp, oldcontext;
24991eaf3e1SJohn Birrell 	volatile uint16_t *flags =
25091eaf3e1SJohn Birrell 	    (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags;
25191eaf3e1SJohn Birrell 	size_t s1, s2;
25291eaf3e1SJohn Birrell 
25391eaf3e1SJohn Birrell 	if (*flags & CPU_DTRACE_FAULT)
25491eaf3e1SJohn Birrell 		return;
25591eaf3e1SJohn Birrell 
25691eaf3e1SJohn Birrell 	if (pcstack_limit <= 0)
25791eaf3e1SJohn Birrell 		return;
25891eaf3e1SJohn Birrell 
25991eaf3e1SJohn Birrell 	/*
26091eaf3e1SJohn Birrell 	 * If there's no user context we still need to zero the stack.
26191eaf3e1SJohn Birrell 	 */
26291eaf3e1SJohn Birrell 	if (lwp == NULL || p == NULL || (rp = lwp->lwp_regs) == NULL)
26391eaf3e1SJohn Birrell 		goto zero;
26491eaf3e1SJohn Birrell 
26591eaf3e1SJohn Birrell 	*pcstack++ = (uint64_t)p->p_pid;
26691eaf3e1SJohn Birrell 	pcstack_limit--;
26791eaf3e1SJohn Birrell 
26891eaf3e1SJohn Birrell 	if (pcstack_limit <= 0)
26991eaf3e1SJohn Birrell 		return;
27091eaf3e1SJohn Birrell 
27191eaf3e1SJohn Birrell 	pc = rp->r_pc;
27291eaf3e1SJohn Birrell 	sp = rp->r_fp;
27391eaf3e1SJohn Birrell 	oldcontext = lwp->lwp_oldcontext;
27491eaf3e1SJohn Birrell 
27591eaf3e1SJohn Birrell 	if (p->p_model == DATAMODEL_NATIVE) {
27691eaf3e1SJohn Birrell 		s1 = sizeof (struct frame) + 2 * sizeof (long);
27791eaf3e1SJohn Birrell 		s2 = s1 + sizeof (siginfo_t);
27891eaf3e1SJohn Birrell 	} else {
27991eaf3e1SJohn Birrell 		s1 = sizeof (struct frame32) + 3 * sizeof (int);
28091eaf3e1SJohn Birrell 		s2 = s1 + sizeof (siginfo32_t);
28191eaf3e1SJohn Birrell 	}
28291eaf3e1SJohn Birrell 
28391eaf3e1SJohn Birrell 	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
28491eaf3e1SJohn Birrell 		*pcstack++ = (uint64_t)pc;
28591eaf3e1SJohn Birrell 		*fpstack++ = 0;
28691eaf3e1SJohn Birrell 		pcstack_limit--;
28791eaf3e1SJohn Birrell 		if (pcstack_limit <= 0)
28891eaf3e1SJohn Birrell 			return;
28991eaf3e1SJohn Birrell 
29091eaf3e1SJohn Birrell 		if (p->p_model == DATAMODEL_NATIVE)
29191eaf3e1SJohn Birrell 			pc = dtrace_fulword((void *)rp->r_sp);
29291eaf3e1SJohn Birrell 		else
29391eaf3e1SJohn Birrell 			pc = dtrace_fuword32((void *)rp->r_sp);
29491eaf3e1SJohn Birrell 	}
29591eaf3e1SJohn Birrell 
29691eaf3e1SJohn Birrell 	while (pc != 0 && sp != 0) {
29791eaf3e1SJohn Birrell 		*pcstack++ = (uint64_t)pc;
29891eaf3e1SJohn Birrell 		*fpstack++ = sp;
29991eaf3e1SJohn Birrell 		pcstack_limit--;
30091eaf3e1SJohn Birrell 		if (pcstack_limit <= 0)
30191eaf3e1SJohn Birrell 			break;
30291eaf3e1SJohn Birrell 
30391eaf3e1SJohn Birrell 		if (oldcontext == sp + s1 || oldcontext == sp + s2) {
30491eaf3e1SJohn Birrell 			if (p->p_model == DATAMODEL_NATIVE) {
30591eaf3e1SJohn Birrell 				ucontext_t *ucp = (ucontext_t *)oldcontext;
30691eaf3e1SJohn Birrell 				greg_t *gregs = ucp->uc_mcontext.gregs;
30791eaf3e1SJohn Birrell 
30891eaf3e1SJohn Birrell 				sp = dtrace_fulword(&gregs[REG_FP]);
30991eaf3e1SJohn Birrell 				pc = dtrace_fulword(&gregs[REG_PC]);
31091eaf3e1SJohn Birrell 
31191eaf3e1SJohn Birrell 				oldcontext = dtrace_fulword(&ucp->uc_link);
31291eaf3e1SJohn Birrell 			} else {
31391eaf3e1SJohn Birrell 				ucontext_t *ucp = (ucontext_t *)oldcontext;
31491eaf3e1SJohn Birrell 				greg_t *gregs = ucp->uc_mcontext.gregs;
31591eaf3e1SJohn Birrell 
31691eaf3e1SJohn Birrell 				sp = dtrace_fuword32(&gregs[EBP]);
31791eaf3e1SJohn Birrell 				pc = dtrace_fuword32(&gregs[EIP]);
31891eaf3e1SJohn Birrell 
31991eaf3e1SJohn Birrell 				oldcontext = dtrace_fuword32(&ucp->uc_link);
32091eaf3e1SJohn Birrell 			}
32191eaf3e1SJohn Birrell 		} else {
32291eaf3e1SJohn Birrell 			if (p->p_model == DATAMODEL_NATIVE) {
32391eaf3e1SJohn Birrell 				struct frame *fr = (struct frame *)sp;
32491eaf3e1SJohn Birrell 
32591eaf3e1SJohn Birrell 				pc = dtrace_fulword(&fr->fr_savpc);
32691eaf3e1SJohn Birrell 				sp = dtrace_fulword(&fr->fr_savfp);
32791eaf3e1SJohn Birrell 			} else {
32891eaf3e1SJohn Birrell 				struct frame32 *fr = (struct frame32 *)sp;
32991eaf3e1SJohn Birrell 
33091eaf3e1SJohn Birrell 				pc = dtrace_fuword32(&fr->fr_savpc);
33191eaf3e1SJohn Birrell 				sp = dtrace_fuword32(&fr->fr_savfp);
33291eaf3e1SJohn Birrell 			}
33391eaf3e1SJohn Birrell 		}
33491eaf3e1SJohn Birrell 
33591eaf3e1SJohn Birrell 		/*
33691eaf3e1SJohn Birrell 		 * This is totally bogus:  if we faulted, we're going to clear
33791eaf3e1SJohn Birrell 		 * the fault and break.  This is to deal with the apparently
33891eaf3e1SJohn Birrell 		 * broken Java stacks on x86.
33991eaf3e1SJohn Birrell 		 */
34091eaf3e1SJohn Birrell 		if (*flags & CPU_DTRACE_FAULT) {
34191eaf3e1SJohn Birrell 			*flags &= ~CPU_DTRACE_FAULT;
34291eaf3e1SJohn Birrell 			break;
34391eaf3e1SJohn Birrell 		}
34491eaf3e1SJohn Birrell 	}
34591eaf3e1SJohn Birrell 
34691eaf3e1SJohn Birrell zero:
34791eaf3e1SJohn Birrell 	while (pcstack_limit-- > 0)
34891eaf3e1SJohn Birrell 		*pcstack++ = NULL;
34991eaf3e1SJohn Birrell }
35091eaf3e1SJohn Birrell #endif
35191eaf3e1SJohn Birrell 
35291eaf3e1SJohn Birrell uint64_t
35391eaf3e1SJohn Birrell dtrace_getarg(int arg, int aframes)
35491eaf3e1SJohn Birrell {
35591eaf3e1SJohn Birrell 	uintptr_t val;
35691eaf3e1SJohn Birrell 	struct i386_frame *fp = (struct i386_frame *)dtrace_getfp();
35791eaf3e1SJohn Birrell 	uintptr_t *stack;
35891eaf3e1SJohn Birrell 	int i;
35991eaf3e1SJohn Birrell 
36091eaf3e1SJohn Birrell 	for (i = 1; i <= aframes; i++) {
36191eaf3e1SJohn Birrell 		fp = fp->f_frame;
36291eaf3e1SJohn Birrell 
36391eaf3e1SJohn Birrell 		if (fp->f_retaddr == (long)dtrace_invop_callsite) {
36491eaf3e1SJohn Birrell 			/*
36591eaf3e1SJohn Birrell 			 * If we pass through the invalid op handler, we will
36691eaf3e1SJohn Birrell 			 * use the pointer that it passed to the stack as the
36791eaf3e1SJohn Birrell 			 * second argument to dtrace_invop() as the pointer to
36891eaf3e1SJohn Birrell 			 * the stack.  When using this stack, we must step
36991eaf3e1SJohn Birrell 			 * beyond the EIP/RIP that was pushed when the trap was
37091eaf3e1SJohn Birrell 			 * taken -- hence the "+ 1" below.
37191eaf3e1SJohn Birrell 			 */
37291eaf3e1SJohn Birrell 			stack = ((uintptr_t **)&fp[1])[1] + 1;
37391eaf3e1SJohn Birrell 			goto load;
37491eaf3e1SJohn Birrell 		}
37591eaf3e1SJohn Birrell 
37691eaf3e1SJohn Birrell 	}
37791eaf3e1SJohn Birrell 
37891eaf3e1SJohn Birrell 	/*
37991eaf3e1SJohn Birrell 	 * We know that we did not come through a trap to get into
38091eaf3e1SJohn Birrell 	 * dtrace_probe() -- the provider simply called dtrace_probe()
38191eaf3e1SJohn Birrell 	 * directly.  As this is the case, we need to shift the argument
38291eaf3e1SJohn Birrell 	 * that we're looking for:  the probe ID is the first argument to
38391eaf3e1SJohn Birrell 	 * dtrace_probe(), so the argument n will actually be found where
38491eaf3e1SJohn Birrell 	 * one would expect to find argument (n + 1).
38591eaf3e1SJohn Birrell 	 */
38691eaf3e1SJohn Birrell 	arg++;
38791eaf3e1SJohn Birrell 
38891eaf3e1SJohn Birrell 	stack = (uintptr_t *)&fp[1];
38991eaf3e1SJohn Birrell 
39091eaf3e1SJohn Birrell load:
39191eaf3e1SJohn Birrell 	DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
39291eaf3e1SJohn Birrell 	val = stack[arg];
39391eaf3e1SJohn Birrell 	DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
39491eaf3e1SJohn Birrell 
39591eaf3e1SJohn Birrell 	return (val);
39691eaf3e1SJohn Birrell }
39791eaf3e1SJohn Birrell 
39891eaf3e1SJohn Birrell int
39991eaf3e1SJohn Birrell dtrace_getstackdepth(int aframes)
40091eaf3e1SJohn Birrell {
40191eaf3e1SJohn Birrell 	int depth = 0;
40291eaf3e1SJohn Birrell 	struct i386_frame *frame;
40391eaf3e1SJohn Birrell 	vm_offset_t ebp;
40491eaf3e1SJohn Birrell 
40591eaf3e1SJohn Birrell 	aframes++;
40691eaf3e1SJohn Birrell 	ebp = dtrace_getfp();
40791eaf3e1SJohn Birrell 	frame = (struct i386_frame *)ebp;
40891eaf3e1SJohn Birrell 	depth++;
40991eaf3e1SJohn Birrell 	for(;;) {
41091eaf3e1SJohn Birrell 		if (!INKERNEL((long) frame))
41191eaf3e1SJohn Birrell 			break;
41291eaf3e1SJohn Birrell 		if (!INKERNEL((long) frame->f_frame))
41391eaf3e1SJohn Birrell 			break;
41491eaf3e1SJohn Birrell 		depth++;
41591eaf3e1SJohn Birrell 		if (frame->f_frame <= frame ||
41691eaf3e1SJohn Birrell 		    (vm_offset_t)frame->f_frame >=
41791eaf3e1SJohn Birrell 		    (vm_offset_t)ebp + KSTACK_PAGES * PAGE_SIZE)
41891eaf3e1SJohn Birrell 			break;
41991eaf3e1SJohn Birrell 		frame = frame->f_frame;
42091eaf3e1SJohn Birrell 	}
42191eaf3e1SJohn Birrell 	if (depth < aframes)
42291eaf3e1SJohn Birrell 		return 0;
42391eaf3e1SJohn Birrell 	else
42491eaf3e1SJohn Birrell 		return depth - aframes;
42591eaf3e1SJohn Birrell }
42691eaf3e1SJohn Birrell 
42791eaf3e1SJohn Birrell #ifdef notyet
42891eaf3e1SJohn Birrell ulong_t
42991eaf3e1SJohn Birrell dtrace_getreg(struct regs *rp, uint_t reg)
43091eaf3e1SJohn Birrell {
43191eaf3e1SJohn Birrell #if defined(__amd64)
43291eaf3e1SJohn Birrell 	int regmap[] = {
43391eaf3e1SJohn Birrell 		REG_GS,		/* GS */
43491eaf3e1SJohn Birrell 		REG_FS,		/* FS */
43591eaf3e1SJohn Birrell 		REG_ES,		/* ES */
43691eaf3e1SJohn Birrell 		REG_DS,		/* DS */
43791eaf3e1SJohn Birrell 		REG_RDI,	/* EDI */
43891eaf3e1SJohn Birrell 		REG_RSI,	/* ESI */
43991eaf3e1SJohn Birrell 		REG_RBP,	/* EBP */
44091eaf3e1SJohn Birrell 		REG_RSP,	/* ESP */
44191eaf3e1SJohn Birrell 		REG_RBX,	/* EBX */
44291eaf3e1SJohn Birrell 		REG_RDX,	/* EDX */
44391eaf3e1SJohn Birrell 		REG_RCX,	/* ECX */
44491eaf3e1SJohn Birrell 		REG_RAX,	/* EAX */
44591eaf3e1SJohn Birrell 		REG_TRAPNO,	/* TRAPNO */
44691eaf3e1SJohn Birrell 		REG_ERR,	/* ERR */
44791eaf3e1SJohn Birrell 		REG_RIP,	/* EIP */
44891eaf3e1SJohn Birrell 		REG_CS,		/* CS */
44991eaf3e1SJohn Birrell 		REG_RFL,	/* EFL */
45091eaf3e1SJohn Birrell 		REG_RSP,	/* UESP */
45191eaf3e1SJohn Birrell 		REG_SS		/* SS */
45291eaf3e1SJohn Birrell 	};
45391eaf3e1SJohn Birrell 
45491eaf3e1SJohn Birrell 	if (reg <= SS) {
45591eaf3e1SJohn Birrell 		if (reg >= sizeof (regmap) / sizeof (int)) {
45691eaf3e1SJohn Birrell 			DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
45791eaf3e1SJohn Birrell 			return (0);
45891eaf3e1SJohn Birrell 		}
45991eaf3e1SJohn Birrell 
46091eaf3e1SJohn Birrell 		reg = regmap[reg];
46191eaf3e1SJohn Birrell 	} else {
46291eaf3e1SJohn Birrell 		reg -= SS + 1;
46391eaf3e1SJohn Birrell 	}
46491eaf3e1SJohn Birrell 
46591eaf3e1SJohn Birrell 	switch (reg) {
46691eaf3e1SJohn Birrell 	case REG_RDI:
46791eaf3e1SJohn Birrell 		return (rp->r_rdi);
46891eaf3e1SJohn Birrell 	case REG_RSI:
46991eaf3e1SJohn Birrell 		return (rp->r_rsi);
47091eaf3e1SJohn Birrell 	case REG_RDX:
47191eaf3e1SJohn Birrell 		return (rp->r_rdx);
47291eaf3e1SJohn Birrell 	case REG_RCX:
47391eaf3e1SJohn Birrell 		return (rp->r_rcx);
47491eaf3e1SJohn Birrell 	case REG_R8:
47591eaf3e1SJohn Birrell 		return (rp->r_r8);
47691eaf3e1SJohn Birrell 	case REG_R9:
47791eaf3e1SJohn Birrell 		return (rp->r_r9);
47891eaf3e1SJohn Birrell 	case REG_RAX:
47991eaf3e1SJohn Birrell 		return (rp->r_rax);
48091eaf3e1SJohn Birrell 	case REG_RBX:
48191eaf3e1SJohn Birrell 		return (rp->r_rbx);
48291eaf3e1SJohn Birrell 	case REG_RBP:
48391eaf3e1SJohn Birrell 		return (rp->r_rbp);
48491eaf3e1SJohn Birrell 	case REG_R10:
48591eaf3e1SJohn Birrell 		return (rp->r_r10);
48691eaf3e1SJohn Birrell 	case REG_R11:
48791eaf3e1SJohn Birrell 		return (rp->r_r11);
48891eaf3e1SJohn Birrell 	case REG_R12:
48991eaf3e1SJohn Birrell 		return (rp->r_r12);
49091eaf3e1SJohn Birrell 	case REG_R13:
49191eaf3e1SJohn Birrell 		return (rp->r_r13);
49291eaf3e1SJohn Birrell 	case REG_R14:
49391eaf3e1SJohn Birrell 		return (rp->r_r14);
49491eaf3e1SJohn Birrell 	case REG_R15:
49591eaf3e1SJohn Birrell 		return (rp->r_r15);
49691eaf3e1SJohn Birrell 	case REG_DS:
49791eaf3e1SJohn Birrell 		return (rp->r_ds);
49891eaf3e1SJohn Birrell 	case REG_ES:
49991eaf3e1SJohn Birrell 		return (rp->r_es);
50091eaf3e1SJohn Birrell 	case REG_FS:
50191eaf3e1SJohn Birrell 		return (rp->r_fs);
50291eaf3e1SJohn Birrell 	case REG_GS:
50391eaf3e1SJohn Birrell 		return (rp->r_gs);
50491eaf3e1SJohn Birrell 	case REG_TRAPNO:
50591eaf3e1SJohn Birrell 		return (rp->r_trapno);
50691eaf3e1SJohn Birrell 	case REG_ERR:
50791eaf3e1SJohn Birrell 		return (rp->r_err);
50891eaf3e1SJohn Birrell 	case REG_RIP:
50991eaf3e1SJohn Birrell 		return (rp->r_rip);
51091eaf3e1SJohn Birrell 	case REG_CS:
51191eaf3e1SJohn Birrell 		return (rp->r_cs);
51291eaf3e1SJohn Birrell 	case REG_SS:
51391eaf3e1SJohn Birrell 		return (rp->r_ss);
51491eaf3e1SJohn Birrell 	case REG_RFL:
51591eaf3e1SJohn Birrell 		return (rp->r_rfl);
51691eaf3e1SJohn Birrell 	case REG_RSP:
51791eaf3e1SJohn Birrell 		return (rp->r_rsp);
51891eaf3e1SJohn Birrell 	default:
51991eaf3e1SJohn Birrell 		DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
52091eaf3e1SJohn Birrell 		return (0);
52191eaf3e1SJohn Birrell 	}
52291eaf3e1SJohn Birrell 
52391eaf3e1SJohn Birrell #else
52491eaf3e1SJohn Birrell 	if (reg > SS) {
52591eaf3e1SJohn Birrell 		DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
52691eaf3e1SJohn Birrell 		return (0);
52791eaf3e1SJohn Birrell 	}
52891eaf3e1SJohn Birrell 
52991eaf3e1SJohn Birrell 	return ((&rp->r_gs)[reg]);
53091eaf3e1SJohn Birrell #endif
53191eaf3e1SJohn Birrell }
53291eaf3e1SJohn Birrell #endif
53391eaf3e1SJohn Birrell 
53491eaf3e1SJohn Birrell static int
53591eaf3e1SJohn Birrell dtrace_copycheck(uintptr_t uaddr, uintptr_t kaddr, size_t size)
53691eaf3e1SJohn Birrell {
53791eaf3e1SJohn Birrell 	ASSERT(kaddr >= kernelbase && kaddr + size >= kaddr);
53891eaf3e1SJohn Birrell 
53991eaf3e1SJohn Birrell 	if (uaddr + size >= kernelbase || uaddr + size < uaddr) {
54091eaf3e1SJohn Birrell 		DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
54191eaf3e1SJohn Birrell 		cpu_core[curcpu].cpuc_dtrace_illval = uaddr;
54291eaf3e1SJohn Birrell 		return (0);
54391eaf3e1SJohn Birrell 	}
54491eaf3e1SJohn Birrell 
54591eaf3e1SJohn Birrell 	return (1);
54691eaf3e1SJohn Birrell }
54791eaf3e1SJohn Birrell 
54891eaf3e1SJohn Birrell void
54991eaf3e1SJohn Birrell dtrace_copyin(uintptr_t uaddr, uintptr_t kaddr, size_t size,
55091eaf3e1SJohn Birrell     volatile uint16_t *flags)
55191eaf3e1SJohn Birrell {
55291eaf3e1SJohn Birrell 	if (dtrace_copycheck(uaddr, kaddr, size))
55391eaf3e1SJohn Birrell 		dtrace_copy(uaddr, kaddr, size);
55491eaf3e1SJohn Birrell }
55591eaf3e1SJohn Birrell 
55691eaf3e1SJohn Birrell void
55791eaf3e1SJohn Birrell dtrace_copyout(uintptr_t kaddr, uintptr_t uaddr, size_t size,
55891eaf3e1SJohn Birrell     volatile uint16_t *flags)
55991eaf3e1SJohn Birrell {
56091eaf3e1SJohn Birrell 	if (dtrace_copycheck(uaddr, kaddr, size))
56191eaf3e1SJohn Birrell 		dtrace_copy(kaddr, uaddr, size);
56291eaf3e1SJohn Birrell }
56391eaf3e1SJohn Birrell 
56491eaf3e1SJohn Birrell void
56591eaf3e1SJohn Birrell dtrace_copyinstr(uintptr_t uaddr, uintptr_t kaddr, size_t size,
56691eaf3e1SJohn Birrell     volatile uint16_t *flags)
56791eaf3e1SJohn Birrell {
56891eaf3e1SJohn Birrell 	if (dtrace_copycheck(uaddr, kaddr, size))
56991eaf3e1SJohn Birrell 		dtrace_copystr(uaddr, kaddr, size, flags);
57091eaf3e1SJohn Birrell }
57191eaf3e1SJohn Birrell 
57291eaf3e1SJohn Birrell void
57391eaf3e1SJohn Birrell dtrace_copyoutstr(uintptr_t kaddr, uintptr_t uaddr, size_t size,
57491eaf3e1SJohn Birrell     volatile uint16_t *flags)
57591eaf3e1SJohn Birrell {
57691eaf3e1SJohn Birrell 	if (dtrace_copycheck(uaddr, kaddr, size))
57791eaf3e1SJohn Birrell 		dtrace_copystr(kaddr, uaddr, size, flags);
57891eaf3e1SJohn Birrell }
57991eaf3e1SJohn Birrell 
58091eaf3e1SJohn Birrell uint8_t
58191eaf3e1SJohn Birrell dtrace_fuword8(void *uaddr)
58291eaf3e1SJohn Birrell {
58391eaf3e1SJohn Birrell 	if ((uintptr_t)uaddr >= kernelbase) {
58491eaf3e1SJohn Birrell 		DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
58591eaf3e1SJohn Birrell 		cpu_core[curcpu].cpuc_dtrace_illval = (uintptr_t)uaddr;
58691eaf3e1SJohn Birrell 		return (0);
58791eaf3e1SJohn Birrell 	}
58891eaf3e1SJohn Birrell 	return (dtrace_fuword8_nocheck(uaddr));
58991eaf3e1SJohn Birrell }
59091eaf3e1SJohn Birrell 
59191eaf3e1SJohn Birrell uint16_t
59291eaf3e1SJohn Birrell dtrace_fuword16(void *uaddr)
59391eaf3e1SJohn Birrell {
59491eaf3e1SJohn Birrell 	if ((uintptr_t)uaddr >= kernelbase) {
59591eaf3e1SJohn Birrell 		DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
59691eaf3e1SJohn Birrell 		cpu_core[curcpu].cpuc_dtrace_illval = (uintptr_t)uaddr;
59791eaf3e1SJohn Birrell 		return (0);
59891eaf3e1SJohn Birrell 	}
59991eaf3e1SJohn Birrell 	return (dtrace_fuword16_nocheck(uaddr));
60091eaf3e1SJohn Birrell }
60191eaf3e1SJohn Birrell 
60291eaf3e1SJohn Birrell uint32_t
60391eaf3e1SJohn Birrell dtrace_fuword32(void *uaddr)
60491eaf3e1SJohn Birrell {
60591eaf3e1SJohn Birrell 	if ((uintptr_t)uaddr >= kernelbase) {
60691eaf3e1SJohn Birrell 		DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
60791eaf3e1SJohn Birrell 		cpu_core[curcpu].cpuc_dtrace_illval = (uintptr_t)uaddr;
60891eaf3e1SJohn Birrell 		return (0);
60991eaf3e1SJohn Birrell 	}
61091eaf3e1SJohn Birrell 	return (dtrace_fuword32_nocheck(uaddr));
61191eaf3e1SJohn Birrell }
61291eaf3e1SJohn Birrell 
61391eaf3e1SJohn Birrell uint64_t
61491eaf3e1SJohn Birrell dtrace_fuword64(void *uaddr)
61591eaf3e1SJohn Birrell {
61691eaf3e1SJohn Birrell 	if ((uintptr_t)uaddr >= kernelbase) {
61791eaf3e1SJohn Birrell 		DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
61891eaf3e1SJohn Birrell 		cpu_core[curcpu].cpuc_dtrace_illval = (uintptr_t)uaddr;
61991eaf3e1SJohn Birrell 		return (0);
62091eaf3e1SJohn Birrell 	}
62191eaf3e1SJohn Birrell 	return (dtrace_fuword64_nocheck(uaddr));
62291eaf3e1SJohn Birrell }
623