1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/dtrace_impl.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/model.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/frame.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/stack.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/machpcb.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/procfs_isa.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate #define	DTRACE_FMT3OP3_MASK	0x81000000
39*7c478bd9Sstevel@tonic-gate #define	DTRACE_FMT3OP3		0x80000000
40*7c478bd9Sstevel@tonic-gate #define	DTRACE_FMT3RS1_SHIFT	14
41*7c478bd9Sstevel@tonic-gate #define	DTRACE_FMT3RD_SHIFT	25
42*7c478bd9Sstevel@tonic-gate #define	DTRACE_RMASK		0x1f
43*7c478bd9Sstevel@tonic-gate #define	DTRACE_REG_L0		16
44*7c478bd9Sstevel@tonic-gate #define	DTRACE_REG_O7		15
45*7c478bd9Sstevel@tonic-gate #define	DTRACE_REG_I0		24
46*7c478bd9Sstevel@tonic-gate #define	DTRACE_REG_I6		30
47*7c478bd9Sstevel@tonic-gate #define	DTRACE_RET		0x81c7e008
48*7c478bd9Sstevel@tonic-gate #define	DTRACE_RETL		0x81c3e008
49*7c478bd9Sstevel@tonic-gate #define	DTRACE_SAVE_MASK	0xc1f80000
50*7c478bd9Sstevel@tonic-gate #define	DTRACE_SAVE		0x81e00000
51*7c478bd9Sstevel@tonic-gate #define	DTRACE_RESTORE		0x81e80000
52*7c478bd9Sstevel@tonic-gate #define	DTRACE_CALL_MASK	0xc0000000
53*7c478bd9Sstevel@tonic-gate #define	DTRACE_CALL		0x40000000
54*7c478bd9Sstevel@tonic-gate #define	DTRACE_JMPL_MASK	0x81f10000
55*7c478bd9Sstevel@tonic-gate #define	DTRACE_JMPL		0x81c00000
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate extern int dtrace_getupcstack_top(uint64_t *, int, uintptr_t *);
58*7c478bd9Sstevel@tonic-gate extern ulong_t dtrace_getreg_win(uint_t, uint_t);
59*7c478bd9Sstevel@tonic-gate extern void dtrace_putreg_win(uint_t, ulong_t);
60*7c478bd9Sstevel@tonic-gate extern int dtrace_fish(int, int, uintptr_t *);
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate /*
63*7c478bd9Sstevel@tonic-gate  * This is similar in principle to getpcstack(), but there are several marked
64*7c478bd9Sstevel@tonic-gate  * differences in implementation:
65*7c478bd9Sstevel@tonic-gate  *
66*7c478bd9Sstevel@tonic-gate  * (a)	dtrace_getpcstack() is called from probe context.  Thus, the call
67*7c478bd9Sstevel@tonic-gate  *	to flush_windows() from getpcstack() is a call to the probe-safe
68*7c478bd9Sstevel@tonic-gate  *	equivalent here.
69*7c478bd9Sstevel@tonic-gate  *
70*7c478bd9Sstevel@tonic-gate  * (b)  dtrace_getpcstack() is willing to sacrifice some performance to get
71*7c478bd9Sstevel@tonic-gate  *	a correct stack.  While consumers of getpcstack() are largely
72*7c478bd9Sstevel@tonic-gate  *	subsystem-specific in-kernel debugging facilities, DTrace consumers
73*7c478bd9Sstevel@tonic-gate  *	are arbitrary user-level analysis tools; dtrace_getpcstack() must
74*7c478bd9Sstevel@tonic-gate  *	deliver as correct a stack as possible.  Details on the issues
75*7c478bd9Sstevel@tonic-gate  *	surrounding stack correctness are found below.
76*7c478bd9Sstevel@tonic-gate  *
77*7c478bd9Sstevel@tonic-gate  * (c)	dtrace_getpcstack() _always_ fills in pstack_limit pc_t's -- filling
78*7c478bd9Sstevel@tonic-gate  *	in the difference between the stack depth and pstack_limit with NULLs.
79*7c478bd9Sstevel@tonic-gate  *	Due to this behavior dtrace_getpcstack() returns void.
80*7c478bd9Sstevel@tonic-gate  *
81*7c478bd9Sstevel@tonic-gate  * (d)	dtrace_getpcstack() takes a third parameter, aframes, that
82*7c478bd9Sstevel@tonic-gate  *	denotes the number of _artificial frames_ on the bottom of the
83*7c478bd9Sstevel@tonic-gate  *	stack.  An artificial frame is one induced by the provider; all
84*7c478bd9Sstevel@tonic-gate  *	artificial frames are stripped off before frames are stored to
85*7c478bd9Sstevel@tonic-gate  *	pcstack.
86*7c478bd9Sstevel@tonic-gate  *
87*7c478bd9Sstevel@tonic-gate  * (e)	dtrace_getpcstack() takes a fourth parameter, pc, that indicates
88*7c478bd9Sstevel@tonic-gate  *	an interrupted program counter (if any).  This should be a non-NULL
89*7c478bd9Sstevel@tonic-gate  *	value if and only if the hit probe is unanchored.  (Anchored probes
90*7c478bd9Sstevel@tonic-gate  *	don't fire through an interrupt source.)  This parameter is used to
91*7c478bd9Sstevel@tonic-gate  *	assure (b), above.
92*7c478bd9Sstevel@tonic-gate  */
93*7c478bd9Sstevel@tonic-gate void
94*7c478bd9Sstevel@tonic-gate dtrace_getpcstack(pc_t *pcstack, int pcstack_limit, int aframes, uint32_t *pc)
95*7c478bd9Sstevel@tonic-gate {
96*7c478bd9Sstevel@tonic-gate 	struct frame *fp, *nextfp, *minfp, *stacktop;
97*7c478bd9Sstevel@tonic-gate 	int depth = 0;
98*7c478bd9Sstevel@tonic-gate 	int on_intr, j = 0;
99*7c478bd9Sstevel@tonic-gate 	uint32_t i, r;
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	fp = (struct frame *)((caddr_t)dtrace_getfp() + STACK_BIAS);
102*7c478bd9Sstevel@tonic-gate 	dtrace_flush_windows();
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 	if (pc != NULL) {
105*7c478bd9Sstevel@tonic-gate 		/*
106*7c478bd9Sstevel@tonic-gate 		 * If we've been passed a non-NULL pc, we need to determine
107*7c478bd9Sstevel@tonic-gate 		 * whether or not the specified program counter falls in a leaf
108*7c478bd9Sstevel@tonic-gate 		 * function.  If it falls within a leaf function, we know that
109*7c478bd9Sstevel@tonic-gate 		 * %o7 is valid in its frame (and we can just drive on).  If
110*7c478bd9Sstevel@tonic-gate 		 * it's a non-leaf, however, we know that %o7 is garbage in the
111*7c478bd9Sstevel@tonic-gate 		 * bottom frame.  To trim this frame, we simply increment
112*7c478bd9Sstevel@tonic-gate 		 * aframes and drop into the stack-walking loop.
113*7c478bd9Sstevel@tonic-gate 		 *
114*7c478bd9Sstevel@tonic-gate 		 * To quickly determine if the specified program counter is in
115*7c478bd9Sstevel@tonic-gate 		 * a leaf function, we exploit the fact that leaf functions
116*7c478bd9Sstevel@tonic-gate 		 * tend to be short and non-leaf functions tend to frequently
117*7c478bd9Sstevel@tonic-gate 		 * perform operations that are only permitted in a non-leaf
118*7c478bd9Sstevel@tonic-gate 		 * function (e.g., using the %i's or %l's; calling a function;
119*7c478bd9Sstevel@tonic-gate 		 * performing a restore).  We exploit these tendencies by
120*7c478bd9Sstevel@tonic-gate 		 * simply scanning forward from the specified %pc -- if we see
121*7c478bd9Sstevel@tonic-gate 		 * an operation only permitted in a non-leaf, we know we're in
122*7c478bd9Sstevel@tonic-gate 		 * a non-leaf; if we see a retl, we know we're in a leaf.
123*7c478bd9Sstevel@tonic-gate 		 * Fortunately, one need not perform anywhere near full
124*7c478bd9Sstevel@tonic-gate 		 * disassembly to effectively determine the former: determining
125*7c478bd9Sstevel@tonic-gate 		 * that an instruction is a format-3 instruction and decoding
126*7c478bd9Sstevel@tonic-gate 		 * its rd and rs1 fields, for example, requires very little
127*7c478bd9Sstevel@tonic-gate 		 * manipulation.  Overall, this method of leaf determination
128*7c478bd9Sstevel@tonic-gate 		 * performs quite well:  on average, we only examine between
129*7c478bd9Sstevel@tonic-gate 		 * 1.5 and 2.5 instructions before making the determination.
130*7c478bd9Sstevel@tonic-gate 		 * (Outliers do exist, however; of note is the non-leaf
131*7c478bd9Sstevel@tonic-gate 		 * function ip_sioctl_not_ours() which -- as of this writing --
132*7c478bd9Sstevel@tonic-gate 		 * has a whopping 455 straight instructions that manipulate
133*7c478bd9Sstevel@tonic-gate 		 * only %g's and %o's.)
134*7c478bd9Sstevel@tonic-gate 		 */
135*7c478bd9Sstevel@tonic-gate 		int delay = 0;
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 		if (depth < pcstack_limit)
138*7c478bd9Sstevel@tonic-gate 			pcstack[depth++] = (pc_t)pc;
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 		for (;;) {
141*7c478bd9Sstevel@tonic-gate 			i = pc[j++];
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 			if ((i & DTRACE_FMT3OP3_MASK) == DTRACE_FMT3OP3) {
144*7c478bd9Sstevel@tonic-gate 				/*
145*7c478bd9Sstevel@tonic-gate 				 * This is a format-3 instruction.  We can
146*7c478bd9Sstevel@tonic-gate 				 * look at rd and rs1.
147*7c478bd9Sstevel@tonic-gate 				 */
148*7c478bd9Sstevel@tonic-gate 				r = (i >> DTRACE_FMT3RS1_SHIFT) & DTRACE_RMASK;
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 				if (r >= DTRACE_REG_L0)
151*7c478bd9Sstevel@tonic-gate 					goto nonleaf;
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 				r = (i >> DTRACE_FMT3RD_SHIFT) & DTRACE_RMASK;
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 				if (r >= DTRACE_REG_L0)
156*7c478bd9Sstevel@tonic-gate 					goto nonleaf;
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate 				if ((i & DTRACE_JMPL_MASK) == DTRACE_JMPL) {
159*7c478bd9Sstevel@tonic-gate 					delay = 1;
160*7c478bd9Sstevel@tonic-gate 					continue;
161*7c478bd9Sstevel@tonic-gate 				}
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 				/*
164*7c478bd9Sstevel@tonic-gate 				 * If we see explicit manipulation with %o7
165*7c478bd9Sstevel@tonic-gate 				 * as a destination register, we know that
166*7c478bd9Sstevel@tonic-gate 				 * %o7 is likely bogus -- and we treat this
167*7c478bd9Sstevel@tonic-gate 				 * function as a non-leaf.
168*7c478bd9Sstevel@tonic-gate 				 */
169*7c478bd9Sstevel@tonic-gate 				if (r == DTRACE_REG_O7) {
170*7c478bd9Sstevel@tonic-gate 					if (delay)
171*7c478bd9Sstevel@tonic-gate 						goto leaf;
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 					i &= DTRACE_JMPL_MASK;
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate 					if (i == DTRACE_JMPL) {
176*7c478bd9Sstevel@tonic-gate 						delay = 1;
177*7c478bd9Sstevel@tonic-gate 						continue;
178*7c478bd9Sstevel@tonic-gate 					}
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 					goto nonleaf;
181*7c478bd9Sstevel@tonic-gate 				}
182*7c478bd9Sstevel@tonic-gate 			} else {
183*7c478bd9Sstevel@tonic-gate 				/*
184*7c478bd9Sstevel@tonic-gate 				 * If this is a call, it may or may not be
185*7c478bd9Sstevel@tonic-gate 				 * a leaf; we need to check the delay slot.
186*7c478bd9Sstevel@tonic-gate 				 */
187*7c478bd9Sstevel@tonic-gate 				if ((i & DTRACE_CALL_MASK) == DTRACE_CALL) {
188*7c478bd9Sstevel@tonic-gate 					delay = 1;
189*7c478bd9Sstevel@tonic-gate 					continue;
190*7c478bd9Sstevel@tonic-gate 				}
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 				/*
193*7c478bd9Sstevel@tonic-gate 				 * If we see a ret it's not a leaf; if we
194*7c478bd9Sstevel@tonic-gate 				 * see a retl, it is a leaf.
195*7c478bd9Sstevel@tonic-gate 				 */
196*7c478bd9Sstevel@tonic-gate 				if (i == DTRACE_RET)
197*7c478bd9Sstevel@tonic-gate 					goto nonleaf;
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate 				if (i == DTRACE_RETL)
200*7c478bd9Sstevel@tonic-gate 					goto leaf;
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate 				/*
203*7c478bd9Sstevel@tonic-gate 				 * Finally, if it's a save, it should be
204*7c478bd9Sstevel@tonic-gate 				 * treated as a leaf; if it's a restore it
205*7c478bd9Sstevel@tonic-gate 				 * should not be treated as a leaf.
206*7c478bd9Sstevel@tonic-gate 				 */
207*7c478bd9Sstevel@tonic-gate 				if ((i & DTRACE_SAVE_MASK) == DTRACE_SAVE)
208*7c478bd9Sstevel@tonic-gate 					goto leaf;
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate 				if ((i & DTRACE_SAVE_MASK) == DTRACE_RESTORE)
211*7c478bd9Sstevel@tonic-gate 					goto nonleaf;
212*7c478bd9Sstevel@tonic-gate 			}
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate 			if (delay) {
215*7c478bd9Sstevel@tonic-gate 				/*
216*7c478bd9Sstevel@tonic-gate 				 * If this was a delay slot instruction and
217*7c478bd9Sstevel@tonic-gate 				 * we didn't pick it up elsewhere, this is a
218*7c478bd9Sstevel@tonic-gate 				 * non-leaf.
219*7c478bd9Sstevel@tonic-gate 				 */
220*7c478bd9Sstevel@tonic-gate 				goto nonleaf;
221*7c478bd9Sstevel@tonic-gate 			}
222*7c478bd9Sstevel@tonic-gate 		}
223*7c478bd9Sstevel@tonic-gate nonleaf:
224*7c478bd9Sstevel@tonic-gate 		aframes++;
225*7c478bd9Sstevel@tonic-gate leaf:
226*7c478bd9Sstevel@tonic-gate 		;
227*7c478bd9Sstevel@tonic-gate 	}
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate 	if ((on_intr = CPU_ON_INTR(CPU)) != 0)
230*7c478bd9Sstevel@tonic-gate 		stacktop = (struct frame *)(CPU->cpu_intr_stack + SA(MINFRAME));
231*7c478bd9Sstevel@tonic-gate 	else
232*7c478bd9Sstevel@tonic-gate 		stacktop = (struct frame *)curthread->t_stk;
233*7c478bd9Sstevel@tonic-gate 	minfp = fp;
234*7c478bd9Sstevel@tonic-gate 
235*7c478bd9Sstevel@tonic-gate 	while (depth < pcstack_limit) {
236*7c478bd9Sstevel@tonic-gate 		nextfp = (struct frame *)((caddr_t)fp->fr_savfp + STACK_BIAS);
237*7c478bd9Sstevel@tonic-gate 		if (nextfp <= minfp || nextfp >= stacktop) {
238*7c478bd9Sstevel@tonic-gate 			if (!on_intr && nextfp == stacktop && aframes != 0) {
239*7c478bd9Sstevel@tonic-gate 				/*
240*7c478bd9Sstevel@tonic-gate 				 * If we are exactly at the top of the stack
241*7c478bd9Sstevel@tonic-gate 				 * with a non-zero number of artificial frames,
242*7c478bd9Sstevel@tonic-gate 				 * it must be that the stack is filled with
243*7c478bd9Sstevel@tonic-gate 				 * nothing _but_ artificial frames.  In this
244*7c478bd9Sstevel@tonic-gate 				 * case, we assert that this is so, zero
245*7c478bd9Sstevel@tonic-gate 				 * pcstack, and return.
246*7c478bd9Sstevel@tonic-gate 				 */
247*7c478bd9Sstevel@tonic-gate 				ASSERT(aframes == 1);
248*7c478bd9Sstevel@tonic-gate 				ASSERT(depth == 0);
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate 				while (depth < pcstack_limit)
251*7c478bd9Sstevel@tonic-gate 					pcstack[depth++] = NULL;
252*7c478bd9Sstevel@tonic-gate 				return;
253*7c478bd9Sstevel@tonic-gate 			}
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate 			if (on_intr) {
256*7c478bd9Sstevel@tonic-gate 				/*
257*7c478bd9Sstevel@tonic-gate 				 * Hop from interrupt stack to thread stack.
258*7c478bd9Sstevel@tonic-gate 				 */
259*7c478bd9Sstevel@tonic-gate 				stacktop = (struct frame *)curthread->t_stk;
260*7c478bd9Sstevel@tonic-gate 				minfp = (struct frame *)curthread->t_stkbase;
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate 				on_intr = 0;
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate 				if (nextfp > minfp && nextfp < stacktop)
265*7c478bd9Sstevel@tonic-gate 					continue;
266*7c478bd9Sstevel@tonic-gate 			} else {
267*7c478bd9Sstevel@tonic-gate 				/*
268*7c478bd9Sstevel@tonic-gate 				 * High-level interrupts may occur when %sp is
269*7c478bd9Sstevel@tonic-gate 				 * not necessarily contained in the stack
270*7c478bd9Sstevel@tonic-gate 				 * bounds implied by %g7 -- interrupt thread
271*7c478bd9Sstevel@tonic-gate 				 * management runs with %pil at DISP_LEVEL,
272*7c478bd9Sstevel@tonic-gate 				 * and high-level interrupts may thus occur
273*7c478bd9Sstevel@tonic-gate 				 * in windows when %sp and %g7 are not self-
274*7c478bd9Sstevel@tonic-gate 				 * consistent.  If we call dtrace_getpcstack()
275*7c478bd9Sstevel@tonic-gate 				 * from a high-level interrupt that has occurred
276*7c478bd9Sstevel@tonic-gate 				 * in such a window, we will fail the above test
277*7c478bd9Sstevel@tonic-gate 				 * of nextfp against minfp/stacktop.  If the
278*7c478bd9Sstevel@tonic-gate 				 * high-level interrupt has in turn interrupted
279*7c478bd9Sstevel@tonic-gate 				 * a non-passivated interrupt thread, we
280*7c478bd9Sstevel@tonic-gate 				 * will execute the below code with non-zero
281*7c478bd9Sstevel@tonic-gate 				 * aframes.  We therefore want to assert that
282*7c478bd9Sstevel@tonic-gate 				 * aframes is zero _or_ we are in a high-level
283*7c478bd9Sstevel@tonic-gate 				 * interrupt -- but because cpu_intr_actv is
284*7c478bd9Sstevel@tonic-gate 				 * updated with high-level interrupts enabled,
285*7c478bd9Sstevel@tonic-gate 				 * we must reduce this to only asserting that
286*7c478bd9Sstevel@tonic-gate 				 * %pil is greater than DISP_LEVEL.
287*7c478bd9Sstevel@tonic-gate 				 */
288*7c478bd9Sstevel@tonic-gate 				ASSERT(aframes == 0 ||
289*7c478bd9Sstevel@tonic-gate 				    dtrace_getipl() > DISP_LEVEL);
290*7c478bd9Sstevel@tonic-gate 				pcstack[depth++] = (pc_t)fp->fr_savpc;
291*7c478bd9Sstevel@tonic-gate 			}
292*7c478bd9Sstevel@tonic-gate 
293*7c478bd9Sstevel@tonic-gate 			while (depth < pcstack_limit)
294*7c478bd9Sstevel@tonic-gate 				pcstack[depth++] = NULL;
295*7c478bd9Sstevel@tonic-gate 			return;
296*7c478bd9Sstevel@tonic-gate 		}
297*7c478bd9Sstevel@tonic-gate 
298*7c478bd9Sstevel@tonic-gate 		if (aframes > 0) {
299*7c478bd9Sstevel@tonic-gate 			aframes--;
300*7c478bd9Sstevel@tonic-gate 		} else {
301*7c478bd9Sstevel@tonic-gate 			pcstack[depth++] = (pc_t)fp->fr_savpc;
302*7c478bd9Sstevel@tonic-gate 		}
303*7c478bd9Sstevel@tonic-gate 
304*7c478bd9Sstevel@tonic-gate 		fp = nextfp;
305*7c478bd9Sstevel@tonic-gate 		minfp = fp;
306*7c478bd9Sstevel@tonic-gate 	}
307*7c478bd9Sstevel@tonic-gate }
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate void
310*7c478bd9Sstevel@tonic-gate dtrace_getupcstack(uint64_t *pcstack, int pcstack_limit)
311*7c478bd9Sstevel@tonic-gate {
312*7c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
313*7c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
314*7c478bd9Sstevel@tonic-gate 	struct regs *rp;
315*7c478bd9Sstevel@tonic-gate 	uintptr_t sp;
316*7c478bd9Sstevel@tonic-gate 	int n;
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate 	if (lwp == NULL || p == NULL || lwp->lwp_regs == NULL)
319*7c478bd9Sstevel@tonic-gate 		return;
320*7c478bd9Sstevel@tonic-gate 
321*7c478bd9Sstevel@tonic-gate 	if (pcstack_limit <= 0)
322*7c478bd9Sstevel@tonic-gate 		return;
323*7c478bd9Sstevel@tonic-gate 
324*7c478bd9Sstevel@tonic-gate 	*pcstack++ = (uint64_t)p->p_pid;
325*7c478bd9Sstevel@tonic-gate 	pcstack_limit--;
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate 	if (pcstack_limit <= 0)
328*7c478bd9Sstevel@tonic-gate 		return;
329*7c478bd9Sstevel@tonic-gate 
330*7c478bd9Sstevel@tonic-gate 	rp = lwp->lwp_regs;
331*7c478bd9Sstevel@tonic-gate 	*pcstack++ = (uint64_t)rp->r_pc;
332*7c478bd9Sstevel@tonic-gate 	pcstack_limit--;
333*7c478bd9Sstevel@tonic-gate 
334*7c478bd9Sstevel@tonic-gate 	if (pcstack_limit <= 0)
335*7c478bd9Sstevel@tonic-gate 		return;
336*7c478bd9Sstevel@tonic-gate 
337*7c478bd9Sstevel@tonic-gate 	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
338*7c478bd9Sstevel@tonic-gate 		*pcstack++ = (uint64_t)rp->r_o7;
339*7c478bd9Sstevel@tonic-gate 		pcstack_limit--;
340*7c478bd9Sstevel@tonic-gate 		if (pcstack_limit <= 0)
341*7c478bd9Sstevel@tonic-gate 			return;
342*7c478bd9Sstevel@tonic-gate 	}
343*7c478bd9Sstevel@tonic-gate 
344*7c478bd9Sstevel@tonic-gate 	sp = rp->r_sp;
345*7c478bd9Sstevel@tonic-gate 
346*7c478bd9Sstevel@tonic-gate 	n = dtrace_getupcstack_top(pcstack, pcstack_limit, &sp);
347*7c478bd9Sstevel@tonic-gate 	ASSERT(n >= 0);
348*7c478bd9Sstevel@tonic-gate 	ASSERT(n <= pcstack_limit);
349*7c478bd9Sstevel@tonic-gate 
350*7c478bd9Sstevel@tonic-gate 	pcstack += n;
351*7c478bd9Sstevel@tonic-gate 	pcstack_limit -= n;
352*7c478bd9Sstevel@tonic-gate 
353*7c478bd9Sstevel@tonic-gate 	if (p->p_model == DATAMODEL_NATIVE) {
354*7c478bd9Sstevel@tonic-gate 		while (pcstack_limit > 0) {
355*7c478bd9Sstevel@tonic-gate 			struct frame *fr = (struct frame *)(sp + STACK_BIAS);
356*7c478bd9Sstevel@tonic-gate 			uintptr_t pc;
357*7c478bd9Sstevel@tonic-gate 
358*7c478bd9Sstevel@tonic-gate 			if (sp == 0 || fr == NULL ||
359*7c478bd9Sstevel@tonic-gate 			    ((uintptr_t)&fr->fr_savpc & 3) != 0 ||
360*7c478bd9Sstevel@tonic-gate 			    ((uintptr_t)&fr->fr_savfp & 3) != 0)
361*7c478bd9Sstevel@tonic-gate 				break;
362*7c478bd9Sstevel@tonic-gate 
363*7c478bd9Sstevel@tonic-gate 			pc = dtrace_fulword(&fr->fr_savpc);
364*7c478bd9Sstevel@tonic-gate 			sp = dtrace_fulword(&fr->fr_savfp);
365*7c478bd9Sstevel@tonic-gate 
366*7c478bd9Sstevel@tonic-gate 			if (pc == 0)
367*7c478bd9Sstevel@tonic-gate 				break;
368*7c478bd9Sstevel@tonic-gate 
369*7c478bd9Sstevel@tonic-gate 			*pcstack++ = pc;
370*7c478bd9Sstevel@tonic-gate 			pcstack_limit--;
371*7c478bd9Sstevel@tonic-gate 		}
372*7c478bd9Sstevel@tonic-gate 	} else {
373*7c478bd9Sstevel@tonic-gate 		while (pcstack_limit > 0) {
374*7c478bd9Sstevel@tonic-gate 			struct frame32 *fr = (struct frame32 *)sp;
375*7c478bd9Sstevel@tonic-gate 			uint32_t pc;
376*7c478bd9Sstevel@tonic-gate 
377*7c478bd9Sstevel@tonic-gate 			if (sp == 0 ||
378*7c478bd9Sstevel@tonic-gate 			    ((uintptr_t)&fr->fr_savpc & 3) != 0 ||
379*7c478bd9Sstevel@tonic-gate 			    ((uintptr_t)&fr->fr_savfp & 3) != 0)
380*7c478bd9Sstevel@tonic-gate 				break;
381*7c478bd9Sstevel@tonic-gate 
382*7c478bd9Sstevel@tonic-gate 			pc = dtrace_fuword32(&fr->fr_savpc);
383*7c478bd9Sstevel@tonic-gate 			sp = dtrace_fuword32(&fr->fr_savfp);
384*7c478bd9Sstevel@tonic-gate 
385*7c478bd9Sstevel@tonic-gate 			*pcstack++ = pc;
386*7c478bd9Sstevel@tonic-gate 			pcstack_limit--;
387*7c478bd9Sstevel@tonic-gate 		}
388*7c478bd9Sstevel@tonic-gate 	}
389*7c478bd9Sstevel@tonic-gate 
390*7c478bd9Sstevel@tonic-gate 	while (pcstack_limit-- > 0)
391*7c478bd9Sstevel@tonic-gate 		*pcstack++ = NULL;
392*7c478bd9Sstevel@tonic-gate }
393*7c478bd9Sstevel@tonic-gate 
394*7c478bd9Sstevel@tonic-gate void
395*7c478bd9Sstevel@tonic-gate dtrace_getufpstack(uint64_t *pcstack, uint64_t *fpstack, int pcstack_limit)
396*7c478bd9Sstevel@tonic-gate {
397*7c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
398*7c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
399*7c478bd9Sstevel@tonic-gate 	struct regs *rp;
400*7c478bd9Sstevel@tonic-gate 	uintptr_t sp;
401*7c478bd9Sstevel@tonic-gate 
402*7c478bd9Sstevel@tonic-gate 	if (lwp == NULL || p == NULL || lwp->lwp_regs == NULL)
403*7c478bd9Sstevel@tonic-gate 		return;
404*7c478bd9Sstevel@tonic-gate 
405*7c478bd9Sstevel@tonic-gate 	if (pcstack_limit <= 0)
406*7c478bd9Sstevel@tonic-gate 		return;
407*7c478bd9Sstevel@tonic-gate 
408*7c478bd9Sstevel@tonic-gate 	*pcstack++ = (uint64_t)p->p_pid;
409*7c478bd9Sstevel@tonic-gate 	pcstack_limit--;
410*7c478bd9Sstevel@tonic-gate 
411*7c478bd9Sstevel@tonic-gate 	if (pcstack_limit <= 0)
412*7c478bd9Sstevel@tonic-gate 		return;
413*7c478bd9Sstevel@tonic-gate 
414*7c478bd9Sstevel@tonic-gate 	rp = lwp->lwp_regs;
415*7c478bd9Sstevel@tonic-gate 
416*7c478bd9Sstevel@tonic-gate 	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_ENTRY)) {
417*7c478bd9Sstevel@tonic-gate 		*fpstack++ = 0;
418*7c478bd9Sstevel@tonic-gate 		*pcstack++ = (uint64_t)rp->r_pc;
419*7c478bd9Sstevel@tonic-gate 		pcstack_limit--;
420*7c478bd9Sstevel@tonic-gate 		if (pcstack_limit <= 0)
421*7c478bd9Sstevel@tonic-gate 			return;
422*7c478bd9Sstevel@tonic-gate 
423*7c478bd9Sstevel@tonic-gate 		*fpstack++ = (uint64_t)rp->r_sp;
424*7c478bd9Sstevel@tonic-gate 		*pcstack++ = (uint64_t)rp->r_o7;
425*7c478bd9Sstevel@tonic-gate 		pcstack_limit--;
426*7c478bd9Sstevel@tonic-gate 	} else {
427*7c478bd9Sstevel@tonic-gate 		*fpstack++ = (uint64_t)rp->r_sp;
428*7c478bd9Sstevel@tonic-gate 		*pcstack++ = (uint64_t)rp->r_pc;
429*7c478bd9Sstevel@tonic-gate 		pcstack_limit--;
430*7c478bd9Sstevel@tonic-gate 	}
431*7c478bd9Sstevel@tonic-gate 
432*7c478bd9Sstevel@tonic-gate 	if (pcstack_limit <= 0)
433*7c478bd9Sstevel@tonic-gate 		return;
434*7c478bd9Sstevel@tonic-gate 
435*7c478bd9Sstevel@tonic-gate 	sp = rp->r_sp;
436*7c478bd9Sstevel@tonic-gate 
437*7c478bd9Sstevel@tonic-gate 	dtrace_flush_user_windows();
438*7c478bd9Sstevel@tonic-gate 
439*7c478bd9Sstevel@tonic-gate 	if (p->p_model == DATAMODEL_NATIVE) {
440*7c478bd9Sstevel@tonic-gate 		while (pcstack_limit > 0) {
441*7c478bd9Sstevel@tonic-gate 			struct frame *fr = (struct frame *)(sp + STACK_BIAS);
442*7c478bd9Sstevel@tonic-gate 			uintptr_t pc;
443*7c478bd9Sstevel@tonic-gate 
444*7c478bd9Sstevel@tonic-gate 			if (sp == 0 || fr == NULL ||
445*7c478bd9Sstevel@tonic-gate 			    ((uintptr_t)&fr->fr_savpc & 3) != 0 ||
446*7c478bd9Sstevel@tonic-gate 			    ((uintptr_t)&fr->fr_savfp & 3) != 0)
447*7c478bd9Sstevel@tonic-gate 				break;
448*7c478bd9Sstevel@tonic-gate 
449*7c478bd9Sstevel@tonic-gate 			pc = dtrace_fulword(&fr->fr_savpc);
450*7c478bd9Sstevel@tonic-gate 			sp = dtrace_fulword(&fr->fr_savfp);
451*7c478bd9Sstevel@tonic-gate 
452*7c478bd9Sstevel@tonic-gate 			if (pc == 0)
453*7c478bd9Sstevel@tonic-gate 				break;
454*7c478bd9Sstevel@tonic-gate 
455*7c478bd9Sstevel@tonic-gate 			*fpstack++ = sp;
456*7c478bd9Sstevel@tonic-gate 			*pcstack++ = pc;
457*7c478bd9Sstevel@tonic-gate 			pcstack_limit--;
458*7c478bd9Sstevel@tonic-gate 		}
459*7c478bd9Sstevel@tonic-gate 	} else {
460*7c478bd9Sstevel@tonic-gate 		while (pcstack_limit > 0) {
461*7c478bd9Sstevel@tonic-gate 			struct frame32 *fr = (struct frame32 *)sp;
462*7c478bd9Sstevel@tonic-gate 			uint32_t pc;
463*7c478bd9Sstevel@tonic-gate 
464*7c478bd9Sstevel@tonic-gate 			if (sp == 0 ||
465*7c478bd9Sstevel@tonic-gate 			    ((uintptr_t)&fr->fr_savpc & 3) != 0 ||
466*7c478bd9Sstevel@tonic-gate 			    ((uintptr_t)&fr->fr_savfp & 3) != 0)
467*7c478bd9Sstevel@tonic-gate 				break;
468*7c478bd9Sstevel@tonic-gate 
469*7c478bd9Sstevel@tonic-gate 			pc = dtrace_fuword32(&fr->fr_savpc);
470*7c478bd9Sstevel@tonic-gate 			sp = dtrace_fuword32(&fr->fr_savfp);
471*7c478bd9Sstevel@tonic-gate 
472*7c478bd9Sstevel@tonic-gate 			*fpstack++ = sp;
473*7c478bd9Sstevel@tonic-gate 			*pcstack++ = pc;
474*7c478bd9Sstevel@tonic-gate 			pcstack_limit--;
475*7c478bd9Sstevel@tonic-gate 		}
476*7c478bd9Sstevel@tonic-gate 	}
477*7c478bd9Sstevel@tonic-gate 
478*7c478bd9Sstevel@tonic-gate 	while (pcstack_limit-- > 0)
479*7c478bd9Sstevel@tonic-gate 		*pcstack++ = NULL;
480*7c478bd9Sstevel@tonic-gate }
481*7c478bd9Sstevel@tonic-gate 
482*7c478bd9Sstevel@tonic-gate uint64_t
483*7c478bd9Sstevel@tonic-gate dtrace_getarg(int arg, int aframes)
484*7c478bd9Sstevel@tonic-gate {
485*7c478bd9Sstevel@tonic-gate 	uintptr_t val;
486*7c478bd9Sstevel@tonic-gate 	struct frame *fp;
487*7c478bd9Sstevel@tonic-gate 	uint64_t rval;
488*7c478bd9Sstevel@tonic-gate 
489*7c478bd9Sstevel@tonic-gate 	/*
490*7c478bd9Sstevel@tonic-gate 	 * Account for the fact that dtrace_getarg() consumes an additional
491*7c478bd9Sstevel@tonic-gate 	 * stack frame.
492*7c478bd9Sstevel@tonic-gate 	 */
493*7c478bd9Sstevel@tonic-gate 	aframes++;
494*7c478bd9Sstevel@tonic-gate 
495*7c478bd9Sstevel@tonic-gate 	if (arg < 6) {
496*7c478bd9Sstevel@tonic-gate 		if (dtrace_fish(aframes, DTRACE_REG_I0 + arg, &val) == 0)
497*7c478bd9Sstevel@tonic-gate 			return (val);
498*7c478bd9Sstevel@tonic-gate 	} else {
499*7c478bd9Sstevel@tonic-gate 		if (dtrace_fish(aframes, DTRACE_REG_I6, &val) == 0) {
500*7c478bd9Sstevel@tonic-gate 			/*
501*7c478bd9Sstevel@tonic-gate 			 * We have a stack pointer; grab the argument.
502*7c478bd9Sstevel@tonic-gate 			 */
503*7c478bd9Sstevel@tonic-gate 			fp = (struct frame *)(val + STACK_BIAS);
504*7c478bd9Sstevel@tonic-gate 
505*7c478bd9Sstevel@tonic-gate 			DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
506*7c478bd9Sstevel@tonic-gate 			rval = fp->fr_argx[arg - 6];
507*7c478bd9Sstevel@tonic-gate 			DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
508*7c478bd9Sstevel@tonic-gate 
509*7c478bd9Sstevel@tonic-gate 			return (rval);
510*7c478bd9Sstevel@tonic-gate 		}
511*7c478bd9Sstevel@tonic-gate 	}
512*7c478bd9Sstevel@tonic-gate 
513*7c478bd9Sstevel@tonic-gate 	/*
514*7c478bd9Sstevel@tonic-gate 	 * There are other ways to do this.  But the slow, painful way works
515*7c478bd9Sstevel@tonic-gate 	 * just fine.  Because this requires some loads, we need to set
516*7c478bd9Sstevel@tonic-gate 	 * CPU_DTRACE_NOFAULT to protect against looking for an argument that
517*7c478bd9Sstevel@tonic-gate 	 * isn't there.
518*7c478bd9Sstevel@tonic-gate 	 */
519*7c478bd9Sstevel@tonic-gate 	fp = (struct frame *)((caddr_t)dtrace_getfp() + STACK_BIAS);
520*7c478bd9Sstevel@tonic-gate 	dtrace_flush_windows();
521*7c478bd9Sstevel@tonic-gate 
522*7c478bd9Sstevel@tonic-gate 	DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
523*7c478bd9Sstevel@tonic-gate 
524*7c478bd9Sstevel@tonic-gate 	for (aframes -= 1; aframes; aframes--)
525*7c478bd9Sstevel@tonic-gate 		fp = (struct frame *)((caddr_t)fp->fr_savfp + STACK_BIAS);
526*7c478bd9Sstevel@tonic-gate 
527*7c478bd9Sstevel@tonic-gate 	if (arg < 6) {
528*7c478bd9Sstevel@tonic-gate 		rval = fp->fr_arg[arg];
529*7c478bd9Sstevel@tonic-gate 	} else {
530*7c478bd9Sstevel@tonic-gate 		fp = (struct frame *)((caddr_t)fp->fr_savfp + STACK_BIAS);
531*7c478bd9Sstevel@tonic-gate 		rval = fp->fr_argx[arg - 6];
532*7c478bd9Sstevel@tonic-gate 	}
533*7c478bd9Sstevel@tonic-gate 
534*7c478bd9Sstevel@tonic-gate 	DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
535*7c478bd9Sstevel@tonic-gate 
536*7c478bd9Sstevel@tonic-gate 	return (rval);
537*7c478bd9Sstevel@tonic-gate }
538*7c478bd9Sstevel@tonic-gate 
539*7c478bd9Sstevel@tonic-gate int
540*7c478bd9Sstevel@tonic-gate dtrace_getstackdepth(int aframes)
541*7c478bd9Sstevel@tonic-gate {
542*7c478bd9Sstevel@tonic-gate 	struct frame *fp, *nextfp, *minfp, *stacktop;
543*7c478bd9Sstevel@tonic-gate 	int depth = 0;
544*7c478bd9Sstevel@tonic-gate 	int on_intr;
545*7c478bd9Sstevel@tonic-gate 
546*7c478bd9Sstevel@tonic-gate 	fp = (struct frame *)((caddr_t)dtrace_getfp() + STACK_BIAS);
547*7c478bd9Sstevel@tonic-gate 	dtrace_flush_windows();
548*7c478bd9Sstevel@tonic-gate 
549*7c478bd9Sstevel@tonic-gate 	if ((on_intr = CPU_ON_INTR(CPU)) != 0)
550*7c478bd9Sstevel@tonic-gate 		stacktop = (struct frame *)CPU->cpu_intr_stack + SA(MINFRAME);
551*7c478bd9Sstevel@tonic-gate 	else
552*7c478bd9Sstevel@tonic-gate 		stacktop = (struct frame *)curthread->t_stk;
553*7c478bd9Sstevel@tonic-gate 	minfp = fp;
554*7c478bd9Sstevel@tonic-gate 
555*7c478bd9Sstevel@tonic-gate 	for (;;) {
556*7c478bd9Sstevel@tonic-gate 		nextfp = (struct frame *)((caddr_t)fp->fr_savfp + STACK_BIAS);
557*7c478bd9Sstevel@tonic-gate 		if (nextfp <= minfp || nextfp >= stacktop) {
558*7c478bd9Sstevel@tonic-gate 			if (on_intr) {
559*7c478bd9Sstevel@tonic-gate 				/*
560*7c478bd9Sstevel@tonic-gate 				 * Hop from interrupt stack to thread stack.
561*7c478bd9Sstevel@tonic-gate 				 */
562*7c478bd9Sstevel@tonic-gate 				stacktop = (struct frame *)curthread->t_stk;
563*7c478bd9Sstevel@tonic-gate 				minfp = (struct frame *)curthread->t_stkbase;
564*7c478bd9Sstevel@tonic-gate 				on_intr = 0;
565*7c478bd9Sstevel@tonic-gate 				continue;
566*7c478bd9Sstevel@tonic-gate 			}
567*7c478bd9Sstevel@tonic-gate 
568*7c478bd9Sstevel@tonic-gate 			return (++depth);
569*7c478bd9Sstevel@tonic-gate 		}
570*7c478bd9Sstevel@tonic-gate 
571*7c478bd9Sstevel@tonic-gate 		if (aframes > 0) {
572*7c478bd9Sstevel@tonic-gate 			aframes--;
573*7c478bd9Sstevel@tonic-gate 		} else {
574*7c478bd9Sstevel@tonic-gate 			depth++;
575*7c478bd9Sstevel@tonic-gate 		}
576*7c478bd9Sstevel@tonic-gate 
577*7c478bd9Sstevel@tonic-gate 		fp = nextfp;
578*7c478bd9Sstevel@tonic-gate 		minfp = fp;
579*7c478bd9Sstevel@tonic-gate 	}
580*7c478bd9Sstevel@tonic-gate }
581*7c478bd9Sstevel@tonic-gate 
582*7c478bd9Sstevel@tonic-gate /*
583*7c478bd9Sstevel@tonic-gate  * This uses the same register numbering scheme as in sys/procfs_isa.h.
584*7c478bd9Sstevel@tonic-gate  */
585*7c478bd9Sstevel@tonic-gate ulong_t
586*7c478bd9Sstevel@tonic-gate dtrace_getreg(struct regs *rp, uint_t reg)
587*7c478bd9Sstevel@tonic-gate {
588*7c478bd9Sstevel@tonic-gate 	ulong_t value;
589*7c478bd9Sstevel@tonic-gate 	uintptr_t fp;
590*7c478bd9Sstevel@tonic-gate 	struct machpcb *mpcb;
591*7c478bd9Sstevel@tonic-gate 
592*7c478bd9Sstevel@tonic-gate 	if (reg == R_G0)
593*7c478bd9Sstevel@tonic-gate 		return (0);
594*7c478bd9Sstevel@tonic-gate 
595*7c478bd9Sstevel@tonic-gate 	if (reg <= R_G7)
596*7c478bd9Sstevel@tonic-gate 		return ((&rp->r_g1)[reg - 1]);
597*7c478bd9Sstevel@tonic-gate 
598*7c478bd9Sstevel@tonic-gate 	if (reg > R_I7) {
599*7c478bd9Sstevel@tonic-gate 		switch (reg) {
600*7c478bd9Sstevel@tonic-gate 		case R_CCR:
601*7c478bd9Sstevel@tonic-gate 			return ((rp->r_tstate >> TSTATE_CCR_SHIFT) &
602*7c478bd9Sstevel@tonic-gate 			    TSTATE_CCR_MASK);
603*7c478bd9Sstevel@tonic-gate 		case R_PC:
604*7c478bd9Sstevel@tonic-gate 			return (rp->r_pc);
605*7c478bd9Sstevel@tonic-gate 		case R_nPC:
606*7c478bd9Sstevel@tonic-gate 			return (rp->r_npc);
607*7c478bd9Sstevel@tonic-gate 		case R_Y:
608*7c478bd9Sstevel@tonic-gate 			return (rp->r_y);
609*7c478bd9Sstevel@tonic-gate 		case R_ASI:
610*7c478bd9Sstevel@tonic-gate 			return ((rp->r_tstate >> TSTATE_ASI_SHIFT) &
611*7c478bd9Sstevel@tonic-gate 			    TSTATE_ASI_MASK);
612*7c478bd9Sstevel@tonic-gate 		case R_FPRS:
613*7c478bd9Sstevel@tonic-gate 			return (dtrace_getfprs());
614*7c478bd9Sstevel@tonic-gate 		default:
615*7c478bd9Sstevel@tonic-gate 			DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
616*7c478bd9Sstevel@tonic-gate 			return (0);
617*7c478bd9Sstevel@tonic-gate 		}
618*7c478bd9Sstevel@tonic-gate 	}
619*7c478bd9Sstevel@tonic-gate 
620*7c478bd9Sstevel@tonic-gate 	/*
621*7c478bd9Sstevel@tonic-gate 	 * We reach go to the fake restore case if the probe we hit was a pid
622*7c478bd9Sstevel@tonic-gate 	 * return probe on a restore instruction. We partially emulate the
623*7c478bd9Sstevel@tonic-gate 	 * restore in the kernel and then execute a simple restore
624*7c478bd9Sstevel@tonic-gate 	 * instruction that we've secreted away to do the actual register
625*7c478bd9Sstevel@tonic-gate 	 * window manipulation. We need to go one register window further
626*7c478bd9Sstevel@tonic-gate 	 * down to get at the %ls, and %is and we need to treat %os like %is
627*7c478bd9Sstevel@tonic-gate 	 * to pull them out of the topmost user frame.
628*7c478bd9Sstevel@tonic-gate 	 */
629*7c478bd9Sstevel@tonic-gate 	if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAKERESTORE)) {
630*7c478bd9Sstevel@tonic-gate 		if (reg > R_O7)
631*7c478bd9Sstevel@tonic-gate 			goto fake_restore;
632*7c478bd9Sstevel@tonic-gate 		else
633*7c478bd9Sstevel@tonic-gate 			reg += R_I0 - R_O0;
634*7c478bd9Sstevel@tonic-gate 
635*7c478bd9Sstevel@tonic-gate 	} else if (reg <= R_O7) {
636*7c478bd9Sstevel@tonic-gate 		return ((&rp->r_g1)[reg - 1]);
637*7c478bd9Sstevel@tonic-gate 	}
638*7c478bd9Sstevel@tonic-gate 
639*7c478bd9Sstevel@tonic-gate 	if (dtrace_getotherwin() > 0)
640*7c478bd9Sstevel@tonic-gate 		return (dtrace_getreg_win(reg, 1));
641*7c478bd9Sstevel@tonic-gate 
642*7c478bd9Sstevel@tonic-gate 	mpcb = (struct machpcb *)((caddr_t)rp - REGOFF);
643*7c478bd9Sstevel@tonic-gate 
644*7c478bd9Sstevel@tonic-gate 	if (curproc->p_model == DATAMODEL_NATIVE) {
645*7c478bd9Sstevel@tonic-gate 		struct frame *fr = (void *)(rp->r_sp + STACK_BIAS);
646*7c478bd9Sstevel@tonic-gate 
647*7c478bd9Sstevel@tonic-gate 		if (mpcb->mpcb_wbcnt > 0) {
648*7c478bd9Sstevel@tonic-gate 			struct rwindow *rwin = (void *)mpcb->mpcb_wbuf;
649*7c478bd9Sstevel@tonic-gate 			int i = mpcb->mpcb_wbcnt;
650*7c478bd9Sstevel@tonic-gate 			do {
651*7c478bd9Sstevel@tonic-gate 				i--;
652*7c478bd9Sstevel@tonic-gate 				if ((long)mpcb->mpcb_spbuf[i] == rp->r_sp)
653*7c478bd9Sstevel@tonic-gate 					return (rwin[i].rw_local[reg - 16]);
654*7c478bd9Sstevel@tonic-gate 			} while (i > 0);
655*7c478bd9Sstevel@tonic-gate 		}
656*7c478bd9Sstevel@tonic-gate 
657*7c478bd9Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
658*7c478bd9Sstevel@tonic-gate 		value = dtrace_fulword(&fr->fr_local[reg - 16]);
659*7c478bd9Sstevel@tonic-gate 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
660*7c478bd9Sstevel@tonic-gate 	} else {
661*7c478bd9Sstevel@tonic-gate 		struct frame32 *fr = (void *)(caddr32_t)rp->r_sp;
662*7c478bd9Sstevel@tonic-gate 
663*7c478bd9Sstevel@tonic-gate 		if (mpcb->mpcb_wbcnt > 0) {
664*7c478bd9Sstevel@tonic-gate 			struct rwindow32 *rwin = (void *)mpcb->mpcb_wbuf;
665*7c478bd9Sstevel@tonic-gate 			int i = mpcb->mpcb_wbcnt;
666*7c478bd9Sstevel@tonic-gate 			do {
667*7c478bd9Sstevel@tonic-gate 				i--;
668*7c478bd9Sstevel@tonic-gate 				if ((long)mpcb->mpcb_spbuf[i] == rp->r_sp)
669*7c478bd9Sstevel@tonic-gate 					return (rwin[i].rw_local[reg - 16]);
670*7c478bd9Sstevel@tonic-gate 			} while (i > 0);
671*7c478bd9Sstevel@tonic-gate 		}
672*7c478bd9Sstevel@tonic-gate 
673*7c478bd9Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
674*7c478bd9Sstevel@tonic-gate 		value = dtrace_fuword32(&fr->fr_local[reg - 16]);
675*7c478bd9Sstevel@tonic-gate 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
676*7c478bd9Sstevel@tonic-gate 	}
677*7c478bd9Sstevel@tonic-gate 
678*7c478bd9Sstevel@tonic-gate 	return (value);
679*7c478bd9Sstevel@tonic-gate 
680*7c478bd9Sstevel@tonic-gate fake_restore:
681*7c478bd9Sstevel@tonic-gate 	ASSERT(R_L0 <= reg && reg <= R_I7);
682*7c478bd9Sstevel@tonic-gate 
683*7c478bd9Sstevel@tonic-gate 	/*
684*7c478bd9Sstevel@tonic-gate 	 * We first look two user windows down to see if we can dig out
685*7c478bd9Sstevel@tonic-gate 	 * the register we're looking for.
686*7c478bd9Sstevel@tonic-gate 	 */
687*7c478bd9Sstevel@tonic-gate 	if (dtrace_getotherwin() > 1)
688*7c478bd9Sstevel@tonic-gate 		return (dtrace_getreg_win(reg, 2));
689*7c478bd9Sstevel@tonic-gate 
690*7c478bd9Sstevel@tonic-gate 	/*
691*7c478bd9Sstevel@tonic-gate 	 * First we need to get the frame pointer and then we perform
692*7c478bd9Sstevel@tonic-gate 	 * the same computation as in the non-fake-o-restore case.
693*7c478bd9Sstevel@tonic-gate 	 */
694*7c478bd9Sstevel@tonic-gate 
695*7c478bd9Sstevel@tonic-gate 	mpcb = (struct machpcb *)((caddr_t)rp - REGOFF);
696*7c478bd9Sstevel@tonic-gate 
697*7c478bd9Sstevel@tonic-gate 	if (dtrace_getotherwin() > 0) {
698*7c478bd9Sstevel@tonic-gate 		fp = dtrace_getreg_win(R_FP, 1);
699*7c478bd9Sstevel@tonic-gate 		goto got_fp;
700*7c478bd9Sstevel@tonic-gate 	}
701*7c478bd9Sstevel@tonic-gate 
702*7c478bd9Sstevel@tonic-gate 	if (curproc->p_model == DATAMODEL_NATIVE) {
703*7c478bd9Sstevel@tonic-gate 		struct frame *fr = (void *)(rp->r_sp + STACK_BIAS);
704*7c478bd9Sstevel@tonic-gate 
705*7c478bd9Sstevel@tonic-gate 		if (mpcb->mpcb_wbcnt > 0) {
706*7c478bd9Sstevel@tonic-gate 			struct rwindow *rwin = (void *)mpcb->mpcb_wbuf;
707*7c478bd9Sstevel@tonic-gate 			int i = mpcb->mpcb_wbcnt;
708*7c478bd9Sstevel@tonic-gate 			do {
709*7c478bd9Sstevel@tonic-gate 				i--;
710*7c478bd9Sstevel@tonic-gate 				if ((long)mpcb->mpcb_spbuf[i] == rp->r_sp) {
711*7c478bd9Sstevel@tonic-gate 					fp = rwin[i].rw_fp;
712*7c478bd9Sstevel@tonic-gate 					goto got_fp;
713*7c478bd9Sstevel@tonic-gate 				}
714*7c478bd9Sstevel@tonic-gate 			} while (i > 0);
715*7c478bd9Sstevel@tonic-gate 		}
716*7c478bd9Sstevel@tonic-gate 
717*7c478bd9Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
718*7c478bd9Sstevel@tonic-gate 		fp = dtrace_fulword(&fr->fr_savfp);
719*7c478bd9Sstevel@tonic-gate 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
720*7c478bd9Sstevel@tonic-gate 		if (cpu_core[CPU->cpu_id].cpuc_dtrace_flags & CPU_DTRACE_FAULT)
721*7c478bd9Sstevel@tonic-gate 			return (0);
722*7c478bd9Sstevel@tonic-gate 	} else {
723*7c478bd9Sstevel@tonic-gate 		struct frame32 *fr = (void *)(caddr32_t)rp->r_sp;
724*7c478bd9Sstevel@tonic-gate 
725*7c478bd9Sstevel@tonic-gate 		if (mpcb->mpcb_wbcnt > 0) {
726*7c478bd9Sstevel@tonic-gate 			struct rwindow32 *rwin = (void *)mpcb->mpcb_wbuf;
727*7c478bd9Sstevel@tonic-gate 			int i = mpcb->mpcb_wbcnt;
728*7c478bd9Sstevel@tonic-gate 			do {
729*7c478bd9Sstevel@tonic-gate 				i--;
730*7c478bd9Sstevel@tonic-gate 				if ((long)mpcb->mpcb_spbuf[i] == rp->r_sp) {
731*7c478bd9Sstevel@tonic-gate 					fp = rwin[i].rw_fp;
732*7c478bd9Sstevel@tonic-gate 					goto got_fp;
733*7c478bd9Sstevel@tonic-gate 				}
734*7c478bd9Sstevel@tonic-gate 			} while (i > 0);
735*7c478bd9Sstevel@tonic-gate 		}
736*7c478bd9Sstevel@tonic-gate 
737*7c478bd9Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
738*7c478bd9Sstevel@tonic-gate 		fp = dtrace_fuword32(&fr->fr_savfp);
739*7c478bd9Sstevel@tonic-gate 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
740*7c478bd9Sstevel@tonic-gate 		if (cpu_core[CPU->cpu_id].cpuc_dtrace_flags & CPU_DTRACE_FAULT)
741*7c478bd9Sstevel@tonic-gate 			return (0);
742*7c478bd9Sstevel@tonic-gate 	}
743*7c478bd9Sstevel@tonic-gate got_fp:
744*7c478bd9Sstevel@tonic-gate 
745*7c478bd9Sstevel@tonic-gate 	if (curproc->p_model == DATAMODEL_NATIVE) {
746*7c478bd9Sstevel@tonic-gate 		struct frame *fr = (void *)(fp + STACK_BIAS);
747*7c478bd9Sstevel@tonic-gate 
748*7c478bd9Sstevel@tonic-gate 		if (mpcb->mpcb_wbcnt > 0) {
749*7c478bd9Sstevel@tonic-gate 			struct rwindow *rwin = (void *)mpcb->mpcb_wbuf;
750*7c478bd9Sstevel@tonic-gate 			int i = mpcb->mpcb_wbcnt;
751*7c478bd9Sstevel@tonic-gate 			do {
752*7c478bd9Sstevel@tonic-gate 				i--;
753*7c478bd9Sstevel@tonic-gate 				if ((long)mpcb->mpcb_spbuf[i] == fp)
754*7c478bd9Sstevel@tonic-gate 					return (rwin[i].rw_local[reg - 16]);
755*7c478bd9Sstevel@tonic-gate 			} while (i > 0);
756*7c478bd9Sstevel@tonic-gate 		}
757*7c478bd9Sstevel@tonic-gate 
758*7c478bd9Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
759*7c478bd9Sstevel@tonic-gate 		value = dtrace_fulword(&fr->fr_local[reg - 16]);
760*7c478bd9Sstevel@tonic-gate 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
761*7c478bd9Sstevel@tonic-gate 	} else {
762*7c478bd9Sstevel@tonic-gate 		struct frame32 *fr = (void *)(caddr32_t)fp;
763*7c478bd9Sstevel@tonic-gate 
764*7c478bd9Sstevel@tonic-gate 		if (mpcb->mpcb_wbcnt > 0) {
765*7c478bd9Sstevel@tonic-gate 			struct rwindow32 *rwin = (void *)mpcb->mpcb_wbuf;
766*7c478bd9Sstevel@tonic-gate 			int i = mpcb->mpcb_wbcnt;
767*7c478bd9Sstevel@tonic-gate 			do {
768*7c478bd9Sstevel@tonic-gate 				i--;
769*7c478bd9Sstevel@tonic-gate 				if ((long)mpcb->mpcb_spbuf[i] == fp)
770*7c478bd9Sstevel@tonic-gate 					return (rwin[i].rw_local[reg - 16]);
771*7c478bd9Sstevel@tonic-gate 			} while (i > 0);
772*7c478bd9Sstevel@tonic-gate 		}
773*7c478bd9Sstevel@tonic-gate 
774*7c478bd9Sstevel@tonic-gate 		DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
775*7c478bd9Sstevel@tonic-gate 		value = dtrace_fuword32(&fr->fr_local[reg - 16]);
776*7c478bd9Sstevel@tonic-gate 		DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
777*7c478bd9Sstevel@tonic-gate 	}
778*7c478bd9Sstevel@tonic-gate 
779*7c478bd9Sstevel@tonic-gate 	return (value);
780*7c478bd9Sstevel@tonic-gate }
781