blob: 94ecbc6676e5d89129abede155e748d19b5368a6 [file] [log] [blame]
Chris Metcalf867e3592010-05-28 23:09:12 -04001/*
2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
3 *
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.
7 *
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
12 * more details.
13 */
14
15#include <linux/sched.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010016#include <linux/sched/debug.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010017#include <linux/sched/task_stack.h>
Chris Metcalf867e3592010-05-28 23:09:12 -040018#include <linux/kernel.h>
19#include <linux/kprobes.h>
20#include <linux/module.h>
21#include <linux/pfn.h>
22#include <linux/kallsyms.h>
23#include <linux/stacktrace.h>
24#include <linux/uaccess.h>
25#include <linux/mmzone.h>
Chris Metcalf5f639fd2012-03-29 14:06:14 -040026#include <linux/dcache.h>
27#include <linux/fs.h>
Chris Metcalf47ad7b92015-05-08 10:27:35 -040028#include <linux/hardirq.h>
Andy Shevchenko0cc96a72013-09-27 11:26:30 +030029#include <linux/string.h>
Chris Metcalf867e3592010-05-28 23:09:12 -040030#include <asm/backtrace.h>
31#include <asm/page.h>
Chris Metcalf867e3592010-05-28 23:09:12 -040032#include <asm/ucontext.h>
Paul Gortmaker34f2c0a2012-04-01 16:38:46 -040033#include <asm/switch_to.h>
Chris Metcalf867e3592010-05-28 23:09:12 -040034#include <asm/sigframe.h>
35#include <asm/stack.h>
Chris Metcalf4a556f42013-08-07 15:33:32 -040036#include <asm/vdso.h>
Chris Metcalf867e3592010-05-28 23:09:12 -040037#include <arch/abi.h>
38#include <arch/interrupts.h>
39
Chris Metcalfdabe98c2010-10-14 15:19:04 -040040#define KBT_ONGOING 0 /* Backtrace still ongoing */
41#define KBT_DONE 1 /* Backtrace cleanly completed */
42#define KBT_RUNNING 2 /* Can't run backtrace on a running task */
43#define KBT_LOOP 3 /* Backtrace entered a loop */
Chris Metcalf867e3592010-05-28 23:09:12 -040044
45/* Is address on the specified kernel stack? */
Chris Metcalf93013a02011-05-02 13:49:14 -040046static int in_kernel_stack(struct KBacktraceIterator *kbt, unsigned long sp)
Chris Metcalf867e3592010-05-28 23:09:12 -040047{
48 ulong kstack_base = (ulong) kbt->task->stack;
49 if (kstack_base == 0) /* corrupt task pointer; just follow stack... */
50 return sp >= PAGE_OFFSET && sp < (unsigned long)high_memory;
51 return sp >= kstack_base && sp < kstack_base + THREAD_SIZE;
52}
53
Chris Metcalf867e3592010-05-28 23:09:12 -040054/* Callback for backtracer; basically a glorified memcpy */
Chris Metcalf93013a02011-05-02 13:49:14 -040055static bool read_memory_func(void *result, unsigned long address,
Chris Metcalf867e3592010-05-28 23:09:12 -040056 unsigned int size, void *vkbt)
57{
58 int retval;
59 struct KBacktraceIterator *kbt = (struct KBacktraceIterator *)vkbt;
Chris Metcalf5f639fd2012-03-29 14:06:14 -040060
61 if (address == 0)
62 return 0;
Chris Metcalf3cebbaf2011-02-28 15:30:16 -050063 if (__kernel_text_address(address)) {
Chris Metcalf867e3592010-05-28 23:09:12 -040064 /* OK to read kernel code. */
65 } else if (address >= PAGE_OFFSET) {
66 /* We only tolerate kernel-space reads of this task's stack */
67 if (!in_kernel_stack(kbt, address))
68 return 0;
Chris Metcalf5f639fd2012-03-29 14:06:14 -040069 } else if (!kbt->is_current) {
70 return 0; /* can't read from other user address spaces */
Chris Metcalf867e3592010-05-28 23:09:12 -040071 }
72 pagefault_disable();
Chris Metcalf0707ad32010-06-25 17:04:17 -040073 retval = __copy_from_user_inatomic(result,
74 (void __user __force *)address,
Chris Metcalf867e3592010-05-28 23:09:12 -040075 size);
76 pagefault_enable();
77 return (retval == 0);
78}
79
80/* Return a pt_regs pointer for a valid fault handler frame */
81static struct pt_regs *valid_fault_handler(struct KBacktraceIterator* kbt)
82{
Chris Metcalf5ac65ab2015-12-17 15:01:38 -050083 char fault[64];
Chris Metcalf93013a02011-05-02 13:49:14 -040084 unsigned long sp = kbt->it.sp;
Chris Metcalf867e3592010-05-28 23:09:12 -040085 struct pt_regs *p;
86
Chris Metcalf5f639fd2012-03-29 14:06:14 -040087 if (sp % sizeof(long) != 0)
88 return NULL;
Chris Metcalf867e3592010-05-28 23:09:12 -040089 if (!in_kernel_stack(kbt, sp))
90 return NULL;
91 if (!in_kernel_stack(kbt, sp + C_ABI_SAVE_AREA_SIZE + PTREGS_SIZE-1))
92 return NULL;
93 p = (struct pt_regs *)(sp + C_ABI_SAVE_AREA_SIZE);
Chris Metcalf5ac65ab2015-12-17 15:01:38 -050094 if (kbt->verbose) { /* else we aren't going to use it */
95 if (p->faultnum == INT_SWINT_1 ||
96 p->faultnum == INT_SWINT_1_SIGRETURN)
97 snprintf(fault, sizeof(fault),
98 "syscall %ld", p->regs[TREG_SYSCALL_NR]);
99 else
100 snprintf(fault, sizeof(fault),
Chris Metcalf867e3592010-05-28 23:09:12 -0400101 "interrupt %ld", p->faultnum);
Chris Metcalf867e3592010-05-28 23:09:12 -0400102 }
103 if (EX1_PL(p->ex1) == KERNEL_PL &&
Chris Metcalf3cebbaf2011-02-28 15:30:16 -0500104 __kernel_text_address(p->pc) &&
Chris Metcalf867e3592010-05-28 23:09:12 -0400105 in_kernel_stack(kbt, p->sp) &&
106 p->sp >= sp) {
107 if (kbt->verbose)
Chris Metcalf0707ad32010-06-25 17:04:17 -0400108 pr_err(" <%s while in kernel mode>\n", fault);
Chris Metcalf051168d2013-09-03 14:45:52 -0400109 } else if (user_mode(p) &&
Chris Metcalf3ef23112013-08-06 16:10:23 -0400110 p->sp < PAGE_OFFSET && p->sp != 0) {
Chris Metcalf867e3592010-05-28 23:09:12 -0400111 if (kbt->verbose)
Chris Metcalf0707ad32010-06-25 17:04:17 -0400112 pr_err(" <%s while in user mode>\n", fault);
Colin Ian King90886162015-03-16 16:14:02 -0400113 } else {
Chris Metcalf47ad7b92015-05-08 10:27:35 -0400114 if (kbt->verbose && (p->pc != 0 || p->sp != 0 || p->ex1 != 0))
Colin Ian King90886162015-03-16 16:14:02 -0400115 pr_err(" (odd fault: pc %#lx, sp %#lx, ex1 %#lx?)\n",
116 p->pc, p->sp, p->ex1);
117 return NULL;
Chris Metcalf867e3592010-05-28 23:09:12 -0400118 }
Colin Ian King90886162015-03-16 16:14:02 -0400119 if (kbt->profile && ((1ULL << p->faultnum) & QUEUED_INTERRUPTS) != 0)
120 return NULL;
121 return p;
Chris Metcalf867e3592010-05-28 23:09:12 -0400122}
123
Chris Metcalf47ad7b92015-05-08 10:27:35 -0400124/* Is the iterator pointing to a sigreturn trampoline? */
125static int is_sigreturn(struct KBacktraceIterator *kbt)
Chris Metcalf867e3592010-05-28 23:09:12 -0400126{
Chris Metcalf47ad7b92015-05-08 10:27:35 -0400127 return kbt->task->mm &&
128 (kbt->it.pc == ((ulong)kbt->task->mm->context.vdso_base +
129 (ulong)&__vdso_rt_sigreturn));
Chris Metcalf867e3592010-05-28 23:09:12 -0400130}
131
132/* Return a pt_regs pointer for a valid signal handler frame */
Chris Metcalf5f639fd2012-03-29 14:06:14 -0400133static struct pt_regs *valid_sigframe(struct KBacktraceIterator* kbt,
134 struct rt_sigframe* kframe)
Chris Metcalf867e3592010-05-28 23:09:12 -0400135{
136 BacktraceIterator *b = &kbt->it;
137
Chris Metcalf47ad7b92015-05-08 10:27:35 -0400138 if (is_sigreturn(kbt) && b->sp < PAGE_OFFSET &&
Chris Metcalf5f639fd2012-03-29 14:06:14 -0400139 b->sp % sizeof(long) == 0) {
140 int retval;
141 pagefault_disable();
142 retval = __copy_from_user_inatomic(
143 kframe, (void __user __force *)b->sp,
144 sizeof(*kframe));
145 pagefault_enable();
146 if (retval != 0 ||
147 (unsigned int)(kframe->info.si_signo) >= _NSIG)
Chris Metcalf867e3592010-05-28 23:09:12 -0400148 return NULL;
Chris Metcalf867e3592010-05-28 23:09:12 -0400149 if (kbt->verbose) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400150 pr_err(" <received signal %d>\n",
Chris Metcalf5f639fd2012-03-29 14:06:14 -0400151 kframe->info.si_signo);
Chris Metcalf867e3592010-05-28 23:09:12 -0400152 }
Chris Metcalf5f639fd2012-03-29 14:06:14 -0400153 return (struct pt_regs *)&kframe->uc.uc_mcontext;
Chris Metcalf867e3592010-05-28 23:09:12 -0400154 }
155 return NULL;
156}
157
Chris Metcalf867e3592010-05-28 23:09:12 -0400158static int KBacktraceIterator_restart(struct KBacktraceIterator *kbt)
159{
160 struct pt_regs *p;
Chris Metcalf5f639fd2012-03-29 14:06:14 -0400161 struct rt_sigframe kframe;
Chris Metcalf867e3592010-05-28 23:09:12 -0400162
163 p = valid_fault_handler(kbt);
164 if (p == NULL)
Chris Metcalf5f639fd2012-03-29 14:06:14 -0400165 p = valid_sigframe(kbt, &kframe);
Chris Metcalf867e3592010-05-28 23:09:12 -0400166 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/* Find a frame that isn't a sigreturn, if there is one. */
175static int KBacktraceIterator_next_item_inclusive(
176 struct KBacktraceIterator *kbt)
177{
178 for (;;) {
179 do {
Chris Metcalf47ad7b92015-05-08 10:27:35 -0400180 if (!is_sigreturn(kbt))
Chris Metcalfdabe98c2010-10-14 15:19:04 -0400181 return KBT_ONGOING;
Chris Metcalf867e3592010-05-28 23:09:12 -0400182 } while (backtrace_next(&kbt->it));
183
184 if (!KBacktraceIterator_restart(kbt))
Chris Metcalfdabe98c2010-10-14 15:19:04 -0400185 return KBT_DONE;
Chris Metcalf867e3592010-05-28 23:09:12 -0400186 }
187}
188
189/*
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.
196 */
197static void validate_stack(struct pt_regs *regs)
198{
Chris Metcalfbc1a2982013-08-07 11:36:54 -0400199 int cpu = raw_smp_processor_id();
Chris Metcalf867e3592010-05-28 23:09:12 -0400200 unsigned long ksp0 = get_current_ksp0();
Chris Metcalf35f05972013-08-10 12:35:02 -0400201 unsigned long ksp0_base = ksp0 & -THREAD_SIZE;
Chris Metcalf867e3592010-05-28 23:09:12 -0400202 unsigned long sp = stack_pointer;
203
204 if (EX1_PL(regs->ex1) == KERNEL_PL && regs->sp >= ksp0) {
Chris Metcalf35f05972013-08-10 12:35:02 -0400205 pr_err("WARNING: cpu %d: kernel stack %#lx..%#lx underrun!\n"
Chris Metcalf867e3592010-05-28 23:09:12 -0400206 " sp %#lx (%#lx in caller), caller pc %#lx, lr %#lx\n",
Chris Metcalf35f05972013-08-10 12:35:02 -0400207 cpu, ksp0_base, ksp0, sp, regs->sp, regs->pc, regs->lr);
Chris Metcalf867e3592010-05-28 23:09:12 -0400208 }
209
210 else if (sp < ksp0_base + sizeof(struct thread_info)) {
Chris Metcalf35f05972013-08-10 12:35:02 -0400211 pr_err("WARNING: cpu %d: kernel stack %#lx..%#lx overrun!\n"
Chris Metcalf867e3592010-05-28 23:09:12 -0400212 " sp %#lx (%#lx in caller), caller pc %#lx, lr %#lx\n",
Chris Metcalf35f05972013-08-10 12:35:02 -0400213 cpu, ksp0_base, ksp0, sp, regs->sp, regs->pc, regs->lr);
Chris Metcalf867e3592010-05-28 23:09:12 -0400214 }
215}
216
217void KBacktraceIterator_init(struct KBacktraceIterator *kbt,
218 struct task_struct *t, struct pt_regs *regs)
219{
Chris Metcalf93013a02011-05-02 13:49:14 -0400220 unsigned long pc, lr, sp, r52;
Chris Metcalf867e3592010-05-28 23:09:12 -0400221 int is_current;
222
223 /*
224 * Set up callback information. We grab the kernel stack base
Chris Metcalf5f639fd2012-03-29 14:06:14 -0400225 * so we will allow reads of that address range.
Chris Metcalf867e3592010-05-28 23:09:12 -0400226 */
Chris Metcalf5f639fd2012-03-29 14:06:14 -0400227 is_current = (t == NULL || t == current);
Chris Metcalf867e3592010-05-28 23:09:12 -0400228 kbt->is_current = is_current;
229 if (is_current)
230 t = validate_current();
231 kbt->task = t;
Chris Metcalf867e3592010-05-28 23:09:12 -0400232 kbt->verbose = 0; /* override in caller if desired */
233 kbt->profile = 0; /* override in caller if desired */
Chris Metcalfdabe98c2010-10-14 15:19:04 -0400234 kbt->end = KBT_ONGOING;
Chris Metcalf5f639fd2012-03-29 14:06:14 -0400235 kbt->new_context = 1;
236 if (is_current)
Chris Metcalf867e3592010-05-28 23:09:12 -0400237 validate_stack(regs);
Chris Metcalf867e3592010-05-28 23:09:12 -0400238
239 if (regs == NULL) {
Chris Metcalf867e3592010-05-28 23:09:12 -0400240 if (is_current || t->state == TASK_RUNNING) {
241 /* Can't do this; we need registers */
Chris Metcalfdabe98c2010-10-14 15:19:04 -0400242 kbt->end = KBT_RUNNING;
Chris Metcalf867e3592010-05-28 23:09:12 -0400243 return;
244 }
Chris Metcalf0707ad32010-06-25 17:04:17 -0400245 pc = get_switch_to_pc();
Chris Metcalf867e3592010-05-28 23:09:12 -0400246 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);
Chris Metcalfdabe98c2010-10-14 15:19:04 -0400257 kbt->end = KBacktraceIterator_next_item_inclusive(kbt);
Chris Metcalf867e3592010-05-28 23:09:12 -0400258}
259EXPORT_SYMBOL(KBacktraceIterator_init);
260
261int KBacktraceIterator_end(struct KBacktraceIterator *kbt)
262{
Chris Metcalfdabe98c2010-10-14 15:19:04 -0400263 return kbt->end != KBT_ONGOING;
Chris Metcalf867e3592010-05-28 23:09:12 -0400264}
265EXPORT_SYMBOL(KBacktraceIterator_end);
266
267void KBacktraceIterator_next(struct KBacktraceIterator *kbt)
268{
Chris Metcalf93013a02011-05-02 13:49:14 -0400269 unsigned long old_pc = kbt->it.pc, old_sp = kbt->it.sp;
Chris Metcalf867e3592010-05-28 23:09:12 -0400270 kbt->new_context = 0;
Chris Metcalfdabe98c2010-10-14 15:19:04 -0400271 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 /* Trapped in a loop; give up. */
278 kbt->end = KBT_LOOP;
279 }
Chris Metcalf867e3592010-05-28 23:09:12 -0400280}
281EXPORT_SYMBOL(KBacktraceIterator_next);
282
Chris Metcalf5f639fd2012-03-29 14:06:14 -0400283static 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 * 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).
299 */
300 adjust = !kbt->new_context;
301 address -= adjust;
302
303 if (address >= PAGE_OFFSET) {
304 /* Handle kernel symbols. */
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 /* If we don't have the mmap_sem, we can't show any more info. */
324 buf[0] = '\0';
325 if (!have_mmap_sem)
326 return;
327
328 /* Find vma info. */
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) {
Miklos Szeredi9bf39ab2015-06-19 10:29:13 +0200336 p = file_path(vma->vm_file, buf, bufsize);
Chris Metcalf5f639fd2012-03-29 14:06:14 -0400337 if (IS_ERR(p))
338 p = "?";
Andy Shevchenko0cc96a72013-09-27 11:26:30 +0300339 name = kbasename(p);
Chris Metcalf5f639fd2012-03-29 14:06:14 -0400340 } else {
Andy Shevchenko0cc96a72013-09-27 11:26:30 +0300341 name = "anon";
Chris Metcalf5f639fd2012-03-29 14:06:14 -0400342 }
343
344 /* Generate a string description of the vma info. */
Andy Shevchenko0cc96a72013-09-27 11:26:30 +0300345 namelen = strlen(name);
Chris Metcalf5f639fd2012-03-29 14:06:14 -0400346 remaining = (bufsize - 1) - namelen;
Andy Shevchenko0cc96a72013-09-27 11:26:30 +0300347 memmove(buf, name, namelen);
Chris Metcalf5f639fd2012-03-29 14:06:14 -0400348 snprintf(buf + namelen, remaining, "[%lx+%lx] ",
349 vma->vm_start, vma->vm_end - vma->vm_start);
350}
351
Chris Metcalf867e3592010-05-28 23:09:12 -0400352/*
Chris Metcalf3ef23112013-08-06 16:10:23 -0400353 * 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.
356 */
357static bool start_backtrace(void)
358{
Chris Metcalf47ad7b92015-05-08 10:27:35 -0400359 if (current_thread_info()->in_backtrace) {
Chris Metcalf3ef23112013-08-06 16:10:23 -0400360 pr_err("Backtrace requested while in backtrace!\n");
361 return false;
362 }
Chris Metcalf47ad7b92015-05-08 10:27:35 -0400363 current_thread_info()->in_backtrace = true;
Chris Metcalf3ef23112013-08-06 16:10:23 -0400364 return true;
365}
366
367static void end_backtrace(void)
368{
Chris Metcalf47ad7b92015-05-08 10:27:35 -0400369 current_thread_info()->in_backtrace = false;
Chris Metcalf3ef23112013-08-06 16:10:23 -0400370}
371
372/*
Chris Metcalf867e3592010-05-28 23:09:12 -0400373 * This method wraps the backtracer's more generic support.
374 * It is only invoked from the architecture-specific code; show_stack()
Chris Metcalf47ad7b92015-05-08 10:27:35 -0400375 * and dump_stack() are architecture-independent entry points.
Chris Metcalf867e3592010-05-28 23:09:12 -0400376 */
Chris Metcalf47ad7b92015-05-08 10:27:35 -0400377void tile_show_stack(struct KBacktraceIterator *kbt)
Chris Metcalf867e3592010-05-28 23:09:12 -0400378{
379 int i;
Chris Metcalf5f639fd2012-03-29 14:06:14 -0400380 int have_mmap_sem = 0;
Chris Metcalf867e3592010-05-28 23:09:12 -0400381
Chris Metcalf3ef23112013-08-06 16:10:23 -0400382 if (!start_backtrace())
383 return;
Chris Metcalf867e3592010-05-28 23:09:12 -0400384 kbt->verbose = 1;
385 i = 0;
386 for (; !KBacktraceIterator_end(kbt); KBacktraceIterator_next(kbt)) {
Chris Metcalf867e3592010-05-28 23:09:12 -0400387 char namebuf[KSYM_NAME_LEN+100];
Chris Metcalf5f639fd2012-03-29 14:06:14 -0400388 unsigned long address = kbt->it.pc;
Chris Metcalf867e3592010-05-28 23:09:12 -0400389
Chris Metcalf47ad7b92015-05-08 10:27:35 -0400390 /*
391 * Try to acquire the mmap_sem as we pass into userspace.
392 * If we're in an interrupt context, don't even try, since
393 * it's not safe to call e.g. d_path() from an interrupt,
394 * since it uses spin locks without disabling interrupts.
395 * Note we test "kbt->task == current", not "kbt->is_current",
396 * since we're checking that "current" will work in d_path().
397 */
398 if (kbt->task == current && address < PAGE_OFFSET &&
399 !have_mmap_sem && kbt->task->mm && !in_interrupt()) {
Chris Metcalf5f639fd2012-03-29 14:06:14 -0400400 have_mmap_sem =
401 down_read_trylock(&kbt->task->mm->mmap_sem);
Chris Metcalf47ad7b92015-05-08 10:27:35 -0400402 }
Chris Metcalf867e3592010-05-28 23:09:12 -0400403
Chris Metcalf5f639fd2012-03-29 14:06:14 -0400404 describe_addr(kbt, address, have_mmap_sem,
405 namebuf, sizeof(namebuf));
Chris Metcalf867e3592010-05-28 23:09:12 -0400406
Chris Metcalf0707ad32010-06-25 17:04:17 -0400407 pr_err(" frame %d: 0x%lx %s(sp 0x%lx)\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400408 i++, address, namebuf, (unsigned long)(kbt->it.sp));
409
410 if (i >= 100) {
Joe Perchesf4743672014-10-31 10:50:46 -0700411 pr_err("Stack dump truncated (%d frames)\n", i);
Chris Metcalf867e3592010-05-28 23:09:12 -0400412 break;
413 }
414 }
Chris Metcalfdabe98c2010-10-14 15:19:04 -0400415 if (kbt->end == KBT_LOOP)
416 pr_err("Stack dump stopped; next frame identical to this one\n");
Chris Metcalf5f639fd2012-03-29 14:06:14 -0400417 if (have_mmap_sem)
418 up_read(&kbt->task->mm->mmap_sem);
Chris Metcalf3ef23112013-08-06 16:10:23 -0400419 end_backtrace();
Chris Metcalf867e3592010-05-28 23:09:12 -0400420}
421EXPORT_SYMBOL(tile_show_stack);
422
Chris Metcalf867e3592010-05-28 23:09:12 -0400423static struct pt_regs *regs_to_pt_regs(struct pt_regs *regs,
424 ulong pc, ulong lr, ulong sp, ulong r52)
425{
426 memset(regs, 0, sizeof(struct pt_regs));
427 regs->pc = pc;
428 regs->lr = lr;
429 regs->sp = sp;
430 regs->regs[52] = r52;
431 return regs;
432}
433
Chris Metcalf47ad7b92015-05-08 10:27:35 -0400434/* Deprecated function currently only used by kernel_double_fault(). */
Chris Metcalf867e3592010-05-28 23:09:12 -0400435void _dump_stack(int dummy, ulong pc, ulong lr, ulong sp, ulong r52)
436{
Chris Metcalf47ad7b92015-05-08 10:27:35 -0400437 struct KBacktraceIterator kbt;
Chris Metcalf867e3592010-05-28 23:09:12 -0400438 struct pt_regs regs;
Chris Metcalf47ad7b92015-05-08 10:27:35 -0400439
440 regs_to_pt_regs(&regs, pc, lr, sp, r52);
441 KBacktraceIterator_init(&kbt, NULL, &regs);
442 tile_show_stack(&kbt);
Chris Metcalf867e3592010-05-28 23:09:12 -0400443}
444
445/* This is called from KBacktraceIterator_init_current() */
446void _KBacktraceIterator_init_current(struct KBacktraceIterator *kbt, ulong pc,
447 ulong lr, ulong sp, ulong r52)
448{
449 struct pt_regs regs;
450 KBacktraceIterator_init(kbt, NULL,
451 regs_to_pt_regs(&regs, pc, lr, sp, r52));
452}
453
Chris Metcalf47ad7b92015-05-08 10:27:35 -0400454/*
455 * Called from sched_show_task() with task != NULL, or dump_stack()
456 * with task == NULL. The esp argument is always NULL.
457 */
Chris Metcalf867e3592010-05-28 23:09:12 -0400458void show_stack(struct task_struct *task, unsigned long *esp)
459{
460 struct KBacktraceIterator kbt;
Chris Metcalf47ad7b92015-05-08 10:27:35 -0400461 if (task == NULL || task == current) {
Chris Metcalf867e3592010-05-28 23:09:12 -0400462 KBacktraceIterator_init_current(&kbt);
Chris Metcalf47ad7b92015-05-08 10:27:35 -0400463 KBacktraceIterator_next(&kbt); /* don't show first frame */
464 } else {
Chris Metcalf867e3592010-05-28 23:09:12 -0400465 KBacktraceIterator_init(&kbt, task, NULL);
Chris Metcalf47ad7b92015-05-08 10:27:35 -0400466 }
467 tile_show_stack(&kbt);
Chris Metcalf867e3592010-05-28 23:09:12 -0400468}
469
470#ifdef CONFIG_STACKTRACE
471
472/* Support generic Linux stack API too */
473
Chris Metcalf47ad7b92015-05-08 10:27:35 -0400474static void save_stack_trace_common(struct task_struct *task,
475 struct pt_regs *regs,
476 bool user,
477 struct stack_trace *trace)
Chris Metcalf867e3592010-05-28 23:09:12 -0400478{
479 struct KBacktraceIterator kbt;
480 int skip = trace->skip;
481 int i = 0;
482
Chris Metcalf3ef23112013-08-06 16:10:23 -0400483 if (!start_backtrace())
484 goto done;
Chris Metcalf47ad7b92015-05-08 10:27:35 -0400485 if (regs != NULL) {
486 KBacktraceIterator_init(&kbt, NULL, regs);
487 } else if (task == NULL || task == current) {
Chris Metcalf867e3592010-05-28 23:09:12 -0400488 KBacktraceIterator_init_current(&kbt);
Chris Metcalf47ad7b92015-05-08 10:27:35 -0400489 skip++; /* don't show KBacktraceIterator_init_current */
490 } else {
Chris Metcalf867e3592010-05-28 23:09:12 -0400491 KBacktraceIterator_init(&kbt, task, NULL);
Chris Metcalf47ad7b92015-05-08 10:27:35 -0400492 }
Chris Metcalf867e3592010-05-28 23:09:12 -0400493 for (; !KBacktraceIterator_end(&kbt); KBacktraceIterator_next(&kbt)) {
494 if (skip) {
495 --skip;
496 continue;
497 }
Chris Metcalf47ad7b92015-05-08 10:27:35 -0400498 if (i >= trace->max_entries ||
499 (!user && kbt.it.pc < PAGE_OFFSET))
Chris Metcalf867e3592010-05-28 23:09:12 -0400500 break;
501 trace->entries[i++] = kbt.it.pc;
502 }
Chris Metcalf3ef23112013-08-06 16:10:23 -0400503 end_backtrace();
504done:
Chris Metcalf47ad7b92015-05-08 10:27:35 -0400505 if (i < trace->max_entries)
506 trace->entries[i++] = ULONG_MAX;
Chris Metcalf867e3592010-05-28 23:09:12 -0400507 trace->nr_entries = i;
508}
Chris Metcalf47ad7b92015-05-08 10:27:35 -0400509
510void save_stack_trace_tsk(struct task_struct *task, struct stack_trace *trace)
511{
512 save_stack_trace_common(task, NULL, false, trace);
513}
Chris Metcalf867e3592010-05-28 23:09:12 -0400514EXPORT_SYMBOL(save_stack_trace_tsk);
515
516void save_stack_trace(struct stack_trace *trace)
517{
Chris Metcalf47ad7b92015-05-08 10:27:35 -0400518 save_stack_trace_common(NULL, NULL, false, trace);
Chris Metcalf867e3592010-05-28 23:09:12 -0400519}
Chris Metcalf7c63e1e2013-02-01 15:06:06 -0500520EXPORT_SYMBOL_GPL(save_stack_trace);
Chris Metcalf867e3592010-05-28 23:09:12 -0400521
Chris Metcalf47ad7b92015-05-08 10:27:35 -0400522void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
523{
524 save_stack_trace_common(NULL, regs, false, trace);
525}
526
527void save_stack_trace_user(struct stack_trace *trace)
528{
529 /* Trace user stack if we are not a kernel thread. */
530 if (current->mm)
531 save_stack_trace_common(NULL, task_pt_regs(current),
532 true, trace);
533 else if (trace->nr_entries < trace->max_entries)
534 trace->entries[trace->nr_entries++] = ULONG_MAX;
535}
Chris Metcalf867e3592010-05-28 23:09:12 -0400536#endif
537
538/* In entry.S */
539EXPORT_SYMBOL(KBacktraceIterator_init_current);