GuanXuetao | f73670e | 2011-01-15 18:16:21 +0800 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/unicore32/kernel/stacktrace.c |
| 3 | * |
| 4 | * Code specific to PKUnity SoC and UniCore ISA |
| 5 | * |
| 6 | * Copyright (C) 2001-2010 GUAN Xue-tao |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/sched.h> |
Ingo Molnar | b17b015 | 2017-02-08 18:51:35 +0100 | [diff] [blame] | 14 | #include <linux/sched/debug.h> |
GuanXuetao | f73670e | 2011-01-15 18:16:21 +0800 | [diff] [blame] | 15 | #include <linux/stacktrace.h> |
| 16 | |
| 17 | #include <asm/stacktrace.h> |
| 18 | |
| 19 | #if defined(CONFIG_FRAME_POINTER) |
| 20 | /* |
| 21 | * Unwind the current stack frame and store the new register values in the |
| 22 | * structure passed as argument. Unwinding is equivalent to a function return, |
| 23 | * hence the new PC value rather than LR should be used for backtrace. |
| 24 | * |
| 25 | * With framepointer enabled, a simple function prologue looks like this: |
| 26 | * mov ip, sp |
| 27 | * stmdb sp!, {fp, ip, lr, pc} |
| 28 | * sub fp, ip, #4 |
| 29 | * |
| 30 | * A simple function epilogue looks like this: |
| 31 | * ldm sp, {fp, sp, pc} |
| 32 | * |
| 33 | * Note that with framepointer enabled, even the leaf functions have the same |
| 34 | * prologue and epilogue, therefore we can ignore the LR value in this case. |
| 35 | */ |
| 36 | int notrace unwind_frame(struct stackframe *frame) |
| 37 | { |
| 38 | unsigned long high, low; |
| 39 | unsigned long fp = frame->fp; |
| 40 | |
| 41 | /* only go to a higher address on the stack */ |
| 42 | low = frame->sp; |
| 43 | high = ALIGN(low, THREAD_SIZE); |
| 44 | |
| 45 | /* check current frame pointer is within bounds */ |
| 46 | if (fp < (low + 12) || fp + 4 >= high) |
| 47 | return -EINVAL; |
| 48 | |
| 49 | /* restore the registers from the stack frame */ |
| 50 | frame->fp = *(unsigned long *)(fp - 12); |
| 51 | frame->sp = *(unsigned long *)(fp - 8); |
| 52 | frame->pc = *(unsigned long *)(fp - 4); |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | #endif |
| 57 | |
| 58 | void notrace walk_stackframe(struct stackframe *frame, |
| 59 | int (*fn)(struct stackframe *, void *), void *data) |
| 60 | { |
| 61 | while (1) { |
| 62 | int ret; |
| 63 | |
| 64 | if (fn(frame, data)) |
| 65 | break; |
| 66 | ret = unwind_frame(frame); |
| 67 | if (ret < 0) |
| 68 | break; |
| 69 | } |
| 70 | } |
| 71 | EXPORT_SYMBOL(walk_stackframe); |
| 72 | |
| 73 | #ifdef CONFIG_STACKTRACE |
| 74 | struct stack_trace_data { |
| 75 | struct stack_trace *trace; |
| 76 | unsigned int no_sched_functions; |
| 77 | unsigned int skip; |
| 78 | }; |
| 79 | |
| 80 | static int save_trace(struct stackframe *frame, void *d) |
| 81 | { |
| 82 | struct stack_trace_data *data = d; |
| 83 | struct stack_trace *trace = data->trace; |
| 84 | unsigned long addr = frame->pc; |
| 85 | |
| 86 | if (data->no_sched_functions && in_sched_functions(addr)) |
| 87 | return 0; |
| 88 | if (data->skip) { |
| 89 | data->skip--; |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | trace->entries[trace->nr_entries++] = addr; |
| 94 | |
| 95 | return trace->nr_entries >= trace->max_entries; |
| 96 | } |
| 97 | |
| 98 | void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace) |
| 99 | { |
| 100 | struct stack_trace_data data; |
| 101 | struct stackframe frame; |
| 102 | |
| 103 | data.trace = trace; |
| 104 | data.skip = trace->skip; |
| 105 | |
| 106 | if (tsk != current) { |
| 107 | data.no_sched_functions = 1; |
| 108 | frame.fp = thread_saved_fp(tsk); |
| 109 | frame.sp = thread_saved_sp(tsk); |
| 110 | frame.lr = 0; /* recovered from the stack */ |
| 111 | frame.pc = thread_saved_pc(tsk); |
| 112 | } else { |
| 113 | register unsigned long current_sp asm("sp"); |
| 114 | |
| 115 | data.no_sched_functions = 0; |
| 116 | frame.fp = (unsigned long)__builtin_frame_address(0); |
| 117 | frame.sp = current_sp; |
| 118 | frame.lr = (unsigned long)__builtin_return_address(0); |
| 119 | frame.pc = (unsigned long)save_stack_trace_tsk; |
| 120 | } |
| 121 | |
| 122 | walk_stackframe(&frame, save_trace, &data); |
GuanXuetao | f73670e | 2011-01-15 18:16:21 +0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void save_stack_trace(struct stack_trace *trace) |
| 126 | { |
| 127 | save_stack_trace_tsk(current, trace); |
| 128 | } |
| 129 | EXPORT_SYMBOL_GPL(save_stack_trace); |
| 130 | #endif |