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