1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/sched.h>
16#include <linux/kernel.h>
17#include <linux/kprobes.h>
18#include <linux/module.h>
19#include <linux/pfn.h>
20#include <linux/kallsyms.h>
21#include <linux/stacktrace.h>
22#include <linux/uaccess.h>
23#include <linux/mmzone.h>
24#include <linux/dcache.h>
25#include <linux/fs.h>
26#include <linux/string.h>
27#include <asm/backtrace.h>
28#include <asm/page.h>
29#include <asm/ucontext.h>
30#include <asm/switch_to.h>
31#include <asm/sigframe.h>
32#include <asm/stack.h>
33#include <asm/vdso.h>
34#include <arch/abi.h>
35#include <arch/interrupts.h>
36
37#define KBT_ONGOING 0
38#define KBT_DONE 1
39#define KBT_RUNNING 2
40#define KBT_LOOP 3
41
42
43static int in_kernel_stack(struct KBacktraceIterator *kbt, unsigned long sp)
44{
45 ulong kstack_base = (ulong) kbt->task->stack;
46 if (kstack_base == 0)
47 return sp >= PAGE_OFFSET && sp < (unsigned long)high_memory;
48 return sp >= kstack_base && sp < kstack_base + THREAD_SIZE;
49}
50
51
52static bool read_memory_func(void *result, unsigned long address,
53 unsigned int size, void *vkbt)
54{
55 int retval;
56 struct KBacktraceIterator *kbt = (struct KBacktraceIterator *)vkbt;
57
58 if (address == 0)
59 return 0;
60 if (__kernel_text_address(address)) {
61
62 } else if (address >= PAGE_OFFSET) {
63
64 if (!in_kernel_stack(kbt, address))
65 return 0;
66 } else if (!kbt->is_current) {
67 return 0;
68 }
69 pagefault_disable();
70 retval = __copy_from_user_inatomic(result,
71 (void __user __force *)address,
72 size);
73 pagefault_enable();
74 return (retval == 0);
75}
76
77
78static struct pt_regs *valid_fault_handler(struct KBacktraceIterator* kbt)
79{
80 const char *fault = NULL;
81 char fault_buf[64];
82 unsigned long sp = kbt->it.sp;
83 struct pt_regs *p;
84
85 if (sp % sizeof(long) != 0)
86 return NULL;
87 if (!in_kernel_stack(kbt, sp))
88 return NULL;
89 if (!in_kernel_stack(kbt, sp + C_ABI_SAVE_AREA_SIZE + PTREGS_SIZE-1))
90 return NULL;
91 p = (struct pt_regs *)(sp + C_ABI_SAVE_AREA_SIZE);
92 if (p->faultnum == INT_SWINT_1 || p->faultnum == INT_SWINT_1_SIGRETURN)
93 fault = "syscall";
94 else {
95 if (kbt->verbose) {
96 snprintf(fault_buf, sizeof(fault_buf),
97 "interrupt %ld", p->faultnum);
98 fault = fault_buf;
99 }
100 }
101 if (EX1_PL(p->ex1) == KERNEL_PL &&
102 __kernel_text_address(p->pc) &&
103 in_kernel_stack(kbt, p->sp) &&
104 p->sp >= sp) {
105 if (kbt->verbose)
106 pr_err(" <%s while in kernel mode>\n", fault);
107 } else if (user_mode(p) &&
108 p->sp < PAGE_OFFSET && p->sp != 0) {
109 if (kbt->verbose)
110 pr_err(" <%s while in user mode>\n", fault);
111 } else if (kbt->verbose) {
112 pr_err(" (odd fault: pc %#lx, sp %#lx, ex1 %#lx?)\n",
113 p->pc, p->sp, p->ex1);
114 p = NULL;
115 }
116 if (!kbt->profile || ((1ULL << p->faultnum) & QUEUED_INTERRUPTS) == 0)
117 return p;
118 return NULL;
119}
120
121
122static int is_sigreturn(unsigned long pc)
123{
124 return current->mm && (pc == VDSO_SYM(&__vdso_rt_sigreturn));
125}
126
127
128static struct pt_regs *valid_sigframe(struct KBacktraceIterator* kbt,
129 struct rt_sigframe* kframe)
130{
131 BacktraceIterator *b = &kbt->it;
132
133 if (is_sigreturn(b->pc) && b->sp < PAGE_OFFSET &&
134 b->sp % sizeof(long) == 0) {
135 int retval;
136 pagefault_disable();
137 retval = __copy_from_user_inatomic(
138 kframe, (void __user __force *)b->sp,
139 sizeof(*kframe));
140 pagefault_enable();
141 if (retval != 0 ||
142 (unsigned int)(kframe->info.si_signo) >= _NSIG)
143 return NULL;
144 if (kbt->verbose) {
145 pr_err(" <received signal %d>\n",
146 kframe->info.si_signo);
147 }
148 return (struct pt_regs *)&kframe->uc.uc_mcontext;
149 }
150 return NULL;
151}
152
153static int KBacktraceIterator_is_sigreturn(struct KBacktraceIterator *kbt)
154{
155 return is_sigreturn(kbt->it.pc);
156}
157
158static int KBacktraceIterator_restart(struct KBacktraceIterator *kbt)
159{
160 struct pt_regs *p;
161 struct rt_sigframe kframe;
162
163 p = valid_fault_handler(kbt);
164 if (p == NULL)
165 p = valid_sigframe(kbt, &kframe);
166 if (p == NULL)
167 return 0;
168 backtrace_init(&kbt->it, read_memory_func, kbt,
169 p->pc, p->lr, p->sp, p->regs[52]);
170 kbt->new_context = 1;
171 return 1;
172}
173
174
175static int KBacktraceIterator_next_item_inclusive(
176 struct KBacktraceIterator *kbt)
177{
178 for (;;) {
179 do {
180 if (!KBacktraceIterator_is_sigreturn(kbt))
181 return KBT_ONGOING;
182 } while (backtrace_next(&kbt->it));
183
184 if (!KBacktraceIterator_restart(kbt))
185 return KBT_DONE;
186 }
187}
188
189
190
191
192
193
194
195
196
197static void validate_stack(struct pt_regs *regs)
198{
199 int cpu = raw_smp_processor_id();
200 unsigned long ksp0 = get_current_ksp0();
201 unsigned long ksp0_base = ksp0 & -THREAD_SIZE;
202 unsigned long sp = stack_pointer;
203
204 if (EX1_PL(regs->ex1) == KERNEL_PL && regs->sp >= ksp0) {
205 pr_err("WARNING: cpu %d: kernel stack %#lx..%#lx underrun!\n"
206 " sp %#lx (%#lx in caller), caller pc %#lx, lr %#lx\n",
207 cpu, ksp0_base, ksp0, sp, regs->sp, regs->pc, regs->lr);
208 }
209
210 else if (sp < ksp0_base + sizeof(struct thread_info)) {
211 pr_err("WARNING: cpu %d: kernel stack %#lx..%#lx overrun!\n"
212 " sp %#lx (%#lx in caller), caller pc %#lx, lr %#lx\n",
213 cpu, ksp0_base, ksp0, sp, regs->sp, regs->pc, regs->lr);
214 }
215}
216
217void KBacktraceIterator_init(struct KBacktraceIterator *kbt,
218 struct task_struct *t, struct pt_regs *regs)
219{
220 unsigned long pc, lr, sp, r52;
221 int is_current;
222
223
224
225
226
227 is_current = (t == NULL || t == current);
228 kbt->is_current = is_current;
229 if (is_current)
230 t = validate_current();
231 kbt->task = t;
232 kbt->verbose = 0;
233 kbt->profile = 0;
234 kbt->end = KBT_ONGOING;
235 kbt->new_context = 1;
236 if (is_current)
237 validate_stack(regs);
238
239 if (regs == NULL) {
240 if (is_current || t->state == TASK_RUNNING) {
241
242 kbt->end = KBT_RUNNING;
243 return;
244 }
245 pc = get_switch_to_pc();
246 lr = t->thread.pc;
247 sp = t->thread.ksp;
248 r52 = 0;
249 } else {
250 pc = regs->pc;
251 lr = regs->lr;
252 sp = regs->sp;
253 r52 = regs->regs[52];
254 }
255
256 backtrace_init(&kbt->it, read_memory_func, kbt, pc, lr, sp, r52);
257 kbt->end = KBacktraceIterator_next_item_inclusive(kbt);
258}
259EXPORT_SYMBOL(KBacktraceIterator_init);
260
261int KBacktraceIterator_end(struct KBacktraceIterator *kbt)
262{
263 return kbt->end != KBT_ONGOING;
264}
265EXPORT_SYMBOL(KBacktraceIterator_end);
266
267void KBacktraceIterator_next(struct KBacktraceIterator *kbt)
268{
269 unsigned long old_pc = kbt->it.pc, old_sp = kbt->it.sp;
270 kbt->new_context = 0;
271 if (!backtrace_next(&kbt->it) && !KBacktraceIterator_restart(kbt)) {
272 kbt->end = KBT_DONE;
273 return;
274 }
275 kbt->end = KBacktraceIterator_next_item_inclusive(kbt);
276 if (old_pc == kbt->it.pc && old_sp == kbt->it.sp) {
277
278 kbt->end = KBT_LOOP;
279 }
280}
281EXPORT_SYMBOL(KBacktraceIterator_next);
282
283static void describe_addr(struct KBacktraceIterator *kbt,
284 unsigned long address,
285 int have_mmap_sem, char *buf, size_t bufsize)
286{
287 struct vm_area_struct *vma;
288 size_t namelen, remaining;
289 unsigned long size, offset, adjust;
290 char *p, *modname;
291 const char *name;
292 int rc;
293
294
295
296
297
298
299
300 adjust = !kbt->new_context;
301 address -= adjust;
302
303 if (address >= PAGE_OFFSET) {
304
305 BUG_ON(bufsize < KSYM_NAME_LEN);
306 name = kallsyms_lookup(address, &size, &offset,
307 &modname, buf);
308 if (name == NULL) {
309 buf[0] = '\0';
310 return;
311 }
312 namelen = strlen(buf);
313 remaining = (bufsize - 1) - namelen;
314 p = buf + namelen;
315 rc = snprintf(p, remaining, "+%#lx/%#lx ",
316 offset + adjust, size);
317 if (modname && rc < remaining)
318 snprintf(p + rc, remaining - rc, "[%s] ", modname);
319 buf[bufsize-1] = '\0';
320 return;
321 }
322
323
324 buf[0] = '\0';
325 if (!have_mmap_sem)
326 return;
327
328
329 vma = find_vma(kbt->task->mm, address);
330 if (vma == NULL || address < vma->vm_start) {
331 snprintf(buf, bufsize, "[unmapped address] ");
332 return;
333 }
334
335 if (vma->vm_file) {
336 p = d_path(&vma->vm_file->f_path, buf, bufsize);
337 if (IS_ERR(p))
338 p = "?";
339 name = kbasename(p);
340 } else {
341 name = "anon";
342 }
343
344
345 namelen = strlen(name);
346 remaining = (bufsize - 1) - namelen;
347 memmove(buf, name, namelen);
348 snprintf(buf + namelen, remaining, "[%lx+%lx] ",
349 vma->vm_start, vma->vm_end - vma->vm_start);
350}
351
352
353
354
355
356
357static bool start_backtrace(void)
358{
359 if (current->thread.in_backtrace) {
360 pr_err("Backtrace requested while in backtrace!\n");
361 return false;
362 }
363 current->thread.in_backtrace = true;
364 return true;
365}
366
367static void end_backtrace(void)
368{
369 current->thread.in_backtrace = false;
370}
371
372
373
374
375
376
377void tile_show_stack(struct KBacktraceIterator *kbt, int headers)
378{
379 int i;
380 int have_mmap_sem = 0;
381
382 if (!start_backtrace())
383 return;
384 if (headers) {
385
386
387
388
389
390 pr_err("Starting stack dump of tid %d, pid %d (%s) on cpu %d at cycle %lld\n",
391 kbt->task->pid, kbt->task->tgid, kbt->task->comm,
392 raw_smp_processor_id(), get_cycles());
393 }
394 kbt->verbose = 1;
395 i = 0;
396 for (; !KBacktraceIterator_end(kbt); KBacktraceIterator_next(kbt)) {
397 char namebuf[KSYM_NAME_LEN+100];
398 unsigned long address = kbt->it.pc;
399
400
401 if (address < PAGE_OFFSET && !have_mmap_sem && kbt->task->mm)
402 have_mmap_sem =
403 down_read_trylock(&kbt->task->mm->mmap_sem);
404
405 describe_addr(kbt, address, have_mmap_sem,
406 namebuf, sizeof(namebuf));
407
408 pr_err(" frame %d: 0x%lx %s(sp 0x%lx)\n",
409 i++, address, namebuf, (unsigned long)(kbt->it.sp));
410
411 if (i >= 100) {
412 pr_err("Stack dump truncated (%d frames)\n", i);
413 break;
414 }
415 }
416 if (kbt->end == KBT_LOOP)
417 pr_err("Stack dump stopped; next frame identical to this one\n");
418 if (headers)
419 pr_err("Stack dump complete\n");
420 if (have_mmap_sem)
421 up_read(&kbt->task->mm->mmap_sem);
422 end_backtrace();
423}
424EXPORT_SYMBOL(tile_show_stack);
425
426
427
428void dump_stack_regs(struct pt_regs *regs)
429{
430 struct KBacktraceIterator kbt;
431 KBacktraceIterator_init(&kbt, NULL, regs);
432 tile_show_stack(&kbt, 1);
433}
434EXPORT_SYMBOL(dump_stack_regs);
435
436static struct pt_regs *regs_to_pt_regs(struct pt_regs *regs,
437 ulong pc, ulong lr, ulong sp, ulong r52)
438{
439 memset(regs, 0, sizeof(struct pt_regs));
440 regs->pc = pc;
441 regs->lr = lr;
442 regs->sp = sp;
443 regs->regs[52] = r52;
444 return regs;
445}
446
447
448void _dump_stack(int dummy, ulong pc, ulong lr, ulong sp, ulong r52)
449{
450 struct pt_regs regs;
451 dump_stack_regs(regs_to_pt_regs(®s, pc, lr, sp, r52));
452}
453
454
455void _KBacktraceIterator_init_current(struct KBacktraceIterator *kbt, ulong pc,
456 ulong lr, ulong sp, ulong r52)
457{
458 struct pt_regs regs;
459 KBacktraceIterator_init(kbt, NULL,
460 regs_to_pt_regs(®s, pc, lr, sp, r52));
461}
462
463
464void show_stack(struct task_struct *task, unsigned long *esp)
465{
466 struct KBacktraceIterator kbt;
467 if (task == NULL || task == current)
468 KBacktraceIterator_init_current(&kbt);
469 else
470 KBacktraceIterator_init(&kbt, task, NULL);
471 tile_show_stack(&kbt, 0);
472}
473
474#ifdef CONFIG_STACKTRACE
475
476
477
478void save_stack_trace_tsk(struct task_struct *task, struct stack_trace *trace)
479{
480 struct KBacktraceIterator kbt;
481 int skip = trace->skip;
482 int i = 0;
483
484 if (!start_backtrace())
485 goto done;
486 if (task == NULL || task == current)
487 KBacktraceIterator_init_current(&kbt);
488 else
489 KBacktraceIterator_init(&kbt, task, NULL);
490 for (; !KBacktraceIterator_end(&kbt); KBacktraceIterator_next(&kbt)) {
491 if (skip) {
492 --skip;
493 continue;
494 }
495 if (i >= trace->max_entries || kbt.it.pc < PAGE_OFFSET)
496 break;
497 trace->entries[i++] = kbt.it.pc;
498 }
499 end_backtrace();
500done:
501 trace->nr_entries = i;
502}
503EXPORT_SYMBOL(save_stack_trace_tsk);
504
505void save_stack_trace(struct stack_trace *trace)
506{
507 save_stack_trace_tsk(NULL, trace);
508}
509EXPORT_SYMBOL_GPL(save_stack_trace);
510
511#endif
512
513
514EXPORT_SYMBOL(KBacktraceIterator_init_current);
515