Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / arch / unicore32 / kernel / stacktrace.c
blob9976e767d51c2eca3803c1dd5f4210549ccf2dc1
1 /*
2 * linux/arch/unicore32/kernel/stacktrace.c
4 * Code specific to PKUnity SoC and UniCore ISA
6 * Copyright (C) 2001-2010 GUAN Xue-tao
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.
12 #include <linux/module.h>
13 #include <linux/sched.h>
14 #include <linux/sched/debug.h>
15 #include <linux/stacktrace.h>
17 #include <asm/stacktrace.h>
19 #if defined(CONFIG_FRAME_POINTER)
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.
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
30 * A simple function epilogue looks like this:
31 * ldm sp, {fp, sp, pc}
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.
36 int notrace unwind_frame(struct stackframe *frame)
38 unsigned long high, low;
39 unsigned long fp = frame->fp;
41 /* only go to a higher address on the stack */
42 low = frame->sp;
43 high = ALIGN(low, THREAD_SIZE);
45 /* check current frame pointer is within bounds */
46 if (fp < (low + 12) || fp + 4 >= high)
47 return -EINVAL;
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);
54 return 0;
56 #endif
58 void notrace walk_stackframe(struct stackframe *frame,
59 int (*fn)(struct stackframe *, void *), void *data)
61 while (1) {
62 int ret;
64 if (fn(frame, data))
65 break;
66 ret = unwind_frame(frame);
67 if (ret < 0)
68 break;
71 EXPORT_SYMBOL(walk_stackframe);
73 #ifdef CONFIG_STACKTRACE
74 struct stack_trace_data {
75 struct stack_trace *trace;
76 unsigned int no_sched_functions;
77 unsigned int skip;
80 static int save_trace(struct stackframe *frame, void *d)
82 struct stack_trace_data *data = d;
83 struct stack_trace *trace = data->trace;
84 unsigned long addr = frame->pc;
86 if (data->no_sched_functions && in_sched_functions(addr))
87 return 0;
88 if (data->skip) {
89 data->skip--;
90 return 0;
93 trace->entries[trace->nr_entries++] = addr;
95 return trace->nr_entries >= trace->max_entries;
98 void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
100 struct stack_trace_data data;
101 struct stackframe frame;
103 data.trace = trace;
104 data.skip = trace->skip;
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");
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;
122 walk_stackframe(&frame, save_trace, &data);
123 if (trace->nr_entries < trace->max_entries)
124 trace->entries[trace->nr_entries++] = ULONG_MAX;
127 void save_stack_trace(struct stack_trace *trace)
129 save_stack_trace_tsk(current, trace);
131 EXPORT_SYMBOL_GPL(save_stack_trace);
132 #endif