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))
47 if (IS_ENABLED(CONFIG_USER_ABI_CALL0_ONLY) ||
48 (IS_ENABLED(CONFIG_USER_ABI_CALL0_PROBE) &&
49 !(regs->ps & PS_WOE_MASK)))
54 * 1. Look through the register window for the
55 * previous PCs in the call trace.
57 * 2. Look on the stack.
61 /* Rotate WINDOWSTART to move the bit corresponding to
62 * the current window to the bit #0.
64 windowstart = (windowstart << WSBITS | windowstart) >> windowbase;
66 /* Look for bits that are set, they correspond to
69 for (index = WSBITS - 1; (index > 0) && depth; depth--, index--)
70 if (windowstart & (1 << index)) {
71 /* Get the PC from a0 and a1. */
72 pc = MAKE_PC_FROM_RA(a0, pc);
73 /* Read a0 and a1 from the
74 * corresponding position in AREGs.
76 a0 = regs->areg[index * 4];
77 a1 = regs->areg[index * 4 + 1];
82 if (pc == 0 || pc >= TASK_SIZE || ufn(&frame, data))
87 /* We are done with the register window, we need to
88 * look through the stack.
93 /* Start from the a1 register. */
94 /* a1 = regs->areg[1]; */
95 while (a0 != 0 && depth--) {
96 pc = MAKE_PC_FROM_RA(a0, pc);
98 /* Check if the region is OK to access. */
99 if (!access_ok(&SPILL_SLOT(a1, 0), 8))
101 /* Copy a1, a0 from user space stack frame. */
102 if (__get_user(a0, &SPILL_SLOT(a1, 0)) ||
103 __get_user(a1, &SPILL_SLOT(a1, 1)))
109 if (pc == 0 || pc >= TASK_SIZE || ufn(&frame, data))
113 EXPORT_SYMBOL(xtensa_backtrace_user);
115 void xtensa_backtrace_kernel(struct pt_regs *regs, unsigned int depth,
116 int (*kfn)(struct stackframe *frame, void *data),
117 int (*ufn)(struct stackframe *frame, void *data),
120 unsigned long pc = regs->depc > VALID_DOUBLE_EXCEPTION_ADDRESS ?
121 regs->depc : regs->pc;
122 unsigned long sp_start, sp_end;
123 unsigned long a0 = regs->areg[0];
124 unsigned long a1 = regs->areg[1];
126 sp_start = a1 & ~(THREAD_SIZE - 1);
127 sp_end = sp_start + THREAD_SIZE;
129 /* Spill the register window to the stack first. */
132 /* Read the stack frames one by one and create the PC
133 * from the a0 and a1 registers saved there.
135 while (a1 > sp_start && a1 < sp_end && depth--) {
136 struct stackframe frame;
141 if (kernel_text_address(pc) && kfn(&frame, data))
144 if (pc == (unsigned long)&common_exception_return) {
145 regs = (struct pt_regs *)a1;
146 if (user_mode(regs)) {
149 xtensa_backtrace_user(regs, depth, ufn, data);
159 pc = MAKE_PC_FROM_RA(a0, pc);
160 a0 = SPILL_SLOT(a1, 0);
161 a1 = SPILL_SLOT(a1, 1);
164 EXPORT_SYMBOL(xtensa_backtrace_kernel);
168 void walk_stackframe(unsigned long *sp,
169 int (*fn)(struct stackframe *frame, void *data),
172 unsigned long a0, a1;
173 unsigned long sp_end;
175 a1 = (unsigned long)sp;
176 sp_end = ALIGN(a1, THREAD_SIZE);
180 while (a1 < sp_end) {
181 struct stackframe frame;
183 sp = (unsigned long *)a1;
185 a0 = SPILL_SLOT(a1, 0);
186 a1 = SPILL_SLOT(a1, 1);
188 if (a1 <= (unsigned long)sp)
191 frame.pc = MAKE_PC_FROM_RA(a0, a1);
194 if (fn(&frame, data))
199 #ifdef CONFIG_STACKTRACE
201 struct stack_trace_data {
202 struct stack_trace *trace;
206 static int stack_trace_cb(struct stackframe *frame, void *data)
208 struct stack_trace_data *trace_data = data;
209 struct stack_trace *trace = trace_data->trace;
211 if (trace_data->skip) {
215 if (!kernel_text_address(frame->pc))
218 trace->entries[trace->nr_entries++] = frame->pc;
219 return trace->nr_entries >= trace->max_entries;
222 void save_stack_trace_tsk(struct task_struct *task, struct stack_trace *trace)
224 struct stack_trace_data trace_data = {
228 walk_stackframe(stack_pointer(task), stack_trace_cb, &trace_data);
230 EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
232 void save_stack_trace(struct stack_trace *trace)
234 save_stack_trace_tsk(current, trace);
236 EXPORT_SYMBOL_GPL(save_stack_trace);
240 #ifdef CONFIG_FRAME_POINTER
242 struct return_addr_data {
247 static int return_address_cb(struct stackframe *frame, void *data)
249 struct return_addr_data *r = data;
255 if (!kernel_text_address(frame->pc))
262 * level == 0 is for the return address from the caller of this function,
263 * not from this function itself.
265 unsigned long return_address(unsigned level)
267 struct return_addr_data r = {
270 walk_stackframe(stack_pointer(NULL), return_address_cb, &r);
273 EXPORT_SYMBOL(return_address);