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