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