2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation, version 2.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 * NON INFRINGEMENT. See the GNU General Public License for
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>
26 #include <linux/string.h>
27 #include <asm/backtrace.h>
29 #include <asm/ucontext.h>
30 #include <asm/switch_to.h>
31 #include <asm/sigframe.h>
32 #include <asm/stack.h>
35 #include <arch/interrupts.h>
37 #define KBT_ONGOING 0 /* Backtrace still ongoing */
38 #define KBT_DONE 1 /* Backtrace cleanly completed */
39 #define KBT_RUNNING 2 /* Can't run backtrace on a running task */
40 #define KBT_LOOP 3 /* Backtrace entered a loop */
42 /* Is address on the specified kernel stack? */
43 static int in_kernel_stack(struct KBacktraceIterator
*kbt
, unsigned long sp
)
45 ulong kstack_base
= (ulong
) kbt
->task
->stack
;
46 if (kstack_base
== 0) /* corrupt task pointer; just follow stack... */
47 return sp
>= PAGE_OFFSET
&& sp
< (unsigned long)high_memory
;
48 return sp
>= kstack_base
&& sp
< kstack_base
+ THREAD_SIZE
;
51 /* Callback for backtracer; basically a glorified memcpy */
52 static bool read_memory_func(void *result
, unsigned long address
,
53 unsigned int size
, void *vkbt
)
56 struct KBacktraceIterator
*kbt
= (struct KBacktraceIterator
*)vkbt
;
60 if (__kernel_text_address(address
)) {
61 /* OK to read kernel code. */
62 } else if (address
>= PAGE_OFFSET
) {
63 /* We only tolerate kernel-space reads of this task's stack */
64 if (!in_kernel_stack(kbt
, address
))
66 } else if (!kbt
->is_current
) {
67 return 0; /* can't read from other user address spaces */
70 retval
= __copy_from_user_inatomic(result
,
71 (void __user __force
*)address
,
77 /* Return a pt_regs pointer for a valid fault handler frame */
78 static struct pt_regs
*valid_fault_handler(struct KBacktraceIterator
* kbt
)
80 const char *fault
= NULL
; /* happy compiler */
82 unsigned long sp
= kbt
->it
.sp
;
85 if (sp
% sizeof(long) != 0)
87 if (!in_kernel_stack(kbt
, sp
))
89 if (!in_kernel_stack(kbt
, sp
+ C_ABI_SAVE_AREA_SIZE
+ PTREGS_SIZE
-1))
91 p
= (struct pt_regs
*)(sp
+ C_ABI_SAVE_AREA_SIZE
);
92 if (p
->faultnum
== INT_SWINT_1
|| p
->faultnum
== INT_SWINT_1_SIGRETURN
)
95 if (kbt
->verbose
) { /* else we aren't going to use it */
96 snprintf(fault_buf
, sizeof(fault_buf
),
97 "interrupt %ld", p
->faultnum
);
101 if (EX1_PL(p
->ex1
) == KERNEL_PL
&&
102 __kernel_text_address(p
->pc
) &&
103 in_kernel_stack(kbt
, p
->sp
) &&
106 pr_err(" <%s while in kernel mode>\n", fault
);
107 } else if (user_mode(p
) &&
108 p
->sp
< PAGE_OFFSET
&& p
->sp
!= 0) {
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
);
116 if (!kbt
->profile
|| ((1ULL << p
->faultnum
) & QUEUED_INTERRUPTS
) == 0)
121 /* Is the pc pointing to a sigreturn trampoline? */
122 static int is_sigreturn(unsigned long pc
)
124 return current
->mm
&& (pc
== VDSO_SYM(&__vdso_rt_sigreturn
));
127 /* Return a pt_regs pointer for a valid signal handler frame */
128 static struct pt_regs
*valid_sigframe(struct KBacktraceIterator
* kbt
,
129 struct rt_sigframe
* kframe
)
131 BacktraceIterator
*b
= &kbt
->it
;
133 if (is_sigreturn(b
->pc
) && b
->sp
< PAGE_OFFSET
&&
134 b
->sp
% sizeof(long) == 0) {
137 retval
= __copy_from_user_inatomic(
138 kframe
, (void __user __force
*)b
->sp
,
142 (unsigned int)(kframe
->info
.si_signo
) >= _NSIG
)
145 pr_err(" <received signal %d>\n",
146 kframe
->info
.si_signo
);
148 return (struct pt_regs
*)&kframe
->uc
.uc_mcontext
;
153 static int KBacktraceIterator_is_sigreturn(struct KBacktraceIterator
*kbt
)
155 return is_sigreturn(kbt
->it
.pc
);
158 static int KBacktraceIterator_restart(struct KBacktraceIterator
*kbt
)
161 struct rt_sigframe kframe
;
163 p
= valid_fault_handler(kbt
);
165 p
= valid_sigframe(kbt
, &kframe
);
168 backtrace_init(&kbt
->it
, read_memory_func
, kbt
,
169 p
->pc
, p
->lr
, p
->sp
, p
->regs
[52]);
170 kbt
->new_context
= 1;
174 /* Find a frame that isn't a sigreturn, if there is one. */
175 static int KBacktraceIterator_next_item_inclusive(
176 struct KBacktraceIterator
*kbt
)
180 if (!KBacktraceIterator_is_sigreturn(kbt
))
182 } while (backtrace_next(&kbt
->it
));
184 if (!KBacktraceIterator_restart(kbt
))
190 * If the current sp is on a page different than what we recorded
191 * as the top-of-kernel-stack last time we context switched, we have
192 * probably blown the stack, and nothing is going to work out well.
193 * If we can at least get out a warning, that may help the debug,
194 * though we probably won't be able to backtrace into the code that
195 * actually did the recursive damage.
197 static void validate_stack(struct pt_regs
*regs
)
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
;
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
);
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
);
217 void KBacktraceIterator_init(struct KBacktraceIterator
*kbt
,
218 struct task_struct
*t
, struct pt_regs
*regs
)
220 unsigned long pc
, lr
, sp
, r52
;
224 * Set up callback information. We grab the kernel stack base
225 * so we will allow reads of that address range.
227 is_current
= (t
== NULL
|| t
== current
);
228 kbt
->is_current
= is_current
;
230 t
= validate_current();
232 kbt
->verbose
= 0; /* override in caller if desired */
233 kbt
->profile
= 0; /* override in caller if desired */
234 kbt
->end
= KBT_ONGOING
;
235 kbt
->new_context
= 1;
237 validate_stack(regs
);
240 if (is_current
|| t
->state
== TASK_RUNNING
) {
241 /* Can't do this; we need registers */
242 kbt
->end
= KBT_RUNNING
;
245 pc
= get_switch_to_pc();
253 r52
= regs
->regs
[52];
256 backtrace_init(&kbt
->it
, read_memory_func
, kbt
, pc
, lr
, sp
, r52
);
257 kbt
->end
= KBacktraceIterator_next_item_inclusive(kbt
);
259 EXPORT_SYMBOL(KBacktraceIterator_init
);
261 int KBacktraceIterator_end(struct KBacktraceIterator
*kbt
)
263 return kbt
->end
!= KBT_ONGOING
;
265 EXPORT_SYMBOL(KBacktraceIterator_end
);
267 void KBacktraceIterator_next(struct KBacktraceIterator
*kbt
)
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
)) {
275 kbt
->end
= KBacktraceIterator_next_item_inclusive(kbt
);
276 if (old_pc
== kbt
->it
.pc
&& old_sp
== kbt
->it
.sp
) {
277 /* Trapped in a loop; give up. */
281 EXPORT_SYMBOL(KBacktraceIterator_next
);
283 static void describe_addr(struct KBacktraceIterator
*kbt
,
284 unsigned long address
,
285 int have_mmap_sem
, char *buf
, size_t bufsize
)
287 struct vm_area_struct
*vma
;
288 size_t namelen
, remaining
;
289 unsigned long size
, offset
, adjust
;
295 * Look one byte back for every caller frame (i.e. those that
296 * aren't a new context) so we look up symbol data for the
297 * call itself, not the following instruction, which may be on
298 * a different line (or in a different function).
300 adjust
= !kbt
->new_context
;
303 if (address
>= PAGE_OFFSET
) {
304 /* Handle kernel symbols. */
305 BUG_ON(bufsize
< KSYM_NAME_LEN
);
306 name
= kallsyms_lookup(address
, &size
, &offset
,
312 namelen
= strlen(buf
);
313 remaining
= (bufsize
- 1) - 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';
323 /* If we don't have the mmap_sem, we can't show any more info. */
329 vma
= find_vma(kbt
->task
->mm
, address
);
330 if (vma
== NULL
|| address
< vma
->vm_start
) {
331 snprintf(buf
, bufsize
, "[unmapped address] ");
336 p
= d_path(&vma
->vm_file
->f_path
, buf
, bufsize
);
344 /* Generate a string description of the vma info. */
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
);
353 * Avoid possible crash recursion during backtrace. If it happens, it
354 * makes it easy to lose the actual root cause of the failure, so we
355 * put a simple guard on all the backtrace loops.
357 static bool start_backtrace(void)
359 if (current
->thread
.in_backtrace
) {
360 pr_err("Backtrace requested while in backtrace!\n");
363 current
->thread
.in_backtrace
= true;
367 static void end_backtrace(void)
369 current
->thread
.in_backtrace
= false;
373 * This method wraps the backtracer's more generic support.
374 * It is only invoked from the architecture-specific code; show_stack()
375 * and dump_stack() (in entry.S) are architecture-independent entry points.
377 void tile_show_stack(struct KBacktraceIterator
*kbt
, int headers
)
380 int have_mmap_sem
= 0;
382 if (!start_backtrace())
386 * Add a blank line since if we are called from panic(),
387 * then bust_spinlocks() spit out a space in front of us
388 * and it will mess up our KERN_ERR.
391 pr_err("Starting stack dump of tid %d, pid %d (%s)"
392 " on cpu %d at cycle %lld\n",
393 kbt
->task
->pid
, kbt
->task
->tgid
, kbt
->task
->comm
,
394 raw_smp_processor_id(), get_cycles());
398 for (; !KBacktraceIterator_end(kbt
); KBacktraceIterator_next(kbt
)) {
399 char namebuf
[KSYM_NAME_LEN
+100];
400 unsigned long address
= kbt
->it
.pc
;
402 /* Try to acquire the mmap_sem as we pass into userspace. */
403 if (address
< PAGE_OFFSET
&& !have_mmap_sem
&& kbt
->task
->mm
)
405 down_read_trylock(&kbt
->task
->mm
->mmap_sem
);
407 describe_addr(kbt
, address
, have_mmap_sem
,
408 namebuf
, sizeof(namebuf
));
410 pr_err(" frame %d: 0x%lx %s(sp 0x%lx)\n",
411 i
++, address
, namebuf
, (unsigned long)(kbt
->it
.sp
));
414 pr_err("Stack dump truncated"
415 " (%d frames)\n", i
);
419 if (kbt
->end
== KBT_LOOP
)
420 pr_err("Stack dump stopped; next frame identical to this one\n");
422 pr_err("Stack dump complete\n");
424 up_read(&kbt
->task
->mm
->mmap_sem
);
427 EXPORT_SYMBOL(tile_show_stack
);
430 /* This is called from show_regs() and _dump_stack() */
431 void dump_stack_regs(struct pt_regs
*regs
)
433 struct KBacktraceIterator kbt
;
434 KBacktraceIterator_init(&kbt
, NULL
, regs
);
435 tile_show_stack(&kbt
, 1);
437 EXPORT_SYMBOL(dump_stack_regs
);
439 static struct pt_regs
*regs_to_pt_regs(struct pt_regs
*regs
,
440 ulong pc
, ulong lr
, ulong sp
, ulong r52
)
442 memset(regs
, 0, sizeof(struct pt_regs
));
446 regs
->regs
[52] = r52
;
450 /* This is called from dump_stack() and just converts to pt_regs */
451 void _dump_stack(int dummy
, ulong pc
, ulong lr
, ulong sp
, ulong r52
)
454 dump_stack_regs(regs_to_pt_regs(®s
, pc
, lr
, sp
, r52
));
457 /* This is called from KBacktraceIterator_init_current() */
458 void _KBacktraceIterator_init_current(struct KBacktraceIterator
*kbt
, ulong pc
,
459 ulong lr
, ulong sp
, ulong r52
)
462 KBacktraceIterator_init(kbt
, NULL
,
463 regs_to_pt_regs(®s
, pc
, lr
, sp
, r52
));
466 /* This is called only from kernel/sched/core.c, with esp == NULL */
467 void show_stack(struct task_struct
*task
, unsigned long *esp
)
469 struct KBacktraceIterator kbt
;
470 if (task
== NULL
|| task
== current
)
471 KBacktraceIterator_init_current(&kbt
);
473 KBacktraceIterator_init(&kbt
, task
, NULL
);
474 tile_show_stack(&kbt
, 0);
477 #ifdef CONFIG_STACKTRACE
479 /* Support generic Linux stack API too */
481 void save_stack_trace_tsk(struct task_struct
*task
, struct stack_trace
*trace
)
483 struct KBacktraceIterator kbt
;
484 int skip
= trace
->skip
;
487 if (!start_backtrace())
489 if (task
== NULL
|| task
== current
)
490 KBacktraceIterator_init_current(&kbt
);
492 KBacktraceIterator_init(&kbt
, task
, NULL
);
493 for (; !KBacktraceIterator_end(&kbt
); KBacktraceIterator_next(&kbt
)) {
498 if (i
>= trace
->max_entries
|| kbt
.it
.pc
< PAGE_OFFSET
)
500 trace
->entries
[i
++] = kbt
.it
.pc
;
504 trace
->nr_entries
= i
;
506 EXPORT_SYMBOL(save_stack_trace_tsk
);
508 void save_stack_trace(struct stack_trace
*trace
)
510 save_stack_trace_tsk(NULL
, trace
);
512 EXPORT_SYMBOL_GPL(save_stack_trace
);
517 EXPORT_SYMBOL(KBacktraceIterator_init_current
);