xref: /onnv-gate/usr/src/uts/intel/dtrace/dtrace_isa.c (revision 3677:b34b45f03271)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51880Sahl  * Common Development and Distribution License (the "License").
61880Sahl  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211880Sahl 
220Sstevel@tonic-gate /*
233446Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/dtrace_impl.h>
300Sstevel@tonic-gate #include <sys/stack.h>
310Sstevel@tonic-gate #include <sys/frame.h>
320Sstevel@tonic-gate #include <sys/cmn_err.h>
330Sstevel@tonic-gate #include <sys/privregs.h>
340Sstevel@tonic-gate #include <sys/sysmacros.h>
350Sstevel@tonic-gate 
360Sstevel@tonic-gate extern uintptr_t kernelbase;
370Sstevel@tonic-gate 
380Sstevel@tonic-gate void
390Sstevel@tonic-gate dtrace_getpcstack(pc_t *pcstack, int pcstack_limit, int aframes,
400Sstevel@tonic-gate     uint32_t *intrpc)
410Sstevel@tonic-gate {
420Sstevel@tonic-gate 	struct frame *fp = (struct frame *)dtrace_getfp();
430Sstevel@tonic-gate 	struct frame *nextfp, *minfp, *stacktop;
440Sstevel@tonic-gate 	int depth = 0;
450Sstevel@tonic-gate 	int on_intr, last = 0;
460Sstevel@tonic-gate 	uintptr_t pc;
470Sstevel@tonic-gate 	uintptr_t caller = CPU->cpu_dtrace_caller;
480Sstevel@tonic-gate 
490Sstevel@tonic-gate 	if ((on_intr = CPU_ON_INTR(CPU)) != 0)
500Sstevel@tonic-gate 		stacktop = (struct frame *)(CPU->cpu_intr_stack + SA(MINFRAME));
510Sstevel@tonic-gate 	else
520Sstevel@tonic-gate 		stacktop = (struct frame *)curthread->t_stk;
530Sstevel@tonic-gate 	minfp = fp;
540Sstevel@tonic-gate 
550Sstevel@tonic-gate 	aframes++;
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 	if (intrpc != NULL && depth < pcstack_limit)
580Sstevel@tonic-gate 		pcstack[depth++] = (pc_t)intrpc;
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 	while (depth < pcstack_limit) {
610Sstevel@tonic-gate 		nextfp = (struct frame *)fp->fr_savfp;
620Sstevel@tonic-gate 		pc = fp->fr_savpc;
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 		if (nextfp <= minfp || nextfp >= stacktop) {
650Sstevel@tonic-gate 			if (on_intr) {
660Sstevel@tonic-gate 				/*
670Sstevel@tonic-gate 				 * Hop from interrupt stack to thread stack.
680Sstevel@tonic-gate 				 */
690Sstevel@tonic-gate 				stacktop = (struct frame *)curthread->t_stk;
700Sstevel@tonic-gate 				minfp = (struct frame *)curthread->t_stkbase;
710Sstevel@tonic-gate 				on_intr = 0;
720Sstevel@tonic-gate 				continue;
730Sstevel@tonic-gate 			}
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 			/*
760Sstevel@tonic-gate 			 * This is the last frame we can process; indicate
770Sstevel@tonic-gate 			 * that we should return after processing this frame.
780Sstevel@tonic-gate 			 */
790Sstevel@tonic-gate 			last = 1;
800Sstevel@tonic-gate 		}
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 		if (aframes > 0) {
830Sstevel@tonic-gate 			if (--aframes == 0 && caller != NULL) {
840Sstevel@tonic-gate 				/*
850Sstevel@tonic-gate 				 * We've just run out of artificial frames,
860Sstevel@tonic-gate 				 * and we have a valid caller -- fill it in
870Sstevel@tonic-gate 				 * now.
880Sstevel@tonic-gate 				 */
890Sstevel@tonic-gate 				ASSERT(depth < pcstack_limit);
900Sstevel@tonic-gate 				pcstack[depth++] = (pc_t)caller;
910Sstevel@tonic-gate 				caller = NULL;
920Sstevel@tonic-gate 			}
930Sstevel@tonic-gate 		} else {
940Sstevel@tonic-gate 			if (depth < pcstack_limit)
950Sstevel@tonic-gate 				pcstack[depth++] = (pc_t)pc;
960Sstevel@tonic-gate 		}
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 		if (last) {
990Sstevel@tonic-gate 			while (depth < pcstack_limit)
1000Sstevel@tonic-gate 				pcstack[depth++] = NULL;
1010Sstevel@tonic-gate 			return;
1020Sstevel@tonic-gate 		}
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 		fp = nextfp;
1050Sstevel@tonic-gate 		minfp = fp;
1060Sstevel@tonic-gate 	}
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate 
109191Sahl static int
110191Sahl dtrace_getustack_common(uint64_t *pcstack, int pcstack_limit, uintptr_t pc,
111191Sahl     uintptr_t sp)
1120Sstevel@tonic-gate {
1130Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
114191Sahl 	proc_t *p = curproc;
115191Sahl 	uintptr_t oldcontext = lwp->lwp_oldcontext;
116191Sahl 	volatile uint16_t *flags =
117191Sahl 	    (volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
1180Sstevel@tonic-gate 	size_t s1, s2;
119191Sahl 	int ret = 0;
1200Sstevel@tonic-gate 
121191Sahl 	ASSERT(pcstack == NULL || pcstack_limit > 0);
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	if (p->p_model == DATAMODEL_NATIVE) {
1240Sstevel@tonic-gate 		s1 = sizeof (struct frame) + 2 * sizeof (long);
1250Sstevel@tonic-gate 		s2 = s1 + sizeof (siginfo_t);
1260Sstevel@tonic-gate 	} else {
1270Sstevel@tonic-gate 		s1 = sizeof (struct frame32) + 3 * sizeof (int);
1280Sstevel@tonic-gate 		s2 = s1 + sizeof (siginfo32_t);
1290Sstevel@tonic-gate 	}
1300Sstevel@tonic-gate 
1311880Sahl 	while (pc != 0) {
132191Sahl 		ret++;
133191Sahl 		if (pcstack != NULL) {
134191Sahl 			*pcstack++ = (uint64_t)pc;
135191Sahl 			pcstack_limit--;
136191Sahl 			if (pcstack_limit <= 0)
137191Sahl 				break;
138191Sahl 		}
1390Sstevel@tonic-gate 
1401880Sahl 		if (sp == 0)
1411880Sahl 			break;
1421880Sahl 
1430Sstevel@tonic-gate 		if (oldcontext == sp + s1 || oldcontext == sp + s2) {
1440Sstevel@tonic-gate 			if (p->p_model == DATAMODEL_NATIVE) {
1450Sstevel@tonic-gate 				ucontext_t *ucp = (ucontext_t *)oldcontext;
1460Sstevel@tonic-gate 				greg_t *gregs = ucp->uc_mcontext.gregs;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 				sp = dtrace_fulword(&gregs[REG_FP]);
1490Sstevel@tonic-gate 				pc = dtrace_fulword(&gregs[REG_PC]);
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 				oldcontext = dtrace_fulword(&ucp->uc_link);
1520Sstevel@tonic-gate 			} else {
1530Sstevel@tonic-gate 				ucontext32_t *ucp = (ucontext32_t *)oldcontext;
1540Sstevel@tonic-gate 				greg32_t *gregs = ucp->uc_mcontext.gregs;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 				sp = dtrace_fuword32(&gregs[EBP]);
1570Sstevel@tonic-gate 				pc = dtrace_fuword32(&gregs[EIP]);
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 				oldcontext = dtrace_fuword32(&ucp->uc_link);
1600Sstevel@tonic-gate 			}
1610Sstevel@tonic-gate 		} else {
1620Sstevel@tonic-gate 			if (p->p_model == DATAMODEL_NATIVE) {
1630Sstevel@tonic-gate 				struct frame *fr = (struct frame *)sp;
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 				pc = dtrace_fulword(&fr->fr_savpc);
1660Sstevel@tonic-gate 				sp = dtrace_fulword(&fr->fr_savfp);
1670Sstevel@tonic-gate 			} else {
1680Sstevel@tonic-gate 				struct frame32 *fr = (struct frame32 *)sp;
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 				pc = dtrace_fuword32(&fr->fr_savpc);
1710Sstevel@tonic-gate 				sp = dtrace_fuword32(&fr->fr_savfp);
1720Sstevel@tonic-gate 			}
1730Sstevel@tonic-gate 		}
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 		/*
1760Sstevel@tonic-gate 		 * This is totally bogus:  if we faulted, we're going to clear
1770Sstevel@tonic-gate 		 * the fault and break.  This is to deal with the apparently
1780Sstevel@tonic-gate 		 * broken Java stacks on x86.
1790Sstevel@tonic-gate 		 */
1800Sstevel@tonic-gate 		if (*flags & CPU_DTRACE_FAULT) {
1810Sstevel@tonic-gate 			*flags &= ~CPU_DTRACE_FAULT;
1820Sstevel@tonic-gate 			break;
1830Sstevel@tonic-gate 		}
1840Sstevel@tonic-gate 	}
1850Sstevel@tonic-gate 
186191Sahl 	return (ret);
187191Sahl }
188191Sahl 
189191Sahl void
190191Sahl dtrace_getupcstack(uint64_t *pcstack, int pcstack_limit)
191191Sahl {
192191Sahl 	klwp_t *lwp = ttolwp(curthread);
193191Sahl 	proc_t *p = curproc;
194191Sahl 	struct regs *rp;
195191Sahl 	uintptr_t pc, sp;
196191Sahl 	volatile uint16_t *flags =
197191Sahl 	    (volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
198191Sahl 	int n;
199191Sahl 
200191Sahl 	if (*flags & CPU_DTRACE_FAULT)
201191Sahl 		return;
202191Sahl 
203191Sahl 	if (pcstack_limit <= 0)
204191Sahl 		return;
205191Sahl 
206630Sahl 	/*
207630Sahl 	 * If there's no user context we still need to zero the stack.
208630Sahl 	 */
209630Sahl 	if (lwp == NULL || p == NULL || (rp = lwp->lwp_regs) == NULL)
210630Sahl 		goto zero;
211630Sahl 
212191Sahl 	*pcstack++ = (uint64_t)p->p_pid;
213191Sahl 	pcstack_limit--;
214191Sahl 
215191Sahl 	if (pcstack_limit <= 0)
216191Sahl 		return;
217191Sahl 
218191Sahl 	pc = rp->r_pc;
219191Sahl 	sp = rp->r_fp;
220191Sahl 
221191Sahl 	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
222191Sahl 		*pcstack++ = (uint64_t)pc;
223191Sahl 		pcstack_limit--;
224191Sahl 		if (pcstack_limit <= 0)
225191Sahl 			return;
226191Sahl 
227191Sahl 		if (p->p_model == DATAMODEL_NATIVE)
228191Sahl 			pc = dtrace_fulword((void *)rp->r_sp);
229191Sahl 		else
230191Sahl 			pc = dtrace_fuword32((void *)rp->r_sp);
231191Sahl 	}
232191Sahl 
233191Sahl 	n = dtrace_getustack_common(pcstack, pcstack_limit, pc, sp);
234191Sahl 	ASSERT(n >= 0);
235191Sahl 	ASSERT(n <= pcstack_limit);
236191Sahl 
237191Sahl 	pcstack += n;
238191Sahl 	pcstack_limit -= n;
239191Sahl 
240630Sahl zero:
2410Sstevel@tonic-gate 	while (pcstack_limit-- > 0)
2420Sstevel@tonic-gate 		*pcstack++ = NULL;
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate 
245191Sahl int
246191Sahl dtrace_getustackdepth(void)
247191Sahl {
248191Sahl 	klwp_t *lwp = ttolwp(curthread);
249191Sahl 	proc_t *p = curproc;
250191Sahl 	struct regs *rp;
251191Sahl 	uintptr_t pc, sp;
252191Sahl 	int n = 0;
253191Sahl 
254191Sahl 	if (lwp == NULL || p == NULL || (rp = lwp->lwp_regs) == NULL)
255191Sahl 		return (0);
256191Sahl 
257191Sahl 	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT))
258191Sahl 		return (-1);
259191Sahl 
260191Sahl 	pc = rp->r_pc;
261191Sahl 	sp = rp->r_fp;
262191Sahl 
263191Sahl 	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
264191Sahl 		n++;
265191Sahl 
266191Sahl 		if (p->p_model == DATAMODEL_NATIVE)
267191Sahl 			pc = dtrace_fulword((void *)rp->r_sp);
268191Sahl 		else
269191Sahl 			pc = dtrace_fuword32((void *)rp->r_sp);
270191Sahl 	}
271191Sahl 
272191Sahl 	n += dtrace_getustack_common(NULL, 0, pc, sp);
273191Sahl 
274191Sahl 	return (n);
275191Sahl }
276191Sahl 
2770Sstevel@tonic-gate void
2780Sstevel@tonic-gate dtrace_getufpstack(uint64_t *pcstack, uint64_t *fpstack, int pcstack_limit)
2790Sstevel@tonic-gate {
2800Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
281191Sahl 	proc_t *p = curproc;
2820Sstevel@tonic-gate 	struct regs *rp;
2830Sstevel@tonic-gate 	uintptr_t pc, sp, oldcontext;
284191Sahl 	volatile uint16_t *flags =
285191Sahl 	    (volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
2860Sstevel@tonic-gate 	size_t s1, s2;
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	if (*flags & CPU_DTRACE_FAULT)
2890Sstevel@tonic-gate 		return;
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	if (pcstack_limit <= 0)
2920Sstevel@tonic-gate 		return;
2930Sstevel@tonic-gate 
294630Sahl 	/*
295630Sahl 	 * If there's no user context we still need to zero the stack.
296630Sahl 	 */
297630Sahl 	if (lwp == NULL || p == NULL || (rp = lwp->lwp_regs) == NULL)
298630Sahl 		goto zero;
299630Sahl 
3000Sstevel@tonic-gate 	*pcstack++ = (uint64_t)p->p_pid;
3010Sstevel@tonic-gate 	pcstack_limit--;
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	if (pcstack_limit <= 0)
3040Sstevel@tonic-gate 		return;
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	pc = rp->r_pc;
3070Sstevel@tonic-gate 	sp = rp->r_fp;
3080Sstevel@tonic-gate 	oldcontext = lwp->lwp_oldcontext;
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	if (p->p_model == DATAMODEL_NATIVE) {
3110Sstevel@tonic-gate 		s1 = sizeof (struct frame) + 2 * sizeof (long);
3120Sstevel@tonic-gate 		s2 = s1 + sizeof (siginfo_t);
3130Sstevel@tonic-gate 	} else {
3140Sstevel@tonic-gate 		s1 = sizeof (struct frame32) + 3 * sizeof (int);
3150Sstevel@tonic-gate 		s2 = s1 + sizeof (siginfo32_t);
3160Sstevel@tonic-gate 	}
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
3190Sstevel@tonic-gate 		*pcstack++ = (uint64_t)pc;
3200Sstevel@tonic-gate 		*fpstack++ = 0;
3210Sstevel@tonic-gate 		pcstack_limit--;
3220Sstevel@tonic-gate 		if (pcstack_limit <= 0)
3230Sstevel@tonic-gate 			return;
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 		if (p->p_model == DATAMODEL_NATIVE)
3260Sstevel@tonic-gate 			pc = dtrace_fulword((void *)rp->r_sp);
3270Sstevel@tonic-gate 		else
3280Sstevel@tonic-gate 			pc = dtrace_fuword32((void *)rp->r_sp);
3290Sstevel@tonic-gate 	}
3300Sstevel@tonic-gate 
3311880Sahl 	while (pc != 0) {
3320Sstevel@tonic-gate 		*pcstack++ = (uint64_t)pc;
3330Sstevel@tonic-gate 		*fpstack++ = sp;
3340Sstevel@tonic-gate 		pcstack_limit--;
3350Sstevel@tonic-gate 		if (pcstack_limit <= 0)
3360Sstevel@tonic-gate 			break;
3370Sstevel@tonic-gate 
3381880Sahl 		if (sp == 0)
3391880Sahl 			break;
3401880Sahl 
3410Sstevel@tonic-gate 		if (oldcontext == sp + s1 || oldcontext == sp + s2) {
3420Sstevel@tonic-gate 			if (p->p_model == DATAMODEL_NATIVE) {
3430Sstevel@tonic-gate 				ucontext_t *ucp = (ucontext_t *)oldcontext;
3440Sstevel@tonic-gate 				greg_t *gregs = ucp->uc_mcontext.gregs;
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 				sp = dtrace_fulword(&gregs[REG_FP]);
3470Sstevel@tonic-gate 				pc = dtrace_fulword(&gregs[REG_PC]);
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 				oldcontext = dtrace_fulword(&ucp->uc_link);
3500Sstevel@tonic-gate 			} else {
3510Sstevel@tonic-gate 				ucontext_t *ucp = (ucontext_t *)oldcontext;
3520Sstevel@tonic-gate 				greg_t *gregs = ucp->uc_mcontext.gregs;
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 				sp = dtrace_fuword32(&gregs[EBP]);
3550Sstevel@tonic-gate 				pc = dtrace_fuword32(&gregs[EIP]);
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 				oldcontext = dtrace_fuword32(&ucp->uc_link);
3580Sstevel@tonic-gate 			}
3590Sstevel@tonic-gate 		} else {
3600Sstevel@tonic-gate 			if (p->p_model == DATAMODEL_NATIVE) {
3610Sstevel@tonic-gate 				struct frame *fr = (struct frame *)sp;
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 				pc = dtrace_fulword(&fr->fr_savpc);
3640Sstevel@tonic-gate 				sp = dtrace_fulword(&fr->fr_savfp);
3650Sstevel@tonic-gate 			} else {
3660Sstevel@tonic-gate 				struct frame32 *fr = (struct frame32 *)sp;
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 				pc = dtrace_fuword32(&fr->fr_savpc);
3690Sstevel@tonic-gate 				sp = dtrace_fuword32(&fr->fr_savfp);
3700Sstevel@tonic-gate 			}
3710Sstevel@tonic-gate 		}
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 		/*
3740Sstevel@tonic-gate 		 * This is totally bogus:  if we faulted, we're going to clear
3750Sstevel@tonic-gate 		 * the fault and break.  This is to deal with the apparently
3760Sstevel@tonic-gate 		 * broken Java stacks on x86.
3770Sstevel@tonic-gate 		 */
3780Sstevel@tonic-gate 		if (*flags & CPU_DTRACE_FAULT) {
3790Sstevel@tonic-gate 			*flags &= ~CPU_DTRACE_FAULT;
3800Sstevel@tonic-gate 			break;
3810Sstevel@tonic-gate 		}
3820Sstevel@tonic-gate 	}
3830Sstevel@tonic-gate 
384630Sahl zero:
3850Sstevel@tonic-gate 	while (pcstack_limit-- > 0)
3860Sstevel@tonic-gate 		*pcstack++ = NULL;
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate /*ARGSUSED*/
3900Sstevel@tonic-gate uint64_t
3910Sstevel@tonic-gate dtrace_getarg(int arg, int aframes)
3920Sstevel@tonic-gate {
3930Sstevel@tonic-gate 	uintptr_t val;
3940Sstevel@tonic-gate 	struct frame *fp = (struct frame *)dtrace_getfp();
3950Sstevel@tonic-gate 	uintptr_t *stack;
3960Sstevel@tonic-gate 	int i;
3970Sstevel@tonic-gate #if defined(__amd64)
3980Sstevel@tonic-gate 	/*
3990Sstevel@tonic-gate 	 * A total of 6 arguments are passed via registers; any argument with
4000Sstevel@tonic-gate 	 * index of 5 or lower is therefore in a register.
4010Sstevel@tonic-gate 	 */
4020Sstevel@tonic-gate 	int inreg = 5;
4030Sstevel@tonic-gate #endif
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	for (i = 1; i <= aframes; i++) {
4060Sstevel@tonic-gate 		fp = (struct frame *)(fp->fr_savfp);
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 		if (fp->fr_savpc == (pc_t)dtrace_invop_callsite) {
4090Sstevel@tonic-gate #if !defined(__amd64)
4100Sstevel@tonic-gate 			/*
4110Sstevel@tonic-gate 			 * If we pass through the invalid op handler, we will
4120Sstevel@tonic-gate 			 * use the pointer that it passed to the stack as the
4130Sstevel@tonic-gate 			 * second argument to dtrace_invop() as the pointer to
4140Sstevel@tonic-gate 			 * the stack.  When using this stack, we must step
4150Sstevel@tonic-gate 			 * beyond the EIP/RIP that was pushed when the trap was
4160Sstevel@tonic-gate 			 * taken -- hence the "+ 1" below.
4170Sstevel@tonic-gate 			 */
4180Sstevel@tonic-gate 			stack = ((uintptr_t **)&fp[1])[1] + 1;
4190Sstevel@tonic-gate #else
4200Sstevel@tonic-gate 			/*
4210Sstevel@tonic-gate 			 * In the case of amd64, we will use the pointer to the
4220Sstevel@tonic-gate 			 * regs structure that was pushed when we took the
4230Sstevel@tonic-gate 			 * trap.  To get this structure, we must increment
4240Sstevel@tonic-gate 			 * beyond the frame structure, and then again beyond
4250Sstevel@tonic-gate 			 * the calling RIP stored in dtrace_invop().  If the
4260Sstevel@tonic-gate 			 * argument that we're seeking is passed on the stack,
4270Sstevel@tonic-gate 			 * we'll pull the true stack pointer out of the saved
4280Sstevel@tonic-gate 			 * registers and decrement our argument by the number
4290Sstevel@tonic-gate 			 * of arguments passed in registers; if the argument
4300Sstevel@tonic-gate 			 * we're seeking is passed in regsiters, we can just
4310Sstevel@tonic-gate 			 * load it directly.
4320Sstevel@tonic-gate 			 */
4330Sstevel@tonic-gate 			struct regs *rp = (struct regs *)((uintptr_t)&fp[1] +
4340Sstevel@tonic-gate 			    sizeof (uintptr_t));
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 			if (arg <= inreg) {
4370Sstevel@tonic-gate 				stack = (uintptr_t *)&rp->r_rdi;
4380Sstevel@tonic-gate 			} else {
4390Sstevel@tonic-gate 				stack = (uintptr_t *)(rp->r_rsp);
4400Sstevel@tonic-gate 				arg -= inreg;
4410Sstevel@tonic-gate 			}
4420Sstevel@tonic-gate #endif
4430Sstevel@tonic-gate 			goto load;
4440Sstevel@tonic-gate 		}
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	}
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	/*
4490Sstevel@tonic-gate 	 * We know that we did not come through a trap to get into
4500Sstevel@tonic-gate 	 * dtrace_probe() -- the provider simply called dtrace_probe()
4510Sstevel@tonic-gate 	 * directly.  As this is the case, we need to shift the argument
4520Sstevel@tonic-gate 	 * that we're looking for:  the probe ID is the first argument to
4530Sstevel@tonic-gate 	 * dtrace_probe(), so the argument n will actually be found where
4540Sstevel@tonic-gate 	 * one would expect to find argument (n + 1).
4550Sstevel@tonic-gate 	 */
4560Sstevel@tonic-gate 	arg++;
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate #if defined(__amd64)
4590Sstevel@tonic-gate 	if (arg <= inreg) {
4600Sstevel@tonic-gate 		/*
4610Sstevel@tonic-gate 		 * This shouldn't happen.  If the argument is passed in a
4620Sstevel@tonic-gate 		 * register then it should have been, well, passed in a
4630Sstevel@tonic-gate 		 * register...
4640Sstevel@tonic-gate 		 */
4650Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
4660Sstevel@tonic-gate 		return (0);
4670Sstevel@tonic-gate 	}
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	arg -= (inreg + 1);
4700Sstevel@tonic-gate #endif
4710Sstevel@tonic-gate 	stack = (uintptr_t *)&fp[1];
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate load:
4740Sstevel@tonic-gate 	DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
4750Sstevel@tonic-gate 	val = stack[arg];
4760Sstevel@tonic-gate 	DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	return (val);
4790Sstevel@tonic-gate }
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate /*ARGSUSED*/
4820Sstevel@tonic-gate int
4830Sstevel@tonic-gate dtrace_getstackdepth(int aframes)
4840Sstevel@tonic-gate {
4850Sstevel@tonic-gate 	struct frame *fp = (struct frame *)dtrace_getfp();
4860Sstevel@tonic-gate 	struct frame *nextfp, *minfp, *stacktop;
4870Sstevel@tonic-gate 	int depth = 0;
4880Sstevel@tonic-gate 	int on_intr;
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	if ((on_intr = CPU_ON_INTR(CPU)) != 0)
4910Sstevel@tonic-gate 		stacktop = (struct frame *)(CPU->cpu_intr_stack + SA(MINFRAME));
4920Sstevel@tonic-gate 	else
4930Sstevel@tonic-gate 		stacktop = (struct frame *)curthread->t_stk;
4940Sstevel@tonic-gate 	minfp = fp;
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	aframes++;
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	for (;;) {
4990Sstevel@tonic-gate 		depth++;
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 		nextfp = (struct frame *)fp->fr_savfp;
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 		if (nextfp <= minfp || nextfp >= stacktop) {
5040Sstevel@tonic-gate 			if (on_intr) {
5050Sstevel@tonic-gate 				/*
5060Sstevel@tonic-gate 				 * Hop from interrupt stack to thread stack.
5070Sstevel@tonic-gate 				 */
5080Sstevel@tonic-gate 				stacktop = (struct frame *)curthread->t_stk;
5090Sstevel@tonic-gate 				minfp = (struct frame *)curthread->t_stkbase;
5100Sstevel@tonic-gate 				on_intr = 0;
5110Sstevel@tonic-gate 				continue;
5120Sstevel@tonic-gate 			}
5130Sstevel@tonic-gate 			break;
5140Sstevel@tonic-gate 		}
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 		fp = nextfp;
5170Sstevel@tonic-gate 		minfp = fp;
5180Sstevel@tonic-gate 	}
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	if (depth <= aframes)
5210Sstevel@tonic-gate 		return (0);
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	return (depth - aframes);
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate ulong_t
5270Sstevel@tonic-gate dtrace_getreg(struct regs *rp, uint_t reg)
5280Sstevel@tonic-gate {
5290Sstevel@tonic-gate #if defined(__amd64)
5300Sstevel@tonic-gate 	int regmap[] = {
5310Sstevel@tonic-gate 		REG_GS,		/* GS */
5320Sstevel@tonic-gate 		REG_FS,		/* FS */
5330Sstevel@tonic-gate 		REG_ES,		/* ES */
5340Sstevel@tonic-gate 		REG_DS,		/* DS */
5350Sstevel@tonic-gate 		REG_RDI,	/* EDI */
5360Sstevel@tonic-gate 		REG_RSI,	/* ESI */
5370Sstevel@tonic-gate 		REG_RBP,	/* EBP */
5380Sstevel@tonic-gate 		REG_RSP,	/* ESP */
5390Sstevel@tonic-gate 		REG_RBX,	/* EBX */
5400Sstevel@tonic-gate 		REG_RDX,	/* EDX */
5410Sstevel@tonic-gate 		REG_RCX,	/* ECX */
5420Sstevel@tonic-gate 		REG_RAX,	/* EAX */
5430Sstevel@tonic-gate 		REG_TRAPNO,	/* TRAPNO */
5440Sstevel@tonic-gate 		REG_ERR,	/* ERR */
5450Sstevel@tonic-gate 		REG_RIP,	/* EIP */
5460Sstevel@tonic-gate 		REG_CS,		/* CS */
5470Sstevel@tonic-gate 		REG_RFL,	/* EFL */
5480Sstevel@tonic-gate 		REG_RSP,	/* UESP */
5490Sstevel@tonic-gate 		REG_SS		/* SS */
5500Sstevel@tonic-gate 	};
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	if (reg <= SS) {
5530Sstevel@tonic-gate 		if (reg >= sizeof (regmap) / sizeof (int)) {
5540Sstevel@tonic-gate 			DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
5550Sstevel@tonic-gate 			return (0);
5560Sstevel@tonic-gate 		}
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 		reg = regmap[reg];
5590Sstevel@tonic-gate 	} else {
5600Sstevel@tonic-gate 		reg -= SS + 1;
5610Sstevel@tonic-gate 	}
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	switch (reg) {
5640Sstevel@tonic-gate 	case REG_RDI:
5650Sstevel@tonic-gate 		return (rp->r_rdi);
5660Sstevel@tonic-gate 	case REG_RSI:
5670Sstevel@tonic-gate 		return (rp->r_rsi);
5680Sstevel@tonic-gate 	case REG_RDX:
5690Sstevel@tonic-gate 		return (rp->r_rdx);
5700Sstevel@tonic-gate 	case REG_RCX:
5710Sstevel@tonic-gate 		return (rp->r_rcx);
5720Sstevel@tonic-gate 	case REG_R8:
5730Sstevel@tonic-gate 		return (rp->r_r8);
5740Sstevel@tonic-gate 	case REG_R9:
5750Sstevel@tonic-gate 		return (rp->r_r9);
5760Sstevel@tonic-gate 	case REG_RAX:
5770Sstevel@tonic-gate 		return (rp->r_rax);
5780Sstevel@tonic-gate 	case REG_RBX:
5790Sstevel@tonic-gate 		return (rp->r_rbx);
5800Sstevel@tonic-gate 	case REG_RBP:
5810Sstevel@tonic-gate 		return (rp->r_rbp);
5820Sstevel@tonic-gate 	case REG_R10:
5830Sstevel@tonic-gate 		return (rp->r_r10);
5840Sstevel@tonic-gate 	case REG_R11:
5850Sstevel@tonic-gate 		return (rp->r_r11);
5860Sstevel@tonic-gate 	case REG_R12:
5870Sstevel@tonic-gate 		return (rp->r_r12);
5880Sstevel@tonic-gate 	case REG_R13:
5890Sstevel@tonic-gate 		return (rp->r_r13);
5900Sstevel@tonic-gate 	case REG_R14:
5910Sstevel@tonic-gate 		return (rp->r_r14);
5920Sstevel@tonic-gate 	case REG_R15:
5930Sstevel@tonic-gate 		return (rp->r_r15);
5940Sstevel@tonic-gate 	case REG_DS:
5950Sstevel@tonic-gate 		return (rp->r_ds);
5960Sstevel@tonic-gate 	case REG_ES:
5970Sstevel@tonic-gate 		return (rp->r_es);
5980Sstevel@tonic-gate 	case REG_FS:
5990Sstevel@tonic-gate 		return (rp->r_fs);
6000Sstevel@tonic-gate 	case REG_GS:
6010Sstevel@tonic-gate 		return (rp->r_gs);
6020Sstevel@tonic-gate 	case REG_TRAPNO:
6030Sstevel@tonic-gate 		return (rp->r_trapno);
6040Sstevel@tonic-gate 	case REG_ERR:
6050Sstevel@tonic-gate 		return (rp->r_err);
6060Sstevel@tonic-gate 	case REG_RIP:
6070Sstevel@tonic-gate 		return (rp->r_rip);
6080Sstevel@tonic-gate 	case REG_CS:
6090Sstevel@tonic-gate 		return (rp->r_cs);
6100Sstevel@tonic-gate 	case REG_SS:
6110Sstevel@tonic-gate 		return (rp->r_ss);
6120Sstevel@tonic-gate 	case REG_RFL:
6130Sstevel@tonic-gate 		return (rp->r_rfl);
6140Sstevel@tonic-gate 	case REG_RSP:
6150Sstevel@tonic-gate 		return (rp->r_rsp);
6160Sstevel@tonic-gate 	default:
6170Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
6180Sstevel@tonic-gate 		return (0);
6190Sstevel@tonic-gate 	}
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate #else
6220Sstevel@tonic-gate 	if (reg > SS) {
6230Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
6240Sstevel@tonic-gate 		return (0);
6250Sstevel@tonic-gate 	}
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	return ((&rp->r_gs)[reg]);
6280Sstevel@tonic-gate #endif
6290Sstevel@tonic-gate }
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate static int
6320Sstevel@tonic-gate dtrace_copycheck(uintptr_t uaddr, uintptr_t kaddr, size_t size)
6330Sstevel@tonic-gate {
6340Sstevel@tonic-gate 	ASSERT(kaddr >= kernelbase && kaddr + size >= kaddr);
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	if (uaddr + size >= kernelbase || uaddr + size < uaddr) {
6370Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
6380Sstevel@tonic-gate 		cpu_core[CPU->cpu_id].cpuc_dtrace_illval = uaddr;
6390Sstevel@tonic-gate 		return (0);
6400Sstevel@tonic-gate 	}
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	return (1);
6430Sstevel@tonic-gate }
6440Sstevel@tonic-gate 
645*3677Ssudheer /*ARGSUSED*/
6460Sstevel@tonic-gate void
647*3677Ssudheer dtrace_copyin(uintptr_t uaddr, uintptr_t kaddr, size_t size,
648*3677Ssudheer     volatile uint16_t *flags)
6490Sstevel@tonic-gate {
6500Sstevel@tonic-gate 	if (dtrace_copycheck(uaddr, kaddr, size))
6510Sstevel@tonic-gate 		dtrace_copy(uaddr, kaddr, size);
6520Sstevel@tonic-gate }
6530Sstevel@tonic-gate 
654*3677Ssudheer /*ARGSUSED*/
6550Sstevel@tonic-gate void
656*3677Ssudheer dtrace_copyout(uintptr_t kaddr, uintptr_t uaddr, size_t size,
657*3677Ssudheer     volatile uint16_t *flags)
6580Sstevel@tonic-gate {
6590Sstevel@tonic-gate 	if (dtrace_copycheck(uaddr, kaddr, size))
6600Sstevel@tonic-gate 		dtrace_copy(kaddr, uaddr, size);
6610Sstevel@tonic-gate }
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate void
664*3677Ssudheer dtrace_copyinstr(uintptr_t uaddr, uintptr_t kaddr, size_t size,
665*3677Ssudheer     volatile uint16_t *flags)
6660Sstevel@tonic-gate {
6670Sstevel@tonic-gate 	if (dtrace_copycheck(uaddr, kaddr, size))
668*3677Ssudheer 		dtrace_copystr(uaddr, kaddr, size, flags);
6690Sstevel@tonic-gate }
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate void
672*3677Ssudheer dtrace_copyoutstr(uintptr_t kaddr, uintptr_t uaddr, size_t size,
673*3677Ssudheer     volatile uint16_t *flags)
6740Sstevel@tonic-gate {
6750Sstevel@tonic-gate 	if (dtrace_copycheck(uaddr, kaddr, size))
676*3677Ssudheer 		dtrace_copystr(kaddr, uaddr, size, flags);
6770Sstevel@tonic-gate }
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate uint8_t
6800Sstevel@tonic-gate dtrace_fuword8(void *uaddr)
6810Sstevel@tonic-gate {
6820Sstevel@tonic-gate 	extern uint8_t dtrace_fuword8_nocheck(void *);
6830Sstevel@tonic-gate 	if ((uintptr_t)uaddr >= _userlimit) {
6840Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
6850Sstevel@tonic-gate 		cpu_core[CPU->cpu_id].cpuc_dtrace_illval = (uintptr_t)uaddr;
6860Sstevel@tonic-gate 		return (0);
6870Sstevel@tonic-gate 	}
6880Sstevel@tonic-gate 	return (dtrace_fuword8_nocheck(uaddr));
6890Sstevel@tonic-gate }
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate uint16_t
6920Sstevel@tonic-gate dtrace_fuword16(void *uaddr)
6930Sstevel@tonic-gate {
6940Sstevel@tonic-gate 	extern uint16_t dtrace_fuword16_nocheck(void *);
6950Sstevel@tonic-gate 	if ((uintptr_t)uaddr >= _userlimit) {
6960Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
6970Sstevel@tonic-gate 		cpu_core[CPU->cpu_id].cpuc_dtrace_illval = (uintptr_t)uaddr;
6980Sstevel@tonic-gate 		return (0);
6990Sstevel@tonic-gate 	}
7000Sstevel@tonic-gate 	return (dtrace_fuword16_nocheck(uaddr));
7010Sstevel@tonic-gate }
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate uint32_t
7040Sstevel@tonic-gate dtrace_fuword32(void *uaddr)
7050Sstevel@tonic-gate {
7060Sstevel@tonic-gate 	extern uint32_t dtrace_fuword32_nocheck(void *);
7070Sstevel@tonic-gate 	if ((uintptr_t)uaddr >= _userlimit) {
7080Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
7090Sstevel@tonic-gate 		cpu_core[CPU->cpu_id].cpuc_dtrace_illval = (uintptr_t)uaddr;
7100Sstevel@tonic-gate 		return (0);
7110Sstevel@tonic-gate 	}
7120Sstevel@tonic-gate 	return (dtrace_fuword32_nocheck(uaddr));
7130Sstevel@tonic-gate }
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate uint64_t
7160Sstevel@tonic-gate dtrace_fuword64(void *uaddr)
7170Sstevel@tonic-gate {
7180Sstevel@tonic-gate 	extern uint64_t dtrace_fuword64_nocheck(void *);
7190Sstevel@tonic-gate 	if ((uintptr_t)uaddr >= _userlimit) {
7200Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR);
7210Sstevel@tonic-gate 		cpu_core[CPU->cpu_id].cpuc_dtrace_illval = (uintptr_t)uaddr;
7220Sstevel@tonic-gate 		return (0);
7230Sstevel@tonic-gate 	}
7240Sstevel@tonic-gate 	return (dtrace_fuword64_nocheck(uaddr));
7250Sstevel@tonic-gate }
726