4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 * Copyright (c) 2016 by Delphix. All rights reserved.
30 #include <sys/frame.h>
31 #include <sys/stack.h>
34 #if defined(__sparc) || defined(__sparcv9)
35 extern void flush_windows(void);
36 #define UMEM_FRAMESIZE MINFRAME
38 #elif defined(__i386) || defined(__amd64)
40 * On x86, MINFRAME is defined to be 0, but we want to be sure we can
41 * dereference the entire frame structure.
43 #define UMEM_FRAMESIZE (sizeof (struct frame))
46 #error needs update for new architecture
50 * Get a pc-only stacktrace. Used for kmem_alloc() buffer ownership tracking.
51 * Returns MIN(current stack depth, pcstack_limit).
55 getpcstack(uintptr_t *pcstack
, int pcstack_limit
, int check_signal
)
58 struct frame
*nextfp
, *minfp
;
62 #ifndef UMEM_STANDALONE
64 uintptr_t sigbase
= 0;
69 if (stack_getbounds(&st
) != 0) {
70 if (thr_stksegment(&st
) != 0 ||
71 (uintptr_t)st
.ss_sp
< st
.ss_size
) {
72 return (0); /* unable to get stack bounds */
75 * thr_stksegment(3C) has a slightly different interface than
76 * stack_getbounds(3C) -- correct it
78 st
.ss_sp
= (void *)(((uintptr_t)st
.ss_sp
) - st
.ss_size
);
79 st
.ss_flags
= 0; /* can't be on-stack */
81 on_altstack
= (st
.ss_flags
& SS_ONSTACK
);
83 if (st
.ss_size
!= 0) {
84 base
= (uintptr_t)st
.ss_sp
;
88 * If size == 0, then ss_sp is the *top* of the stack.
90 * Since we only allow increasing frame pointers, and we
91 * know our caller set its up correctly, we can treat ss_sp
92 * as an upper bound safely.
95 size
= (uintptr_t)st
.ss_sp
;
98 if (check_signal
!= 0) {
99 void (*sigfunc
)() = NULL
;
101 extern void thr_sighndlrinfo(void (**)(), int *);
103 thr_sighndlrinfo(&sigfunc
, &sigfuncsize
);
104 sigbase
= (uintptr_t)sigfunc
;
105 sigsize
= sigfuncsize
;
107 #else /* UMEM_STANDALONE */
108 base
= (uintptr_t)umem_min_stack
;
109 size
= umem_max_stack
- umem_min_stack
;
113 * shorten size so that fr_savfp and fr_savpc will be within the stack
116 if (size
>= UMEM_FRAMESIZE
- 1)
117 size
-= (UMEM_FRAMESIZE
- 1);
121 #if defined(__sparc) || defined(__sparcv9)
125 /* LINTED alignment */
126 fp
= (struct frame
*)((caddr_t
)getfp() + STACK_BIAS
);
130 if (((uintptr_t)fp
- base
) >= size
)
131 return (0); /* the frame pointer isn't in our stack */
133 while (depth
< pcstack_limit
) {
136 /* LINTED alignment */
137 nextfp
= (struct frame
*)((caddr_t
)fp
->fr_savfp
+ STACK_BIAS
);
138 tmp
= (uintptr_t)nextfp
;
141 * Check nextfp for validity. It must be properly aligned,
142 * increasing compared to the last %fp (or the top of the
143 * stack we just switched to), and it must be inside
144 * [base, base + size).
148 else if (nextfp
<= minfp
|| (tmp
- base
) >= size
) {
149 #ifndef UMEM_STANDALONE
150 if (tmp
== (uintptr_t)NULL
|| !on_altstack
)
153 * If we're on an alternate signal stack, try jumping
154 * to the main thread stack.
156 * If the main thread stack has an unlimited size, we
157 * punt, since we don't know where the frame pointer's
160 * (thr_stksegment() returns the *top of stack*
161 * in ss_sp, not the bottom)
163 if (thr_stksegment(&st
) == 0) {
164 if (st
.ss_size
>= (uintptr_t)st
.ss_sp
||
165 st
.ss_size
< UMEM_FRAMESIZE
- 1)
169 base
= (uintptr_t)st
.ss_sp
- st
.ss_size
;
170 size
= st
.ss_size
- (UMEM_FRAMESIZE
- 1);
171 minfp
= (struct frame
*)base
;
172 continue; /* try again */
178 #ifndef UMEM_STANDALONE
179 if (check_signal
&& (fp
->fr_savpc
- sigbase
) <= sigsize
)
180 umem_panic("called from signal handler");
182 pcstack
[depth
++] = fp
->fr_savpc
;