xref: /onnv-gate/usr/src/uts/sparc/dtrace/dtrace_isa.c (revision 9397:e667d620a75c)
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
53682Sjhaslam  * Common Development and Distribution License (the "License").
63682Sjhaslam  * 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  */
211048Sraf 
220Sstevel@tonic-gate /*
23*9397SJonathan.Haslam@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <sys/dtrace_impl.h>
280Sstevel@tonic-gate #include <sys/atomic.h>
290Sstevel@tonic-gate #include <sys/model.h>
300Sstevel@tonic-gate #include <sys/frame.h>
310Sstevel@tonic-gate #include <sys/stack.h>
320Sstevel@tonic-gate #include <sys/machpcb.h>
330Sstevel@tonic-gate #include <sys/procfs_isa.h>
340Sstevel@tonic-gate #include <sys/cmn_err.h>
35191Sahl #include <sys/sysmacros.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #define	DTRACE_FMT3OP3_MASK	0x81000000
380Sstevel@tonic-gate #define	DTRACE_FMT3OP3		0x80000000
390Sstevel@tonic-gate #define	DTRACE_FMT3RS1_SHIFT	14
400Sstevel@tonic-gate #define	DTRACE_FMT3RD_SHIFT	25
41457Sbmc #define	DTRACE_DISP22_SHIFT	10
420Sstevel@tonic-gate #define	DTRACE_RMASK		0x1f
430Sstevel@tonic-gate #define	DTRACE_REG_L0		16
440Sstevel@tonic-gate #define	DTRACE_REG_O7		15
450Sstevel@tonic-gate #define	DTRACE_REG_I0		24
460Sstevel@tonic-gate #define	DTRACE_REG_I6		30
470Sstevel@tonic-gate #define	DTRACE_RET		0x81c7e008
480Sstevel@tonic-gate #define	DTRACE_RETL		0x81c3e008
490Sstevel@tonic-gate #define	DTRACE_SAVE_MASK	0xc1f80000
500Sstevel@tonic-gate #define	DTRACE_SAVE		0x81e00000
510Sstevel@tonic-gate #define	DTRACE_RESTORE		0x81e80000
520Sstevel@tonic-gate #define	DTRACE_CALL_MASK	0xc0000000
530Sstevel@tonic-gate #define	DTRACE_CALL		0x40000000
54*9397SJonathan.Haslam@Sun.COM #define	DTRACE_JMPL_MASK	0x81f80000
550Sstevel@tonic-gate #define	DTRACE_JMPL		0x81c00000
56457Sbmc #define	DTRACE_BA_MASK		0xdfc00000
57457Sbmc #define	DTRACE_BA		0x10800000
58457Sbmc #define	DTRACE_BA_MAX		10
590Sstevel@tonic-gate 
600Sstevel@tonic-gate extern int dtrace_getupcstack_top(uint64_t *, int, uintptr_t *);
61191Sahl extern int dtrace_getustackdepth_top(uintptr_t *);
620Sstevel@tonic-gate extern ulong_t dtrace_getreg_win(uint_t, uint_t);
630Sstevel@tonic-gate extern void dtrace_putreg_win(uint_t, ulong_t);
640Sstevel@tonic-gate extern int dtrace_fish(int, int, uintptr_t *);
650Sstevel@tonic-gate 
663682Sjhaslam int	dtrace_ustackdepth_max = 2048;
673682Sjhaslam 
680Sstevel@tonic-gate /*
690Sstevel@tonic-gate  * This is similar in principle to getpcstack(), but there are several marked
700Sstevel@tonic-gate  * differences in implementation:
710Sstevel@tonic-gate  *
720Sstevel@tonic-gate  * (a)	dtrace_getpcstack() is called from probe context.  Thus, the call
730Sstevel@tonic-gate  *	to flush_windows() from getpcstack() is a call to the probe-safe
740Sstevel@tonic-gate  *	equivalent here.
750Sstevel@tonic-gate  *
760Sstevel@tonic-gate  * (b)  dtrace_getpcstack() is willing to sacrifice some performance to get
770Sstevel@tonic-gate  *	a correct stack.  While consumers of getpcstack() are largely
780Sstevel@tonic-gate  *	subsystem-specific in-kernel debugging facilities, DTrace consumers
790Sstevel@tonic-gate  *	are arbitrary user-level analysis tools; dtrace_getpcstack() must
800Sstevel@tonic-gate  *	deliver as correct a stack as possible.  Details on the issues
810Sstevel@tonic-gate  *	surrounding stack correctness are found below.
820Sstevel@tonic-gate  *
83191Sahl  * (c)	dtrace_getpcstack() _always_ fills in pcstack_limit pc_t's -- filling
84191Sahl  *	in the difference between the stack depth and pcstack_limit with NULLs.
850Sstevel@tonic-gate  *	Due to this behavior dtrace_getpcstack() returns void.
860Sstevel@tonic-gate  *
870Sstevel@tonic-gate  * (d)	dtrace_getpcstack() takes a third parameter, aframes, that
880Sstevel@tonic-gate  *	denotes the number of _artificial frames_ on the bottom of the
890Sstevel@tonic-gate  *	stack.  An artificial frame is one induced by the provider; all
900Sstevel@tonic-gate  *	artificial frames are stripped off before frames are stored to
910Sstevel@tonic-gate  *	pcstack.
920Sstevel@tonic-gate  *
930Sstevel@tonic-gate  * (e)	dtrace_getpcstack() takes a fourth parameter, pc, that indicates
940Sstevel@tonic-gate  *	an interrupted program counter (if any).  This should be a non-NULL
950Sstevel@tonic-gate  *	value if and only if the hit probe is unanchored.  (Anchored probes
960Sstevel@tonic-gate  *	don't fire through an interrupt source.)  This parameter is used to
970Sstevel@tonic-gate  *	assure (b), above.
980Sstevel@tonic-gate  */
990Sstevel@tonic-gate void
dtrace_getpcstack(pc_t * pcstack,int pcstack_limit,int aframes,uint32_t * pc)1000Sstevel@tonic-gate dtrace_getpcstack(pc_t *pcstack, int pcstack_limit, int aframes, uint32_t *pc)
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate 	struct frame *fp, *nextfp, *minfp, *stacktop;
1030Sstevel@tonic-gate 	int depth = 0;
1040Sstevel@tonic-gate 	int on_intr, j = 0;
1050Sstevel@tonic-gate 	uint32_t i, r;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	fp = (struct frame *)((caddr_t)dtrace_getfp() + STACK_BIAS);
1080Sstevel@tonic-gate 	dtrace_flush_windows();
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	if (pc != NULL) {
1110Sstevel@tonic-gate 		/*
1120Sstevel@tonic-gate 		 * If we've been passed a non-NULL pc, we need to determine
1130Sstevel@tonic-gate 		 * whether or not the specified program counter falls in a leaf
1140Sstevel@tonic-gate 		 * function.  If it falls within a leaf function, we know that
1150Sstevel@tonic-gate 		 * %o7 is valid in its frame (and we can just drive on).  If
1160Sstevel@tonic-gate 		 * it's a non-leaf, however, we know that %o7 is garbage in the
1170Sstevel@tonic-gate 		 * bottom frame.  To trim this frame, we simply increment
1180Sstevel@tonic-gate 		 * aframes and drop into the stack-walking loop.
1190Sstevel@tonic-gate 		 *
1200Sstevel@tonic-gate 		 * To quickly determine if the specified program counter is in
1210Sstevel@tonic-gate 		 * a leaf function, we exploit the fact that leaf functions
1220Sstevel@tonic-gate 		 * tend to be short and non-leaf functions tend to frequently
1230Sstevel@tonic-gate 		 * perform operations that are only permitted in a non-leaf
1240Sstevel@tonic-gate 		 * function (e.g., using the %i's or %l's; calling a function;
1250Sstevel@tonic-gate 		 * performing a restore).  We exploit these tendencies by
1260Sstevel@tonic-gate 		 * simply scanning forward from the specified %pc -- if we see
1270Sstevel@tonic-gate 		 * an operation only permitted in a non-leaf, we know we're in
1280Sstevel@tonic-gate 		 * a non-leaf; if we see a retl, we know we're in a leaf.
1290Sstevel@tonic-gate 		 * Fortunately, one need not perform anywhere near full
1300Sstevel@tonic-gate 		 * disassembly to effectively determine the former: determining
1310Sstevel@tonic-gate 		 * that an instruction is a format-3 instruction and decoding
1320Sstevel@tonic-gate 		 * its rd and rs1 fields, for example, requires very little
1330Sstevel@tonic-gate 		 * manipulation.  Overall, this method of leaf determination
1340Sstevel@tonic-gate 		 * performs quite well:  on average, we only examine between
1350Sstevel@tonic-gate 		 * 1.5 and 2.5 instructions before making the determination.
1360Sstevel@tonic-gate 		 * (Outliers do exist, however; of note is the non-leaf
1370Sstevel@tonic-gate 		 * function ip_sioctl_not_ours() which -- as of this writing --
1380Sstevel@tonic-gate 		 * has a whopping 455 straight instructions that manipulate
1390Sstevel@tonic-gate 		 * only %g's and %o's.)
1400Sstevel@tonic-gate 		 */
141457Sbmc 		int delay = 0, branches = 0, taken = 0;
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 		if (depth < pcstack_limit)
1441048Sraf 			pcstack[depth++] = (pc_t)(uintptr_t)pc;
1450Sstevel@tonic-gate 
146457Sbmc 		/*
147457Sbmc 		 * Our heuristic is exactly that -- a heuristic -- and there
148457Sbmc 		 * exists a possibility that we could be either be vectored
149457Sbmc 		 * off into the weeds (by following a bogus branch) or could
150457Sbmc 		 * wander off the end of the function and off the end of a
151457Sbmc 		 * text mapping (by not following a conditional branch at the
152457Sbmc 		 * end of the function that is effectively always taken).  So
153457Sbmc 		 * as a precautionary measure, we set the NOFAULT flag.
154457Sbmc 		 */
155457Sbmc 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
156457Sbmc 
1570Sstevel@tonic-gate 		for (;;) {
1580Sstevel@tonic-gate 			i = pc[j++];
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 			if ((i & DTRACE_FMT3OP3_MASK) == DTRACE_FMT3OP3) {
1610Sstevel@tonic-gate 				/*
1620Sstevel@tonic-gate 				 * This is a format-3 instruction.  We can
1630Sstevel@tonic-gate 				 * look at rd and rs1.
1640Sstevel@tonic-gate 				 */
1650Sstevel@tonic-gate 				r = (i >> DTRACE_FMT3RS1_SHIFT) & DTRACE_RMASK;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 				if (r >= DTRACE_REG_L0)
1680Sstevel@tonic-gate 					goto nonleaf;
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 				r = (i >> DTRACE_FMT3RD_SHIFT) & DTRACE_RMASK;
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 				if (r >= DTRACE_REG_L0)
1730Sstevel@tonic-gate 					goto nonleaf;
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 				if ((i & DTRACE_JMPL_MASK) == DTRACE_JMPL) {
1760Sstevel@tonic-gate 					delay = 1;
1770Sstevel@tonic-gate 					continue;
1780Sstevel@tonic-gate 				}
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 				/*
1810Sstevel@tonic-gate 				 * If we see explicit manipulation with %o7
1820Sstevel@tonic-gate 				 * as a destination register, we know that
1830Sstevel@tonic-gate 				 * %o7 is likely bogus -- and we treat this
1840Sstevel@tonic-gate 				 * function as a non-leaf.
1850Sstevel@tonic-gate 				 */
1860Sstevel@tonic-gate 				if (r == DTRACE_REG_O7) {
1870Sstevel@tonic-gate 					if (delay)
1880Sstevel@tonic-gate 						goto leaf;
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 					i &= DTRACE_JMPL_MASK;
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 					if (i == DTRACE_JMPL) {
1930Sstevel@tonic-gate 						delay = 1;
1940Sstevel@tonic-gate 						continue;
1950Sstevel@tonic-gate 					}
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 					goto nonleaf;
1980Sstevel@tonic-gate 				}
1990Sstevel@tonic-gate 			} else {
2000Sstevel@tonic-gate 				/*
2010Sstevel@tonic-gate 				 * If this is a call, it may or may not be
2020Sstevel@tonic-gate 				 * a leaf; we need to check the delay slot.
2030Sstevel@tonic-gate 				 */
2040Sstevel@tonic-gate 				if ((i & DTRACE_CALL_MASK) == DTRACE_CALL) {
2050Sstevel@tonic-gate 					delay = 1;
2060Sstevel@tonic-gate 					continue;
2070Sstevel@tonic-gate 				}
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 				/*
2100Sstevel@tonic-gate 				 * If we see a ret it's not a leaf; if we
2110Sstevel@tonic-gate 				 * see a retl, it is a leaf.
2120Sstevel@tonic-gate 				 */
2130Sstevel@tonic-gate 				if (i == DTRACE_RET)
2140Sstevel@tonic-gate 					goto nonleaf;
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 				if (i == DTRACE_RETL)
2170Sstevel@tonic-gate 					goto leaf;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 				/*
220457Sbmc 				 * If this is a ba (annulled or not), then we
221457Sbmc 				 * need to actually follow the branch.  No, we
222457Sbmc 				 * don't look at the delay slot -- hopefully
223457Sbmc 				 * anything that can be gleaned from the delay
224457Sbmc 				 * slot can also be gleaned from the branch
225457Sbmc 				 * target.  To prevent ourselves from iterating
226457Sbmc 				 * infinitely, we clamp the number of branches
227457Sbmc 				 * that we'll follow, and we refuse to follow
228457Sbmc 				 * the same branch twice consecutively.  In
229457Sbmc 				 * both cases, we abort by deciding that we're
230457Sbmc 				 * looking at a leaf.  While in theory this
231457Sbmc 				 * could be wrong (we could be in the middle of
232457Sbmc 				 * a loop in a non-leaf that ends with a ba and
233457Sbmc 				 * only manipulates outputs and globals in the
234457Sbmc 				 * body of the loop -- therefore leading us to
235457Sbmc 				 * the wrong conclusion), this doesn't seem to
236457Sbmc 				 * crop up in practice.  (Or rather, this
237457Sbmc 				 * condition could not be deliberately induced,
238457Sbmc 				 * despite concerted effort.)
239457Sbmc 				 */
240457Sbmc 				if ((i & DTRACE_BA_MASK) == DTRACE_BA) {
241457Sbmc 					if (++branches == DTRACE_BA_MAX ||
242457Sbmc 					    taken == j)
243457Sbmc 						goto nonleaf;
244457Sbmc 
245457Sbmc 					taken = j;
246457Sbmc 					j += ((int)(i << DTRACE_DISP22_SHIFT) >>
247457Sbmc 					    DTRACE_DISP22_SHIFT) - 1;
248457Sbmc 					continue;
249457Sbmc 				}
250457Sbmc 
251457Sbmc 				/*
2520Sstevel@tonic-gate 				 * Finally, if it's a save, it should be
2530Sstevel@tonic-gate 				 * treated as a leaf; if it's a restore it
2540Sstevel@tonic-gate 				 * should not be treated as a leaf.
2550Sstevel@tonic-gate 				 */
2560Sstevel@tonic-gate 				if ((i & DTRACE_SAVE_MASK) == DTRACE_SAVE)
2570Sstevel@tonic-gate 					goto leaf;
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 				if ((i & DTRACE_SAVE_MASK) == DTRACE_RESTORE)
2600Sstevel@tonic-gate 					goto nonleaf;
2610Sstevel@tonic-gate 			}
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 			if (delay) {
2640Sstevel@tonic-gate 				/*
2650Sstevel@tonic-gate 				 * If this was a delay slot instruction and
2660Sstevel@tonic-gate 				 * we didn't pick it up elsewhere, this is a
2670Sstevel@tonic-gate 				 * non-leaf.
2680Sstevel@tonic-gate 				 */
2690Sstevel@tonic-gate 				goto nonleaf;
2700Sstevel@tonic-gate 			}
2710Sstevel@tonic-gate 		}
2720Sstevel@tonic-gate nonleaf:
2730Sstevel@tonic-gate 		aframes++;
2740Sstevel@tonic-gate leaf:
275457Sbmc 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
2760Sstevel@tonic-gate 	}
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	if ((on_intr = CPU_ON_INTR(CPU)) != 0)
2790Sstevel@tonic-gate 		stacktop = (struct frame *)(CPU->cpu_intr_stack + SA(MINFRAME));
2800Sstevel@tonic-gate 	else
2810Sstevel@tonic-gate 		stacktop = (struct frame *)curthread->t_stk;
2820Sstevel@tonic-gate 	minfp = fp;
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	while (depth < pcstack_limit) {
2850Sstevel@tonic-gate 		nextfp = (struct frame *)((caddr_t)fp->fr_savfp + STACK_BIAS);
2860Sstevel@tonic-gate 		if (nextfp <= minfp || nextfp >= stacktop) {
2870Sstevel@tonic-gate 			if (!on_intr && nextfp == stacktop && aframes != 0) {
2880Sstevel@tonic-gate 				/*
2890Sstevel@tonic-gate 				 * If we are exactly at the top of the stack
2900Sstevel@tonic-gate 				 * with a non-zero number of artificial frames,
2910Sstevel@tonic-gate 				 * it must be that the stack is filled with
2920Sstevel@tonic-gate 				 * nothing _but_ artificial frames.  In this
2930Sstevel@tonic-gate 				 * case, we assert that this is so, zero
2940Sstevel@tonic-gate 				 * pcstack, and return.
2950Sstevel@tonic-gate 				 */
2960Sstevel@tonic-gate 				ASSERT(aframes == 1);
2970Sstevel@tonic-gate 				ASSERT(depth == 0);
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 				while (depth < pcstack_limit)
3000Sstevel@tonic-gate 					pcstack[depth++] = NULL;
3010Sstevel@tonic-gate 				return;
3020Sstevel@tonic-gate 			}
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 			if (on_intr) {
3050Sstevel@tonic-gate 				/*
3060Sstevel@tonic-gate 				 * Hop from interrupt stack to thread stack.
3070Sstevel@tonic-gate 				 */
3080Sstevel@tonic-gate 				stacktop = (struct frame *)curthread->t_stk;
3090Sstevel@tonic-gate 				minfp = (struct frame *)curthread->t_stkbase;
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 				on_intr = 0;
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 				if (nextfp > minfp && nextfp < stacktop)
3140Sstevel@tonic-gate 					continue;
3150Sstevel@tonic-gate 			} else {
3160Sstevel@tonic-gate 				/*
3170Sstevel@tonic-gate 				 * High-level interrupts may occur when %sp is
3180Sstevel@tonic-gate 				 * not necessarily contained in the stack
3190Sstevel@tonic-gate 				 * bounds implied by %g7 -- interrupt thread
3200Sstevel@tonic-gate 				 * management runs with %pil at DISP_LEVEL,
3210Sstevel@tonic-gate 				 * and high-level interrupts may thus occur
3220Sstevel@tonic-gate 				 * in windows when %sp and %g7 are not self-
3230Sstevel@tonic-gate 				 * consistent.  If we call dtrace_getpcstack()
3240Sstevel@tonic-gate 				 * from a high-level interrupt that has occurred
3250Sstevel@tonic-gate 				 * in such a window, we will fail the above test
3260Sstevel@tonic-gate 				 * of nextfp against minfp/stacktop.  If the
3270Sstevel@tonic-gate 				 * high-level interrupt has in turn interrupted
3280Sstevel@tonic-gate 				 * a non-passivated interrupt thread, we
3290Sstevel@tonic-gate 				 * will execute the below code with non-zero
3300Sstevel@tonic-gate 				 * aframes.  We therefore want to assert that
3310Sstevel@tonic-gate 				 * aframes is zero _or_ we are in a high-level
3320Sstevel@tonic-gate 				 * interrupt -- but because cpu_intr_actv is
3330Sstevel@tonic-gate 				 * updated with high-level interrupts enabled,
3340Sstevel@tonic-gate 				 * we must reduce this to only asserting that
3350Sstevel@tonic-gate 				 * %pil is greater than DISP_LEVEL.
3360Sstevel@tonic-gate 				 */
3370Sstevel@tonic-gate 				ASSERT(aframes == 0 ||
3380Sstevel@tonic-gate 				    dtrace_getipl() > DISP_LEVEL);
3390Sstevel@tonic-gate 				pcstack[depth++] = (pc_t)fp->fr_savpc;
3400Sstevel@tonic-gate 			}
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 			while (depth < pcstack_limit)
3430Sstevel@tonic-gate 				pcstack[depth++] = NULL;
3440Sstevel@tonic-gate 			return;
3450Sstevel@tonic-gate 		}
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 		if (aframes > 0) {
3480Sstevel@tonic-gate 			aframes--;
3490Sstevel@tonic-gate 		} else {
3500Sstevel@tonic-gate 			pcstack[depth++] = (pc_t)fp->fr_savpc;
3510Sstevel@tonic-gate 		}
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 		fp = nextfp;
3540Sstevel@tonic-gate 		minfp = fp;
3550Sstevel@tonic-gate 	}
3560Sstevel@tonic-gate }
3570Sstevel@tonic-gate 
358191Sahl static int
dtrace_getustack_common(uint64_t * pcstack,int pcstack_limit,uintptr_t sp)359191Sahl dtrace_getustack_common(uint64_t *pcstack, int pcstack_limit, uintptr_t sp)
360191Sahl {
361191Sahl 	proc_t *p = curproc;
362191Sahl 	int ret = 0;
3633682Sjhaslam 	uintptr_t oldsp;
3643682Sjhaslam 	volatile uint16_t *flags =
3653682Sjhaslam 	    (volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
366191Sahl 
367191Sahl 	ASSERT(pcstack == NULL || pcstack_limit > 0);
3683682Sjhaslam 	ASSERT(dtrace_ustackdepth_max > 0);
369191Sahl 
370191Sahl 	if (p->p_model == DATAMODEL_NATIVE) {
371191Sahl 		for (;;) {
372191Sahl 			struct frame *fr = (struct frame *)(sp + STACK_BIAS);
373191Sahl 			uintptr_t pc;
374191Sahl 
375191Sahl 			if (sp == 0 || fr == NULL ||
376191Sahl 			    !IS_P2ALIGNED((uintptr_t)fr, STACK_ALIGN))
377191Sahl 				break;
378191Sahl 
3793682Sjhaslam 			oldsp = sp;
3803682Sjhaslam 
381191Sahl 			pc = dtrace_fulword(&fr->fr_savpc);
382191Sahl 			sp = dtrace_fulword(&fr->fr_savfp);
383191Sahl 
384191Sahl 			if (pc == 0)
385191Sahl 				break;
386191Sahl 
3873682Sjhaslam 			/*
3883682Sjhaslam 			 * We limit the number of times we can go around this
3893682Sjhaslam 			 * loop to account for a circular stack.
3903682Sjhaslam 			 */
3913682Sjhaslam 			if (sp == oldsp || ret++ >= dtrace_ustackdepth_max) {
3923682Sjhaslam 				*flags |= CPU_DTRACE_BADSTACK;
3933682Sjhaslam 				cpu_core[CPU->cpu_id].cpuc_dtrace_illval = sp;
3943682Sjhaslam 				break;
3953682Sjhaslam 			}
396191Sahl 
397191Sahl 			if (pcstack != NULL) {
398191Sahl 				*pcstack++ = pc;
399191Sahl 				pcstack_limit--;
400191Sahl 				if (pcstack_limit == 0)
401191Sahl 					break;
402191Sahl 			}
403191Sahl 		}
404191Sahl 	} else {
4051399Sahl 		/*
4061399Sahl 		 * Truncate the stack pointer to 32-bits as there may be
4071399Sahl 		 * garbage in the upper bits which would normally be ignored
4081399Sahl 		 * by the processor in 32-bit mode.
4091399Sahl 		 */
4101399Sahl 		sp = (uint32_t)sp;
4111399Sahl 
412191Sahl 		for (;;) {
413191Sahl 			struct frame32 *fr = (struct frame32 *)sp;
414191Sahl 			uint32_t pc;
415191Sahl 
416191Sahl 			if (sp == 0 ||
417191Sahl 			    !IS_P2ALIGNED((uintptr_t)fr, STACK_ALIGN32))
418191Sahl 				break;
419191Sahl 
4203682Sjhaslam 			oldsp = sp;
4213682Sjhaslam 
422191Sahl 			pc = dtrace_fuword32(&fr->fr_savpc);
423191Sahl 			sp = dtrace_fuword32(&fr->fr_savfp);
424191Sahl 
425191Sahl 			if (pc == 0)
426191Sahl 				break;
427191Sahl 
4283682Sjhaslam 			if (sp == oldsp || ret++ >= dtrace_ustackdepth_max) {
4293682Sjhaslam 				*flags |= CPU_DTRACE_BADSTACK;
4303682Sjhaslam 				cpu_core[CPU->cpu_id].cpuc_dtrace_illval = sp;
4313682Sjhaslam 				break;
4323682Sjhaslam 			}
433191Sahl 
434191Sahl 			if (pcstack != NULL) {
435191Sahl 				*pcstack++ = pc;
436191Sahl 				pcstack_limit--;
437191Sahl 				if (pcstack_limit == 0)
438191Sahl 					break;
439191Sahl 			}
440191Sahl 		}
441191Sahl 	}
442191Sahl 
443191Sahl 	return (ret);
444191Sahl }
445191Sahl 
4460Sstevel@tonic-gate void
dtrace_getupcstack(uint64_t * pcstack,int pcstack_limit)4470Sstevel@tonic-gate dtrace_getupcstack(uint64_t *pcstack, int pcstack_limit)
4480Sstevel@tonic-gate {
4490Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
450191Sahl 	proc_t *p = curproc;
4510Sstevel@tonic-gate 	struct regs *rp;
4520Sstevel@tonic-gate 	uintptr_t sp;
4530Sstevel@tonic-gate 	int n;
4540Sstevel@tonic-gate 
4555114Sahl 	ASSERT(DTRACE_CPUFLAG_ISSET(CPU_DTRACE_NOFAULT));
4565114Sahl 
457630Sahl 	if (pcstack_limit <= 0)
4580Sstevel@tonic-gate 		return;
4590Sstevel@tonic-gate 
460630Sahl 	/*
461630Sahl 	 * If there's no user context we still need to zero the stack.
462630Sahl 	 */
463630Sahl 	if (lwp == NULL || p == NULL || (rp = lwp->lwp_regs) == NULL)
464630Sahl 		goto zero;
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	*pcstack++ = (uint64_t)p->p_pid;
4670Sstevel@tonic-gate 	pcstack_limit--;
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	if (pcstack_limit <= 0)
4700Sstevel@tonic-gate 		return;
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	*pcstack++ = (uint64_t)rp->r_pc;
4730Sstevel@tonic-gate 	pcstack_limit--;
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	if (pcstack_limit <= 0)
4760Sstevel@tonic-gate 		return;
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
4790Sstevel@tonic-gate 		*pcstack++ = (uint64_t)rp->r_o7;
4800Sstevel@tonic-gate 		pcstack_limit--;
4810Sstevel@tonic-gate 		if (pcstack_limit <= 0)
4820Sstevel@tonic-gate 			return;
4830Sstevel@tonic-gate 	}
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	sp = rp->r_sp;
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	n = dtrace_getupcstack_top(pcstack, pcstack_limit, &sp);
4880Sstevel@tonic-gate 	ASSERT(n >= 0);
4890Sstevel@tonic-gate 	ASSERT(n <= pcstack_limit);
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	pcstack += n;
4920Sstevel@tonic-gate 	pcstack_limit -= n;
493191Sahl 	if (pcstack_limit <= 0)
494191Sahl 		return;
4950Sstevel@tonic-gate 
496191Sahl 	n = dtrace_getustack_common(pcstack, pcstack_limit, sp);
497191Sahl 	ASSERT(n >= 0);
498191Sahl 	ASSERT(n <= pcstack_limit);
4990Sstevel@tonic-gate 
500191Sahl 	pcstack += n;
501191Sahl 	pcstack_limit -= n;
5020Sstevel@tonic-gate 
503630Sahl zero:
5040Sstevel@tonic-gate 	while (pcstack_limit-- > 0)
5050Sstevel@tonic-gate 		*pcstack++ = NULL;
5060Sstevel@tonic-gate }
5070Sstevel@tonic-gate 
508191Sahl int
dtrace_getustackdepth(void)509191Sahl dtrace_getustackdepth(void)
510191Sahl {
511191Sahl 	klwp_t *lwp = ttolwp(curthread);
512191Sahl 	proc_t *p = curproc;
513191Sahl 	struct regs *rp;
514191Sahl 	uintptr_t sp;
515191Sahl 	int n = 1;
516191Sahl 
517191Sahl 	if (lwp == NULL || p == NULL || (rp = lwp->lwp_regs) == NULL)
518191Sahl 		return (0);
519191Sahl 
520191Sahl 	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT))
521191Sahl 		return (-1);
522191Sahl 
523191Sahl 	sp = rp->r_sp;
524191Sahl 
525191Sahl 	n += dtrace_getustackdepth_top(&sp);
526191Sahl 	n += dtrace_getustack_common(NULL, 0, sp);
527191Sahl 
528630Sahl 	/*
529630Sahl 	 * Add one more to the stack depth if we're in an entry probe as long
530630Sahl 	 * as the return address is non-NULL or there are additional frames
531630Sahl 	 * beyond that NULL return address.
532630Sahl 	 */
533630Sahl 	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY) &&
534630Sahl 	    (rp->r_o7 != NULL || n != 1))
535630Sahl 		n++;
536630Sahl 
537191Sahl 	return (n);
538191Sahl }
539191Sahl 
5400Sstevel@tonic-gate void
dtrace_getufpstack(uint64_t * pcstack,uint64_t * fpstack,int pcstack_limit)5410Sstevel@tonic-gate dtrace_getufpstack(uint64_t *pcstack, uint64_t *fpstack, int pcstack_limit)
5420Sstevel@tonic-gate {
5430Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
5440Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
5450Sstevel@tonic-gate 	struct regs *rp;
5460Sstevel@tonic-gate 	uintptr_t sp;
5470Sstevel@tonic-gate 
548630Sahl 	if (pcstack_limit <= 0)
5490Sstevel@tonic-gate 		return;
5500Sstevel@tonic-gate 
551630Sahl 	/*
552630Sahl 	 * If there's no user context we still need to zero the stack.
553630Sahl 	 */
554630Sahl 	if (lwp == NULL || p == NULL || (rp = lwp->lwp_regs) == NULL)
555630Sahl 		goto zero;
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	*pcstack++ = (uint64_t)p->p_pid;
5580Sstevel@tonic-gate 	pcstack_limit--;
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	if (pcstack_limit <= 0)
5610Sstevel@tonic-gate 		return;
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
5640Sstevel@tonic-gate 		*fpstack++ = 0;
5650Sstevel@tonic-gate 		*pcstack++ = (uint64_t)rp->r_pc;
5660Sstevel@tonic-gate 		pcstack_limit--;
5670Sstevel@tonic-gate 		if (pcstack_limit <= 0)
5680Sstevel@tonic-gate 			return;
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 		*fpstack++ = (uint64_t)rp->r_sp;
5710Sstevel@tonic-gate 		*pcstack++ = (uint64_t)rp->r_o7;
5720Sstevel@tonic-gate 		pcstack_limit--;
5730Sstevel@tonic-gate 	} else {
5740Sstevel@tonic-gate 		*fpstack++ = (uint64_t)rp->r_sp;
5750Sstevel@tonic-gate 		*pcstack++ = (uint64_t)rp->r_pc;
5760Sstevel@tonic-gate 		pcstack_limit--;
5770Sstevel@tonic-gate 	}
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	if (pcstack_limit <= 0)
5800Sstevel@tonic-gate 		return;
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 	sp = rp->r_sp;
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 	dtrace_flush_user_windows();
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	if (p->p_model == DATAMODEL_NATIVE) {
5870Sstevel@tonic-gate 		while (pcstack_limit > 0) {
5880Sstevel@tonic-gate 			struct frame *fr = (struct frame *)(sp + STACK_BIAS);
5890Sstevel@tonic-gate 			uintptr_t pc;
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 			if (sp == 0 || fr == NULL ||
5920Sstevel@tonic-gate 			    ((uintptr_t)&fr->fr_savpc & 3) != 0 ||
5930Sstevel@tonic-gate 			    ((uintptr_t)&fr->fr_savfp & 3) != 0)
5940Sstevel@tonic-gate 				break;
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 			pc = dtrace_fulword(&fr->fr_savpc);
5970Sstevel@tonic-gate 			sp = dtrace_fulword(&fr->fr_savfp);
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 			if (pc == 0)
6000Sstevel@tonic-gate 				break;
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 			*fpstack++ = sp;
6030Sstevel@tonic-gate 			*pcstack++ = pc;
6040Sstevel@tonic-gate 			pcstack_limit--;
6050Sstevel@tonic-gate 		}
6060Sstevel@tonic-gate 	} else {
6071399Sahl 		/*
6081399Sahl 		 * Truncate the stack pointer to 32-bits as there may be
6091399Sahl 		 * garbage in the upper bits which would normally be ignored
6101399Sahl 		 * by the processor in 32-bit mode.
6111399Sahl 		 */
6121399Sahl 		sp = (uint32_t)sp;
6131399Sahl 
6140Sstevel@tonic-gate 		while (pcstack_limit > 0) {
6150Sstevel@tonic-gate 			struct frame32 *fr = (struct frame32 *)sp;
6160Sstevel@tonic-gate 			uint32_t pc;
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 			if (sp == 0 ||
6190Sstevel@tonic-gate 			    ((uintptr_t)&fr->fr_savpc & 3) != 0 ||
6200Sstevel@tonic-gate 			    ((uintptr_t)&fr->fr_savfp & 3) != 0)
6210Sstevel@tonic-gate 				break;
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 			pc = dtrace_fuword32(&fr->fr_savpc);
6240Sstevel@tonic-gate 			sp = dtrace_fuword32(&fr->fr_savfp);
6250Sstevel@tonic-gate 
626191Sahl 			if (pc == 0)
627191Sahl 				break;
628191Sahl 
6290Sstevel@tonic-gate 			*fpstack++ = sp;
6300Sstevel@tonic-gate 			*pcstack++ = pc;
6310Sstevel@tonic-gate 			pcstack_limit--;
6320Sstevel@tonic-gate 		}
6330Sstevel@tonic-gate 	}
6340Sstevel@tonic-gate 
635630Sahl zero:
6360Sstevel@tonic-gate 	while (pcstack_limit-- > 0)
6370Sstevel@tonic-gate 		*pcstack++ = NULL;
6380Sstevel@tonic-gate }
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate uint64_t
dtrace_getarg(int arg,int aframes)6410Sstevel@tonic-gate dtrace_getarg(int arg, int aframes)
6420Sstevel@tonic-gate {
6430Sstevel@tonic-gate 	uintptr_t val;
6440Sstevel@tonic-gate 	struct frame *fp;
6450Sstevel@tonic-gate 	uint64_t rval;
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	/*
6480Sstevel@tonic-gate 	 * Account for the fact that dtrace_getarg() consumes an additional
6490Sstevel@tonic-gate 	 * stack frame.
6500Sstevel@tonic-gate 	 */
6510Sstevel@tonic-gate 	aframes++;
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	if (arg < 6) {
6540Sstevel@tonic-gate 		if (dtrace_fish(aframes, DTRACE_REG_I0 + arg, &val) == 0)
6550Sstevel@tonic-gate 			return (val);
6560Sstevel@tonic-gate 	} else {
6570Sstevel@tonic-gate 		if (dtrace_fish(aframes, DTRACE_REG_I6, &val) == 0) {
6580Sstevel@tonic-gate 			/*
6590Sstevel@tonic-gate 			 * We have a stack pointer; grab the argument.
6600Sstevel@tonic-gate 			 */
6610Sstevel@tonic-gate 			fp = (struct frame *)(val + STACK_BIAS);
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
6640Sstevel@tonic-gate 			rval = fp->fr_argx[arg - 6];
6650Sstevel@tonic-gate 			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 			return (rval);
6680Sstevel@tonic-gate 		}
6690Sstevel@tonic-gate 	}
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 	/*
6720Sstevel@tonic-gate 	 * There are other ways to do this.  But the slow, painful way works
6730Sstevel@tonic-gate 	 * just fine.  Because this requires some loads, we need to set
6740Sstevel@tonic-gate 	 * CPU_DTRACE_NOFAULT to protect against looking for an argument that
6750Sstevel@tonic-gate 	 * isn't there.
6760Sstevel@tonic-gate 	 */
6770Sstevel@tonic-gate 	fp = (struct frame *)((caddr_t)dtrace_getfp() + STACK_BIAS);
6780Sstevel@tonic-gate 	dtrace_flush_windows();
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 	for (aframes -= 1; aframes; aframes--)
6830Sstevel@tonic-gate 		fp = (struct frame *)((caddr_t)fp->fr_savfp + STACK_BIAS);
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 	if (arg < 6) {
6860Sstevel@tonic-gate 		rval = fp->fr_arg[arg];
6870Sstevel@tonic-gate 	} else {
6880Sstevel@tonic-gate 		fp = (struct frame *)((caddr_t)fp->fr_savfp + STACK_BIAS);
6890Sstevel@tonic-gate 		rval = fp->fr_argx[arg - 6];
6900Sstevel@tonic-gate 	}
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 	return (rval);
6950Sstevel@tonic-gate }
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate int
dtrace_getstackdepth(int aframes)6980Sstevel@tonic-gate dtrace_getstackdepth(int aframes)
6990Sstevel@tonic-gate {
7000Sstevel@tonic-gate 	struct frame *fp, *nextfp, *minfp, *stacktop;
7010Sstevel@tonic-gate 	int depth = 0;
7020Sstevel@tonic-gate 	int on_intr;
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 	fp = (struct frame *)((caddr_t)dtrace_getfp() + STACK_BIAS);
7050Sstevel@tonic-gate 	dtrace_flush_windows();
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 	if ((on_intr = CPU_ON_INTR(CPU)) != 0)
7080Sstevel@tonic-gate 		stacktop = (struct frame *)CPU->cpu_intr_stack + SA(MINFRAME);
7090Sstevel@tonic-gate 	else
7100Sstevel@tonic-gate 		stacktop = (struct frame *)curthread->t_stk;
7110Sstevel@tonic-gate 	minfp = fp;
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	for (;;) {
7140Sstevel@tonic-gate 		nextfp = (struct frame *)((caddr_t)fp->fr_savfp + STACK_BIAS);
7150Sstevel@tonic-gate 		if (nextfp <= minfp || nextfp >= stacktop) {
7160Sstevel@tonic-gate 			if (on_intr) {
7170Sstevel@tonic-gate 				/*
7180Sstevel@tonic-gate 				 * Hop from interrupt stack to thread stack.
7190Sstevel@tonic-gate 				 */
7200Sstevel@tonic-gate 				stacktop = (struct frame *)curthread->t_stk;
7210Sstevel@tonic-gate 				minfp = (struct frame *)curthread->t_stkbase;
7220Sstevel@tonic-gate 				on_intr = 0;
7230Sstevel@tonic-gate 				continue;
7240Sstevel@tonic-gate 			}
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 			return (++depth);
7270Sstevel@tonic-gate 		}
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 		if (aframes > 0) {
7300Sstevel@tonic-gate 			aframes--;
7310Sstevel@tonic-gate 		} else {
7320Sstevel@tonic-gate 			depth++;
7330Sstevel@tonic-gate 		}
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 		fp = nextfp;
7360Sstevel@tonic-gate 		minfp = fp;
7370Sstevel@tonic-gate 	}
7380Sstevel@tonic-gate }
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate /*
7410Sstevel@tonic-gate  * This uses the same register numbering scheme as in sys/procfs_isa.h.
7420Sstevel@tonic-gate  */
7430Sstevel@tonic-gate ulong_t
dtrace_getreg(struct regs * rp,uint_t reg)7440Sstevel@tonic-gate dtrace_getreg(struct regs *rp, uint_t reg)
7450Sstevel@tonic-gate {
7460Sstevel@tonic-gate 	ulong_t value;
7470Sstevel@tonic-gate 	uintptr_t fp;
7480Sstevel@tonic-gate 	struct machpcb *mpcb;
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 	if (reg == R_G0)
7510Sstevel@tonic-gate 		return (0);
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	if (reg <= R_G7)
7540Sstevel@tonic-gate 		return ((&rp->r_g1)[reg - 1]);
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate 	if (reg > R_I7) {
7570Sstevel@tonic-gate 		switch (reg) {
7580Sstevel@tonic-gate 		case R_CCR:
7590Sstevel@tonic-gate 			return ((rp->r_tstate >> TSTATE_CCR_SHIFT) &
7600Sstevel@tonic-gate 			    TSTATE_CCR_MASK);
7610Sstevel@tonic-gate 		case R_PC:
7620Sstevel@tonic-gate 			return (rp->r_pc);
7630Sstevel@tonic-gate 		case R_nPC:
7640Sstevel@tonic-gate 			return (rp->r_npc);
7650Sstevel@tonic-gate 		case R_Y:
7660Sstevel@tonic-gate 			return (rp->r_y);
7670Sstevel@tonic-gate 		case R_ASI:
7680Sstevel@tonic-gate 			return ((rp->r_tstate >> TSTATE_ASI_SHIFT) &
7690Sstevel@tonic-gate 			    TSTATE_ASI_MASK);
7700Sstevel@tonic-gate 		case R_FPRS:
7710Sstevel@tonic-gate 			return (dtrace_getfprs());
7720Sstevel@tonic-gate 		default:
7730Sstevel@tonic-gate 			DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
7740Sstevel@tonic-gate 			return (0);
7750Sstevel@tonic-gate 		}
7760Sstevel@tonic-gate 	}
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	/*
7790Sstevel@tonic-gate 	 * We reach go to the fake restore case if the probe we hit was a pid
7800Sstevel@tonic-gate 	 * return probe on a restore instruction. We partially emulate the
7810Sstevel@tonic-gate 	 * restore in the kernel and then execute a simple restore
7820Sstevel@tonic-gate 	 * instruction that we've secreted away to do the actual register
7830Sstevel@tonic-gate 	 * window manipulation. We need to go one register window further
7840Sstevel@tonic-gate 	 * down to get at the %ls, and %is and we need to treat %os like %is
7850Sstevel@tonic-gate 	 * to pull them out of the topmost user frame.
7860Sstevel@tonic-gate 	 */
7870Sstevel@tonic-gate 	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAKERESTORE)) {
7880Sstevel@tonic-gate 		if (reg > R_O7)
7890Sstevel@tonic-gate 			goto fake_restore;
7900Sstevel@tonic-gate 		else
7910Sstevel@tonic-gate 			reg += R_I0 - R_O0;
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	} else if (reg <= R_O7) {
7940Sstevel@tonic-gate 		return ((&rp->r_g1)[reg - 1]);
7950Sstevel@tonic-gate 	}
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 	if (dtrace_getotherwin() > 0)
7980Sstevel@tonic-gate 		return (dtrace_getreg_win(reg, 1));
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 	mpcb = (struct machpcb *)((caddr_t)rp - REGOFF);
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 	if (curproc->p_model == DATAMODEL_NATIVE) {
8030Sstevel@tonic-gate 		struct frame *fr = (void *)(rp->r_sp + STACK_BIAS);
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 		if (mpcb->mpcb_wbcnt > 0) {
8060Sstevel@tonic-gate 			struct rwindow *rwin = (void *)mpcb->mpcb_wbuf;
8070Sstevel@tonic-gate 			int i = mpcb->mpcb_wbcnt;
8080Sstevel@tonic-gate 			do {
8090Sstevel@tonic-gate 				i--;
8100Sstevel@tonic-gate 				if ((long)mpcb->mpcb_spbuf[i] == rp->r_sp)
8110Sstevel@tonic-gate 					return (rwin[i].rw_local[reg - 16]);
8120Sstevel@tonic-gate 			} while (i > 0);
8130Sstevel@tonic-gate 		}
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
8160Sstevel@tonic-gate 		value = dtrace_fulword(&fr->fr_local[reg - 16]);
8170Sstevel@tonic-gate 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
8180Sstevel@tonic-gate 	} else {
8191048Sraf 		struct frame32 *fr = (void *)(uintptr_t)(caddr32_t)rp->r_sp;
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 		if (mpcb->mpcb_wbcnt > 0) {
8220Sstevel@tonic-gate 			struct rwindow32 *rwin = (void *)mpcb->mpcb_wbuf;
8230Sstevel@tonic-gate 			int i = mpcb->mpcb_wbcnt;
8240Sstevel@tonic-gate 			do {
8250Sstevel@tonic-gate 				i--;
8260Sstevel@tonic-gate 				if ((long)mpcb->mpcb_spbuf[i] == rp->r_sp)
8270Sstevel@tonic-gate 					return (rwin[i].rw_local[reg - 16]);
8280Sstevel@tonic-gate 			} while (i > 0);
8290Sstevel@tonic-gate 		}
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
8320Sstevel@tonic-gate 		value = dtrace_fuword32(&fr->fr_local[reg - 16]);
8330Sstevel@tonic-gate 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
8340Sstevel@tonic-gate 	}
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	return (value);
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate fake_restore:
8390Sstevel@tonic-gate 	ASSERT(R_L0 <= reg && reg <= R_I7);
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 	/*
8420Sstevel@tonic-gate 	 * We first look two user windows down to see if we can dig out
8430Sstevel@tonic-gate 	 * the register we're looking for.
8440Sstevel@tonic-gate 	 */
8450Sstevel@tonic-gate 	if (dtrace_getotherwin() > 1)
8460Sstevel@tonic-gate 		return (dtrace_getreg_win(reg, 2));
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	/*
8490Sstevel@tonic-gate 	 * First we need to get the frame pointer and then we perform
8500Sstevel@tonic-gate 	 * the same computation as in the non-fake-o-restore case.
8510Sstevel@tonic-gate 	 */
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 	mpcb = (struct machpcb *)((caddr_t)rp - REGOFF);
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 	if (dtrace_getotherwin() > 0) {
8560Sstevel@tonic-gate 		fp = dtrace_getreg_win(R_FP, 1);
8570Sstevel@tonic-gate 		goto got_fp;
8580Sstevel@tonic-gate 	}
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 	if (curproc->p_model == DATAMODEL_NATIVE) {
8610Sstevel@tonic-gate 		struct frame *fr = (void *)(rp->r_sp + STACK_BIAS);
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 		if (mpcb->mpcb_wbcnt > 0) {
8640Sstevel@tonic-gate 			struct rwindow *rwin = (void *)mpcb->mpcb_wbuf;
8650Sstevel@tonic-gate 			int i = mpcb->mpcb_wbcnt;
8660Sstevel@tonic-gate 			do {
8670Sstevel@tonic-gate 				i--;
8680Sstevel@tonic-gate 				if ((long)mpcb->mpcb_spbuf[i] == rp->r_sp) {
8690Sstevel@tonic-gate 					fp = rwin[i].rw_fp;
8700Sstevel@tonic-gate 					goto got_fp;
8710Sstevel@tonic-gate 				}
8720Sstevel@tonic-gate 			} while (i > 0);
8730Sstevel@tonic-gate 		}
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
8760Sstevel@tonic-gate 		fp = dtrace_fulword(&fr->fr_savfp);
8770Sstevel@tonic-gate 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
8780Sstevel@tonic-gate 		if (cpu_core[CPU->cpu_id].cpuc_dtrace_flags & CPU_DTRACE_FAULT)
8790Sstevel@tonic-gate 			return (0);
8800Sstevel@tonic-gate 	} else {
8811048Sraf 		struct frame32 *fr = (void *)(uintptr_t)(caddr32_t)rp->r_sp;
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 		if (mpcb->mpcb_wbcnt > 0) {
8840Sstevel@tonic-gate 			struct rwindow32 *rwin = (void *)mpcb->mpcb_wbuf;
8850Sstevel@tonic-gate 			int i = mpcb->mpcb_wbcnt;
8860Sstevel@tonic-gate 			do {
8870Sstevel@tonic-gate 				i--;
8880Sstevel@tonic-gate 				if ((long)mpcb->mpcb_spbuf[i] == rp->r_sp) {
8890Sstevel@tonic-gate 					fp = rwin[i].rw_fp;
8900Sstevel@tonic-gate 					goto got_fp;
8910Sstevel@tonic-gate 				}
8920Sstevel@tonic-gate 			} while (i > 0);
8930Sstevel@tonic-gate 		}
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
8960Sstevel@tonic-gate 		fp = dtrace_fuword32(&fr->fr_savfp);
8970Sstevel@tonic-gate 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
8980Sstevel@tonic-gate 		if (cpu_core[CPU->cpu_id].cpuc_dtrace_flags & CPU_DTRACE_FAULT)
8990Sstevel@tonic-gate 			return (0);
9000Sstevel@tonic-gate 	}
9010Sstevel@tonic-gate got_fp:
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 	if (curproc->p_model == DATAMODEL_NATIVE) {
9040Sstevel@tonic-gate 		struct frame *fr = (void *)(fp + STACK_BIAS);
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 		if (mpcb->mpcb_wbcnt > 0) {
9070Sstevel@tonic-gate 			struct rwindow *rwin = (void *)mpcb->mpcb_wbuf;
9080Sstevel@tonic-gate 			int i = mpcb->mpcb_wbcnt;
9090Sstevel@tonic-gate 			do {
9100Sstevel@tonic-gate 				i--;
9110Sstevel@tonic-gate 				if ((long)mpcb->mpcb_spbuf[i] == fp)
9120Sstevel@tonic-gate 					return (rwin[i].rw_local[reg - 16]);
9130Sstevel@tonic-gate 			} while (i > 0);
9140Sstevel@tonic-gate 		}
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
9170Sstevel@tonic-gate 		value = dtrace_fulword(&fr->fr_local[reg - 16]);
9180Sstevel@tonic-gate 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
9190Sstevel@tonic-gate 	} else {
9201048Sraf 		struct frame32 *fr = (void *)(uintptr_t)(caddr32_t)fp;
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 		if (mpcb->mpcb_wbcnt > 0) {
9230Sstevel@tonic-gate 			struct rwindow32 *rwin = (void *)mpcb->mpcb_wbuf;
9240Sstevel@tonic-gate 			int i = mpcb->mpcb_wbcnt;
9250Sstevel@tonic-gate 			do {
9260Sstevel@tonic-gate 				i--;
9270Sstevel@tonic-gate 				if ((long)mpcb->mpcb_spbuf[i] == fp)
9280Sstevel@tonic-gate 					return (rwin[i].rw_local[reg - 16]);
9290Sstevel@tonic-gate 			} while (i > 0);
9300Sstevel@tonic-gate 		}
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
9330Sstevel@tonic-gate 		value = dtrace_fuword32(&fr->fr_local[reg - 16]);
9340Sstevel@tonic-gate 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
9350Sstevel@tonic-gate 	}
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 	return (value);
9380Sstevel@tonic-gate }
939