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.
28 #pragma ident "%Z%%M% %I% %E% SMI"
32 #include <sys/frame.h>
33 #include <sys/stack.h>
36 #if defined(__sparc) || defined(__sparcv9)
37 extern void flush_windows(void);
38 #define UMEM_FRAMESIZE MINFRAME
40 #elif defined(__i386) || defined(__amd64)
42 * On x86, MINFRAME is defined to be 0, but we want to be sure we can
43 * dereference the entire frame structure.
45 #define UMEM_FRAMESIZE (sizeof (struct frame))
48 #error needs update for new architecture
52 * Get a pc-only stacktrace. Used for kmem_alloc() buffer ownership tracking.
53 * Returns MIN(current stack depth, pcstack_limit).
57 getpcstack(uintptr_t *pcstack
, int pcstack_limit
, int check_signal
)
60 struct frame
*nextfp
, *minfp
;
64 #ifndef UMEM_STANDALONE
66 uintptr_t sigbase
= 0;
71 if (stack_getbounds(&st
) != 0) {
72 if (thr_stksegment(&st
) != 0 ||
73 (uintptr_t)st
.ss_sp
< st
.ss_size
) {
74 return (0); /* unable to get stack bounds */
77 * thr_stksegment(3C) has a slightly different interface than
78 * stack_getbounds(3C) -- correct it
80 st
.ss_sp
= (void *)(((uintptr_t)st
.ss_sp
) - st
.ss_size
);
81 st
.ss_flags
= 0; /* can't be on-stack */
83 on_altstack
= (st
.ss_flags
& SS_ONSTACK
);
85 if (st
.ss_size
!= 0) {
86 base
= (uintptr_t)st
.ss_sp
;
90 * If size == 0, then ss_sp is the *top* of the stack.
92 * Since we only allow increasing frame pointers, and we
93 * know our caller set its up correctly, we can treat ss_sp
94 * as an upper bound safely.
97 size
= (uintptr_t)st
.ss_sp
;
100 if (check_signal
!= 0) {
101 void (*sigfunc
)() = NULL
;
103 extern void thr_sighndlrinfo(void (**)(), int *);
105 thr_sighndlrinfo(&sigfunc
, &sigfuncsize
);
106 sigbase
= (uintptr_t)sigfunc
;
107 sigsize
= sigfuncsize
;
109 #else /* UMEM_STANDALONE */
110 base
= (uintptr_t)umem_min_stack
;
111 size
= umem_max_stack
- umem_min_stack
;
115 * shorten size so that fr_savfp and fr_savpc will be within the stack
118 if (size
>= UMEM_FRAMESIZE
- 1)
119 size
-= (UMEM_FRAMESIZE
- 1);
123 #if defined(__sparc) || defined(__sparcv9)
127 /* LINTED alignment */
128 fp
= (struct frame
*)((caddr_t
)getfp() + STACK_BIAS
);
132 if (((uintptr_t)fp
- base
) >= size
)
133 return (0); /* the frame pointer isn't in our stack */
135 while (depth
< pcstack_limit
) {
138 /* LINTED alignment */
139 nextfp
= (struct frame
*)((caddr_t
)fp
->fr_savfp
+ STACK_BIAS
);
140 tmp
= (uintptr_t)nextfp
;
143 * Check nextfp for validity. It must be properly aligned,
144 * increasing compared to the last %fp (or the top of the
145 * stack we just switched to), and it must be inside
146 * [base, base + size).
150 else if (nextfp
<= minfp
|| (tmp
- base
) >= size
) {
151 #ifndef UMEM_STANDALONE
152 if (tmp
== NULL
|| !on_altstack
)
155 * If we're on an alternate signal stack, try jumping
156 * to the main thread stack.
158 * If the main thread stack has an unlimited size, we
159 * punt, since we don't know where the frame pointer's
162 * (thr_stksegment() returns the *top of stack*
163 * in ss_sp, not the bottom)
165 if (thr_stksegment(&st
) == 0) {
166 if (st
.ss_size
>= (uintptr_t)st
.ss_sp
||
167 st
.ss_size
< UMEM_FRAMESIZE
- 1)
171 base
= (uintptr_t)st
.ss_sp
- st
.ss_size
;
172 size
= st
.ss_size
- (UMEM_FRAMESIZE
- 1);
173 minfp
= (struct frame
*)base
;
174 continue; /* try again */
180 #ifndef UMEM_STANDALONE
181 if (check_signal
&& (fp
->fr_savpc
- sigbase
) <= sigsize
)
182 umem_panic("called from signal handler");
184 pcstack
[depth
++] = fp
->fr_savpc
;