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