/*1* Backtrace support for Microblaze2*3* Copyright (C) 2010 Digital Design Corporation4*5* Based on arch/sh/kernel/cpu/sh5/unwind.c code which is:6* Copyright (C) 2004 Paul Mundt7* Copyright (C) 2004 Richard Curnow8*9* This file is subject to the terms and conditions of the GNU General Public10* License. See the file "COPYING" in the main directory of this archive11* for more details.12*/1314/* #define DEBUG 1 */15#include <linux/kallsyms.h>16#include <linux/kernel.h>17#include <linux/sched.h>18#include <linux/stacktrace.h>19#include <linux/types.h>20#include <linux/errno.h>21#include <linux/module.h>22#include <linux/io.h>23#include <asm/sections.h>24#include <asm/exceptions.h>25#include <asm/unwind.h>2627struct stack_trace;2829/*30* On Microblaze, finding the previous stack frame is a little tricky.31* At this writing (3/2010), Microblaze does not support CONFIG_FRAME_POINTERS,32* and even if it did, gcc (4.1.2) does not store the frame pointer at33* a consistent offset within each frame. To determine frame size, it is34* necessary to search for the assembly instruction that creates or reclaims35* the frame and extract the size from it.36*37* Microblaze stores the stack pointer in r1, and creates a frame via38*39* addik r1, r1, -FRAME_SIZE40*41* The frame is reclaimed via42*43* addik r1, r1, FRAME_SIZE44*45* Frame creation occurs at or near the top of a function.46* Depending on the compiler, reclaim may occur at the end, or before47* a mid-function return.48*49* A stack frame is usually not created in a leaf function.50*51*/5253/**54* get_frame_size - Extract the stack adjustment from an55* "addik r1, r1, adjust" instruction56* @instr : Microblaze instruction57*58* Return - Number of stack bytes the instruction reserves or reclaims59*/60inline long get_frame_size(unsigned long instr)61{62return abs((s16)(instr & 0xFFFF));63}6465/**66* find_frame_creation - Search backward to find the instruction that creates67* the stack frame (hopefully, for the same function the68* initial PC is in).69* @pc : Program counter at which to begin the search70*71* Return - PC at which stack frame creation occurs72* NULL if this cannot be found, i.e. a leaf function73*/74static unsigned long *find_frame_creation(unsigned long *pc)75{76int i;7778/* NOTE: Distance to search is arbitrary79* 250 works well for most things,80* 750 picks up things like tcp_recvmsg(),81* 1000 needed for fat_fill_super()82*/83for (i = 0; i < 1000; i++, pc--) {84unsigned long instr;85s16 frame_size;8687if (!kernel_text_address((unsigned long) pc))88return NULL;8990instr = *pc;9192/* addik r1, r1, foo ? */93if ((instr & 0xFFFF0000) != 0x30210000)94continue; /* No */9596frame_size = get_frame_size(instr);97if ((frame_size < 8) || (frame_size & 3)) {98pr_debug(" Invalid frame size %d at 0x%p\n",99frame_size, pc);100return NULL;101}102103pr_debug(" Found frame creation at 0x%p, size %d\n", pc,104frame_size);105return pc;106}107108return NULL;109}110111/**112* lookup_prev_stack_frame - Find the stack frame of the previous function.113* @fp : Frame (stack) pointer for current function114* @pc : Program counter within current function115* @leaf_return : r15 value within current function. If the current function116* is a leaf, this is the caller's return address.117* @pprev_fp : On exit, set to frame (stack) pointer for previous function118* @pprev_pc : On exit, set to current function caller's return address119*120* Return - 0 on success, -EINVAL if the previous frame cannot be found121*/122static int lookup_prev_stack_frame(unsigned long fp, unsigned long pc,123unsigned long leaf_return,124unsigned long *pprev_fp,125unsigned long *pprev_pc)126{127unsigned long *prologue = NULL;128129/* _switch_to is a special leaf function */130if (pc != (unsigned long) &_switch_to)131prologue = find_frame_creation((unsigned long *)pc);132133if (prologue) {134long frame_size = get_frame_size(*prologue);135136*pprev_fp = fp + frame_size;137*pprev_pc = *(unsigned long *)fp;138} else {139if (!leaf_return)140return -EINVAL;141*pprev_pc = leaf_return;142*pprev_fp = fp;143}144145/* NOTE: don't check kernel_text_address here, to allow display146* of userland return address147*/148return (!*pprev_pc || (*pprev_pc & 3)) ? -EINVAL : 0;149}150151static void microblaze_unwind_inner(struct task_struct *task,152unsigned long pc, unsigned long fp,153unsigned long leaf_return,154struct stack_trace *trace);155156/**157* unwind_trap - Unwind through a system trap, that stored previous state158* on the stack.159*/160#ifdef CONFIG_MMU161static inline void unwind_trap(struct task_struct *task, unsigned long pc,162unsigned long fp, struct stack_trace *trace)163{164/* To be implemented */165}166#else167static inline void unwind_trap(struct task_struct *task, unsigned long pc,168unsigned long fp, struct stack_trace *trace)169{170const struct pt_regs *regs = (const struct pt_regs *) fp;171microblaze_unwind_inner(task, regs->pc, regs->r1, regs->r15, trace);172}173#endif174175/**176* microblaze_unwind_inner - Unwind the stack from the specified point177* @task : Task whose stack we are to unwind (may be NULL)178* @pc : Program counter from which we start unwinding179* @fp : Frame (stack) pointer from which we start unwinding180* @leaf_return : Value of r15 at pc. If the function is a leaf, this is181* the caller's return address.182* @trace : Where to store stack backtrace (PC values).183* NULL == print backtrace to kernel log184*/185static void microblaze_unwind_inner(struct task_struct *task,186unsigned long pc, unsigned long fp,187unsigned long leaf_return,188struct stack_trace *trace)189{190int ofs = 0;191192pr_debug(" Unwinding with PC=%p, FP=%p\n", (void *)pc, (void *)fp);193if (!pc || !fp || (pc & 3) || (fp & 3)) {194pr_debug(" Invalid state for unwind, aborting\n");195return;196}197for (; pc != 0;) {198unsigned long next_fp, next_pc = 0;199unsigned long return_to = pc + 2 * sizeof(unsigned long);200const struct trap_handler_info *handler =201µblaze_trap_handlers;202203/* Is previous function the HW exception handler? */204if ((return_to >= (unsigned long)&_hw_exception_handler)205&&(return_to < (unsigned long)&ex_handler_unhandled)) {206/*207* HW exception handler doesn't save all registers,208* so we open-code a special case of unwind_trap()209*/210#ifndef CONFIG_MMU211const struct pt_regs *regs =212(const struct pt_regs *) fp;213#endif214pr_info("HW EXCEPTION\n");215#ifndef CONFIG_MMU216microblaze_unwind_inner(task, regs->r17 - 4,217fp + EX_HANDLER_STACK_SIZ,218regs->r15, trace);219#endif220return;221}222223/* Is previous function a trap handler? */224for (; handler->start_addr; ++handler) {225if ((return_to >= handler->start_addr)226&& (return_to <= handler->end_addr)) {227if (!trace)228pr_info("%s\n", handler->trap_name);229unwind_trap(task, pc, fp, trace);230return;231}232}233pc -= ofs;234235if (trace) {236#ifdef CONFIG_STACKTRACE237if (trace->skip > 0)238trace->skip--;239else240trace->entries[trace->nr_entries++] = pc;241242if (trace->nr_entries >= trace->max_entries)243break;244#endif245} else {246/* Have we reached userland? */247if (unlikely(pc == task_pt_regs(task)->pc)) {248pr_info("[<%p>] PID %lu [%s]\n",249(void *) pc,250(unsigned long) task->pid,251task->comm);252break;253} else254print_ip_sym(pc);255}256257/* Stop when we reach anything not part of the kernel */258if (!kernel_text_address(pc))259break;260261if (lookup_prev_stack_frame(fp, pc, leaf_return, &next_fp,262&next_pc) == 0) {263ofs = sizeof(unsigned long);264pc = next_pc & ~3;265fp = next_fp;266leaf_return = 0;267} else {268pr_debug(" Failed to find previous stack frame\n");269break;270}271272pr_debug(" Next PC=%p, next FP=%p\n",273(void *)next_pc, (void *)next_fp);274}275}276277/**278* microblaze_unwind - Stack unwinder for Microblaze (external entry point)279* @task : Task whose stack we are to unwind (NULL == current)280* @trace : Where to store stack backtrace (PC values).281* NULL == print backtrace to kernel log282*/283void microblaze_unwind(struct task_struct *task, struct stack_trace *trace)284{285if (task) {286if (task == current) {287const struct pt_regs *regs = task_pt_regs(task);288microblaze_unwind_inner(task, regs->pc, regs->r1,289regs->r15, trace);290} else {291struct thread_info *thread_info =292(struct thread_info *)(task->stack);293const struct cpu_context *cpu_context =294&thread_info->cpu_context;295296microblaze_unwind_inner(task,297(unsigned long) &_switch_to,298cpu_context->r1,299cpu_context->r15, trace);300}301} else {302unsigned long pc, fp;303304__asm__ __volatile__ ("or %0, r1, r0" : "=r" (fp));305306__asm__ __volatile__ (307"brlid %0, 0f;"308"nop;"309"0:"310: "=r" (pc)311);312313/* Since we are not a leaf function, use leaf_return = 0 */314microblaze_unwind_inner(current, pc, fp, 0, trace);315}316}317318319320