dccp: do not assume DCCP code is non preemptible
[linux/fpc-iii.git] / arch / xtensa / kernel / stacktrace.c
blob7538d802b65abbc88f2e3769ff77e117282470c7
1 /*
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
6 * for more details.
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
27 * a1 registers.
29 struct frame_start {
30 unsigned long a0;
31 unsigned long a1;
34 void xtensa_backtrace_user(struct pt_regs *regs, unsigned int depth,
35 int (*ufn)(struct stackframe *frame, void *data),
36 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;
44 int index;
46 if (!depth--)
47 return;
49 frame.pc = pc;
50 frame.sp = a1;
52 if (pc == 0 || pc >= TASK_SIZE || ufn(&frame, data))
53 return;
55 /* Two steps:
57 * 1. Look through the register window for the
58 * previous PCs in the call trace.
60 * 2. Look on the stack.
63 /* Step 1. */
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
70 * valid windows.
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];
82 frame.pc = pc;
83 frame.sp = a1;
85 if (pc == 0 || pc >= TASK_SIZE || ufn(&frame, data))
86 return;
89 /* Step 2. */
90 /* We are done with the register window, we need to
91 * look through the stack.
93 if (!depth)
94 return;
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;
105 psp -= 4;
107 /* Check if the region is OK to access. */
108 if (!access_ok(VERIFY_READ, psp, sizeof(frame_start)))
109 return;
110 /* Copy a1, a0 from user space stack frame. */
111 if (__copy_from_user_inatomic(&frame_start, psp,
112 sizeof(frame_start)))
113 return;
115 pc = MAKE_PC_FROM_RA(a0, pc);
116 a0 = frame_start.a0;
117 a1 = frame_start.a1;
119 frame.pc = pc;
120 frame.sp = a1;
122 if (pc == 0 || pc >= TASK_SIZE || ufn(&frame, data))
123 return;
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),
131 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. */
143 spill_registers();
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;
152 frame.pc = pc;
153 frame.sp = a1;
155 if (kernel_text_address(pc) && kfn(&frame, data))
156 return;
158 if (pc == (unsigned long)&common_exception_return) {
159 regs = (struct pt_regs *)a1;
160 if (user_mode(regs)) {
161 if (ufn == NULL)
162 return;
163 xtensa_backtrace_user(regs, depth, ufn, data);
164 return;
166 a0 = regs->areg[0];
167 a1 = regs->areg[1];
168 continue;
171 sp_start = a1;
173 pc = MAKE_PC_FROM_RA(a0, pc);
174 a0 = *(psp - 4);
175 a1 = *(psp - 3);
178 EXPORT_SYMBOL(xtensa_backtrace_kernel);
180 #endif
182 void walk_stackframe(unsigned long *sp,
183 int (*fn)(struct stackframe *frame, void *data),
184 void *data)
186 unsigned long a0, a1;
187 unsigned long sp_end;
189 a1 = (unsigned long)sp;
190 sp_end = ALIGN(a1, THREAD_SIZE);
192 spill_registers();
194 while (a1 < sp_end) {
195 struct stackframe frame;
197 sp = (unsigned long *)a1;
199 a0 = *(sp - 4);
200 a1 = *(sp - 3);
202 if (a1 <= (unsigned long)sp)
203 break;
205 frame.pc = MAKE_PC_FROM_RA(a0, a1);
206 frame.sp = a1;
208 if (fn(&frame, data))
209 return;
213 #ifdef CONFIG_STACKTRACE
215 struct stack_trace_data {
216 struct stack_trace *trace;
217 unsigned skip;
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) {
226 --trace_data->skip;
227 return 0;
229 if (!kernel_text_address(frame->pc))
230 return 0;
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 = {
239 .trace = trace,
240 .skip = trace->skip,
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);
252 #endif
254 #ifdef CONFIG_FRAME_POINTER
256 struct return_addr_data {
257 unsigned long addr;
258 unsigned skip;
261 static int return_address_cb(struct stackframe *frame, void *data)
263 struct return_addr_data *r = data;
265 if (r->skip) {
266 --r->skip;
267 return 0;
269 if (!kernel_text_address(frame->pc))
270 return 0;
271 r->addr = frame->pc;
272 return 1;
275 unsigned long return_address(unsigned level)
277 struct return_addr_data r = {
278 .skip = level + 1,
280 walk_stackframe(stack_pointer(NULL), return_address_cb, &r);
281 return r.addr;
283 EXPORT_SYMBOL(return_address);
285 #endif