2 * Kernel and userspace stack tracing.
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License. See the file "COPYING" in the main directory of this archive
8 * Copyright (C) 2001 - 2013 Tensilica Inc.
9 * Copyright (C) 2015 Cadence Design Systems Inc.
11 #include <linux/export.h>
12 #include <linux/sched.h>
13 #include <linux/stacktrace.h>
15 #include <asm/stacktrace.h>
16 #include <asm/traps.h>
17 #include <linux/uaccess.h>
19 #if IS_ENABLED(CONFIG_OPROFILE) || IS_ENABLED(CONFIG_PERF_EVENTS)
21 /* Address of common_exception_return, used to check the
22 * transition from kernel to user space.
24 extern int common_exception_return
;
26 void xtensa_backtrace_user(struct pt_regs
*regs
, unsigned int depth
,
27 int (*ufn
)(struct stackframe
*frame
, void *data
),
30 unsigned long windowstart
= regs
->windowstart
;
31 unsigned long windowbase
= regs
->windowbase
;
32 unsigned long a0
= regs
->areg
[0];
33 unsigned long a1
= regs
->areg
[1];
34 unsigned long pc
= regs
->pc
;
35 struct stackframe frame
;
44 if (pc
== 0 || pc
>= TASK_SIZE
|| ufn(&frame
, data
))
49 * 1. Look through the register window for the
50 * previous PCs in the call trace.
52 * 2. Look on the stack.
56 /* Rotate WINDOWSTART to move the bit corresponding to
57 * the current window to the bit #0.
59 windowstart
= (windowstart
<< WSBITS
| windowstart
) >> windowbase
;
61 /* Look for bits that are set, they correspond to
64 for (index
= WSBITS
- 1; (index
> 0) && depth
; depth
--, index
--)
65 if (windowstart
& (1 << index
)) {
66 /* Get the PC from a0 and a1. */
67 pc
= MAKE_PC_FROM_RA(a0
, pc
);
68 /* Read a0 and a1 from the
69 * corresponding position in AREGs.
71 a0
= regs
->areg
[index
* 4];
72 a1
= regs
->areg
[index
* 4 + 1];
77 if (pc
== 0 || pc
>= TASK_SIZE
|| ufn(&frame
, data
))
82 /* We are done with the register window, we need to
83 * look through the stack.
88 /* Start from the a1 register. */
89 /* a1 = regs->areg[1]; */
90 while (a0
!= 0 && depth
--) {
91 pc
= MAKE_PC_FROM_RA(a0
, pc
);
93 /* Check if the region is OK to access. */
94 if (!access_ok(VERIFY_READ
, &SPILL_SLOT(a1
, 0), 8))
96 /* Copy a1, a0 from user space stack frame. */
97 if (__get_user(a0
, &SPILL_SLOT(a1
, 0)) ||
98 __get_user(a1
, &SPILL_SLOT(a1
, 1)))
104 if (pc
== 0 || pc
>= TASK_SIZE
|| ufn(&frame
, data
))
108 EXPORT_SYMBOL(xtensa_backtrace_user
);
110 void xtensa_backtrace_kernel(struct pt_regs
*regs
, unsigned int depth
,
111 int (*kfn
)(struct stackframe
*frame
, void *data
),
112 int (*ufn
)(struct stackframe
*frame
, void *data
),
115 unsigned long pc
= regs
->depc
> VALID_DOUBLE_EXCEPTION_ADDRESS
?
116 regs
->depc
: regs
->pc
;
117 unsigned long sp_start
, sp_end
;
118 unsigned long a0
= regs
->areg
[0];
119 unsigned long a1
= regs
->areg
[1];
121 sp_start
= a1
& ~(THREAD_SIZE
- 1);
122 sp_end
= sp_start
+ THREAD_SIZE
;
124 /* Spill the register window to the stack first. */
127 /* Read the stack frames one by one and create the PC
128 * from the a0 and a1 registers saved there.
130 while (a1
> sp_start
&& a1
< sp_end
&& depth
--) {
131 struct stackframe frame
;
136 if (kernel_text_address(pc
) && kfn(&frame
, data
))
139 if (pc
== (unsigned long)&common_exception_return
) {
140 regs
= (struct pt_regs
*)a1
;
141 if (user_mode(regs
)) {
144 xtensa_backtrace_user(regs
, depth
, ufn
, data
);
154 pc
= MAKE_PC_FROM_RA(a0
, pc
);
155 a0
= SPILL_SLOT(a1
, 0);
156 a1
= SPILL_SLOT(a1
, 1);
159 EXPORT_SYMBOL(xtensa_backtrace_kernel
);
163 void walk_stackframe(unsigned long *sp
,
164 int (*fn
)(struct stackframe
*frame
, void *data
),
167 unsigned long a0
, a1
;
168 unsigned long sp_end
;
170 a1
= (unsigned long)sp
;
171 sp_end
= ALIGN(a1
, THREAD_SIZE
);
175 while (a1
< sp_end
) {
176 struct stackframe frame
;
178 sp
= (unsigned long *)a1
;
180 a0
= SPILL_SLOT(a1
, 0);
181 a1
= SPILL_SLOT(a1
, 1);
183 if (a1
<= (unsigned long)sp
)
186 frame
.pc
= MAKE_PC_FROM_RA(a0
, a1
);
189 if (fn(&frame
, data
))
194 #ifdef CONFIG_STACKTRACE
196 struct stack_trace_data
{
197 struct stack_trace
*trace
;
201 static int stack_trace_cb(struct stackframe
*frame
, void *data
)
203 struct stack_trace_data
*trace_data
= data
;
204 struct stack_trace
*trace
= trace_data
->trace
;
206 if (trace_data
->skip
) {
210 if (!kernel_text_address(frame
->pc
))
213 trace
->entries
[trace
->nr_entries
++] = frame
->pc
;
214 return trace
->nr_entries
>= trace
->max_entries
;
217 void save_stack_trace_tsk(struct task_struct
*task
, struct stack_trace
*trace
)
219 struct stack_trace_data trace_data
= {
223 walk_stackframe(stack_pointer(task
), stack_trace_cb
, &trace_data
);
225 EXPORT_SYMBOL_GPL(save_stack_trace_tsk
);
227 void save_stack_trace(struct stack_trace
*trace
)
229 save_stack_trace_tsk(current
, trace
);
231 EXPORT_SYMBOL_GPL(save_stack_trace
);
235 #ifdef CONFIG_FRAME_POINTER
237 struct return_addr_data
{
242 static int return_address_cb(struct stackframe
*frame
, void *data
)
244 struct return_addr_data
*r
= data
;
250 if (!kernel_text_address(frame
->pc
))
256 unsigned long return_address(unsigned level
)
258 struct return_addr_data r
= {
261 walk_stackframe(stack_pointer(NULL
), return_address_cb
, &r
);
264 EXPORT_SYMBOL(return_address
);