xref: /onnv-gate/usr/src/uts/sparc/dtrace/dtrace_isa.c (revision 3682:48f0fd311ddb)
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
5*3682Sjhaslam  * Common Development and Distribution License (the "License").
6*3682Sjhaslam  * 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*3682Sjhaslam  * 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/atomic.h>
310Sstevel@tonic-gate #include <sys/model.h>
320Sstevel@tonic-gate #include <sys/frame.h>
330Sstevel@tonic-gate #include <sys/stack.h>
340Sstevel@tonic-gate #include <sys/machpcb.h>
350Sstevel@tonic-gate #include <sys/procfs_isa.h>
360Sstevel@tonic-gate #include <sys/cmn_err.h>
37191Sahl #include <sys/sysmacros.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #define	DTRACE_FMT3OP3_MASK	0x81000000
400Sstevel@tonic-gate #define	DTRACE_FMT3OP3		0x80000000
410Sstevel@tonic-gate #define	DTRACE_FMT3RS1_SHIFT	14
420Sstevel@tonic-gate #define	DTRACE_FMT3RD_SHIFT	25
43457Sbmc #define	DTRACE_DISP22_SHIFT	10
440Sstevel@tonic-gate #define	DTRACE_RMASK		0x1f
450Sstevel@tonic-gate #define	DTRACE_REG_L0		16
460Sstevel@tonic-gate #define	DTRACE_REG_O7		15
470Sstevel@tonic-gate #define	DTRACE_REG_I0		24
480Sstevel@tonic-gate #define	DTRACE_REG_I6		30
490Sstevel@tonic-gate #define	DTRACE_RET		0x81c7e008
500Sstevel@tonic-gate #define	DTRACE_RETL		0x81c3e008
510Sstevel@tonic-gate #define	DTRACE_SAVE_MASK	0xc1f80000
520Sstevel@tonic-gate #define	DTRACE_SAVE		0x81e00000
530Sstevel@tonic-gate #define	DTRACE_RESTORE		0x81e80000
540Sstevel@tonic-gate #define	DTRACE_CALL_MASK	0xc0000000
550Sstevel@tonic-gate #define	DTRACE_CALL		0x40000000
560Sstevel@tonic-gate #define	DTRACE_JMPL_MASK	0x81f10000
570Sstevel@tonic-gate #define	DTRACE_JMPL		0x81c00000
58457Sbmc #define	DTRACE_BA_MASK		0xdfc00000
59457Sbmc #define	DTRACE_BA		0x10800000
60457Sbmc #define	DTRACE_BA_MAX		10
610Sstevel@tonic-gate 
620Sstevel@tonic-gate extern int dtrace_getupcstack_top(uint64_t *, int, uintptr_t *);
63191Sahl extern int dtrace_getustackdepth_top(uintptr_t *);
640Sstevel@tonic-gate extern ulong_t dtrace_getreg_win(uint_t, uint_t);
650Sstevel@tonic-gate extern void dtrace_putreg_win(uint_t, ulong_t);
660Sstevel@tonic-gate extern int dtrace_fish(int, int, uintptr_t *);
670Sstevel@tonic-gate 
68*3682Sjhaslam int	dtrace_ustackdepth_max = 2048;
69*3682Sjhaslam 
700Sstevel@tonic-gate /*
710Sstevel@tonic-gate  * This is similar in principle to getpcstack(), but there are several marked
720Sstevel@tonic-gate  * differences in implementation:
730Sstevel@tonic-gate  *
740Sstevel@tonic-gate  * (a)	dtrace_getpcstack() is called from probe context.  Thus, the call
750Sstevel@tonic-gate  *	to flush_windows() from getpcstack() is a call to the probe-safe
760Sstevel@tonic-gate  *	equivalent here.
770Sstevel@tonic-gate  *
780Sstevel@tonic-gate  * (b)  dtrace_getpcstack() is willing to sacrifice some performance to get
790Sstevel@tonic-gate  *	a correct stack.  While consumers of getpcstack() are largely
800Sstevel@tonic-gate  *	subsystem-specific in-kernel debugging facilities, DTrace consumers
810Sstevel@tonic-gate  *	are arbitrary user-level analysis tools; dtrace_getpcstack() must
820Sstevel@tonic-gate  *	deliver as correct a stack as possible.  Details on the issues
830Sstevel@tonic-gate  *	surrounding stack correctness are found below.
840Sstevel@tonic-gate  *
85191Sahl  * (c)	dtrace_getpcstack() _always_ fills in pcstack_limit pc_t's -- filling
86191Sahl  *	in the difference between the stack depth and pcstack_limit with NULLs.
870Sstevel@tonic-gate  *	Due to this behavior dtrace_getpcstack() returns void.
880Sstevel@tonic-gate  *
890Sstevel@tonic-gate  * (d)	dtrace_getpcstack() takes a third parameter, aframes, that
900Sstevel@tonic-gate  *	denotes the number of _artificial frames_ on the bottom of the
910Sstevel@tonic-gate  *	stack.  An artificial frame is one induced by the provider; all
920Sstevel@tonic-gate  *	artificial frames are stripped off before frames are stored to
930Sstevel@tonic-gate  *	pcstack.
940Sstevel@tonic-gate  *
950Sstevel@tonic-gate  * (e)	dtrace_getpcstack() takes a fourth parameter, pc, that indicates
960Sstevel@tonic-gate  *	an interrupted program counter (if any).  This should be a non-NULL
970Sstevel@tonic-gate  *	value if and only if the hit probe is unanchored.  (Anchored probes
980Sstevel@tonic-gate  *	don't fire through an interrupt source.)  This parameter is used to
990Sstevel@tonic-gate  *	assure (b), above.
1000Sstevel@tonic-gate  */
1010Sstevel@tonic-gate void
1020Sstevel@tonic-gate dtrace_getpcstack(pc_t *pcstack, int pcstack_limit, int aframes, uint32_t *pc)
1030Sstevel@tonic-gate {
1040Sstevel@tonic-gate 	struct frame *fp, *nextfp, *minfp, *stacktop;
1050Sstevel@tonic-gate 	int depth = 0;
1060Sstevel@tonic-gate 	int on_intr, j = 0;
1070Sstevel@tonic-gate 	uint32_t i, r;
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	fp = (struct frame *)((caddr_t)dtrace_getfp() + STACK_BIAS);
1100Sstevel@tonic-gate 	dtrace_flush_windows();
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	if (pc != NULL) {
1130Sstevel@tonic-gate 		/*
1140Sstevel@tonic-gate 		 * If we've been passed a non-NULL pc, we need to determine
1150Sstevel@tonic-gate 		 * whether or not the specified program counter falls in a leaf
1160Sstevel@tonic-gate 		 * function.  If it falls within a leaf function, we know that
1170Sstevel@tonic-gate 		 * %o7 is valid in its frame (and we can just drive on).  If
1180Sstevel@tonic-gate 		 * it's a non-leaf, however, we know that %o7 is garbage in the
1190Sstevel@tonic-gate 		 * bottom frame.  To trim this frame, we simply increment
1200Sstevel@tonic-gate 		 * aframes and drop into the stack-walking loop.
1210Sstevel@tonic-gate 		 *
1220Sstevel@tonic-gate 		 * To quickly determine if the specified program counter is in
1230Sstevel@tonic-gate 		 * a leaf function, we exploit the fact that leaf functions
1240Sstevel@tonic-gate 		 * tend to be short and non-leaf functions tend to frequently
1250Sstevel@tonic-gate 		 * perform operations that are only permitted in a non-leaf
1260Sstevel@tonic-gate 		 * function (e.g., using the %i's or %l's; calling a function;
1270Sstevel@tonic-gate 		 * performing a restore).  We exploit these tendencies by
1280Sstevel@tonic-gate 		 * simply scanning forward from the specified %pc -- if we see
1290Sstevel@tonic-gate 		 * an operation only permitted in a non-leaf, we know we're in
1300Sstevel@tonic-gate 		 * a non-leaf; if we see a retl, we know we're in a leaf.
1310Sstevel@tonic-gate 		 * Fortunately, one need not perform anywhere near full
1320Sstevel@tonic-gate 		 * disassembly to effectively determine the former: determining
1330Sstevel@tonic-gate 		 * that an instruction is a format-3 instruction and decoding
1340Sstevel@tonic-gate 		 * its rd and rs1 fields, for example, requires very little
1350Sstevel@tonic-gate 		 * manipulation.  Overall, this method of leaf determination
1360Sstevel@tonic-gate 		 * performs quite well:  on average, we only examine between
1370Sstevel@tonic-gate 		 * 1.5 and 2.5 instructions before making the determination.
1380Sstevel@tonic-gate 		 * (Outliers do exist, however; of note is the non-leaf
1390Sstevel@tonic-gate 		 * function ip_sioctl_not_ours() which -- as of this writing --
1400Sstevel@tonic-gate 		 * has a whopping 455 straight instructions that manipulate
1410Sstevel@tonic-gate 		 * only %g's and %o's.)
1420Sstevel@tonic-gate 		 */
143457Sbmc 		int delay = 0, branches = 0, taken = 0;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 		if (depth < pcstack_limit)
1461048Sraf 			pcstack[depth++] = (pc_t)(uintptr_t)pc;
1470Sstevel@tonic-gate 
148457Sbmc 		/*
149457Sbmc 		 * Our heuristic is exactly that -- a heuristic -- and there
150457Sbmc 		 * exists a possibility that we could be either be vectored
151457Sbmc 		 * off into the weeds (by following a bogus branch) or could
152457Sbmc 		 * wander off the end of the function and off the end of a
153457Sbmc 		 * text mapping (by not following a conditional branch at the
154457Sbmc 		 * end of the function that is effectively always taken).  So
155457Sbmc 		 * as a precautionary measure, we set the NOFAULT flag.
156457Sbmc 		 */
157457Sbmc 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
158457Sbmc 
1590Sstevel@tonic-gate 		for (;;) {
1600Sstevel@tonic-gate 			i = pc[j++];
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 			if ((i & DTRACE_FMT3OP3_MASK) == DTRACE_FMT3OP3) {
1630Sstevel@tonic-gate 				/*
1640Sstevel@tonic-gate 				 * This is a format-3 instruction.  We can
1650Sstevel@tonic-gate 				 * look at rd and rs1.
1660Sstevel@tonic-gate 				 */
1670Sstevel@tonic-gate 				r = (i >> DTRACE_FMT3RS1_SHIFT) & DTRACE_RMASK;
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 				if (r >= DTRACE_REG_L0)
1700Sstevel@tonic-gate 					goto nonleaf;
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 				r = (i >> DTRACE_FMT3RD_SHIFT) & DTRACE_RMASK;
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 				if (r >= DTRACE_REG_L0)
1750Sstevel@tonic-gate 					goto nonleaf;
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 				if ((i & DTRACE_JMPL_MASK) == DTRACE_JMPL) {
1780Sstevel@tonic-gate 					delay = 1;
1790Sstevel@tonic-gate 					continue;
1800Sstevel@tonic-gate 				}
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 				/*
1830Sstevel@tonic-gate 				 * If we see explicit manipulation with %o7
1840Sstevel@tonic-gate 				 * as a destination register, we know that
1850Sstevel@tonic-gate 				 * %o7 is likely bogus -- and we treat this
1860Sstevel@tonic-gate 				 * function as a non-leaf.
1870Sstevel@tonic-gate 				 */
1880Sstevel@tonic-gate 				if (r == DTRACE_REG_O7) {
1890Sstevel@tonic-gate 					if (delay)
1900Sstevel@tonic-gate 						goto leaf;
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 					i &= DTRACE_JMPL_MASK;
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 					if (i == DTRACE_JMPL) {
1950Sstevel@tonic-gate 						delay = 1;
1960Sstevel@tonic-gate 						continue;
1970Sstevel@tonic-gate 					}
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 					goto nonleaf;
2000Sstevel@tonic-gate 				}
2010Sstevel@tonic-gate 			} else {
2020Sstevel@tonic-gate 				/*
2030Sstevel@tonic-gate 				 * If this is a call, it may or may not be
2040Sstevel@tonic-gate 				 * a leaf; we need to check the delay slot.
2050Sstevel@tonic-gate 				 */
2060Sstevel@tonic-gate 				if ((i & DTRACE_CALL_MASK) == DTRACE_CALL) {
2070Sstevel@tonic-gate 					delay = 1;
2080Sstevel@tonic-gate 					continue;
2090Sstevel@tonic-gate 				}
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 				/*
2120Sstevel@tonic-gate 				 * If we see a ret it's not a leaf; if we
2130Sstevel@tonic-gate 				 * see a retl, it is a leaf.
2140Sstevel@tonic-gate 				 */
2150Sstevel@tonic-gate 				if (i == DTRACE_RET)
2160Sstevel@tonic-gate 					goto nonleaf;
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 				if (i == DTRACE_RETL)
2190Sstevel@tonic-gate 					goto leaf;
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 				/*
222457Sbmc 				 * If this is a ba (annulled or not), then we
223457Sbmc 				 * need to actually follow the branch.  No, we
224457Sbmc 				 * don't look at the delay slot -- hopefully
225457Sbmc 				 * anything that can be gleaned from the delay
226457Sbmc 				 * slot can also be gleaned from the branch
227457Sbmc 				 * target.  To prevent ourselves from iterating
228457Sbmc 				 * infinitely, we clamp the number of branches
229457Sbmc 				 * that we'll follow, and we refuse to follow
230457Sbmc 				 * the same branch twice consecutively.  In
231457Sbmc 				 * both cases, we abort by deciding that we're
232457Sbmc 				 * looking at a leaf.  While in theory this
233457Sbmc 				 * could be wrong (we could be in the middle of
234457Sbmc 				 * a loop in a non-leaf that ends with a ba and
235457Sbmc 				 * only manipulates outputs and globals in the
236457Sbmc 				 * body of the loop -- therefore leading us to
237457Sbmc 				 * the wrong conclusion), this doesn't seem to
238457Sbmc 				 * crop up in practice.  (Or rather, this
239457Sbmc 				 * condition could not be deliberately induced,
240457Sbmc 				 * despite concerted effort.)
241457Sbmc 				 */
242457Sbmc 				if ((i & DTRACE_BA_MASK) == DTRACE_BA) {
243457Sbmc 					if (++branches == DTRACE_BA_MAX ||
244457Sbmc 					    taken == j)
245457Sbmc 						goto nonleaf;
246457Sbmc 
247457Sbmc 					taken = j;
248457Sbmc 					j += ((int)(i << DTRACE_DISP22_SHIFT) >>
249457Sbmc 					    DTRACE_DISP22_SHIFT) - 1;
250457Sbmc 					continue;
251457Sbmc 				}
252457Sbmc 
253457Sbmc 				/*
2540Sstevel@tonic-gate 				 * Finally, if it's a save, it should be
2550Sstevel@tonic-gate 				 * treated as a leaf; if it's a restore it
2560Sstevel@tonic-gate 				 * should not be treated as a leaf.
2570Sstevel@tonic-gate 				 */
2580Sstevel@tonic-gate 				if ((i & DTRACE_SAVE_MASK) == DTRACE_SAVE)
2590Sstevel@tonic-gate 					goto leaf;
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 				if ((i & DTRACE_SAVE_MASK) == DTRACE_RESTORE)
2620Sstevel@tonic-gate 					goto nonleaf;
2630Sstevel@tonic-gate 			}
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 			if (delay) {
2660Sstevel@tonic-gate 				/*
2670Sstevel@tonic-gate 				 * If this was a delay slot instruction and
2680Sstevel@tonic-gate 				 * we didn't pick it up elsewhere, this is a
2690Sstevel@tonic-gate 				 * non-leaf.
2700Sstevel@tonic-gate 				 */
2710Sstevel@tonic-gate 				goto nonleaf;
2720Sstevel@tonic-gate 			}
2730Sstevel@tonic-gate 		}
2740Sstevel@tonic-gate nonleaf:
2750Sstevel@tonic-gate 		aframes++;
2760Sstevel@tonic-gate leaf:
277457Sbmc 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
2780Sstevel@tonic-gate 	}
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	if ((on_intr = CPU_ON_INTR(CPU)) != 0)
2810Sstevel@tonic-gate 		stacktop = (struct frame *)(CPU->cpu_intr_stack + SA(MINFRAME));
2820Sstevel@tonic-gate 	else
2830Sstevel@tonic-gate 		stacktop = (struct frame *)curthread->t_stk;
2840Sstevel@tonic-gate 	minfp = fp;
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	while (depth < pcstack_limit) {
2870Sstevel@tonic-gate 		nextfp = (struct frame *)((caddr_t)fp->fr_savfp + STACK_BIAS);
2880Sstevel@tonic-gate 		if (nextfp <= minfp || nextfp >= stacktop) {
2890Sstevel@tonic-gate 			if (!on_intr && nextfp == stacktop && aframes != 0) {
2900Sstevel@tonic-gate 				/*
2910Sstevel@tonic-gate 				 * If we are exactly at the top of the stack
2920Sstevel@tonic-gate 				 * with a non-zero number of artificial frames,
2930Sstevel@tonic-gate 				 * it must be that the stack is filled with
2940Sstevel@tonic-gate 				 * nothing _but_ artificial frames.  In this
2950Sstevel@tonic-gate 				 * case, we assert that this is so, zero
2960Sstevel@tonic-gate 				 * pcstack, and return.
2970Sstevel@tonic-gate 				 */
2980Sstevel@tonic-gate 				ASSERT(aframes == 1);
2990Sstevel@tonic-gate 				ASSERT(depth == 0);
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 				while (depth < pcstack_limit)
3020Sstevel@tonic-gate 					pcstack[depth++] = NULL;
3030Sstevel@tonic-gate 				return;
3040Sstevel@tonic-gate 			}
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 			if (on_intr) {
3070Sstevel@tonic-gate 				/*
3080Sstevel@tonic-gate 				 * Hop from interrupt stack to thread stack.
3090Sstevel@tonic-gate 				 */
3100Sstevel@tonic-gate 				stacktop = (struct frame *)curthread->t_stk;
3110Sstevel@tonic-gate 				minfp = (struct frame *)curthread->t_stkbase;
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 				on_intr = 0;
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 				if (nextfp > minfp && nextfp < stacktop)
3160Sstevel@tonic-gate 					continue;
3170Sstevel@tonic-gate 			} else {
3180Sstevel@tonic-gate 				/*
3190Sstevel@tonic-gate 				 * High-level interrupts may occur when %sp is
3200Sstevel@tonic-gate 				 * not necessarily contained in the stack
3210Sstevel@tonic-gate 				 * bounds implied by %g7 -- interrupt thread
3220Sstevel@tonic-gate 				 * management runs with %pil at DISP_LEVEL,
3230Sstevel@tonic-gate 				 * and high-level interrupts may thus occur
3240Sstevel@tonic-gate 				 * in windows when %sp and %g7 are not self-
3250Sstevel@tonic-gate 				 * consistent.  If we call dtrace_getpcstack()
3260Sstevel@tonic-gate 				 * from a high-level interrupt that has occurred
3270Sstevel@tonic-gate 				 * in such a window, we will fail the above test
3280Sstevel@tonic-gate 				 * of nextfp against minfp/stacktop.  If the
3290Sstevel@tonic-gate 				 * high-level interrupt has in turn interrupted
3300Sstevel@tonic-gate 				 * a non-passivated interrupt thread, we
3310Sstevel@tonic-gate 				 * will execute the below code with non-zero
3320Sstevel@tonic-gate 				 * aframes.  We therefore want to assert that
3330Sstevel@tonic-gate 				 * aframes is zero _or_ we are in a high-level
3340Sstevel@tonic-gate 				 * interrupt -- but because cpu_intr_actv is
3350Sstevel@tonic-gate 				 * updated with high-level interrupts enabled,
3360Sstevel@tonic-gate 				 * we must reduce this to only asserting that
3370Sstevel@tonic-gate 				 * %pil is greater than DISP_LEVEL.
3380Sstevel@tonic-gate 				 */
3390Sstevel@tonic-gate 				ASSERT(aframes == 0 ||
3400Sstevel@tonic-gate 				    dtrace_getipl() > DISP_LEVEL);
3410Sstevel@tonic-gate 				pcstack[depth++] = (pc_t)fp->fr_savpc;
3420Sstevel@tonic-gate 			}
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 			while (depth < pcstack_limit)
3450Sstevel@tonic-gate 				pcstack[depth++] = NULL;
3460Sstevel@tonic-gate 			return;
3470Sstevel@tonic-gate 		}
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 		if (aframes > 0) {
3500Sstevel@tonic-gate 			aframes--;
3510Sstevel@tonic-gate 		} else {
3520Sstevel@tonic-gate 			pcstack[depth++] = (pc_t)fp->fr_savpc;
3530Sstevel@tonic-gate 		}
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 		fp = nextfp;
3560Sstevel@tonic-gate 		minfp = fp;
3570Sstevel@tonic-gate 	}
3580Sstevel@tonic-gate }
3590Sstevel@tonic-gate 
360191Sahl static int
361191Sahl dtrace_getustack_common(uint64_t *pcstack, int pcstack_limit, uintptr_t sp)
362191Sahl {
363191Sahl 	proc_t *p = curproc;
364191Sahl 	int ret = 0;
365*3682Sjhaslam 	uintptr_t oldsp;
366*3682Sjhaslam 	volatile uint16_t *flags =
367*3682Sjhaslam 	    (volatile uint16_t *)&cpu_core[CPU->cpu_id].cpuc_dtrace_flags;
368191Sahl 
369191Sahl 	ASSERT(pcstack == NULL || pcstack_limit > 0);
370*3682Sjhaslam 	ASSERT(dtrace_ustackdepth_max > 0);
371191Sahl 
372191Sahl 	if (p->p_model == DATAMODEL_NATIVE) {
373191Sahl 		for (;;) {
374191Sahl 			struct frame *fr = (struct frame *)(sp + STACK_BIAS);
375191Sahl 			uintptr_t pc;
376191Sahl 
377191Sahl 			if (sp == 0 || fr == NULL ||
378191Sahl 			    !IS_P2ALIGNED((uintptr_t)fr, STACK_ALIGN))
379191Sahl 				break;
380191Sahl 
381*3682Sjhaslam 			oldsp = sp;
382*3682Sjhaslam 
383191Sahl 			pc = dtrace_fulword(&fr->fr_savpc);
384191Sahl 			sp = dtrace_fulword(&fr->fr_savfp);
385191Sahl 
386191Sahl 			if (pc == 0)
387191Sahl 				break;
388191Sahl 
389*3682Sjhaslam 			/*
390*3682Sjhaslam 			 * We limit the number of times we can go around this
391*3682Sjhaslam 			 * loop to account for a circular stack.
392*3682Sjhaslam 			 */
393*3682Sjhaslam 			if (sp == oldsp || ret++ >= dtrace_ustackdepth_max) {
394*3682Sjhaslam 				*flags |= CPU_DTRACE_BADSTACK;
395*3682Sjhaslam 				cpu_core[CPU->cpu_id].cpuc_dtrace_illval = sp;
396*3682Sjhaslam 				break;
397*3682Sjhaslam 			}
398191Sahl 
399191Sahl 			if (pcstack != NULL) {
400191Sahl 				*pcstack++ = pc;
401191Sahl 				pcstack_limit--;
402191Sahl 				if (pcstack_limit == 0)
403191Sahl 					break;
404191Sahl 			}
405191Sahl 		}
406191Sahl 	} else {
4071399Sahl 		/*
4081399Sahl 		 * Truncate the stack pointer to 32-bits as there may be
4091399Sahl 		 * garbage in the upper bits which would normally be ignored
4101399Sahl 		 * by the processor in 32-bit mode.
4111399Sahl 		 */
4121399Sahl 		sp = (uint32_t)sp;
4131399Sahl 
414191Sahl 		for (;;) {
415191Sahl 			struct frame32 *fr = (struct frame32 *)sp;
416191Sahl 			uint32_t pc;
417191Sahl 
418191Sahl 			if (sp == 0 ||
419191Sahl 			    !IS_P2ALIGNED((uintptr_t)fr, STACK_ALIGN32))
420191Sahl 				break;
421191Sahl 
422*3682Sjhaslam 			oldsp = sp;
423*3682Sjhaslam 
424191Sahl 			pc = dtrace_fuword32(&fr->fr_savpc);
425191Sahl 			sp = dtrace_fuword32(&fr->fr_savfp);
426191Sahl 
427191Sahl 			if (pc == 0)
428191Sahl 				break;
429191Sahl 
430*3682Sjhaslam 			if (sp == oldsp || ret++ >= dtrace_ustackdepth_max) {
431*3682Sjhaslam 				*flags |= CPU_DTRACE_BADSTACK;
432*3682Sjhaslam 				cpu_core[CPU->cpu_id].cpuc_dtrace_illval = sp;
433*3682Sjhaslam 				break;
434*3682Sjhaslam 			}
435191Sahl 
436191Sahl 			if (pcstack != NULL) {
437191Sahl 				*pcstack++ = pc;
438191Sahl 				pcstack_limit--;
439191Sahl 				if (pcstack_limit == 0)
440191Sahl 					break;
441191Sahl 			}
442191Sahl 		}
443191Sahl 	}
444191Sahl 
445191Sahl 	return (ret);
446191Sahl }
447191Sahl 
4480Sstevel@tonic-gate void
4490Sstevel@tonic-gate dtrace_getupcstack(uint64_t *pcstack, int pcstack_limit)
4500Sstevel@tonic-gate {
4510Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
452191Sahl 	proc_t *p = curproc;
4530Sstevel@tonic-gate 	struct regs *rp;
4540Sstevel@tonic-gate 	uintptr_t sp;
4550Sstevel@tonic-gate 	int n;
4560Sstevel@tonic-gate 
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
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
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
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
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
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