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 <asm/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 /* A struct that maps to the part of the frame containing the a0 and
34 void xtensa_backtrace_user(struct pt_regs *regs, unsigned int depth,
35 int (*ufn)(struct stackframe *frame, void *data),
38 unsigned long windowstart = regs->windowstart;
39 unsigned long windowbase = regs->windowbase;
40 unsigned long a0 = regs->areg[0];
41 unsigned long a1 = regs->areg[1];
42 unsigned long pc = regs->pc;
43 struct stackframe frame;
52 if (pc == 0 || pc >= TASK_SIZE || ufn(&frame, data))
57 * 1. Look through the register window for the
58 * previous PCs in the call trace.
60 * 2. Look on the stack.
64 /* Rotate WINDOWSTART to move the bit corresponding to
65 * the current window to the bit #0.
67 windowstart = (windowstart << WSBITS | windowstart) >> windowbase;
69 /* Look for bits that are set, they correspond to
72 for (index = WSBITS - 1; (index > 0) && depth; depth--, index--)
73 if (windowstart & (1 << index)) {
74 /* Get the PC from a0 and a1. */
75 pc = MAKE_PC_FROM_RA(a0, pc);
76 /* Read a0 and a1 from the
77 * corresponding position in AREGs.
79 a0 = regs->areg[index * 4];
80 a1 = regs->areg[index * 4 + 1];
85 if (pc == 0 || pc >= TASK_SIZE || ufn(&frame, data))
90 /* We are done with the register window, we need to
91 * look through the stack.
96 /* Start from the a1 register. */
97 /* a1 = regs->areg[1]; */
98 while (a0 != 0 && depth--) {
99 struct frame_start frame_start;
100 /* Get the location for a1, a0 for the
101 * previous frame from the current a1.
103 unsigned long *psp = (unsigned long *)a1;
107 /* Check if the region is OK to access. */
108 if (!access_ok(VERIFY_READ, psp, sizeof(frame_start)))
110 /* Copy a1, a0 from user space stack frame. */
111 if (__copy_from_user_inatomic(&frame_start, psp,
112 sizeof(frame_start)))
115 pc = MAKE_PC_FROM_RA(a0, pc);
122 if (pc == 0 || pc >= TASK_SIZE || ufn(&frame, data))
126 EXPORT_SYMBOL(xtensa_backtrace_user);
128 void xtensa_backtrace_kernel(struct pt_regs *regs, unsigned int depth,
129 int (*kfn)(struct stackframe *frame, void *data),
130 int (*ufn)(struct stackframe *frame, void *data),
133 unsigned long pc = regs->depc > VALID_DOUBLE_EXCEPTION_ADDRESS ?
134 regs->depc : regs->pc;
135 unsigned long sp_start, sp_end;
136 unsigned long a0 = regs->areg[0];
137 unsigned long a1 = regs->areg[1];
139 sp_start = a1 & ~(THREAD_SIZE - 1);
140 sp_end = sp_start + THREAD_SIZE;
142 /* Spill the register window to the stack first. */
145 /* Read the stack frames one by one and create the PC
146 * from the a0 and a1 registers saved there.
148 while (a1 > sp_start && a1 < sp_end && depth--) {
149 struct stackframe frame;
150 unsigned long *psp = (unsigned long *)a1;
155 if (kernel_text_address(pc) && kfn(&frame, data))
158 if (pc == (unsigned long)&common_exception_return) {
159 regs = (struct pt_regs *)a1;
160 if (user_mode(regs)) {
163 xtensa_backtrace_user(regs, depth, ufn, data);
173 pc = MAKE_PC_FROM_RA(a0, pc);
178 EXPORT_SYMBOL(xtensa_backtrace_kernel);
182 void walk_stackframe(unsigned long *sp,
183 int (*fn)(struct stackframe *frame, void *data),
186 unsigned long a0, a1;
187 unsigned long sp_end;
189 a1 = (unsigned long)sp;
190 sp_end = ALIGN(a1, THREAD_SIZE);
194 while (a1 < sp_end) {
195 struct stackframe frame;
197 sp = (unsigned long *)a1;
202 if (a1 <= (unsigned long)sp)
205 frame.pc = MAKE_PC_FROM_RA(a0, a1);
208 if (fn(&frame, data))
213 #ifdef CONFIG_STACKTRACE
215 struct stack_trace_data {
216 struct stack_trace *trace;
220 static int stack_trace_cb(struct stackframe *frame, void *data)
222 struct stack_trace_data *trace_data = data;
223 struct stack_trace *trace = trace_data->trace;
225 if (trace_data->skip) {
229 if (!kernel_text_address(frame->pc))
232 trace->entries[trace->nr_entries++] = frame->pc;
233 return trace->nr_entries >= trace->max_entries;
236 void save_stack_trace_tsk(struct task_struct *task, struct stack_trace *trace)
238 struct stack_trace_data trace_data = {
242 walk_stackframe(stack_pointer(task), stack_trace_cb, &trace_data);
244 EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
246 void save_stack_trace(struct stack_trace *trace)
248 save_stack_trace_tsk(current, trace);
250 EXPORT_SYMBOL_GPL(save_stack_trace);
254 #ifdef CONFIG_FRAME_POINTER
256 struct return_addr_data {
261 static int return_address_cb(struct stackframe *frame, void *data)
263 struct return_addr_data *r = data;
269 if (!kernel_text_address(frame->pc))
275 unsigned long return_address(unsigned level)
277 struct return_addr_data r = {
280 walk_stackframe(stack_pointer(NULL), return_address_cb, &r);
283 EXPORT_SYMBOL(return_address);