GNU Linux-libre 4.9.317-gnu1
[releases.git] / arch / arc / kernel / stacktrace.c
1 /*
2  *      stacktrace.c : stacktracing APIs needed by rest of kernel
3  *                      (wrappers over ARC dwarf based unwinder)
4  *
5  * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  *  vineetg: aug 2009
12  *  -Implemented CONFIG_STACKTRACE APIs, primarily save_stack_trace_tsk( )
13  *   for displaying task's kernel mode call stack in /proc/<pid>/stack
14  *  -Iterator based approach to have single copy of unwinding core and APIs
15  *   needing unwinding, implement the logic in iterator regarding:
16  *      = which frame onwards to start capture
17  *      = which frame to stop capturing (wchan)
18  *      = specifics of data structs where trace is saved(CONFIG_STACKTRACE etc)
19  *
20  *  vineetg: March 2009
21  *  -Implemented correct versions of thread_saved_pc() and get_wchan()
22  *
23  *  rajeshwarr: 2008
24  *  -Initial implementation
25  */
26
27 #include <linux/ptrace.h>
28 #include <linux/export.h>
29 #include <linux/stacktrace.h>
30 #include <linux/kallsyms.h>
31 #include <asm/arcregs.h>
32 #include <asm/unwind.h>
33 #include <asm/switch_to.h>
34
35 /*-------------------------------------------------------------------------
36  *              Unwinder Iterator
37  *-------------------------------------------------------------------------
38  */
39
40 #ifdef CONFIG_ARC_DW2_UNWIND
41
42 static int
43 seed_unwind_frame_info(struct task_struct *tsk, struct pt_regs *regs,
44                        struct unwind_frame_info *frame_info)
45 {
46         /*
47          * synchronous unwinding (e.g. dump_stack)
48          *  - uses current values of SP and friends
49          */
50         if (regs == NULL && (tsk == NULL || tsk == current)) {
51                 unsigned long fp, sp, blink, ret;
52                 frame_info->task = current;
53
54                 __asm__ __volatile__(
55                         "mov %0,r27\n\t"
56                         "mov %1,r28\n\t"
57                         "mov %2,r31\n\t"
58                         "mov %3,r63\n\t"
59                         : "=r"(fp), "=r"(sp), "=r"(blink), "=r"(ret)
60                 );
61
62                 frame_info->regs.r27 = fp;
63                 frame_info->regs.r28 = sp;
64                 frame_info->regs.r31 = blink;
65                 frame_info->regs.r63 = ret;
66                 frame_info->call_frame = 0;
67         } else if (regs == NULL) {
68                 /*
69                  * Asynchronous unwinding of a likely sleeping task
70                  *  - first ensure it is actually sleeping
71                  *  - if so, it will be in __switch_to, kernel mode SP of task
72                  *    is safe-kept and BLINK at a well known location in there
73                  */
74
75                 if (tsk->state == TASK_RUNNING)
76                         return -1;
77
78                 frame_info->task = tsk;
79
80                 frame_info->regs.r27 = TSK_K_FP(tsk);
81                 frame_info->regs.r28 = TSK_K_ESP(tsk);
82                 frame_info->regs.r31 = TSK_K_BLINK(tsk);
83                 frame_info->regs.r63 = (unsigned int)__switch_to;
84
85                 /* In the prologue of __switch_to, first FP is saved on stack
86                  * and then SP is copied to FP. Dwarf assumes cfa as FP based
87                  * but we didn't save FP. The value retrieved above is FP's
88                  * state in previous frame.
89                  * As a work around for this, we unwind from __switch_to start
90                  * and adjust SP accordingly. The other limitation is that
91                  * __switch_to macro is dwarf rules are not generated for inline
92                  * assembly code
93                  */
94                 frame_info->regs.r27 = 0;
95                 frame_info->regs.r28 += 60;
96                 frame_info->call_frame = 0;
97
98         } else {
99                 /*
100                  * Asynchronous unwinding of intr/exception
101                  *  - Just uses the pt_regs passed
102                  */
103                 frame_info->task = tsk;
104
105                 frame_info->regs.r27 = regs->fp;
106                 frame_info->regs.r28 = regs->sp;
107                 frame_info->regs.r31 = regs->blink;
108                 frame_info->regs.r63 = regs->ret;
109                 frame_info->call_frame = 0;
110         }
111
112         return 0;
113 }
114
115 #endif
116
117 notrace noinline unsigned int
118 arc_unwind_core(struct task_struct *tsk, struct pt_regs *regs,
119                 int (*consumer_fn) (unsigned int, void *), void *arg)
120 {
121 #ifdef CONFIG_ARC_DW2_UNWIND
122         int ret = 0, cnt = 0;
123         unsigned int address;
124         struct unwind_frame_info frame_info;
125
126         if (seed_unwind_frame_info(tsk, regs, &frame_info))
127                 return 0;
128
129         while (1) {
130                 address = UNW_PC(&frame_info);
131
132                 if (!address || !__kernel_text_address(address))
133                         break;
134
135                 if (consumer_fn(address, arg) == -1)
136                         break;
137
138                 ret = arc_unwind(&frame_info);
139                 if (ret)
140                         break;
141
142                 frame_info.regs.r63 = frame_info.regs.r31;
143
144                 if (cnt++ > 128) {
145                         printk("unwinder looping too long, aborting !\n");
146                         return 0;
147                 }
148         }
149
150         return address;         /* return the last address it saw */
151 #else
152         /* On ARC, only Dward based unwinder works. fp based backtracing is
153          * not possible (-fno-omit-frame-pointer) because of the way function
154          * prelogue is setup (callee regs saved and then fp set and not other
155          * way around
156          */
157         pr_warn_once("CONFIG_ARC_DW2_UNWIND needs to be enabled\n");
158         return 0;
159
160 #endif
161 }
162
163 /*-------------------------------------------------------------------------
164  * callbacks called by unwinder iterator to implement kernel APIs
165  *
166  * The callback can return -1 to force the iterator to stop, which by default
167  * keeps going till the bottom-most frame.
168  *-------------------------------------------------------------------------
169  */
170
171 /* Call-back which plugs into unwinding core to dump the stack in
172  * case of panic/OOPs/BUG etc
173  */
174 static int __print_sym(unsigned int address, void *unused)
175 {
176         __print_symbol("  %s\n", address);
177         return 0;
178 }
179
180 #ifdef CONFIG_STACKTRACE
181
182 /* Call-back which plugs into unwinding core to capture the
183  * traces needed by kernel on /proc/<pid>/stack
184  */
185 static int __collect_all(unsigned int address, void *arg)
186 {
187         struct stack_trace *trace = arg;
188
189         if (trace->skip > 0)
190                 trace->skip--;
191         else
192                 trace->entries[trace->nr_entries++] = address;
193
194         if (trace->nr_entries >= trace->max_entries)
195                 return -1;
196
197         return 0;
198 }
199
200 static int __collect_all_but_sched(unsigned int address, void *arg)
201 {
202         struct stack_trace *trace = arg;
203
204         if (in_sched_functions(address))
205                 return 0;
206
207         if (trace->skip > 0)
208                 trace->skip--;
209         else
210                 trace->entries[trace->nr_entries++] = address;
211
212         if (trace->nr_entries >= trace->max_entries)
213                 return -1;
214
215         return 0;
216 }
217
218 #endif
219
220 static int __get_first_nonsched(unsigned int address, void *unused)
221 {
222         if (in_sched_functions(address))
223                 return 0;
224
225         return -1;
226 }
227
228 /*-------------------------------------------------------------------------
229  *              APIs expected by various kernel sub-systems
230  *-------------------------------------------------------------------------
231  */
232
233 noinline void show_stacktrace(struct task_struct *tsk, struct pt_regs *regs)
234 {
235         pr_info("\nStack Trace:\n");
236         arc_unwind_core(tsk, regs, __print_sym, NULL);
237 }
238 EXPORT_SYMBOL(show_stacktrace);
239
240 /* Expected by sched Code */
241 void show_stack(struct task_struct *tsk, unsigned long *sp)
242 {
243         show_stacktrace(tsk, NULL);
244 }
245
246 /* Another API expected by schedular, shows up in "ps" as Wait Channel
247  * Of course just returning schedule( ) would be pointless so unwind until
248  * the function is not in schedular code
249  */
250 unsigned int get_wchan(struct task_struct *tsk)
251 {
252         return arc_unwind_core(tsk, NULL, __get_first_nonsched, NULL);
253 }
254
255 #ifdef CONFIG_STACKTRACE
256
257 /*
258  * API required by CONFIG_STACKTRACE, CONFIG_LATENCYTOP.
259  * A typical use is when /proc/<pid>/stack is queried by userland
260  */
261 void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
262 {
263         /* Assumes @tsk is sleeping so unwinds from __switch_to */
264         arc_unwind_core(tsk, NULL, __collect_all_but_sched, trace);
265 }
266
267 void save_stack_trace(struct stack_trace *trace)
268 {
269         /* Pass NULL for task so it unwinds the current call frame */
270         arc_unwind_core(NULL, NULL, __collect_all, trace);
271 }
272 EXPORT_SYMBOL_GPL(save_stack_trace);
273 #endif