Path: blob/master/arch/unicore32/kernel/stacktrace.c
10817 views
/*1* linux/arch/unicore32/kernel/stacktrace.c2*3* Code specific to PKUnity SoC and UniCore ISA4*5* Copyright (C) 2001-2010 GUAN Xue-tao6*7* This program is free software; you can redistribute it and/or modify8* it under the terms of the GNU General Public License version 2 as9* published by the Free Software Foundation.10*/11#include <linux/module.h>12#include <linux/sched.h>13#include <linux/stacktrace.h>1415#include <asm/stacktrace.h>1617#if defined(CONFIG_FRAME_POINTER)18/*19* Unwind the current stack frame and store the new register values in the20* structure passed as argument. Unwinding is equivalent to a function return,21* hence the new PC value rather than LR should be used for backtrace.22*23* With framepointer enabled, a simple function prologue looks like this:24* mov ip, sp25* stmdb sp!, {fp, ip, lr, pc}26* sub fp, ip, #427*28* A simple function epilogue looks like this:29* ldm sp, {fp, sp, pc}30*31* Note that with framepointer enabled, even the leaf functions have the same32* prologue and epilogue, therefore we can ignore the LR value in this case.33*/34int notrace unwind_frame(struct stackframe *frame)35{36unsigned long high, low;37unsigned long fp = frame->fp;3839/* only go to a higher address on the stack */40low = frame->sp;41high = ALIGN(low, THREAD_SIZE);4243/* check current frame pointer is within bounds */44if (fp < (low + 12) || fp + 4 >= high)45return -EINVAL;4647/* restore the registers from the stack frame */48frame->fp = *(unsigned long *)(fp - 12);49frame->sp = *(unsigned long *)(fp - 8);50frame->pc = *(unsigned long *)(fp - 4);5152return 0;53}54#endif5556void notrace walk_stackframe(struct stackframe *frame,57int (*fn)(struct stackframe *, void *), void *data)58{59while (1) {60int ret;6162if (fn(frame, data))63break;64ret = unwind_frame(frame);65if (ret < 0)66break;67}68}69EXPORT_SYMBOL(walk_stackframe);7071#ifdef CONFIG_STACKTRACE72struct stack_trace_data {73struct stack_trace *trace;74unsigned int no_sched_functions;75unsigned int skip;76};7778static int save_trace(struct stackframe *frame, void *d)79{80struct stack_trace_data *data = d;81struct stack_trace *trace = data->trace;82unsigned long addr = frame->pc;8384if (data->no_sched_functions && in_sched_functions(addr))85return 0;86if (data->skip) {87data->skip--;88return 0;89}9091trace->entries[trace->nr_entries++] = addr;9293return trace->nr_entries >= trace->max_entries;94}9596void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)97{98struct stack_trace_data data;99struct stackframe frame;100101data.trace = trace;102data.skip = trace->skip;103104if (tsk != current) {105data.no_sched_functions = 1;106frame.fp = thread_saved_fp(tsk);107frame.sp = thread_saved_sp(tsk);108frame.lr = 0; /* recovered from the stack */109frame.pc = thread_saved_pc(tsk);110} else {111register unsigned long current_sp asm("sp");112113data.no_sched_functions = 0;114frame.fp = (unsigned long)__builtin_frame_address(0);115frame.sp = current_sp;116frame.lr = (unsigned long)__builtin_return_address(0);117frame.pc = (unsigned long)save_stack_trace_tsk;118}119120walk_stackframe(&frame, save_trace, &data);121if (trace->nr_entries < trace->max_entries)122trace->entries[trace->nr_entries++] = ULONG_MAX;123}124125void save_stack_trace(struct stack_trace *trace)126{127save_stack_trace_tsk(current, trace);128}129EXPORT_SYMBOL_GPL(save_stack_trace);130#endif131132133