xref: /onnv-gate/usr/src/uts/sparc/dtrace/dtrace_isa.c (revision 1048:e29b523f29b4)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22*1048Sraf 
230Sstevel@tonic-gate /*
240Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <sys/dtrace_impl.h>
310Sstevel@tonic-gate #include <sys/atomic.h>
320Sstevel@tonic-gate #include <sys/model.h>
330Sstevel@tonic-gate #include <sys/frame.h>
340Sstevel@tonic-gate #include <sys/stack.h>
350Sstevel@tonic-gate #include <sys/machpcb.h>
360Sstevel@tonic-gate #include <sys/procfs_isa.h>
370Sstevel@tonic-gate #include <sys/cmn_err.h>
38191Sahl #include <sys/sysmacros.h>
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #define	DTRACE_FMT3OP3_MASK	0x81000000
410Sstevel@tonic-gate #define	DTRACE_FMT3OP3		0x80000000
420Sstevel@tonic-gate #define	DTRACE_FMT3RS1_SHIFT	14
430Sstevel@tonic-gate #define	DTRACE_FMT3RD_SHIFT	25
44457Sbmc #define	DTRACE_DISP22_SHIFT	10
450Sstevel@tonic-gate #define	DTRACE_RMASK		0x1f
460Sstevel@tonic-gate #define	DTRACE_REG_L0		16
470Sstevel@tonic-gate #define	DTRACE_REG_O7		15
480Sstevel@tonic-gate #define	DTRACE_REG_I0		24
490Sstevel@tonic-gate #define	DTRACE_REG_I6		30
500Sstevel@tonic-gate #define	DTRACE_RET		0x81c7e008
510Sstevel@tonic-gate #define	DTRACE_RETL		0x81c3e008
520Sstevel@tonic-gate #define	DTRACE_SAVE_MASK	0xc1f80000
530Sstevel@tonic-gate #define	DTRACE_SAVE		0x81e00000
540Sstevel@tonic-gate #define	DTRACE_RESTORE		0x81e80000
550Sstevel@tonic-gate #define	DTRACE_CALL_MASK	0xc0000000
560Sstevel@tonic-gate #define	DTRACE_CALL		0x40000000
570Sstevel@tonic-gate #define	DTRACE_JMPL_MASK	0x81f10000
580Sstevel@tonic-gate #define	DTRACE_JMPL		0x81c00000
59457Sbmc #define	DTRACE_BA_MASK		0xdfc00000
60457Sbmc #define	DTRACE_BA		0x10800000
61457Sbmc #define	DTRACE_BA_MAX		10
620Sstevel@tonic-gate 
630Sstevel@tonic-gate extern int dtrace_getupcstack_top(uint64_t *, int, uintptr_t *);
64191Sahl extern int dtrace_getustackdepth_top(uintptr_t *);
650Sstevel@tonic-gate extern ulong_t dtrace_getreg_win(uint_t, uint_t);
660Sstevel@tonic-gate extern void dtrace_putreg_win(uint_t, ulong_t);
670Sstevel@tonic-gate extern int dtrace_fish(int, int, uintptr_t *);
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /*
700Sstevel@tonic-gate  * This is similar in principle to getpcstack(), but there are several marked
710Sstevel@tonic-gate  * differences in implementation:
720Sstevel@tonic-gate  *
730Sstevel@tonic-gate  * (a)	dtrace_getpcstack() is called from probe context.  Thus, the call
740Sstevel@tonic-gate  *	to flush_windows() from getpcstack() is a call to the probe-safe
750Sstevel@tonic-gate  *	equivalent here.
760Sstevel@tonic-gate  *
770Sstevel@tonic-gate  * (b)  dtrace_getpcstack() is willing to sacrifice some performance to get
780Sstevel@tonic-gate  *	a correct stack.  While consumers of getpcstack() are largely
790Sstevel@tonic-gate  *	subsystem-specific in-kernel debugging facilities, DTrace consumers
800Sstevel@tonic-gate  *	are arbitrary user-level analysis tools; dtrace_getpcstack() must
810Sstevel@tonic-gate  *	deliver as correct a stack as possible.  Details on the issues
820Sstevel@tonic-gate  *	surrounding stack correctness are found below.
830Sstevel@tonic-gate  *
84191Sahl  * (c)	dtrace_getpcstack() _always_ fills in pcstack_limit pc_t's -- filling
85191Sahl  *	in the difference between the stack depth and pcstack_limit with NULLs.
860Sstevel@tonic-gate  *	Due to this behavior dtrace_getpcstack() returns void.
870Sstevel@tonic-gate  *
880Sstevel@tonic-gate  * (d)	dtrace_getpcstack() takes a third parameter, aframes, that
890Sstevel@tonic-gate  *	denotes the number of _artificial frames_ on the bottom of the
900Sstevel@tonic-gate  *	stack.  An artificial frame is one induced by the provider; all
910Sstevel@tonic-gate  *	artificial frames are stripped off before frames are stored to
920Sstevel@tonic-gate  *	pcstack.
930Sstevel@tonic-gate  *
940Sstevel@tonic-gate  * (e)	dtrace_getpcstack() takes a fourth parameter, pc, that indicates
950Sstevel@tonic-gate  *	an interrupted program counter (if any).  This should be a non-NULL
960Sstevel@tonic-gate  *	value if and only if the hit probe is unanchored.  (Anchored probes
970Sstevel@tonic-gate  *	don't fire through an interrupt source.)  This parameter is used to
980Sstevel@tonic-gate  *	assure (b), above.
990Sstevel@tonic-gate  */
1000Sstevel@tonic-gate void
1010Sstevel@tonic-gate dtrace_getpcstack(pc_t *pcstack, int pcstack_limit, int aframes, uint32_t *pc)
1020Sstevel@tonic-gate {
1030Sstevel@tonic-gate 	struct frame *fp, *nextfp, *minfp, *stacktop;
1040Sstevel@tonic-gate 	int depth = 0;
1050Sstevel@tonic-gate 	int on_intr, j = 0;
1060Sstevel@tonic-gate 	uint32_t i, r;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	fp = (struct frame *)((caddr_t)dtrace_getfp() + STACK_BIAS);
1090Sstevel@tonic-gate 	dtrace_flush_windows();
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	if (pc != NULL) {
1120Sstevel@tonic-gate 		/*
1130Sstevel@tonic-gate 		 * If we've been passed a non-NULL pc, we need to determine
1140Sstevel@tonic-gate 		 * whether or not the specified program counter falls in a leaf
1150Sstevel@tonic-gate 		 * function.  If it falls within a leaf function, we know that
1160Sstevel@tonic-gate 		 * %o7 is valid in its frame (and we can just drive on).  If
1170Sstevel@tonic-gate 		 * it's a non-leaf, however, we know that %o7 is garbage in the
1180Sstevel@tonic-gate 		 * bottom frame.  To trim this frame, we simply increment
1190Sstevel@tonic-gate 		 * aframes and drop into the stack-walking loop.
1200Sstevel@tonic-gate 		 *
1210Sstevel@tonic-gate 		 * To quickly determine if the specified program counter is in
1220Sstevel@tonic-gate 		 * a leaf function, we exploit the fact that leaf functions
1230Sstevel@tonic-gate 		 * tend to be short and non-leaf functions tend to frequently
1240Sstevel@tonic-gate 		 * perform operations that are only permitted in a non-leaf
1250Sstevel@tonic-gate 		 * function (e.g., using the %i's or %l's; calling a function;
1260Sstevel@tonic-gate 		 * performing a restore).  We exploit these tendencies by
1270Sstevel@tonic-gate 		 * simply scanning forward from the specified %pc -- if we see
1280Sstevel@tonic-gate 		 * an operation only permitted in a non-leaf, we know we're in
1290Sstevel@tonic-gate 		 * a non-leaf; if we see a retl, we know we're in a leaf.
1300Sstevel@tonic-gate 		 * Fortunately, one need not perform anywhere near full
1310Sstevel@tonic-gate 		 * disassembly to effectively determine the former: determining
1320Sstevel@tonic-gate 		 * that an instruction is a format-3 instruction and decoding
1330Sstevel@tonic-gate 		 * its rd and rs1 fields, for example, requires very little
1340Sstevel@tonic-gate 		 * manipulation.  Overall, this method of leaf determination
1350Sstevel@tonic-gate 		 * performs quite well:  on average, we only examine between
1360Sstevel@tonic-gate 		 * 1.5 and 2.5 instructions before making the determination.
1370Sstevel@tonic-gate 		 * (Outliers do exist, however; of note is the non-leaf
1380Sstevel@tonic-gate 		 * function ip_sioctl_not_ours() which -- as of this writing --
1390Sstevel@tonic-gate 		 * has a whopping 455 straight instructions that manipulate
1400Sstevel@tonic-gate 		 * only %g's and %o's.)
1410Sstevel@tonic-gate 		 */
142457Sbmc 		int delay = 0, branches = 0, taken = 0;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 		if (depth < pcstack_limit)
145*1048Sraf 			pcstack[depth++] = (pc_t)(uintptr_t)pc;
1460Sstevel@tonic-gate 
147457Sbmc 		/*
148457Sbmc 		 * Our heuristic is exactly that -- a heuristic -- and there
149457Sbmc 		 * exists a possibility that we could be either be vectored
150457Sbmc 		 * off into the weeds (by following a bogus branch) or could
151457Sbmc 		 * wander off the end of the function and off the end of a
152457Sbmc 		 * text mapping (by not following a conditional branch at the
153457Sbmc 		 * end of the function that is effectively always taken).  So
154457Sbmc 		 * as a precautionary measure, we set the NOFAULT flag.
155457Sbmc 		 */
156457Sbmc 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
157457Sbmc 
1580Sstevel@tonic-gate 		for (;;) {
1590Sstevel@tonic-gate 			i = pc[j++];
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 			if ((i & DTRACE_FMT3OP3_MASK) == DTRACE_FMT3OP3) {
1620Sstevel@tonic-gate 				/*
1630Sstevel@tonic-gate 				 * This is a format-3 instruction.  We can
1640Sstevel@tonic-gate 				 * look at rd and rs1.
1650Sstevel@tonic-gate 				 */
1660Sstevel@tonic-gate 				r = (i >> DTRACE_FMT3RS1_SHIFT) & DTRACE_RMASK;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 				if (r >= DTRACE_REG_L0)
1690Sstevel@tonic-gate 					goto nonleaf;
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 				r = (i >> DTRACE_FMT3RD_SHIFT) & DTRACE_RMASK;
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 				if (r >= DTRACE_REG_L0)
1740Sstevel@tonic-gate 					goto nonleaf;
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 				if ((i & DTRACE_JMPL_MASK) == DTRACE_JMPL) {
1770Sstevel@tonic-gate 					delay = 1;
1780Sstevel@tonic-gate 					continue;
1790Sstevel@tonic-gate 				}
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 				/*
1820Sstevel@tonic-gate 				 * If we see explicit manipulation with %o7
1830Sstevel@tonic-gate 				 * as a destination register, we know that
1840Sstevel@tonic-gate 				 * %o7 is likely bogus -- and we treat this
1850Sstevel@tonic-gate 				 * function as a non-leaf.
1860Sstevel@tonic-gate 				 */
1870Sstevel@tonic-gate 				if (r == DTRACE_REG_O7) {
1880Sstevel@tonic-gate 					if (delay)
1890Sstevel@tonic-gate 						goto leaf;
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 					i &= DTRACE_JMPL_MASK;
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 					if (i == DTRACE_JMPL) {
1940Sstevel@tonic-gate 						delay = 1;
1950Sstevel@tonic-gate 						continue;
1960Sstevel@tonic-gate 					}
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 					goto nonleaf;
1990Sstevel@tonic-gate 				}
2000Sstevel@tonic-gate 			} else {
2010Sstevel@tonic-gate 				/*
2020Sstevel@tonic-gate 				 * If this is a call, it may or may not be
2030Sstevel@tonic-gate 				 * a leaf; we need to check the delay slot.
2040Sstevel@tonic-gate 				 */
2050Sstevel@tonic-gate 				if ((i & DTRACE_CALL_MASK) == DTRACE_CALL) {
2060Sstevel@tonic-gate 					delay = 1;
2070Sstevel@tonic-gate 					continue;
2080Sstevel@tonic-gate 				}
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 				/*
2110Sstevel@tonic-gate 				 * If we see a ret it's not a leaf; if we
2120Sstevel@tonic-gate 				 * see a retl, it is a leaf.
2130Sstevel@tonic-gate 				 */
2140Sstevel@tonic-gate 				if (i == DTRACE_RET)
2150Sstevel@tonic-gate 					goto nonleaf;
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 				if (i == DTRACE_RETL)
2180Sstevel@tonic-gate 					goto leaf;
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 				/*
221457Sbmc 				 * If this is a ba (annulled or not), then we
222457Sbmc 				 * need to actually follow the branch.  No, we
223457Sbmc 				 * don't look at the delay slot -- hopefully
224457Sbmc 				 * anything that can be gleaned from the delay
225457Sbmc 				 * slot can also be gleaned from the branch
226457Sbmc 				 * target.  To prevent ourselves from iterating
227457Sbmc 				 * infinitely, we clamp the number of branches
228457Sbmc 				 * that we'll follow, and we refuse to follow
229457Sbmc 				 * the same branch twice consecutively.  In
230457Sbmc 				 * both cases, we abort by deciding that we're
231457Sbmc 				 * looking at a leaf.  While in theory this
232457Sbmc 				 * could be wrong (we could be in the middle of
233457Sbmc 				 * a loop in a non-leaf that ends with a ba and
234457Sbmc 				 * only manipulates outputs and globals in the
235457Sbmc 				 * body of the loop -- therefore leading us to
236457Sbmc 				 * the wrong conclusion), this doesn't seem to
237457Sbmc 				 * crop up in practice.  (Or rather, this
238457Sbmc 				 * condition could not be deliberately induced,
239457Sbmc 				 * despite concerted effort.)
240457Sbmc 				 */
241457Sbmc 				if ((i & DTRACE_BA_MASK) == DTRACE_BA) {
242457Sbmc 					if (++branches == DTRACE_BA_MAX ||
243457Sbmc 					    taken == j)
244457Sbmc 						goto nonleaf;
245457Sbmc 
246457Sbmc 					taken = j;
247457Sbmc 					j += ((int)(i << DTRACE_DISP22_SHIFT) >>
248457Sbmc 					    DTRACE_DISP22_SHIFT) - 1;
249457Sbmc 					continue;
250457Sbmc 				}
251457Sbmc 
252457Sbmc 				/*
2530Sstevel@tonic-gate 				 * Finally, if it's a save, it should be
2540Sstevel@tonic-gate 				 * treated as a leaf; if it's a restore it
2550Sstevel@tonic-gate 				 * should not be treated as a leaf.
2560Sstevel@tonic-gate 				 */
2570Sstevel@tonic-gate 				if ((i & DTRACE_SAVE_MASK) == DTRACE_SAVE)
2580Sstevel@tonic-gate 					goto leaf;
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 				if ((i & DTRACE_SAVE_MASK) == DTRACE_RESTORE)
2610Sstevel@tonic-gate 					goto nonleaf;
2620Sstevel@tonic-gate 			}
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 			if (delay) {
2650Sstevel@tonic-gate 				/*
2660Sstevel@tonic-gate 				 * If this was a delay slot instruction and
2670Sstevel@tonic-gate 				 * we didn't pick it up elsewhere, this is a
2680Sstevel@tonic-gate 				 * non-leaf.
2690Sstevel@tonic-gate 				 */
2700Sstevel@tonic-gate 				goto nonleaf;
2710Sstevel@tonic-gate 			}
2720Sstevel@tonic-gate 		}
2730Sstevel@tonic-gate nonleaf:
2740Sstevel@tonic-gate 		aframes++;
2750Sstevel@tonic-gate leaf:
276457Sbmc 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
2770Sstevel@tonic-gate 	}
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	if ((on_intr = CPU_ON_INTR(CPU)) != 0)
2800Sstevel@tonic-gate 		stacktop = (struct frame *)(CPU->cpu_intr_stack + SA(MINFRAME));
2810Sstevel@tonic-gate 	else
2820Sstevel@tonic-gate 		stacktop = (struct frame *)curthread->t_stk;
2830Sstevel@tonic-gate 	minfp = fp;
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	while (depth < pcstack_limit) {
2860Sstevel@tonic-gate 		nextfp = (struct frame *)((caddr_t)fp->fr_savfp + STACK_BIAS);
2870Sstevel@tonic-gate 		if (nextfp <= minfp || nextfp >= stacktop) {
2880Sstevel@tonic-gate 			if (!on_intr && nextfp == stacktop && aframes != 0) {
2890Sstevel@tonic-gate 				/*
2900Sstevel@tonic-gate 				 * If we are exactly at the top of the stack
2910Sstevel@tonic-gate 				 * with a non-zero number of artificial frames,
2920Sstevel@tonic-gate 				 * it must be that the stack is filled with
2930Sstevel@tonic-gate 				 * nothing _but_ artificial frames.  In this
2940Sstevel@tonic-gate 				 * case, we assert that this is so, zero
2950Sstevel@tonic-gate 				 * pcstack, and return.
2960Sstevel@tonic-gate 				 */
2970Sstevel@tonic-gate 				ASSERT(aframes == 1);
2980Sstevel@tonic-gate 				ASSERT(depth == 0);
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 				while (depth < pcstack_limit)
3010Sstevel@tonic-gate 					pcstack[depth++] = NULL;
3020Sstevel@tonic-gate 				return;
3030Sstevel@tonic-gate 			}
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 			if (on_intr) {
3060Sstevel@tonic-gate 				/*
3070Sstevel@tonic-gate 				 * Hop from interrupt stack to thread stack.
3080Sstevel@tonic-gate 				 */
3090Sstevel@tonic-gate 				stacktop = (struct frame *)curthread->t_stk;
3100Sstevel@tonic-gate 				minfp = (struct frame *)curthread->t_stkbase;
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 				on_intr = 0;
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 				if (nextfp > minfp && nextfp < stacktop)
3150Sstevel@tonic-gate 					continue;
3160Sstevel@tonic-gate 			} else {
3170Sstevel@tonic-gate 				/*
3180Sstevel@tonic-gate 				 * High-level interrupts may occur when %sp is
3190Sstevel@tonic-gate 				 * not necessarily contained in the stack
3200Sstevel@tonic-gate 				 * bounds implied by %g7 -- interrupt thread
3210Sstevel@tonic-gate 				 * management runs with %pil at DISP_LEVEL,
3220Sstevel@tonic-gate 				 * and high-level interrupts may thus occur
3230Sstevel@tonic-gate 				 * in windows when %sp and %g7 are not self-
3240Sstevel@tonic-gate 				 * consistent.  If we call dtrace_getpcstack()
3250Sstevel@tonic-gate 				 * from a high-level interrupt that has occurred
3260Sstevel@tonic-gate 				 * in such a window, we will fail the above test
3270Sstevel@tonic-gate 				 * of nextfp against minfp/stacktop.  If the
3280Sstevel@tonic-gate 				 * high-level interrupt has in turn interrupted
3290Sstevel@tonic-gate 				 * a non-passivated interrupt thread, we
3300Sstevel@tonic-gate 				 * will execute the below code with non-zero
3310Sstevel@tonic-gate 				 * aframes.  We therefore want to assert that
3320Sstevel@tonic-gate 				 * aframes is zero _or_ we are in a high-level
3330Sstevel@tonic-gate 				 * interrupt -- but because cpu_intr_actv is
3340Sstevel@tonic-gate 				 * updated with high-level interrupts enabled,
3350Sstevel@tonic-gate 				 * we must reduce this to only asserting that
3360Sstevel@tonic-gate 				 * %pil is greater than DISP_LEVEL.
3370Sstevel@tonic-gate 				 */
3380Sstevel@tonic-gate 				ASSERT(aframes == 0 ||
3390Sstevel@tonic-gate 				    dtrace_getipl() > DISP_LEVEL);
3400Sstevel@tonic-gate 				pcstack[depth++] = (pc_t)fp->fr_savpc;
3410Sstevel@tonic-gate 			}
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 			while (depth < pcstack_limit)
3440Sstevel@tonic-gate 				pcstack[depth++] = NULL;
3450Sstevel@tonic-gate 			return;
3460Sstevel@tonic-gate 		}
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 		if (aframes > 0) {
3490Sstevel@tonic-gate 			aframes--;
3500Sstevel@tonic-gate 		} else {
3510Sstevel@tonic-gate 			pcstack[depth++] = (pc_t)fp->fr_savpc;
3520Sstevel@tonic-gate 		}
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 		fp = nextfp;
3550Sstevel@tonic-gate 		minfp = fp;
3560Sstevel@tonic-gate 	}
3570Sstevel@tonic-gate }
3580Sstevel@tonic-gate 
359191Sahl static int
360191Sahl dtrace_getustack_common(uint64_t *pcstack, int pcstack_limit, uintptr_t sp)
361191Sahl {
362191Sahl 	proc_t *p = curproc;
363191Sahl 	int ret = 0;
364191Sahl 
365191Sahl 	ASSERT(pcstack == NULL || pcstack_limit > 0);
366191Sahl 
367191Sahl 	if (p->p_model == DATAMODEL_NATIVE) {
368191Sahl 		for (;;) {
369191Sahl 			struct frame *fr = (struct frame *)(sp + STACK_BIAS);
370191Sahl 			uintptr_t pc;
371191Sahl 
372191Sahl 			if (sp == 0 || fr == NULL ||
373191Sahl 			    !IS_P2ALIGNED((uintptr_t)fr, STACK_ALIGN))
374191Sahl 				break;
375191Sahl 
376191Sahl 			pc = dtrace_fulword(&fr->fr_savpc);
377191Sahl 			sp = dtrace_fulword(&fr->fr_savfp);
378191Sahl 
379191Sahl 			if (pc == 0)
380191Sahl 				break;
381191Sahl 
382191Sahl 			ret++;
383191Sahl 
384191Sahl 			if (pcstack != NULL) {
385191Sahl 				*pcstack++ = pc;
386191Sahl 				pcstack_limit--;
387191Sahl 				if (pcstack_limit == 0)
388191Sahl 					break;
389191Sahl 			}
390191Sahl 		}
391191Sahl 	} else {
392191Sahl 		for (;;) {
393191Sahl 			struct frame32 *fr = (struct frame32 *)sp;
394191Sahl 			uint32_t pc;
395191Sahl 
396191Sahl 			if (sp == 0 ||
397191Sahl 			    !IS_P2ALIGNED((uintptr_t)fr, STACK_ALIGN32))
398191Sahl 				break;
399191Sahl 
400191Sahl 			pc = dtrace_fuword32(&fr->fr_savpc);
401191Sahl 			sp = dtrace_fuword32(&fr->fr_savfp);
402191Sahl 
403191Sahl 			if (pc == 0)
404191Sahl 				break;
405191Sahl 
406191Sahl 			ret++;
407191Sahl 
408191Sahl 			if (pcstack != NULL) {
409191Sahl 				*pcstack++ = pc;
410191Sahl 				pcstack_limit--;
411191Sahl 				if (pcstack_limit == 0)
412191Sahl 					break;
413191Sahl 			}
414191Sahl 		}
415191Sahl 	}
416191Sahl 
417191Sahl 	return (ret);
418191Sahl }
419191Sahl 
4200Sstevel@tonic-gate void
4210Sstevel@tonic-gate dtrace_getupcstack(uint64_t *pcstack, int pcstack_limit)
4220Sstevel@tonic-gate {
4230Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
424191Sahl 	proc_t *p = curproc;
4250Sstevel@tonic-gate 	struct regs *rp;
4260Sstevel@tonic-gate 	uintptr_t sp;
4270Sstevel@tonic-gate 	int n;
4280Sstevel@tonic-gate 
429630Sahl 	if (pcstack_limit <= 0)
4300Sstevel@tonic-gate 		return;
4310Sstevel@tonic-gate 
432630Sahl 	/*
433630Sahl 	 * If there's no user context we still need to zero the stack.
434630Sahl 	 */
435630Sahl 	if (lwp == NULL || p == NULL || (rp = lwp->lwp_regs) == NULL)
436630Sahl 		goto zero;
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	*pcstack++ = (uint64_t)p->p_pid;
4390Sstevel@tonic-gate 	pcstack_limit--;
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	if (pcstack_limit <= 0)
4420Sstevel@tonic-gate 		return;
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	*pcstack++ = (uint64_t)rp->r_pc;
4450Sstevel@tonic-gate 	pcstack_limit--;
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 	if (pcstack_limit <= 0)
4480Sstevel@tonic-gate 		return;
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
4510Sstevel@tonic-gate 		*pcstack++ = (uint64_t)rp->r_o7;
4520Sstevel@tonic-gate 		pcstack_limit--;
4530Sstevel@tonic-gate 		if (pcstack_limit <= 0)
4540Sstevel@tonic-gate 			return;
4550Sstevel@tonic-gate 	}
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	sp = rp->r_sp;
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	n = dtrace_getupcstack_top(pcstack, pcstack_limit, &sp);
4600Sstevel@tonic-gate 	ASSERT(n >= 0);
4610Sstevel@tonic-gate 	ASSERT(n <= pcstack_limit);
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	pcstack += n;
4640Sstevel@tonic-gate 	pcstack_limit -= n;
465191Sahl 	if (pcstack_limit <= 0)
466191Sahl 		return;
4670Sstevel@tonic-gate 
468191Sahl 	n = dtrace_getustack_common(pcstack, pcstack_limit, sp);
469191Sahl 	ASSERT(n >= 0);
470191Sahl 	ASSERT(n <= pcstack_limit);
4710Sstevel@tonic-gate 
472191Sahl 	pcstack += n;
473191Sahl 	pcstack_limit -= n;
4740Sstevel@tonic-gate 
475630Sahl zero:
4760Sstevel@tonic-gate 	while (pcstack_limit-- > 0)
4770Sstevel@tonic-gate 		*pcstack++ = NULL;
4780Sstevel@tonic-gate }
4790Sstevel@tonic-gate 
480191Sahl int
481191Sahl dtrace_getustackdepth(void)
482191Sahl {
483191Sahl 	klwp_t *lwp = ttolwp(curthread);
484191Sahl 	proc_t *p = curproc;
485191Sahl 	struct regs *rp;
486191Sahl 	uintptr_t sp;
487191Sahl 	int n = 1;
488191Sahl 
489191Sahl 	if (lwp == NULL || p == NULL || (rp = lwp->lwp_regs) == NULL)
490191Sahl 		return (0);
491191Sahl 
492191Sahl 	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT))
493191Sahl 		return (-1);
494191Sahl 
495191Sahl 	sp = rp->r_sp;
496191Sahl 
497191Sahl 	n += dtrace_getustackdepth_top(&sp);
498191Sahl 	n += dtrace_getustack_common(NULL, 0, sp);
499191Sahl 
500630Sahl 	/*
501630Sahl 	 * Add one more to the stack depth if we're in an entry probe as long
502630Sahl 	 * as the return address is non-NULL or there are additional frames
503630Sahl 	 * beyond that NULL return address.
504630Sahl 	 */
505630Sahl 	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY) &&
506630Sahl 	    (rp->r_o7 != NULL || n != 1))
507630Sahl 		n++;
508630Sahl 
509191Sahl 	return (n);
510191Sahl }
511191Sahl 
5120Sstevel@tonic-gate void
5130Sstevel@tonic-gate dtrace_getufpstack(uint64_t *pcstack, uint64_t *fpstack, int pcstack_limit)
5140Sstevel@tonic-gate {
5150Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
5160Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
5170Sstevel@tonic-gate 	struct regs *rp;
5180Sstevel@tonic-gate 	uintptr_t sp;
5190Sstevel@tonic-gate 
520630Sahl 	if (pcstack_limit <= 0)
5210Sstevel@tonic-gate 		return;
5220Sstevel@tonic-gate 
523630Sahl 	/*
524630Sahl 	 * If there's no user context we still need to zero the stack.
525630Sahl 	 */
526630Sahl 	if (lwp == NULL || p == NULL || (rp = lwp->lwp_regs) == NULL)
527630Sahl 		goto zero;
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	*pcstack++ = (uint64_t)p->p_pid;
5300Sstevel@tonic-gate 	pcstack_limit--;
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	if (pcstack_limit <= 0)
5330Sstevel@tonic-gate 		return;
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
5360Sstevel@tonic-gate 		*fpstack++ = 0;
5370Sstevel@tonic-gate 		*pcstack++ = (uint64_t)rp->r_pc;
5380Sstevel@tonic-gate 		pcstack_limit--;
5390Sstevel@tonic-gate 		if (pcstack_limit <= 0)
5400Sstevel@tonic-gate 			return;
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 		*fpstack++ = (uint64_t)rp->r_sp;
5430Sstevel@tonic-gate 		*pcstack++ = (uint64_t)rp->r_o7;
5440Sstevel@tonic-gate 		pcstack_limit--;
5450Sstevel@tonic-gate 	} else {
5460Sstevel@tonic-gate 		*fpstack++ = (uint64_t)rp->r_sp;
5470Sstevel@tonic-gate 		*pcstack++ = (uint64_t)rp->r_pc;
5480Sstevel@tonic-gate 		pcstack_limit--;
5490Sstevel@tonic-gate 	}
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	if (pcstack_limit <= 0)
5520Sstevel@tonic-gate 		return;
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 	sp = rp->r_sp;
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 	dtrace_flush_user_windows();
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	if (p->p_model == DATAMODEL_NATIVE) {
5590Sstevel@tonic-gate 		while (pcstack_limit > 0) {
5600Sstevel@tonic-gate 			struct frame *fr = (struct frame *)(sp + STACK_BIAS);
5610Sstevel@tonic-gate 			uintptr_t pc;
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 			if (sp == 0 || fr == NULL ||
5640Sstevel@tonic-gate 			    ((uintptr_t)&fr->fr_savpc & 3) != 0 ||
5650Sstevel@tonic-gate 			    ((uintptr_t)&fr->fr_savfp & 3) != 0)
5660Sstevel@tonic-gate 				break;
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 			pc = dtrace_fulword(&fr->fr_savpc);
5690Sstevel@tonic-gate 			sp = dtrace_fulword(&fr->fr_savfp);
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 			if (pc == 0)
5720Sstevel@tonic-gate 				break;
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 			*fpstack++ = sp;
5750Sstevel@tonic-gate 			*pcstack++ = pc;
5760Sstevel@tonic-gate 			pcstack_limit--;
5770Sstevel@tonic-gate 		}
5780Sstevel@tonic-gate 	} else {
5790Sstevel@tonic-gate 		while (pcstack_limit > 0) {
5800Sstevel@tonic-gate 			struct frame32 *fr = (struct frame32 *)sp;
5810Sstevel@tonic-gate 			uint32_t pc;
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 			if (sp == 0 ||
5840Sstevel@tonic-gate 			    ((uintptr_t)&fr->fr_savpc & 3) != 0 ||
5850Sstevel@tonic-gate 			    ((uintptr_t)&fr->fr_savfp & 3) != 0)
5860Sstevel@tonic-gate 				break;
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 			pc = dtrace_fuword32(&fr->fr_savpc);
5890Sstevel@tonic-gate 			sp = dtrace_fuword32(&fr->fr_savfp);
5900Sstevel@tonic-gate 
591191Sahl 			if (pc == 0)
592191Sahl 				break;
593191Sahl 
5940Sstevel@tonic-gate 			*fpstack++ = sp;
5950Sstevel@tonic-gate 			*pcstack++ = pc;
5960Sstevel@tonic-gate 			pcstack_limit--;
5970Sstevel@tonic-gate 		}
5980Sstevel@tonic-gate 	}
5990Sstevel@tonic-gate 
600630Sahl zero:
6010Sstevel@tonic-gate 	while (pcstack_limit-- > 0)
6020Sstevel@tonic-gate 		*pcstack++ = NULL;
6030Sstevel@tonic-gate }
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate uint64_t
6060Sstevel@tonic-gate dtrace_getarg(int arg, int aframes)
6070Sstevel@tonic-gate {
6080Sstevel@tonic-gate 	uintptr_t val;
6090Sstevel@tonic-gate 	struct frame *fp;
6100Sstevel@tonic-gate 	uint64_t rval;
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 	/*
6130Sstevel@tonic-gate 	 * Account for the fact that dtrace_getarg() consumes an additional
6140Sstevel@tonic-gate 	 * stack frame.
6150Sstevel@tonic-gate 	 */
6160Sstevel@tonic-gate 	aframes++;
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 	if (arg < 6) {
6190Sstevel@tonic-gate 		if (dtrace_fish(aframes, DTRACE_REG_I0 + arg, &val) == 0)
6200Sstevel@tonic-gate 			return (val);
6210Sstevel@tonic-gate 	} else {
6220Sstevel@tonic-gate 		if (dtrace_fish(aframes, DTRACE_REG_I6, &val) == 0) {
6230Sstevel@tonic-gate 			/*
6240Sstevel@tonic-gate 			 * We have a stack pointer; grab the argument.
6250Sstevel@tonic-gate 			 */
6260Sstevel@tonic-gate 			fp = (struct frame *)(val + STACK_BIAS);
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
6290Sstevel@tonic-gate 			rval = fp->fr_argx[arg - 6];
6300Sstevel@tonic-gate 			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 			return (rval);
6330Sstevel@tonic-gate 		}
6340Sstevel@tonic-gate 	}
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	/*
6370Sstevel@tonic-gate 	 * There are other ways to do this.  But the slow, painful way works
6380Sstevel@tonic-gate 	 * just fine.  Because this requires some loads, we need to set
6390Sstevel@tonic-gate 	 * CPU_DTRACE_NOFAULT to protect against looking for an argument that
6400Sstevel@tonic-gate 	 * isn't there.
6410Sstevel@tonic-gate 	 */
6420Sstevel@tonic-gate 	fp = (struct frame *)((caddr_t)dtrace_getfp() + STACK_BIAS);
6430Sstevel@tonic-gate 	dtrace_flush_windows();
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	for (aframes -= 1; aframes; aframes--)
6480Sstevel@tonic-gate 		fp = (struct frame *)((caddr_t)fp->fr_savfp + STACK_BIAS);
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 	if (arg < 6) {
6510Sstevel@tonic-gate 		rval = fp->fr_arg[arg];
6520Sstevel@tonic-gate 	} else {
6530Sstevel@tonic-gate 		fp = (struct frame *)((caddr_t)fp->fr_savfp + STACK_BIAS);
6540Sstevel@tonic-gate 		rval = fp->fr_argx[arg - 6];
6550Sstevel@tonic-gate 	}
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 	return (rval);
6600Sstevel@tonic-gate }
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate int
6630Sstevel@tonic-gate dtrace_getstackdepth(int aframes)
6640Sstevel@tonic-gate {
6650Sstevel@tonic-gate 	struct frame *fp, *nextfp, *minfp, *stacktop;
6660Sstevel@tonic-gate 	int depth = 0;
6670Sstevel@tonic-gate 	int on_intr;
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	fp = (struct frame *)((caddr_t)dtrace_getfp() + STACK_BIAS);
6700Sstevel@tonic-gate 	dtrace_flush_windows();
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	if ((on_intr = CPU_ON_INTR(CPU)) != 0)
6730Sstevel@tonic-gate 		stacktop = (struct frame *)CPU->cpu_intr_stack + SA(MINFRAME);
6740Sstevel@tonic-gate 	else
6750Sstevel@tonic-gate 		stacktop = (struct frame *)curthread->t_stk;
6760Sstevel@tonic-gate 	minfp = fp;
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	for (;;) {
6790Sstevel@tonic-gate 		nextfp = (struct frame *)((caddr_t)fp->fr_savfp + STACK_BIAS);
6800Sstevel@tonic-gate 		if (nextfp <= minfp || nextfp >= stacktop) {
6810Sstevel@tonic-gate 			if (on_intr) {
6820Sstevel@tonic-gate 				/*
6830Sstevel@tonic-gate 				 * Hop from interrupt stack to thread stack.
6840Sstevel@tonic-gate 				 */
6850Sstevel@tonic-gate 				stacktop = (struct frame *)curthread->t_stk;
6860Sstevel@tonic-gate 				minfp = (struct frame *)curthread->t_stkbase;
6870Sstevel@tonic-gate 				on_intr = 0;
6880Sstevel@tonic-gate 				continue;
6890Sstevel@tonic-gate 			}
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 			return (++depth);
6920Sstevel@tonic-gate 		}
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 		if (aframes > 0) {
6950Sstevel@tonic-gate 			aframes--;
6960Sstevel@tonic-gate 		} else {
6970Sstevel@tonic-gate 			depth++;
6980Sstevel@tonic-gate 		}
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 		fp = nextfp;
7010Sstevel@tonic-gate 		minfp = fp;
7020Sstevel@tonic-gate 	}
7030Sstevel@tonic-gate }
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate /*
7060Sstevel@tonic-gate  * This uses the same register numbering scheme as in sys/procfs_isa.h.
7070Sstevel@tonic-gate  */
7080Sstevel@tonic-gate ulong_t
7090Sstevel@tonic-gate dtrace_getreg(struct regs *rp, uint_t reg)
7100Sstevel@tonic-gate {
7110Sstevel@tonic-gate 	ulong_t value;
7120Sstevel@tonic-gate 	uintptr_t fp;
7130Sstevel@tonic-gate 	struct machpcb *mpcb;
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 	if (reg == R_G0)
7160Sstevel@tonic-gate 		return (0);
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	if (reg <= R_G7)
7190Sstevel@tonic-gate 		return ((&rp->r_g1)[reg - 1]);
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 	if (reg > R_I7) {
7220Sstevel@tonic-gate 		switch (reg) {
7230Sstevel@tonic-gate 		case R_CCR:
7240Sstevel@tonic-gate 			return ((rp->r_tstate >> TSTATE_CCR_SHIFT) &
7250Sstevel@tonic-gate 			    TSTATE_CCR_MASK);
7260Sstevel@tonic-gate 		case R_PC:
7270Sstevel@tonic-gate 			return (rp->r_pc);
7280Sstevel@tonic-gate 		case R_nPC:
7290Sstevel@tonic-gate 			return (rp->r_npc);
7300Sstevel@tonic-gate 		case R_Y:
7310Sstevel@tonic-gate 			return (rp->r_y);
7320Sstevel@tonic-gate 		case R_ASI:
7330Sstevel@tonic-gate 			return ((rp->r_tstate >> TSTATE_ASI_SHIFT) &
7340Sstevel@tonic-gate 			    TSTATE_ASI_MASK);
7350Sstevel@tonic-gate 		case R_FPRS:
7360Sstevel@tonic-gate 			return (dtrace_getfprs());
7370Sstevel@tonic-gate 		default:
7380Sstevel@tonic-gate 			DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
7390Sstevel@tonic-gate 			return (0);
7400Sstevel@tonic-gate 		}
7410Sstevel@tonic-gate 	}
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 	/*
7440Sstevel@tonic-gate 	 * We reach go to the fake restore case if the probe we hit was a pid
7450Sstevel@tonic-gate 	 * return probe on a restore instruction. We partially emulate the
7460Sstevel@tonic-gate 	 * restore in the kernel and then execute a simple restore
7470Sstevel@tonic-gate 	 * instruction that we've secreted away to do the actual register
7480Sstevel@tonic-gate 	 * window manipulation. We need to go one register window further
7490Sstevel@tonic-gate 	 * down to get at the %ls, and %is and we need to treat %os like %is
7500Sstevel@tonic-gate 	 * to pull them out of the topmost user frame.
7510Sstevel@tonic-gate 	 */
7520Sstevel@tonic-gate 	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAKERESTORE)) {
7530Sstevel@tonic-gate 		if (reg > R_O7)
7540Sstevel@tonic-gate 			goto fake_restore;
7550Sstevel@tonic-gate 		else
7560Sstevel@tonic-gate 			reg += R_I0 - R_O0;
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 	} else if (reg <= R_O7) {
7590Sstevel@tonic-gate 		return ((&rp->r_g1)[reg - 1]);
7600Sstevel@tonic-gate 	}
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 	if (dtrace_getotherwin() > 0)
7630Sstevel@tonic-gate 		return (dtrace_getreg_win(reg, 1));
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	mpcb = (struct machpcb *)((caddr_t)rp - REGOFF);
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 	if (curproc->p_model == DATAMODEL_NATIVE) {
7680Sstevel@tonic-gate 		struct frame *fr = (void *)(rp->r_sp + STACK_BIAS);
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 		if (mpcb->mpcb_wbcnt > 0) {
7710Sstevel@tonic-gate 			struct rwindow *rwin = (void *)mpcb->mpcb_wbuf;
7720Sstevel@tonic-gate 			int i = mpcb->mpcb_wbcnt;
7730Sstevel@tonic-gate 			do {
7740Sstevel@tonic-gate 				i--;
7750Sstevel@tonic-gate 				if ((long)mpcb->mpcb_spbuf[i] == rp->r_sp)
7760Sstevel@tonic-gate 					return (rwin[i].rw_local[reg - 16]);
7770Sstevel@tonic-gate 			} while (i > 0);
7780Sstevel@tonic-gate 		}
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
7810Sstevel@tonic-gate 		value = dtrace_fulword(&fr->fr_local[reg - 16]);
7820Sstevel@tonic-gate 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
7830Sstevel@tonic-gate 	} else {
784*1048Sraf 		struct frame32 *fr = (void *)(uintptr_t)(caddr32_t)rp->r_sp;
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 		if (mpcb->mpcb_wbcnt > 0) {
7870Sstevel@tonic-gate 			struct rwindow32 *rwin = (void *)mpcb->mpcb_wbuf;
7880Sstevel@tonic-gate 			int i = mpcb->mpcb_wbcnt;
7890Sstevel@tonic-gate 			do {
7900Sstevel@tonic-gate 				i--;
7910Sstevel@tonic-gate 				if ((long)mpcb->mpcb_spbuf[i] == rp->r_sp)
7920Sstevel@tonic-gate 					return (rwin[i].rw_local[reg - 16]);
7930Sstevel@tonic-gate 			} while (i > 0);
7940Sstevel@tonic-gate 		}
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
7970Sstevel@tonic-gate 		value = dtrace_fuword32(&fr->fr_local[reg - 16]);
7980Sstevel@tonic-gate 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
7990Sstevel@tonic-gate 	}
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	return (value);
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate fake_restore:
8040Sstevel@tonic-gate 	ASSERT(R_L0 <= reg && reg <= R_I7);
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	/*
8070Sstevel@tonic-gate 	 * We first look two user windows down to see if we can dig out
8080Sstevel@tonic-gate 	 * the register we're looking for.
8090Sstevel@tonic-gate 	 */
8100Sstevel@tonic-gate 	if (dtrace_getotherwin() > 1)
8110Sstevel@tonic-gate 		return (dtrace_getreg_win(reg, 2));
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 	/*
8140Sstevel@tonic-gate 	 * First we need to get the frame pointer and then we perform
8150Sstevel@tonic-gate 	 * the same computation as in the non-fake-o-restore case.
8160Sstevel@tonic-gate 	 */
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 	mpcb = (struct machpcb *)((caddr_t)rp - REGOFF);
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 	if (dtrace_getotherwin() > 0) {
8210Sstevel@tonic-gate 		fp = dtrace_getreg_win(R_FP, 1);
8220Sstevel@tonic-gate 		goto got_fp;
8230Sstevel@tonic-gate 	}
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	if (curproc->p_model == DATAMODEL_NATIVE) {
8260Sstevel@tonic-gate 		struct frame *fr = (void *)(rp->r_sp + STACK_BIAS);
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 		if (mpcb->mpcb_wbcnt > 0) {
8290Sstevel@tonic-gate 			struct rwindow *rwin = (void *)mpcb->mpcb_wbuf;
8300Sstevel@tonic-gate 			int i = mpcb->mpcb_wbcnt;
8310Sstevel@tonic-gate 			do {
8320Sstevel@tonic-gate 				i--;
8330Sstevel@tonic-gate 				if ((long)mpcb->mpcb_spbuf[i] == rp->r_sp) {
8340Sstevel@tonic-gate 					fp = rwin[i].rw_fp;
8350Sstevel@tonic-gate 					goto got_fp;
8360Sstevel@tonic-gate 				}
8370Sstevel@tonic-gate 			} while (i > 0);
8380Sstevel@tonic-gate 		}
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
8410Sstevel@tonic-gate 		fp = dtrace_fulword(&fr->fr_savfp);
8420Sstevel@tonic-gate 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
8430Sstevel@tonic-gate 		if (cpu_core[CPU->cpu_id].cpuc_dtrace_flags & CPU_DTRACE_FAULT)
8440Sstevel@tonic-gate 			return (0);
8450Sstevel@tonic-gate 	} else {
846*1048Sraf 		struct frame32 *fr = (void *)(uintptr_t)(caddr32_t)rp->r_sp;
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 		if (mpcb->mpcb_wbcnt > 0) {
8490Sstevel@tonic-gate 			struct rwindow32 *rwin = (void *)mpcb->mpcb_wbuf;
8500Sstevel@tonic-gate 			int i = mpcb->mpcb_wbcnt;
8510Sstevel@tonic-gate 			do {
8520Sstevel@tonic-gate 				i--;
8530Sstevel@tonic-gate 				if ((long)mpcb->mpcb_spbuf[i] == rp->r_sp) {
8540Sstevel@tonic-gate 					fp = rwin[i].rw_fp;
8550Sstevel@tonic-gate 					goto got_fp;
8560Sstevel@tonic-gate 				}
8570Sstevel@tonic-gate 			} while (i > 0);
8580Sstevel@tonic-gate 		}
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
8610Sstevel@tonic-gate 		fp = dtrace_fuword32(&fr->fr_savfp);
8620Sstevel@tonic-gate 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
8630Sstevel@tonic-gate 		if (cpu_core[CPU->cpu_id].cpuc_dtrace_flags & CPU_DTRACE_FAULT)
8640Sstevel@tonic-gate 			return (0);
8650Sstevel@tonic-gate 	}
8660Sstevel@tonic-gate got_fp:
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate 	if (curproc->p_model == DATAMODEL_NATIVE) {
8690Sstevel@tonic-gate 		struct frame *fr = (void *)(fp + STACK_BIAS);
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 		if (mpcb->mpcb_wbcnt > 0) {
8720Sstevel@tonic-gate 			struct rwindow *rwin = (void *)mpcb->mpcb_wbuf;
8730Sstevel@tonic-gate 			int i = mpcb->mpcb_wbcnt;
8740Sstevel@tonic-gate 			do {
8750Sstevel@tonic-gate 				i--;
8760Sstevel@tonic-gate 				if ((long)mpcb->mpcb_spbuf[i] == fp)
8770Sstevel@tonic-gate 					return (rwin[i].rw_local[reg - 16]);
8780Sstevel@tonic-gate 			} while (i > 0);
8790Sstevel@tonic-gate 		}
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
8820Sstevel@tonic-gate 		value = dtrace_fulword(&fr->fr_local[reg - 16]);
8830Sstevel@tonic-gate 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
8840Sstevel@tonic-gate 	} else {
885*1048Sraf 		struct frame32 *fr = (void *)(uintptr_t)(caddr32_t)fp;
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 		if (mpcb->mpcb_wbcnt > 0) {
8880Sstevel@tonic-gate 			struct rwindow32 *rwin = (void *)mpcb->mpcb_wbuf;
8890Sstevel@tonic-gate 			int i = mpcb->mpcb_wbcnt;
8900Sstevel@tonic-gate 			do {
8910Sstevel@tonic-gate 				i--;
8920Sstevel@tonic-gate 				if ((long)mpcb->mpcb_spbuf[i] == fp)
8930Sstevel@tonic-gate 					return (rwin[i].rw_local[reg - 16]);
8940Sstevel@tonic-gate 			} while (i > 0);
8950Sstevel@tonic-gate 		}
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
8980Sstevel@tonic-gate 		value = dtrace_fuword32(&fr->fr_local[reg - 16]);
8990Sstevel@tonic-gate 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
9000Sstevel@tonic-gate 	}
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 	return (value);
9030Sstevel@tonic-gate }
904