Merge branch 'drm-intel-fixes' of git://people.freedesktop.org/~keithp/linux
[linux-btrfs-devel.git] / arch / microblaze / kernel / unwind.c
blob9781a528cfc9179c3c50c2f457fc62db54131b8b
1 /*
2 * Backtrace support for Microblaze
4 * Copyright (C) 2010 Digital Design Corporation
6 * Based on arch/sh/kernel/cpu/sh5/unwind.c code which is:
7 * Copyright (C) 2004 Paul Mundt
8 * Copyright (C) 2004 Richard Curnow
10 * This file is subject to the terms and conditions of the GNU General Public
11 * License. See the file "COPYING" in the main directory of this archive
12 * for more details.
15 /* #define DEBUG 1 */
16 #include <linux/kallsyms.h>
17 #include <linux/kernel.h>
18 #include <linux/sched.h>
19 #include <linux/stacktrace.h>
20 #include <linux/types.h>
21 #include <linux/errno.h>
22 #include <linux/module.h>
23 #include <linux/io.h>
24 #include <asm/sections.h>
25 #include <asm/exceptions.h>
26 #include <asm/unwind.h>
28 struct stack_trace;
31 * On Microblaze, finding the previous stack frame is a little tricky.
32 * At this writing (3/2010), Microblaze does not support CONFIG_FRAME_POINTERS,
33 * and even if it did, gcc (4.1.2) does not store the frame pointer at
34 * a consistent offset within each frame. To determine frame size, it is
35 * necessary to search for the assembly instruction that creates or reclaims
36 * the frame and extract the size from it.
38 * Microblaze stores the stack pointer in r1, and creates a frame via
40 * addik r1, r1, -FRAME_SIZE
42 * The frame is reclaimed via
44 * addik r1, r1, FRAME_SIZE
46 * Frame creation occurs at or near the top of a function.
47 * Depending on the compiler, reclaim may occur at the end, or before
48 * a mid-function return.
50 * A stack frame is usually not created in a leaf function.
54 /**
55 * get_frame_size - Extract the stack adjustment from an
56 * "addik r1, r1, adjust" instruction
57 * @instr : Microblaze instruction
59 * Return - Number of stack bytes the instruction reserves or reclaims
61 inline long get_frame_size(unsigned long instr)
63 return abs((s16)(instr & 0xFFFF));
66 /**
67 * find_frame_creation - Search backward to find the instruction that creates
68 * the stack frame (hopefully, for the same function the
69 * initial PC is in).
70 * @pc : Program counter at which to begin the search
72 * Return - PC at which stack frame creation occurs
73 * NULL if this cannot be found, i.e. a leaf function
75 static unsigned long *find_frame_creation(unsigned long *pc)
77 int i;
79 /* NOTE: Distance to search is arbitrary
80 * 250 works well for most things,
81 * 750 picks up things like tcp_recvmsg(),
82 * 1000 needed for fat_fill_super()
84 for (i = 0; i < 1000; i++, pc--) {
85 unsigned long instr;
86 s16 frame_size;
88 if (!kernel_text_address((unsigned long) pc))
89 return NULL;
91 instr = *pc;
93 /* addik r1, r1, foo ? */
94 if ((instr & 0xFFFF0000) != 0x30210000)
95 continue; /* No */
97 frame_size = get_frame_size(instr);
98 if ((frame_size < 8) || (frame_size & 3)) {
99 pr_debug(" Invalid frame size %d at 0x%p\n",
100 frame_size, pc);
101 return NULL;
104 pr_debug(" Found frame creation at 0x%p, size %d\n", pc,
105 frame_size);
106 return pc;
109 return NULL;
113 * lookup_prev_stack_frame - Find the stack frame of the previous function.
114 * @fp : Frame (stack) pointer for current function
115 * @pc : Program counter within current function
116 * @leaf_return : r15 value within current function. If the current function
117 * is a leaf, this is the caller's return address.
118 * @pprev_fp : On exit, set to frame (stack) pointer for previous function
119 * @pprev_pc : On exit, set to current function caller's return address
121 * Return - 0 on success, -EINVAL if the previous frame cannot be found
123 static int lookup_prev_stack_frame(unsigned long fp, unsigned long pc,
124 unsigned long leaf_return,
125 unsigned long *pprev_fp,
126 unsigned long *pprev_pc)
128 unsigned long *prologue = NULL;
130 /* _switch_to is a special leaf function */
131 if (pc != (unsigned long) &_switch_to)
132 prologue = find_frame_creation((unsigned long *)pc);
134 if (prologue) {
135 long frame_size = get_frame_size(*prologue);
137 *pprev_fp = fp + frame_size;
138 *pprev_pc = *(unsigned long *)fp;
139 } else {
140 if (!leaf_return)
141 return -EINVAL;
142 *pprev_pc = leaf_return;
143 *pprev_fp = fp;
146 /* NOTE: don't check kernel_text_address here, to allow display
147 * of userland return address
149 return (!*pprev_pc || (*pprev_pc & 3)) ? -EINVAL : 0;
152 static void microblaze_unwind_inner(struct task_struct *task,
153 unsigned long pc, unsigned long fp,
154 unsigned long leaf_return,
155 struct stack_trace *trace);
158 * unwind_trap - Unwind through a system trap, that stored previous state
159 * on the stack.
161 #ifdef CONFIG_MMU
162 static inline void unwind_trap(struct task_struct *task, unsigned long pc,
163 unsigned long fp, struct stack_trace *trace)
165 /* To be implemented */
167 #else
168 static inline void unwind_trap(struct task_struct *task, unsigned long pc,
169 unsigned long fp, struct stack_trace *trace)
171 const struct pt_regs *regs = (const struct pt_regs *) fp;
172 microblaze_unwind_inner(task, regs->pc, regs->r1, regs->r15, trace);
174 #endif
177 * microblaze_unwind_inner - Unwind the stack from the specified point
178 * @task : Task whose stack we are to unwind (may be NULL)
179 * @pc : Program counter from which we start unwinding
180 * @fp : Frame (stack) pointer from which we start unwinding
181 * @leaf_return : Value of r15 at pc. If the function is a leaf, this is
182 * the caller's return address.
183 * @trace : Where to store stack backtrace (PC values).
184 * NULL == print backtrace to kernel log
186 static void microblaze_unwind_inner(struct task_struct *task,
187 unsigned long pc, unsigned long fp,
188 unsigned long leaf_return,
189 struct stack_trace *trace)
191 int ofs = 0;
193 pr_debug(" Unwinding with PC=%p, FP=%p\n", (void *)pc, (void *)fp);
194 if (!pc || !fp || (pc & 3) || (fp & 3)) {
195 pr_debug(" Invalid state for unwind, aborting\n");
196 return;
198 for (; pc != 0;) {
199 unsigned long next_fp, next_pc = 0;
200 unsigned long return_to = pc + 2 * sizeof(unsigned long);
201 const struct trap_handler_info *handler =
202 &microblaze_trap_handlers;
204 /* Is previous function the HW exception handler? */
205 if ((return_to >= (unsigned long)&_hw_exception_handler)
206 &&(return_to < (unsigned long)&ex_handler_unhandled)) {
208 * HW exception handler doesn't save all registers,
209 * so we open-code a special case of unwind_trap()
211 #ifndef CONFIG_MMU
212 const struct pt_regs *regs =
213 (const struct pt_regs *) fp;
214 #endif
215 pr_info("HW EXCEPTION\n");
216 #ifndef CONFIG_MMU
217 microblaze_unwind_inner(task, regs->r17 - 4,
218 fp + EX_HANDLER_STACK_SIZ,
219 regs->r15, trace);
220 #endif
221 return;
224 /* Is previous function a trap handler? */
225 for (; handler->start_addr; ++handler) {
226 if ((return_to >= handler->start_addr)
227 && (return_to <= handler->end_addr)) {
228 if (!trace)
229 pr_info("%s\n", handler->trap_name);
230 unwind_trap(task, pc, fp, trace);
231 return;
234 pc -= ofs;
236 if (trace) {
237 #ifdef CONFIG_STACKTRACE
238 if (trace->skip > 0)
239 trace->skip--;
240 else
241 trace->entries[trace->nr_entries++] = pc;
243 if (trace->nr_entries >= trace->max_entries)
244 break;
245 #endif
246 } else {
247 /* Have we reached userland? */
248 if (unlikely(pc == task_pt_regs(task)->pc)) {
249 pr_info("[<%p>] PID %lu [%s]\n",
250 (void *) pc,
251 (unsigned long) task->pid,
252 task->comm);
253 break;
254 } else
255 print_ip_sym(pc);
258 /* Stop when we reach anything not part of the kernel */
259 if (!kernel_text_address(pc))
260 break;
262 if (lookup_prev_stack_frame(fp, pc, leaf_return, &next_fp,
263 &next_pc) == 0) {
264 ofs = sizeof(unsigned long);
265 pc = next_pc & ~3;
266 fp = next_fp;
267 leaf_return = 0;
268 } else {
269 pr_debug(" Failed to find previous stack frame\n");
270 break;
273 pr_debug(" Next PC=%p, next FP=%p\n",
274 (void *)next_pc, (void *)next_fp);
279 * microblaze_unwind - Stack unwinder for Microblaze (external entry point)
280 * @task : Task whose stack we are to unwind (NULL == current)
281 * @trace : Where to store stack backtrace (PC values).
282 * NULL == print backtrace to kernel log
284 void microblaze_unwind(struct task_struct *task, struct stack_trace *trace)
286 if (task) {
287 if (task == current) {
288 const struct pt_regs *regs = task_pt_regs(task);
289 microblaze_unwind_inner(task, regs->pc, regs->r1,
290 regs->r15, trace);
291 } else {
292 struct thread_info *thread_info =
293 (struct thread_info *)(task->stack);
294 const struct cpu_context *cpu_context =
295 &thread_info->cpu_context;
297 microblaze_unwind_inner(task,
298 (unsigned long) &_switch_to,
299 cpu_context->r1,
300 cpu_context->r15, trace);
302 } else {
303 unsigned long pc, fp;
305 __asm__ __volatile__ ("or %0, r1, r0" : "=r" (fp));
307 __asm__ __volatile__ (
308 "brlid %0, 0f;"
309 "nop;"
310 "0:"
311 : "=r" (pc)
314 /* Since we are not a leaf function, use leaf_return = 0 */
315 microblaze_unwind_inner(current, pc, fp, 0, trace);