17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
2275521904Sraf 
237c478bd9Sstevel@tonic-gate /*
24*900524f3Sahl  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <sys/dtrace_impl.h>
317c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
327c478bd9Sstevel@tonic-gate #include <sys/model.h>
337c478bd9Sstevel@tonic-gate #include <sys/frame.h>
347c478bd9Sstevel@tonic-gate #include <sys/stack.h>
357c478bd9Sstevel@tonic-gate #include <sys/machpcb.h>
367c478bd9Sstevel@tonic-gate #include <sys/procfs_isa.h>
377c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
380b38a8bdSahl #include <sys/sysmacros.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #define	DTRACE_FMT3OP3_MASK	0x81000000
417c478bd9Sstevel@tonic-gate #define	DTRACE_FMT3OP3		0x80000000
427c478bd9Sstevel@tonic-gate #define	DTRACE_FMT3RS1_SHIFT	14
437c478bd9Sstevel@tonic-gate #define	DTRACE_FMT3RD_SHIFT	25
44a1b5e537Sbmc #define	DTRACE_DISP22_SHIFT	10
457c478bd9Sstevel@tonic-gate #define	DTRACE_RMASK		0x1f
467c478bd9Sstevel@tonic-gate #define	DTRACE_REG_L0		16
477c478bd9Sstevel@tonic-gate #define	DTRACE_REG_O7		15
487c478bd9Sstevel@tonic-gate #define	DTRACE_REG_I0		24
497c478bd9Sstevel@tonic-gate #define	DTRACE_REG_I6		30
507c478bd9Sstevel@tonic-gate #define	DTRACE_RET		0x81c7e008
517c478bd9Sstevel@tonic-gate #define	DTRACE_RETL		0x81c3e008
527c478bd9Sstevel@tonic-gate #define	DTRACE_SAVE_MASK	0xc1f80000
537c478bd9Sstevel@tonic-gate #define	DTRACE_SAVE		0x81e00000
547c478bd9Sstevel@tonic-gate #define	DTRACE_RESTORE		0x81e80000
557c478bd9Sstevel@tonic-gate #define	DTRACE_CALL_MASK	0xc0000000
567c478bd9Sstevel@tonic-gate #define	DTRACE_CALL		0x40000000
577c478bd9Sstevel@tonic-gate #define	DTRACE_JMPL_MASK	0x81f10000
587c478bd9Sstevel@tonic-gate #define	DTRACE_JMPL		0x81c00000
59a1b5e537Sbmc #define	DTRACE_BA_MASK		0xdfc00000
60a1b5e537Sbmc #define	DTRACE_BA		0x10800000
61a1b5e537Sbmc #define	DTRACE_BA_MAX		10
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate extern int dtrace_getupcstack_top(uint64_t *, int, uintptr_t *);
640b38a8bdSahl extern int dtrace_getustackdepth_top(uintptr_t *);
657c478bd9Sstevel@tonic-gate extern ulong_t dtrace_getreg_win(uint_t, uint_t);
667c478bd9Sstevel@tonic-gate extern void dtrace_putreg_win(uint_t, ulong_t);
677c478bd9Sstevel@tonic-gate extern int dtrace_fish(int, int, uintptr_t *);
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate /*
707c478bd9Sstevel@tonic-gate  * This is similar in principle to getpcstack(), but there are several marked
717c478bd9Sstevel@tonic-gate  * differences in implementation:
727c478bd9Sstevel@tonic-gate  *
737c478bd9Sstevel@tonic-gate  * (a)	dtrace_getpcstack() is called from probe context.  Thus, the call
747c478bd9Sstevel@tonic-gate  *	to flush_windows() from getpcstack() is a call to the probe-safe
757c478bd9Sstevel@tonic-gate  *	equivalent here.
767c478bd9Sstevel@tonic-gate  *
777c478bd9Sstevel@tonic-gate  * (b)  dtrace_getpcstack() is willing to sacrifice some performance to get
787c478bd9Sstevel@tonic-gate  *	a correct stack.  While consumers of getpcstack() are largely
797c478bd9Sstevel@tonic-gate  *	subsystem-specific in-kernel debugging facilities, DTrace consumers
807c478bd9Sstevel@tonic-gate  *	are arbitrary user-level analysis tools; dtrace_getpcstack() must
817c478bd9Sstevel@tonic-gate  *	deliver as correct a stack as possible.  Details on the issues
827c478bd9Sstevel@tonic-gate  *	surrounding stack correctness are found below.
837c478bd9Sstevel@tonic-gate  *
840b38a8bdSahl  * (c)	dtrace_getpcstack() _always_ fills in pcstack_limit pc_t's -- filling
850b38a8bdSahl  *	in the difference between the stack depth and pcstack_limit with NULLs.
867c478bd9Sstevel@tonic-gate  *	Due to this behavior dtrace_getpcstack() returns void.
877c478bd9Sstevel@tonic-gate  *
887c478bd9Sstevel@tonic-gate  * (d)	dtrace_getpcstack() takes a third parameter, aframes, that
897c478bd9Sstevel@tonic-gate  *	denotes the number of _artificial frames_ on the bottom of the
907c478bd9Sstevel@tonic-gate  *	stack.  An artificial frame is one induced by the provider; all
917c478bd9Sstevel@tonic-gate  *	artificial frames are stripped off before frames are stored to
927c478bd9Sstevel@tonic-gate  *	pcstack.
937c478bd9Sstevel@tonic-gate  *
947c478bd9Sstevel@tonic-gate  * (e)	dtrace_getpcstack() takes a fourth parameter, pc, that indicates
957c478bd9Sstevel@tonic-gate  *	an interrupted program counter (if any).  This should be a non-NULL
967c478bd9Sstevel@tonic-gate  *	value if and only if the hit probe is unanchored.  (Anchored probes
977c478bd9Sstevel@tonic-gate  *	don't fire through an interrupt source.)  This parameter is used to
987c478bd9Sstevel@tonic-gate  *	assure (b), above.
997c478bd9Sstevel@tonic-gate  */
1007c478bd9Sstevel@tonic-gate void
1017c478bd9Sstevel@tonic-gate dtrace_getpcstack(pc_t *pcstack, int pcstack_limit, int aframes, uint32_t *pc)
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate 	struct frame *fp, *nextfp, *minfp, *stacktop;
1047c478bd9Sstevel@tonic-gate 	int depth = 0;
1057c478bd9Sstevel@tonic-gate 	int on_intr, j = 0;
1067c478bd9Sstevel@tonic-gate 	uint32_t i, r;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	fp = (struct frame *)((caddr_t)dtrace_getfp() + STACK_BIAS);
1097c478bd9Sstevel@tonic-gate 	dtrace_flush_windows();
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	if (pc != NULL) {
1127c478bd9Sstevel@tonic-gate 		/*
1137c478bd9Sstevel@tonic-gate 		 * If we've been passed a non-NULL pc, we need to determine
1147c478bd9Sstevel@tonic-gate 		 * whether or not the specified program counter falls in a leaf
1157c478bd9Sstevel@tonic-gate 		 * function.  If it falls within a leaf function, we know that
1167c478bd9Sstevel@tonic-gate 		 * %o7 is valid in its frame (and we can just drive on).  If
1177c478bd9Sstevel@tonic-gate 		 * it's a non-leaf, however, we know that %o7 is garbage in the
1187c478bd9Sstevel@tonic-gate 		 * bottom frame.  To trim this frame, we simply increment
1197c478bd9Sstevel@tonic-gate 		 * aframes and drop into the stack-walking loop.
1207c478bd9Sstevel@tonic-gate 		 *
1217c478bd9Sstevel@tonic-gate 		 * To quickly determine if the specified program counter is in
1227c478bd9Sstevel@tonic-gate 		 * a leaf function, we exploit the fact that leaf functions
1237c478bd9Sstevel@tonic-gate 		 * tend to be short and non-leaf functions tend to frequently
1247c478bd9Sstevel@tonic-gate 		 * perform operations that are only permitted in a non-leaf
1257c478bd9Sstevel@tonic-gate 		 * function (e.g., using the %i's or %l's; calling a function;
1267c478bd9Sstevel@tonic-gate 		 * performing a restore).  We exploit these tendencies by
1277c478bd9Sstevel@tonic-gate 		 * simply scanning forward from the specified %pc -- if we see
1287c478bd9Sstevel@tonic-gate 		 * an operation only permitted in a non-leaf, we know we're in
1297c478bd9Sstevel@tonic-gate 		 * a non-leaf; if we see a retl, we know we're in a leaf.
1307c478bd9Sstevel@tonic-gate 		 * Fortunately, one need not perform anywhere near full
1317c478bd9Sstevel@tonic-gate 		 * disassembly to effectively determine the former: determining
1327c478bd9Sstevel@tonic-gate 		 * that an instruction is a format-3 instruction and decoding
1337c478bd9Sstevel@tonic-gate 		 * its rd and rs1 fields, for example, requires very little
1347c478bd9Sstevel@tonic-gate 		 * manipulation.  Overall, this method of leaf determination
1357c478bd9Sstevel@tonic-gate 		 * performs quite well:  on average, we only examine between
1367c478bd9Sstevel@tonic-gate 		 * 1.5 and 2.5 instructions before making the determination.
1377c478bd9Sstevel@tonic-gate 		 * (Outliers do exist, however; of note is the non-leaf
1387c478bd9Sstevel@tonic-gate 		 * function ip_sioctl_not_ours() which -- as of this writing --
1397c478bd9Sstevel@tonic-gate 		 * has a whopping 455 straight instructions that manipulate
1407c478bd9Sstevel@tonic-gate 		 * only %g's and %o's.)
1417c478bd9Sstevel@tonic-gate 		 */
142a1b5e537Sbmc 		int delay = 0, branches = 0, taken = 0;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 		if (depth < pcstack_limit)
14575521904Sraf 			pcstack[depth++] = (pc_t)(uintptr_t)pc;
1467c478bd9Sstevel@tonic-gate 
147a1b5e537Sbmc 		/*
148a1b5e537Sbmc 		 * Our heuristic is exactly that -- a heuristic -- and there
149a1b5e537Sbmc 		 * exists a possibility that we could be either be vectored
150a1b5e537Sbmc 		 * off into the weeds (by following a bogus branch) or could
151a1b5e537Sbmc 		 * wander off the end of the function and off the end of a
152a1b5e537Sbmc 		 * text mapping (by not following a conditional branch at the
153a1b5e537Sbmc 		 * end of the function that is effectively always taken).  So
154a1b5e537Sbmc 		 * as a precautionary measure, we set the NOFAULT flag.
155a1b5e537Sbmc 		 */
156a1b5e537Sbmc 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
157a1b5e537Sbmc 
1587c478bd9Sstevel@tonic-gate 		for (;;) {
1597c478bd9Sstevel@tonic-gate 			i = pc[j++];
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 			if ((i & DTRACE_FMT3OP3_MASK) == DTRACE_FMT3OP3) {
1627c478bd9Sstevel@tonic-gate 				/*
1637c478bd9Sstevel@tonic-gate 				 * This is a format-3 instruction.  We can
1647c478bd9Sstevel@tonic-gate 				 * look at rd and rs1.
1657c478bd9Sstevel@tonic-gate 				 */
1667c478bd9Sstevel@tonic-gate 				r = (i >> DTRACE_FMT3RS1_SHIFT) & DTRACE_RMASK;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 				if (r >= DTRACE_REG_L0)
1697c478bd9Sstevel@tonic-gate 					goto nonleaf;
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 				r = (i >> DTRACE_FMT3RD_SHIFT) & DTRACE_RMASK;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 				if (r >= DTRACE_REG_L0)
1747c478bd9Sstevel@tonic-gate 					goto nonleaf;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 				if ((i & DTRACE_JMPL_MASK) == DTRACE_JMPL) {
1777c478bd9Sstevel@tonic-gate 					delay = 1;
1787c478bd9Sstevel@tonic-gate 					continue;
1797c478bd9Sstevel@tonic-gate 				}
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 				/*
1827c478bd9Sstevel@tonic-gate 				 * If we see explicit manipulation with %o7
1837c478bd9Sstevel@tonic-gate 				 * as a destination register, we know that
1847c478bd9Sstevel@tonic-gate 				 * %o7 is likely bogus -- and we treat this
1857c478bd9Sstevel@tonic-gate 				 * function as a non-leaf.
1867c478bd9Sstevel@tonic-gate 				 */
1877c478bd9Sstevel@tonic-gate 				if (r == DTRACE_REG_O7) {
1887c478bd9Sstevel@tonic-gate 					if (delay)
1897c478bd9Sstevel@tonic-gate 						goto leaf;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 					i &= DTRACE_JMPL_MASK;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 					if (i == DTRACE_JMPL) {
1947c478bd9Sstevel@tonic-gate 						delay = 1;
1957c478bd9Sstevel@tonic-gate 						continue;
1967c478bd9Sstevel@tonic-gate 					}
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 					goto nonleaf;
1997c478bd9Sstevel@tonic-gate 				}
2007c478bd9Sstevel@tonic-gate 			} else {
2017c478bd9Sstevel@tonic-gate 				/*
2027c478bd9Sstevel@tonic-gate 				 * If this is a call, it may or may not be
2037c478bd9Sstevel@tonic-gate 				 * a leaf; we need to check the delay slot.
2047c478bd9Sstevel@tonic-gate 				 */
2057c478bd9Sstevel@tonic-gate 				if ((i & DTRACE_CALL_MASK) == DTRACE_CALL) {
2067c478bd9Sstevel@tonic-gate 					delay = 1;
2077c478bd9Sstevel@tonic-gate 					continue;
2087c478bd9Sstevel@tonic-gate 				}
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 				/*
2117c478bd9Sstevel@tonic-gate 				 * If we see a ret it's not a leaf; if we
2127c478bd9Sstevel@tonic-gate 				 * see a retl, it is a leaf.
2137c478bd9Sstevel@tonic-gate 				 */
2147c478bd9Sstevel@tonic-gate 				if (i == DTRACE_RET)
2157c478bd9Sstevel@tonic-gate 					goto nonleaf;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 				if (i == DTRACE_RETL)
2187c478bd9Sstevel@tonic-gate 					goto leaf;
2197c478bd9Sstevel@tonic-gate 
220a1b5e537Sbmc 				/*
221a1b5e537Sbmc 				 * If this is a ba (annulled or not), then we
222a1b5e537Sbmc 				 * need to actually follow the branch.  No, we
223a1b5e537Sbmc 				 * don't look at the delay slot -- hopefully
224a1b5e537Sbmc 				 * anything that can be gleaned from the delay
225a1b5e537Sbmc 				 * slot can also be gleaned from the branch
226a1b5e537Sbmc 				 * target.  To prevent ourselves from iterating
227a1b5e537Sbmc 				 * infinitely, we clamp the number of branches
228a1b5e537Sbmc 				 * that we'll follow, and we refuse to follow
229a1b5e537Sbmc 				 * the same branch twice consecutively.  In
230a1b5e537Sbmc 				 * both cases, we abort by deciding that we're
231a1b5e537Sbmc 				 * looking at a leaf.  While in theory this
232a1b5e537Sbmc 				 * could be wrong (we could be in the middle of
233a1b5e537Sbmc 				 * a loop in a non-leaf that ends with a ba and
234a1b5e537Sbmc 				 * only manipulates outputs and globals in the
235a1b5e537Sbmc 				 * body of the loop -- therefore leading us to
236a1b5e537Sbmc 				 * the wrong conclusion), this doesn't seem to
237a1b5e537Sbmc 				 * crop up in practice.  (Or rather, this
238a1b5e537Sbmc 				 * condition could not be deliberately induced,
239a1b5e537Sbmc 				 * despite concerted effort.)
240a1b5e537Sbmc 				 */
241a1b5e537Sbmc 				if ((i & DTRACE_BA_MASK) == DTRACE_BA) {
242a1b5e537Sbmc 					if (++branches == DTRACE_BA_MAX ||
243a1b5e537Sbmc 					    taken == j)
244a1b5e537Sbmc 						goto nonleaf;
245a1b5e537Sbmc 
246a1b5e537Sbmc 					taken = j;
247a1b5e537Sbmc 					j += ((int)(i << DTRACE_DISP22_SHIFT) >>
248a1b5e537Sbmc 					    DTRACE_DISP22_SHIFT) - 1;
249a1b5e537Sbmc 					continue;
250a1b5e537Sbmc 				}
251a1b5e537Sbmc 
2527c478bd9Sstevel@tonic-gate 				/*
2537c478bd9Sstevel@tonic-gate 				 * Finally, if it's a save, it should be
2547c478bd9Sstevel@tonic-gate 				 * treated as a leaf; if it's a restore it
2557c478bd9Sstevel@tonic-gate 				 * should not be treated as a leaf.
2567c478bd9Sstevel@tonic-gate 				 */
2577c478bd9Sstevel@tonic-gate 				if ((i & DTRACE_SAVE_MASK) == DTRACE_SAVE)
2587c478bd9Sstevel@tonic-gate 					goto leaf;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 				if ((i & DTRACE_SAVE_MASK) == DTRACE_RESTORE)
2617c478bd9Sstevel@tonic-gate 					goto nonleaf;
2627c478bd9Sstevel@tonic-gate 			}
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 			if (delay) {
2657c478bd9Sstevel@tonic-gate 				/*
2667c478bd9Sstevel@tonic-gate 				 * If this was a delay slot instruction and
2677c478bd9Sstevel@tonic-gate 				 * we didn't pick it up elsewhere, this is a
2687c478bd9Sstevel@tonic-gate 				 * non-leaf.
2697c478bd9Sstevel@tonic-gate 				 */
2707c478bd9Sstevel@tonic-gate 				goto nonleaf;
2717c478bd9Sstevel@tonic-gate 			}
2727c478bd9Sstevel@tonic-gate 		}
2737c478bd9Sstevel@tonic-gate nonleaf:
2747c478bd9Sstevel@tonic-gate 		aframes++;
2757c478bd9Sstevel@tonic-gate leaf:
276a1b5e537Sbmc 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	if ((on_intr = CPU_ON_INTR(CPU)) != 0)
2807c478bd9Sstevel@tonic-gate 		stacktop = (struct frame *)(CPU->cpu_intr_stack + SA(MINFRAME));
2817c478bd9Sstevel@tonic-gate 	else
2827c478bd9Sstevel@tonic-gate 		stacktop = (struct frame *)curthread->t_stk;
2837c478bd9Sstevel@tonic-gate 	minfp = fp;
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	while (depth < pcstack_limit) {
2867c478bd9Sstevel@tonic-gate 		nextfp = (struct frame *)((caddr_t)fp->fr_savfp + STACK_BIAS);
2877c478bd9Sstevel@tonic-gate 		if (nextfp <= minfp || nextfp >= stacktop) {
2887c478bd9Sstevel@tonic-gate 			if (!on_intr && nextfp == stacktop && aframes != 0) {
2897c478bd9Sstevel@tonic-gate 				/*
2907c478bd9Sstevel@tonic-gate 				 * If we are exactly at the top of the stack
2917c478bd9Sstevel@tonic-gate 				 * with a non-zero number of artificial frames,
2927c478bd9Sstevel@tonic-gate 				 * it must be that the stack is filled with
2937c478bd9Sstevel@tonic-gate 				 * nothing _but_ artificial frames.  In this
2947c478bd9Sstevel@tonic-gate 				 * case, we assert that this is so, zero
2957c478bd9Sstevel@tonic-gate 				 * pcstack, and return.
2967c478bd9Sstevel@tonic-gate 				 */
2977c478bd9Sstevel@tonic-gate 				ASSERT(aframes == 1);
2987c478bd9Sstevel@tonic-gate 				ASSERT(depth == 0);
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 				while (depth < pcstack_limit)
3017c478bd9Sstevel@tonic-gate 					pcstack[depth++] = NULL;
3027c478bd9Sstevel@tonic-gate 				return;
3037c478bd9Sstevel@tonic-gate 			}
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 			if (on_intr) {
3067c478bd9Sstevel@tonic-gate 				/*
3077c478bd9Sstevel@tonic-gate 				 * Hop from interrupt stack to thread stack.
3087c478bd9Sstevel@tonic-gate 				 */
3097c478bd9Sstevel@tonic-gate 				stacktop = (struct frame *)curthread->t_stk;
3107c478bd9Sstevel@tonic-gate 				minfp = (struct frame *)curthread->t_stkbase;
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 				on_intr = 0;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 				if (nextfp > minfp && nextfp < stacktop)
3157c478bd9Sstevel@tonic-gate 					continue;
3167c478bd9Sstevel@tonic-gate 			} else {
3177c478bd9Sstevel@tonic-gate 				/*
3187c478bd9Sstevel@tonic-gate 				 * High-level interrupts may occur when %sp is
3197c478bd9Sstevel@tonic-gate 				 * not necessarily contained in the stack
3207c478bd9Sstevel@tonic-gate 				 * bounds implied by %g7 -- interrupt thread
3217c478bd9Sstevel@tonic-gate 				 * management runs with %pil at DISP_LEVEL,
3227c478bd9Sstevel@tonic-gate 				 * and high-level interrupts may thus occur
3237c478bd9Sstevel@tonic-gate 				 * in windows when %sp and %g7 are not self-
3247c478bd9Sstevel@tonic-gate 				 * consistent.  If we call dtrace_getpcstack()
3257c478bd9Sstevel@tonic-gate 				 * from a high-level interrupt that has occurred
3267c478bd9Sstevel@tonic-gate 				 * in such a window, we will fail the above test
3277c478bd9Sstevel@tonic-gate 				 * of nextfp against minfp/stacktop.  If the
3287c478bd9Sstevel@tonic-gate 				 * high-level interrupt has in turn interrupted
3297c478bd9Sstevel@tonic-gate 				 * a non-passivated interrupt thread, we
3307c478bd9Sstevel@tonic-gate 				 * will execute the below code with non-zero
3317c478bd9Sstevel@tonic-gate 				 * aframes.  We therefore want to assert that
3327c478bd9Sstevel@tonic-gate 				 * aframes is zero _or_ we are in a high-level
3337c478bd9Sstevel@tonic-gate 				 * interrupt -- but because cpu_intr_actv is
3347c478bd9Sstevel@tonic-gate 				 * updated with high-level interrupts enabled,
3357c478bd9Sstevel@tonic-gate 				 * we must reduce this to only asserting that
3367c478bd9Sstevel@tonic-gate 				 * %pil is greater than DISP_LEVEL.
3377c478bd9Sstevel@tonic-gate 				 */
3387c478bd9Sstevel@tonic-gate 				ASSERT(aframes == 0 ||
3397c478bd9Sstevel@tonic-gate 				    dtrace_getipl() > DISP_LEVEL);
3407c478bd9Sstevel@tonic-gate 				pcstack[depth++] = (pc_t)fp->fr_savpc;
3417c478bd9Sstevel@tonic-gate 			}
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 			while (depth < pcstack_limit)
3447c478bd9Sstevel@tonic-gate 				pcstack[depth++] = NULL;
3457c478bd9Sstevel@tonic-gate 			return;
3467c478bd9Sstevel@tonic-gate 		}
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 		if (aframes > 0) {
3497c478bd9Sstevel@tonic-gate 			aframes--;
3507c478bd9Sstevel@tonic-gate 		} else {
3517c478bd9Sstevel@tonic-gate 			pcstack[depth++] = (pc_t)fp->fr_savpc;
3527c478bd9Sstevel@tonic-gate 		}
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 		fp = nextfp;
3557c478bd9Sstevel@tonic-gate 		minfp = fp;
3567c478bd9Sstevel@tonic-gate 	}
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate 
3590b38a8bdSahl static int
3600b38a8bdSahl dtrace_getustack_common(uint64_t *pcstack, int pcstack_limit, uintptr_t sp)
3610b38a8bdSahl {
3620b38a8bdSahl 	proc_t *p = curproc;
3630b38a8bdSahl 	int ret = 0;
3640b38a8bdSahl 
3650b38a8bdSahl 	ASSERT(pcstack == NULL || pcstack_limit > 0);
3660b38a8bdSahl 
3670b38a8bdSahl 	if (p->p_model == DATAMODEL_NATIVE) {
3680b38a8bdSahl 		for (;;) {
3690b38a8bdSahl 			struct frame *fr = (struct frame *)(sp + STACK_BIAS);
3700b38a8bdSahl 			uintptr_t pc;
3710b38a8bdSahl 
3720b38a8bdSahl 			if (sp == 0 || fr == NULL ||
3730b38a8bdSahl 			    !IS_P2ALIGNED((uintptr_t)fr, STACK_ALIGN))
3740b38a8bdSahl 				break;
3750b38a8bdSahl 
3760b38a8bdSahl 			pc = dtrace_fulword(&fr->fr_savpc);
3770b38a8bdSahl 			sp = dtrace_fulword(&fr->fr_savfp);
3780b38a8bdSahl 
3790b38a8bdSahl 			if (pc == 0)
3800b38a8bdSahl 				break;
3810b38a8bdSahl 
3820b38a8bdSahl 			ret++;
3830b38a8bdSahl 
3840b38a8bdSahl 			if (pcstack != NULL) {
3850b38a8bdSahl 				*pcstack++ = pc;
3860b38a8bdSahl 				pcstack_limit--;
3870b38a8bdSahl 				if (pcstack_limit == 0)
3880b38a8bdSahl 					break;
3890b38a8bdSahl 			}
3900b38a8bdSahl 		}
3910b38a8bdSahl 	} else {
392*900524f3Sahl 		/*
393*900524f3Sahl 		 * Truncate the stack pointer to 32-bits as there may be
394*900524f3Sahl 		 * garbage in the upper bits which would normally be ignored
395*900524f3Sahl 		 * by the processor in 32-bit mode.
396*900524f3Sahl 		 */
397*900524f3Sahl 		sp = (uint32_t)sp;
398*900524f3Sahl 
3990b38a8bdSahl 		for (;;) {
4000b38a8bdSahl 			struct frame32 *fr = (struct frame32 *)sp;
4010b38a8bdSahl 			uint32_t pc;
4020b38a8bdSahl 
4030b38a8bdSahl 			if (sp == 0 ||
4040b38a8bdSahl 			    !IS_P2ALIGNED((uintptr_t)fr, STACK_ALIGN32))
4050b38a8bdSahl 				break;
4060b38a8bdSahl 
4070b38a8bdSahl 			pc = dtrace_fuword32(&fr->fr_savpc);
4080b38a8bdSahl 			sp = dtrace_fuword32(&fr->fr_savfp);
4090b38a8bdSahl 
4100b38a8bdSahl 			if (pc == 0)
4110b38a8bdSahl 				break;
4120b38a8bdSahl 
4130b38a8bdSahl 			ret++;
4140b38a8bdSahl 
4150b38a8bdSahl 			if (pcstack != NULL) {
4160b38a8bdSahl 				*pcstack++ = pc;
4170b38a8bdSahl 				pcstack_limit--;
4180b38a8bdSahl 				if (pcstack_limit == 0)
4190b38a8bdSahl 					break;
4200b38a8bdSahl 			}
4210b38a8bdSahl 		}
4220b38a8bdSahl 	}
4230b38a8bdSahl 
4240b38a8bdSahl 	return (ret);
4250b38a8bdSahl }
4260b38a8bdSahl 
4277c478bd9Sstevel@tonic-gate void
4287c478bd9Sstevel@tonic-gate dtrace_getupcstack(uint64_t *pcstack, int pcstack_limit)
4297c478bd9Sstevel@tonic-gate {
4307c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
4310b38a8bdSahl 	proc_t *p = curproc;
4327c478bd9Sstevel@tonic-gate 	struct regs *rp;
4337c478bd9Sstevel@tonic-gate 	uintptr_t sp;
4347c478bd9Sstevel@tonic-gate 	int n;
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	if (pcstack_limit <= 0)
4377c478bd9Sstevel@tonic-gate 		return;
4387c478bd9Sstevel@tonic-gate 
43935b7f6ccSahl 	/*
44035b7f6ccSahl 	 * If there's no user context we still need to zero the stack.
44135b7f6ccSahl 	 */
44235b7f6ccSahl 	if (lwp == NULL || p == NULL || (rp = lwp->lwp_regs) == NULL)
44335b7f6ccSahl 		goto zero;
44435b7f6ccSahl 
4457c478bd9Sstevel@tonic-gate 	*pcstack++ = (uint64_t)p->p_pid;
4467c478bd9Sstevel@tonic-gate 	pcstack_limit--;
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	if (pcstack_limit <= 0)
4497c478bd9Sstevel@tonic-gate 		return;
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	*pcstack++ = (uint64_t)rp->r_pc;
4527c478bd9Sstevel@tonic-gate 	pcstack_limit--;
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	if (pcstack_limit <= 0)
4557c478bd9Sstevel@tonic-gate 		return;
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
4587c478bd9Sstevel@tonic-gate 		*pcstack++ = (uint64_t)rp->r_o7;
4597c478bd9Sstevel@tonic-gate 		pcstack_limit--;
4607c478bd9Sstevel@tonic-gate 		if (pcstack_limit <= 0)
4617c478bd9Sstevel@tonic-gate 			return;
4627c478bd9Sstevel@tonic-gate 	}
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	sp = rp->r_sp;
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	n = dtrace_getupcstack_top(pcstack, pcstack_limit, &sp);
4677c478bd9Sstevel@tonic-gate 	ASSERT(n >= 0);
4687c478bd9Sstevel@tonic-gate 	ASSERT(n <= pcstack_limit);
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	pcstack += n;
4717c478bd9Sstevel@tonic-gate 	pcstack_limit -= n;
4720b38a8bdSahl 	if (pcstack_limit <= 0)
4730b38a8bdSahl 		return;
4747c478bd9Sstevel@tonic-gate 
4750b38a8bdSahl 	n = dtrace_getustack_common(pcstack, pcstack_limit, sp);
4760b38a8bdSahl 	ASSERT(n >= 0);
4770b38a8bdSahl 	ASSERT(n <= pcstack_limit);
4787c478bd9Sstevel@tonic-gate 
4790b38a8bdSahl 	pcstack += n;
4800b38a8bdSahl 	pcstack_limit -= n;
4817c478bd9Sstevel@tonic-gate 
48235b7f6ccSahl zero:
4830b38a8bdSahl 	while (pcstack_limit-- > 0)
4840b38a8bdSahl 		*pcstack++ = NULL;
4850b38a8bdSahl }
4867c478bd9Sstevel@tonic-gate 
4870b38a8bdSahl int
4880b38a8bdSahl dtrace_getustackdepth(void)
4890b38a8bdSahl {
4900b38a8bdSahl 	klwp_t *lwp = ttolwp(curthread);
4910b38a8bdSahl 	proc_t *p = curproc;
4920b38a8bdSahl 	struct regs *rp;
4930b38a8bdSahl 	uintptr_t sp;
4940b38a8bdSahl 	int n = 1;
4957c478bd9Sstevel@tonic-gate 
4960b38a8bdSahl 	if (lwp == NULL || p == NULL || (rp = lwp->lwp_regs) == NULL)
4970b38a8bdSahl 		return (0);
4987c478bd9Sstevel@tonic-gate 
4990b38a8bdSahl 	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT))
5000b38a8bdSahl 		return (-1);
5017c478bd9Sstevel@tonic-gate 
5020b38a8bdSahl 	sp = rp->r_sp;
5037c478bd9Sstevel@tonic-gate 
5040b38a8bdSahl 	n += dtrace_getustackdepth_top(&sp);
5050b38a8bdSahl 	n += dtrace_getustack_common(NULL, 0, sp);
5060b38a8bdSahl 
50735b7f6ccSahl 	/*
50835b7f6ccSahl 	 * Add one more to the stack depth if we're in an entry probe as long
50935b7f6ccSahl 	 * as the return address is non-NULL or there are additional frames
51035b7f6ccSahl 	 * beyond that NULL return address.
51135b7f6ccSahl 	 */
51235b7f6ccSahl 	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY) &&
51335b7f6ccSahl 	    (rp->r_o7 != NULL || n != 1))
51435b7f6ccSahl 		n++;
51535b7f6ccSahl 
5160b38a8bdSahl 	return (n);
5177c478bd9Sstevel@tonic-gate }
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate void
5207c478bd9Sstevel@tonic-gate dtrace_getufpstack(uint64_t *pcstack, uint64_t *fpstack, int pcstack_limit)
5217c478bd9Sstevel@tonic-gate {
5227c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
5237c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
5247c478bd9Sstevel@tonic-gate 	struct regs *rp;
5257c478bd9Sstevel@tonic-gate 	uintptr_t sp;
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	if (pcstack_limit <= 0)
5287c478bd9Sstevel@tonic-gate 		return;
5297c478bd9Sstevel@tonic-gate 
53035b7f6ccSahl 	/*
53135b7f6ccSahl 	 * If there's no user context we still need to zero the stack.
53235b7f6ccSahl 	 */
53335b7f6ccSahl 	if (lwp == NULL || p == NULL || (rp = lwp->lwp_regs) == NULL)
53435b7f6ccSahl 		goto zero;
53535b7f6ccSahl 
5367c478bd9Sstevel@tonic-gate 	*pcstack++ = (uint64_t)p->p_pid;
5377c478bd9Sstevel@tonic-gate 	pcstack_limit--;
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	if (pcstack_limit <= 0)
5407c478bd9Sstevel@tonic-gate 		return;
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
5437c478bd9Sstevel@tonic-gate 		*fpstack++ = 0;
5447c478bd9Sstevel@tonic-gate 		*pcstack++ = (uint64_t)rp->r_pc;
5457c478bd9Sstevel@tonic-gate 		pcstack_limit--;
5467c478bd9Sstevel@tonic-gate 		if (pcstack_limit <= 0)
5477c478bd9Sstevel@tonic-gate 			return;
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 		*fpstack++ = (uint64_t)rp->r_sp;
5507c478bd9Sstevel@tonic-gate 		*pcstack++ = (uint64_t)rp->r_o7;
5517c478bd9Sstevel@tonic-gate 		pcstack_limit--;
5527c478bd9Sstevel@tonic-gate 	} else {
5537c478bd9Sstevel@tonic-gate 		*fpstack++ = (uint64_t)rp->r_sp;
5547c478bd9Sstevel@tonic-gate 		*pcstack++ = (uint64_t)rp->r_pc;
5557c478bd9Sstevel@tonic-gate 		pcstack_limit--;
5567c478bd9Sstevel@tonic-gate 	}
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 	if (pcstack_limit <= 0)
5597c478bd9Sstevel@tonic-gate 		return;
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 	sp = rp->r_sp;
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	dtrace_flush_user_windows();
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	if (p->p_model == DATAMODEL_NATIVE) {
5667c478bd9Sstevel@tonic-gate 		while (pcstack_limit > 0) {
5677c478bd9Sstevel@tonic-gate 			struct frame *fr = (struct frame *)(sp + STACK_BIAS);
5687c478bd9Sstevel@tonic-gate 			uintptr_t pc;
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 			if (sp == 0 || fr == NULL ||
5717c478bd9Sstevel@tonic-gate 			    ((uintptr_t)&fr->fr_savpc & 3) != 0 ||
5727c478bd9Sstevel@tonic-gate 			    ((uintptr_t)&fr->fr_savfp & 3) != 0)
5737c478bd9Sstevel@tonic-gate 				break;
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 			pc = dtrace_fulword(&fr->fr_savpc);
5767c478bd9Sstevel@tonic-gate 			sp = dtrace_fulword(&fr->fr_savfp);
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 			if (pc == 0)
5797c478bd9Sstevel@tonic-gate 				break;
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 			*fpstack++ = sp;
5827c478bd9Sstevel@tonic-gate 			*pcstack++ = pc;
5837c478bd9Sstevel@tonic-gate 			pcstack_limit--;
5847c478bd9Sstevel@tonic-gate 		}
5857c478bd9Sstevel@tonic-gate 	} else {
586*900524f3Sahl 		/*
587*900524f3Sahl 		 * Truncate the stack pointer to 32-bits as there may be
588*900524f3Sahl 		 * garbage in the upper bits which would normally be ignored
589*900524f3Sahl 		 * by the processor in 32-bit mode.
590*900524f3Sahl 		 */
591*900524f3Sahl 		sp = (uint32_t)sp;
592*900524f3Sahl 
5937c478bd9Sstevel@tonic-gate 		while (pcstack_limit > 0) {
5947c478bd9Sstevel@tonic-gate 			struct frame32 *fr = (struct frame32 *)sp;
5957c478bd9Sstevel@tonic-gate 			uint32_t pc;
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 			if (sp == 0 ||
5987c478bd9Sstevel@tonic-gate 			    ((uintptr_t)&fr->fr_savpc & 3) != 0 ||
5997c478bd9Sstevel@tonic-gate 			    ((uintptr_t)&fr->fr_savfp & 3) != 0)
6007c478bd9Sstevel@tonic-gate 				break;
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 			pc = dtrace_fuword32(&fr->fr_savpc);
6037c478bd9Sstevel@tonic-gate 			sp = dtrace_fuword32(&fr->fr_savfp);
6047c478bd9Sstevel@tonic-gate 
6050b38a8bdSahl 			if (pc == 0)
6060b38a8bdSahl 				break;
6070b38a8bdSahl 
6087c478bd9Sstevel@tonic-gate 			*fpstack++ = sp;
6097c478bd9Sstevel@tonic-gate 			*pcstack++ = pc;
6107c478bd9Sstevel@tonic-gate 			pcstack_limit--;
6117c478bd9Sstevel@tonic-gate 		}
6127c478bd9Sstevel@tonic-gate 	}
6137c478bd9Sstevel@tonic-gate 
61435b7f6ccSahl zero:
6157c478bd9Sstevel@tonic-gate 	while (pcstack_limit-- > 0)
6167c478bd9Sstevel@tonic-gate 		*pcstack++ = NULL;
6177c478bd9Sstevel@tonic-gate }
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate uint64_t
6207c478bd9Sstevel@tonic-gate dtrace_getarg(int arg, int aframes)
6217c478bd9Sstevel@tonic-gate {
6227c478bd9Sstevel@tonic-gate 	uintptr_t val;
6237c478bd9Sstevel@tonic-gate 	struct frame *fp;
6247c478bd9Sstevel@tonic-gate 	uint64_t rval;
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	/*
6277c478bd9Sstevel@tonic-gate 	 * Account for the fact that dtrace_getarg() consumes an additional
6287c478bd9Sstevel@tonic-gate 	 * stack frame.
6297c478bd9Sstevel@tonic-gate 	 */
6307c478bd9Sstevel@tonic-gate 	aframes++;
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	if (arg < 6) {
6337c478bd9Sstevel@tonic-gate 		if (dtrace_fish(aframes, DTRACE_REG_I0 + arg, &val) == 0)
6347c478bd9Sstevel@tonic-gate 			return (val);
6357c478bd9Sstevel@tonic-gate 	} else {
6367c478bd9Sstevel@tonic-gate 		if (dtrace_fish(aframes, DTRACE_REG_I6, &val) == 0) {
6377c478bd9Sstevel@tonic-gate 			/*
6387c478bd9Sstevel@tonic-gate 			 * We have a stack pointer; grab the argument.
6397c478bd9Sstevel@tonic-gate 			 */
6407c478bd9Sstevel@tonic-gate 			fp = (struct frame *)(val + STACK_BIAS);
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
6437c478bd9Sstevel@tonic-gate 			rval = fp->fr_argx[arg - 6];
6447c478bd9Sstevel@tonic-gate 			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 			return (rval);
6477c478bd9Sstevel@tonic-gate 		}
6487c478bd9Sstevel@tonic-gate 	}
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	/*
6517c478bd9Sstevel@tonic-gate 	 * There are other ways to do this.  But the slow, painful way works
6527c478bd9Sstevel@tonic-gate 	 * just fine.  Because this requires some loads, we need to set
6537c478bd9Sstevel@tonic-gate 	 * CPU_DTRACE_NOFAULT to protect against looking for an argument that
6547c478bd9Sstevel@tonic-gate 	 * isn't there.
6557c478bd9Sstevel@tonic-gate 	 */
6567c478bd9Sstevel@tonic-gate 	fp = (struct frame *)((caddr_t)dtrace_getfp() + STACK_BIAS);
6577c478bd9Sstevel@tonic-gate 	dtrace_flush_windows();
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	for (aframes -= 1; aframes; aframes--)
6627c478bd9Sstevel@tonic-gate 		fp = (struct frame *)((caddr_t)fp->fr_savfp + STACK_BIAS);
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	if (arg < 6) {
6657c478bd9Sstevel@tonic-gate 		rval = fp->fr_arg[arg];
6667c478bd9Sstevel@tonic-gate 	} else {
6677c478bd9Sstevel@tonic-gate 		fp = (struct frame *)((caddr_t)fp->fr_savfp + STACK_BIAS);
6687c478bd9Sstevel@tonic-gate 		rval = fp->fr_argx[arg - 6];
6697c478bd9Sstevel@tonic-gate 	}
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	return (rval);
6747c478bd9Sstevel@tonic-gate }
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate int
6777c478bd9Sstevel@tonic-gate dtrace_getstackdepth(int aframes)
6787c478bd9Sstevel@tonic-gate {
6797c478bd9Sstevel@tonic-gate 	struct frame *fp, *nextfp, *minfp, *stacktop;
6807c478bd9Sstevel@tonic-gate 	int depth = 0;
6817c478bd9Sstevel@tonic-gate 	int on_intr;
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	fp = (struct frame *)((caddr_t)dtrace_getfp() + STACK_BIAS);
6847c478bd9Sstevel@tonic-gate 	dtrace_flush_windows();
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	if ((on_intr = CPU_ON_INTR(CPU)) != 0)
6877c478bd9Sstevel@tonic-gate 		stacktop = (struct frame *)CPU->cpu_intr_stack + SA(MINFRAME);
6887c478bd9Sstevel@tonic-gate 	else
6897c478bd9Sstevel@tonic-gate 		stacktop = (struct frame *)curthread->t_stk;
6907c478bd9Sstevel@tonic-gate 	minfp = fp;
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	for (;;) {
6937c478bd9Sstevel@tonic-gate 		nextfp = (struct frame *)((caddr_t)fp->fr_savfp + STACK_BIAS);
6947c478bd9Sstevel@tonic-gate 		if (nextfp <= minfp || nextfp >= stacktop) {
6957c478bd9Sstevel@tonic-gate 			if (on_intr) {
6967c478bd9Sstevel@tonic-gate 				/*
6977c478bd9Sstevel@tonic-gate 				 * Hop from interrupt stack to thread stack.
6987c478bd9Sstevel@tonic-gate 				 */
6997c478bd9Sstevel@tonic-gate 				stacktop = (struct frame *)curthread->t_stk;
7007c478bd9Sstevel@tonic-gate 				minfp = (struct frame *)curthread->t_stkbase;
7017c478bd9Sstevel@tonic-gate 				on_intr = 0;
7027c478bd9Sstevel@tonic-gate 				continue;
7037c478bd9Sstevel@tonic-gate 			}
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 			return (++depth);
7067c478bd9Sstevel@tonic-gate 		}
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 		if (aframes > 0) {
7097c478bd9Sstevel@tonic-gate 			aframes--;
7107c478bd9Sstevel@tonic-gate 		} else {
7117c478bd9Sstevel@tonic-gate 			depth++;
7127c478bd9Sstevel@tonic-gate 		}
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 		fp = nextfp;
7157c478bd9Sstevel@tonic-gate 		minfp = fp;
7167c478bd9Sstevel@tonic-gate 	}
7177c478bd9Sstevel@tonic-gate }
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate /*
7207c478bd9Sstevel@tonic-gate  * This uses the same register numbering scheme as in sys/procfs_isa.h.
7217c478bd9Sstevel@tonic-gate  */
7227c478bd9Sstevel@tonic-gate ulong_t
7237c478bd9Sstevel@tonic-gate dtrace_getreg(struct regs *rp, uint_t reg)
7247c478bd9Sstevel@tonic-gate {
7257c478bd9Sstevel@tonic-gate 	ulong_t value;
7267c478bd9Sstevel@tonic-gate 	uintptr_t fp;
7277c478bd9Sstevel@tonic-gate 	struct machpcb *mpcb;
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	if (reg == R_G0)
7307c478bd9Sstevel@tonic-gate 		return (0);
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	if (reg <= R_G7)
7337c478bd9Sstevel@tonic-gate 		return ((&rp->r_g1)[reg - 1]);
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 	if (reg > R_I7) {
7367c478bd9Sstevel@tonic-gate 		switch (reg) {
7377c478bd9Sstevel@tonic-gate 		case R_CCR:
7387c478bd9Sstevel@tonic-gate 			return ((rp->r_tstate >> TSTATE_CCR_SHIFT) &
7397c478bd9Sstevel@tonic-gate 			    TSTATE_CCR_MASK);
7407c478bd9Sstevel@tonic-gate 		case R_PC:
7417c478bd9Sstevel@tonic-gate 			return (rp->r_pc);
7427c478bd9Sstevel@tonic-gate 		case R_nPC:
7437c478bd9Sstevel@tonic-gate 			return (rp->r_npc);
7447c478bd9Sstevel@tonic-gate 		case R_Y:
7457c478bd9Sstevel@tonic-gate 			return (rp->r_y);
7467c478bd9Sstevel@tonic-gate 		case R_ASI:
7477c478bd9Sstevel@tonic-gate 			return ((rp->r_tstate >> TSTATE_ASI_SHIFT) &
7487c478bd9Sstevel@tonic-gate 			    TSTATE_ASI_MASK);
7497c478bd9Sstevel@tonic-gate 		case R_FPRS:
7507c478bd9Sstevel@tonic-gate 			return (dtrace_getfprs());
7517c478bd9Sstevel@tonic-gate 		default:
7527c478bd9Sstevel@tonic-gate 			DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
7537c478bd9Sstevel@tonic-gate 			return (0);
7547c478bd9Sstevel@tonic-gate 		}
7557c478bd9Sstevel@tonic-gate 	}
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	/*
7587c478bd9Sstevel@tonic-gate 	 * We reach go to the fake restore case if the probe we hit was a pid
7597c478bd9Sstevel@tonic-gate 	 * return probe on a restore instruction. We partially emulate the
7607c478bd9Sstevel@tonic-gate 	 * restore in the kernel and then execute a simple restore
7617c478bd9Sstevel@tonic-gate 	 * instruction that we've secreted away to do the actual register
7627c478bd9Sstevel@tonic-gate 	 * window manipulation. We need to go one register window further
7637c478bd9Sstevel@tonic-gate 	 * down to get at the %ls, and %is and we need to treat %os like %is
7647c478bd9Sstevel@tonic-gate 	 * to pull them out of the topmost user frame.
7657c478bd9Sstevel@tonic-gate 	 */
7667c478bd9Sstevel@tonic-gate 	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAKERESTORE)) {
7677c478bd9Sstevel@tonic-gate 		if (reg > R_O7)
7687c478bd9Sstevel@tonic-gate 			goto fake_restore;
7697c478bd9Sstevel@tonic-gate 		else
7707c478bd9Sstevel@tonic-gate 			reg += R_I0 - R_O0;
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	} else if (reg <= R_O7) {
7737c478bd9Sstevel@tonic-gate 		return ((&rp->r_g1)[reg - 1]);
7747c478bd9Sstevel@tonic-gate 	}
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	if (dtrace_getotherwin() > 0)
7777c478bd9Sstevel@tonic-gate 		return (dtrace_getreg_win(reg, 1));
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	mpcb = (struct machpcb *)((caddr_t)rp - REGOFF);
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 	if (curproc->p_model == DATAMODEL_NATIVE) {
7827c478bd9Sstevel@tonic-gate 		struct frame *fr = (void *)(rp->r_sp + STACK_BIAS);
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 		if (mpcb->mpcb_wbcnt > 0) {
7857c478bd9Sstevel@tonic-gate 			struct rwindow *rwin = (void *)mpcb->mpcb_wbuf;
7867c478bd9Sstevel@tonic-gate 			int i = mpcb->mpcb_wbcnt;
7877c478bd9Sstevel@tonic-gate 			do {
7887c478bd9Sstevel@tonic-gate 				i--;
7897c478bd9Sstevel@tonic-gate 				if ((long)mpcb->mpcb_spbuf[i] == rp->r_sp)
7907c478bd9Sstevel@tonic-gate 					return (rwin[i].rw_local[reg - 16]);
7917c478bd9Sstevel@tonic-gate 			} while (i > 0);
7927c478bd9Sstevel@tonic-gate 		}
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
7957c478bd9Sstevel@tonic-gate 		value = dtrace_fulword(&fr->fr_local[reg - 16]);
7967c478bd9Sstevel@tonic-gate 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
7977c478bd9Sstevel@tonic-gate 	} else {
79875521904Sraf 		struct frame32 *fr = (void *)(uintptr_t)(caddr32_t)rp->r_sp;
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 		if (mpcb->mpcb_wbcnt > 0) {
8017c478bd9Sstevel@tonic-gate 			struct rwindow32 *rwin = (void *)mpcb->mpcb_wbuf;
8027c478bd9Sstevel@tonic-gate 			int i = mpcb->mpcb_wbcnt;
8037c478bd9Sstevel@tonic-gate 			do {
8047c478bd9Sstevel@tonic-gate 				i--;
8057c478bd9Sstevel@tonic-gate 				if ((long)mpcb->mpcb_spbuf[i] == rp->r_sp)
8067c478bd9Sstevel@tonic-gate 					return (rwin[i].rw_local[reg - 16]);
8077c478bd9Sstevel@tonic-gate 			} while (i > 0);
8087c478bd9Sstevel@tonic-gate 		}
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
8117c478bd9Sstevel@tonic-gate 		value = dtrace_fuword32(&fr->fr_local[reg - 16]);
8127c478bd9Sstevel@tonic-gate 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
8137c478bd9Sstevel@tonic-gate 	}
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 	return (value);
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate fake_restore:
8187c478bd9Sstevel@tonic-gate 	ASSERT(R_L0 <= reg && reg <= R_I7);
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	/*
8217c478bd9Sstevel@tonic-gate 	 * We first look two user windows down to see if we can dig out
8227c478bd9Sstevel@tonic-gate 	 * the register we're looking for.
8237c478bd9Sstevel@tonic-gate 	 */
8247c478bd9Sstevel@tonic-gate 	if (dtrace_getotherwin() > 1)
8257c478bd9Sstevel@tonic-gate 		return (dtrace_getreg_win(reg, 2));
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	/*
8287c478bd9Sstevel@tonic-gate 	 * First we need to get the frame pointer and then we perform
8297c478bd9Sstevel@tonic-gate 	 * the same computation as in the non-fake-o-restore case.
8307c478bd9Sstevel@tonic-gate 	 */
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	mpcb = (struct machpcb *)((caddr_t)rp - REGOFF);
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 	if (dtrace_getotherwin() > 0) {
8357c478bd9Sstevel@tonic-gate 		fp = dtrace_getreg_win(R_FP, 1);
8367c478bd9Sstevel@tonic-gate 		goto got_fp;
8377c478bd9Sstevel@tonic-gate 	}
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 	if (curproc->p_model == DATAMODEL_NATIVE) {
8407c478bd9Sstevel@tonic-gate 		struct frame *fr = (void *)(rp->r_sp + STACK_BIAS);
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 		if (mpcb->mpcb_wbcnt > 0) {
8437c478bd9Sstevel@tonic-gate 			struct rwindow *rwin = (void *)mpcb->mpcb_wbuf;
8447c478bd9Sstevel@tonic-gate 			int i = mpcb->mpcb_wbcnt;
8457c478bd9Sstevel@tonic-gate 			do {
8467c478bd9Sstevel@tonic-gate 				i--;
8477c478bd9Sstevel@tonic-gate 				if ((long)mpcb->mpcb_spbuf[i] == rp->r_sp) {
8487c478bd9Sstevel@tonic-gate 					fp = rwin[i].rw_fp;
8497c478bd9Sstevel@tonic-gate 					goto got_fp;
8507c478bd9Sstevel@tonic-gate 				}
8517c478bd9Sstevel@tonic-gate 			} while (i > 0);
8527c478bd9Sstevel@tonic-gate 		}
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
8557c478bd9Sstevel@tonic-gate 		fp = dtrace_fulword(&fr->fr_savfp);
8567c478bd9Sstevel@tonic-gate 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
8577c478bd9Sstevel@tonic-gate 		if (cpu_core[CPU->cpu_id].cpuc_dtrace_flags & CPU_DTRACE_FAULT)
8587c478bd9Sstevel@tonic-gate 			return (0);
8597c478bd9Sstevel@tonic-gate 	} else {
86075521904Sraf 		struct frame32 *fr = (void *)(uintptr_t)(caddr32_t)rp->r_sp;
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 		if (mpcb->mpcb_wbcnt > 0) {
8637c478bd9Sstevel@tonic-gate 			struct rwindow32 *rwin = (void *)mpcb->mpcb_wbuf;
8647c478bd9Sstevel@tonic-gate 			int i = mpcb->mpcb_wbcnt;
8657c478bd9Sstevel@tonic-gate 			do {
8667c478bd9Sstevel@tonic-gate 				i--;
8677c478bd9Sstevel@tonic-gate 				if ((long)mpcb->mpcb_spbuf[i] == rp->r_sp) {
8687c478bd9Sstevel@tonic-gate 					fp = rwin[i].rw_fp;
8697c478bd9Sstevel@tonic-gate 					goto got_fp;
8707c478bd9Sstevel@tonic-gate 				}
8717c478bd9Sstevel@tonic-gate 			} while (i > 0);
8727c478bd9Sstevel@tonic-gate 		}
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
8757c478bd9Sstevel@tonic-gate 		fp = dtrace_fuword32(&fr->fr_savfp);
8767c478bd9Sstevel@tonic-gate 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
8777c478bd9Sstevel@tonic-gate 		if (cpu_core[CPU->cpu_id].cpuc_dtrace_flags & CPU_DTRACE_FAULT)
8787c478bd9Sstevel@tonic-gate 			return (0);
8797c478bd9Sstevel@tonic-gate 	}
8807c478bd9Sstevel@tonic-gate got_fp:
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 	if (curproc->p_model == DATAMODEL_NATIVE) {
8837c478bd9Sstevel@tonic-gate 		struct frame *fr = (void *)(fp + STACK_BIAS);
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 		if (mpcb->mpcb_wbcnt > 0) {
8867c478bd9Sstevel@tonic-gate 			struct rwindow *rwin = (void *)mpcb->mpcb_wbuf;
8877c478bd9Sstevel@tonic-gate 			int i = mpcb->mpcb_wbcnt;
8887c478bd9Sstevel@tonic-gate 			do {
8897c478bd9Sstevel@tonic-gate 				i--;
8907c478bd9Sstevel@tonic-gate 				if ((long)mpcb->mpcb_spbuf[i] == fp)
8917c478bd9Sstevel@tonic-gate 					return (rwin[i].rw_local[reg - 16]);
8927c478bd9Sstevel@tonic-gate 			} while (i > 0);
8937c478bd9Sstevel@tonic-gate 		}
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
8967c478bd9Sstevel@tonic-gate 		value = dtrace_fulword(&fr->fr_local[reg - 16]);
8977c478bd9Sstevel@tonic-gate 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
8987c478bd9Sstevel@tonic-gate 	} else {
89975521904Sraf 		struct frame32 *fr = (void *)(uintptr_t)(caddr32_t)fp;
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 		if (mpcb->mpcb_wbcnt > 0) {
9027c478bd9Sstevel@tonic-gate 			struct rwindow32 *rwin = (void *)mpcb->mpcb_wbuf;
9037c478bd9Sstevel@tonic-gate 			int i = mpcb->mpcb_wbcnt;
9047c478bd9Sstevel@tonic-gate 			do {
9057c478bd9Sstevel@tonic-gate 				i--;
9067c478bd9Sstevel@tonic-gate 				if ((long)mpcb->mpcb_spbuf[i] == fp)
9077c478bd9Sstevel@tonic-gate 					return (rwin[i].rw_local[reg - 16]);
9087c478bd9Sstevel@tonic-gate 			} while (i > 0);
9097c478bd9Sstevel@tonic-gate 		}
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
9127c478bd9Sstevel@tonic-gate 		value = dtrace_fuword32(&fr->fr_local[reg - 16]);
9137c478bd9Sstevel@tonic-gate 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
9147c478bd9Sstevel@tonic-gate 	}
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 	return (value);
9177c478bd9Sstevel@tonic-gate }
918