]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - arch/tile/kernel/stack.c
Merge tag 'metag-for-v3.12' of git://git.kernel.org/pub/scm/linux/kernel/git/jhogan...
[mirror_ubuntu-zesty-kernel.git] / arch / tile / kernel / stack.c
CommitLineData
867e359b
CM
1/*
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>
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>
5f639fdc
CM
24#include <linux/dcache.h>
25#include <linux/fs.h>
867e359b
CM
26#include <asm/backtrace.h>
27#include <asm/page.h>
867e359b 28#include <asm/ucontext.h>
34f2c0ac 29#include <asm/switch_to.h>
867e359b
CM
30#include <asm/sigframe.h>
31#include <asm/stack.h>
4a556f4f 32#include <asm/vdso.h>
867e359b
CM
33#include <arch/abi.h>
34#include <arch/interrupts.h>
35
dabe98c9
CM
36#define KBT_ONGOING 0 /* Backtrace still ongoing */
37#define KBT_DONE 1 /* Backtrace cleanly completed */
38#define KBT_RUNNING 2 /* Can't run backtrace on a running task */
39#define KBT_LOOP 3 /* Backtrace entered a loop */
867e359b
CM
40
41/* Is address on the specified kernel stack? */
93013a0f 42static int in_kernel_stack(struct KBacktraceIterator *kbt, unsigned long sp)
867e359b
CM
43{
44 ulong kstack_base = (ulong) kbt->task->stack;
45 if (kstack_base == 0) /* corrupt task pointer; just follow stack... */
46 return sp >= PAGE_OFFSET && sp < (unsigned long)high_memory;
47 return sp >= kstack_base && sp < kstack_base + THREAD_SIZE;
48}
49
867e359b 50/* Callback for backtracer; basically a glorified memcpy */
93013a0f 51static bool read_memory_func(void *result, unsigned long address,
867e359b
CM
52 unsigned int size, void *vkbt)
53{
54 int retval;
55 struct KBacktraceIterator *kbt = (struct KBacktraceIterator *)vkbt;
5f639fdc
CM
56
57 if (address == 0)
58 return 0;
3cebbafd 59 if (__kernel_text_address(address)) {
867e359b
CM
60 /* OK to read kernel code. */
61 } else if (address >= PAGE_OFFSET) {
62 /* We only tolerate kernel-space reads of this task's stack */
63 if (!in_kernel_stack(kbt, address))
64 return 0;
5f639fdc
CM
65 } else if (!kbt->is_current) {
66 return 0; /* can't read from other user address spaces */
867e359b
CM
67 }
68 pagefault_disable();
0707ad30
CM
69 retval = __copy_from_user_inatomic(result,
70 (void __user __force *)address,
867e359b
CM
71 size);
72 pagefault_enable();
73 return (retval == 0);
74}
75
76/* Return a pt_regs pointer for a valid fault handler frame */
77static struct pt_regs *valid_fault_handler(struct KBacktraceIterator* kbt)
78{
867e359b
CM
79 const char *fault = NULL; /* happy compiler */
80 char fault_buf[64];
93013a0f 81 unsigned long sp = kbt->it.sp;
867e359b
CM
82 struct pt_regs *p;
83
5f639fdc
CM
84 if (sp % sizeof(long) != 0)
85 return NULL;
867e359b
CM
86 if (!in_kernel_stack(kbt, sp))
87 return NULL;
88 if (!in_kernel_stack(kbt, sp + C_ABI_SAVE_AREA_SIZE + PTREGS_SIZE-1))
89 return NULL;
90 p = (struct pt_regs *)(sp + C_ABI_SAVE_AREA_SIZE);
91 if (p->faultnum == INT_SWINT_1 || p->faultnum == INT_SWINT_1_SIGRETURN)
92 fault = "syscall";
93 else {
94 if (kbt->verbose) { /* else we aren't going to use it */
95 snprintf(fault_buf, sizeof(fault_buf),
96 "interrupt %ld", p->faultnum);
97 fault = fault_buf;
98 }
99 }
100 if (EX1_PL(p->ex1) == KERNEL_PL &&
3cebbafd 101 __kernel_text_address(p->pc) &&
867e359b
CM
102 in_kernel_stack(kbt, p->sp) &&
103 p->sp >= sp) {
104 if (kbt->verbose)
0707ad30 105 pr_err(" <%s while in kernel mode>\n", fault);
051168df 106 } else if (user_mode(p) &&
3ef23111 107 p->sp < PAGE_OFFSET && p->sp != 0) {
867e359b 108 if (kbt->verbose)
0707ad30 109 pr_err(" <%s while in user mode>\n", fault);
867e359b 110 } else if (kbt->verbose) {
0707ad30 111 pr_err(" (odd fault: pc %#lx, sp %#lx, ex1 %#lx?)\n",
867e359b
CM
112 p->pc, p->sp, p->ex1);
113 p = NULL;
114 }
7f04f081 115 if (!kbt->profile || ((1ULL << p->faultnum) & QUEUED_INTERRUPTS) == 0)
867e359b 116 return p;
867e359b
CM
117 return NULL;
118}
119
120/* Is the pc pointing to a sigreturn trampoline? */
93013a0f 121static int is_sigreturn(unsigned long pc)
867e359b 122{
4a556f4f 123 return current->mm && (pc == VDSO_SYM(&__vdso_rt_sigreturn));
867e359b
CM
124}
125
126/* Return a pt_regs pointer for a valid signal handler frame */
5f639fdc
CM
127static struct pt_regs *valid_sigframe(struct KBacktraceIterator* kbt,
128 struct rt_sigframe* kframe)
867e359b
CM
129{
130 BacktraceIterator *b = &kbt->it;
131
4a556f4f 132 if (is_sigreturn(b->pc) && b->sp < PAGE_OFFSET &&
5f639fdc
CM
133 b->sp % sizeof(long) == 0) {
134 int retval;
135 pagefault_disable();
136 retval = __copy_from_user_inatomic(
137 kframe, (void __user __force *)b->sp,
138 sizeof(*kframe));
139 pagefault_enable();
140 if (retval != 0 ||
141 (unsigned int)(kframe->info.si_signo) >= _NSIG)
867e359b 142 return NULL;
867e359b 143 if (kbt->verbose) {
0707ad30 144 pr_err(" <received signal %d>\n",
5f639fdc 145 kframe->info.si_signo);
867e359b 146 }
5f639fdc 147 return (struct pt_regs *)&kframe->uc.uc_mcontext;
867e359b
CM
148 }
149 return NULL;
150}
151
0707ad30 152static int KBacktraceIterator_is_sigreturn(struct KBacktraceIterator *kbt)
867e359b
CM
153{
154 return is_sigreturn(kbt->it.pc);
155}
156
157static int KBacktraceIterator_restart(struct KBacktraceIterator *kbt)
158{
159 struct pt_regs *p;
5f639fdc 160 struct rt_sigframe kframe;
867e359b
CM
161
162 p = valid_fault_handler(kbt);
163 if (p == NULL)
5f639fdc 164 p = valid_sigframe(kbt, &kframe);
867e359b
CM
165 if (p == NULL)
166 return 0;
167 backtrace_init(&kbt->it, read_memory_func, kbt,
168 p->pc, p->lr, p->sp, p->regs[52]);
169 kbt->new_context = 1;
170 return 1;
171}
172
173/* Find a frame that isn't a sigreturn, if there is one. */
174static int KBacktraceIterator_next_item_inclusive(
175 struct KBacktraceIterator *kbt)
176{
177 for (;;) {
178 do {
179 if (!KBacktraceIterator_is_sigreturn(kbt))
dabe98c9 180 return KBT_ONGOING;
867e359b
CM
181 } while (backtrace_next(&kbt->it));
182
183 if (!KBacktraceIterator_restart(kbt))
dabe98c9 184 return KBT_DONE;
867e359b
CM
185 }
186}
187
188/*
189 * If the current sp is on a page different than what we recorded
190 * as the top-of-kernel-stack last time we context switched, we have
191 * probably blown the stack, and nothing is going to work out well.
192 * If we can at least get out a warning, that may help the debug,
193 * though we probably won't be able to backtrace into the code that
194 * actually did the recursive damage.
195 */
196static void validate_stack(struct pt_regs *regs)
197{
bc1a298f 198 int cpu = raw_smp_processor_id();
867e359b 199 unsigned long ksp0 = get_current_ksp0();
35f05976 200 unsigned long ksp0_base = ksp0 & -THREAD_SIZE;
867e359b
CM
201 unsigned long sp = stack_pointer;
202
203 if (EX1_PL(regs->ex1) == KERNEL_PL && regs->sp >= ksp0) {
35f05976 204 pr_err("WARNING: cpu %d: kernel stack %#lx..%#lx underrun!\n"
867e359b 205 " sp %#lx (%#lx in caller), caller pc %#lx, lr %#lx\n",
35f05976 206 cpu, ksp0_base, ksp0, sp, regs->sp, regs->pc, regs->lr);
867e359b
CM
207 }
208
209 else if (sp < ksp0_base + sizeof(struct thread_info)) {
35f05976 210 pr_err("WARNING: cpu %d: kernel stack %#lx..%#lx overrun!\n"
867e359b 211 " sp %#lx (%#lx in caller), caller pc %#lx, lr %#lx\n",
35f05976 212 cpu, ksp0_base, ksp0, sp, regs->sp, regs->pc, regs->lr);
867e359b
CM
213 }
214}
215
216void KBacktraceIterator_init(struct KBacktraceIterator *kbt,
217 struct task_struct *t, struct pt_regs *regs)
218{
93013a0f 219 unsigned long pc, lr, sp, r52;
867e359b
CM
220 int is_current;
221
222 /*
223 * Set up callback information. We grab the kernel stack base
5f639fdc 224 * so we will allow reads of that address range.
867e359b 225 */
5f639fdc 226 is_current = (t == NULL || t == current);
867e359b
CM
227 kbt->is_current = is_current;
228 if (is_current)
229 t = validate_current();
230 kbt->task = t;
867e359b
CM
231 kbt->verbose = 0; /* override in caller if desired */
232 kbt->profile = 0; /* override in caller if desired */
dabe98c9 233 kbt->end = KBT_ONGOING;
5f639fdc
CM
234 kbt->new_context = 1;
235 if (is_current)
867e359b 236 validate_stack(regs);
867e359b
CM
237
238 if (regs == NULL) {
867e359b
CM
239 if (is_current || t->state == TASK_RUNNING) {
240 /* Can't do this; we need registers */
dabe98c9 241 kbt->end = KBT_RUNNING;
867e359b
CM
242 return;
243 }
0707ad30 244 pc = get_switch_to_pc();
867e359b
CM
245 lr = t->thread.pc;
246 sp = t->thread.ksp;
247 r52 = 0;
248 } else {
249 pc = regs->pc;
250 lr = regs->lr;
251 sp = regs->sp;
252 r52 = regs->regs[52];
253 }
254
255 backtrace_init(&kbt->it, read_memory_func, kbt, pc, lr, sp, r52);
dabe98c9 256 kbt->end = KBacktraceIterator_next_item_inclusive(kbt);
867e359b
CM
257}
258EXPORT_SYMBOL(KBacktraceIterator_init);
259
260int KBacktraceIterator_end(struct KBacktraceIterator *kbt)
261{
dabe98c9 262 return kbt->end != KBT_ONGOING;
867e359b
CM
263}
264EXPORT_SYMBOL(KBacktraceIterator_end);
265
266void KBacktraceIterator_next(struct KBacktraceIterator *kbt)
267{
93013a0f 268 unsigned long old_pc = kbt->it.pc, old_sp = kbt->it.sp;
867e359b 269 kbt->new_context = 0;
dabe98c9
CM
270 if (!backtrace_next(&kbt->it) && !KBacktraceIterator_restart(kbt)) {
271 kbt->end = KBT_DONE;
272 return;
273 }
274 kbt->end = KBacktraceIterator_next_item_inclusive(kbt);
275 if (old_pc == kbt->it.pc && old_sp == kbt->it.sp) {
276 /* Trapped in a loop; give up. */
277 kbt->end = KBT_LOOP;
278 }
867e359b
CM
279}
280EXPORT_SYMBOL(KBacktraceIterator_next);
281
5f639fdc
CM
282static void describe_addr(struct KBacktraceIterator *kbt,
283 unsigned long address,
284 int have_mmap_sem, char *buf, size_t bufsize)
285{
286 struct vm_area_struct *vma;
287 size_t namelen, remaining;
288 unsigned long size, offset, adjust;
289 char *p, *modname;
290 const char *name;
291 int rc;
292
293 /*
294 * Look one byte back for every caller frame (i.e. those that
295 * aren't a new context) so we look up symbol data for the
296 * call itself, not the following instruction, which may be on
297 * a different line (or in a different function).
298 */
299 adjust = !kbt->new_context;
300 address -= adjust;
301
302 if (address >= PAGE_OFFSET) {
303 /* Handle kernel symbols. */
304 BUG_ON(bufsize < KSYM_NAME_LEN);
305 name = kallsyms_lookup(address, &size, &offset,
306 &modname, buf);
307 if (name == NULL) {
308 buf[0] = '\0';
309 return;
310 }
311 namelen = strlen(buf);
312 remaining = (bufsize - 1) - namelen;
313 p = buf + namelen;
314 rc = snprintf(p, remaining, "+%#lx/%#lx ",
315 offset + adjust, size);
316 if (modname && rc < remaining)
317 snprintf(p + rc, remaining - rc, "[%s] ", modname);
318 buf[bufsize-1] = '\0';
319 return;
320 }
321
322 /* If we don't have the mmap_sem, we can't show any more info. */
323 buf[0] = '\0';
324 if (!have_mmap_sem)
325 return;
326
327 /* Find vma info. */
328 vma = find_vma(kbt->task->mm, address);
329 if (vma == NULL || address < vma->vm_start) {
330 snprintf(buf, bufsize, "[unmapped address] ");
331 return;
332 }
333
334 if (vma->vm_file) {
335 char *s;
336 p = d_path(&vma->vm_file->f_path, buf, bufsize);
337 if (IS_ERR(p))
338 p = "?";
339 s = strrchr(p, '/');
340 if (s)
341 p = s+1;
342 } else {
343 p = "anon";
344 }
345
346 /* Generate a string description of the vma info. */
347 namelen = strlen(p);
348 remaining = (bufsize - 1) - namelen;
349 memmove(buf, p, namelen);
350 snprintf(buf + namelen, remaining, "[%lx+%lx] ",
351 vma->vm_start, vma->vm_end - vma->vm_start);
352}
353
3ef23111
CM
354/*
355 * Avoid possible crash recursion during backtrace. If it happens, it
356 * makes it easy to lose the actual root cause of the failure, so we
357 * put a simple guard on all the backtrace loops.
358 */
359static bool start_backtrace(void)
360{
361 if (current->thread.in_backtrace) {
362 pr_err("Backtrace requested while in backtrace!\n");
363 return false;
364 }
365 current->thread.in_backtrace = true;
366 return true;
367}
368
369static void end_backtrace(void)
370{
371 current->thread.in_backtrace = false;
372}
373
867e359b
CM
374/*
375 * This method wraps the backtracer's more generic support.
376 * It is only invoked from the architecture-specific code; show_stack()
377 * and dump_stack() (in entry.S) are architecture-independent entry points.
378 */
379void tile_show_stack(struct KBacktraceIterator *kbt, int headers)
380{
381 int i;
5f639fdc 382 int have_mmap_sem = 0;
867e359b 383
3ef23111
CM
384 if (!start_backtrace())
385 return;
867e359b
CM
386 if (headers) {
387 /*
388 * Add a blank line since if we are called from panic(),
389 * then bust_spinlocks() spit out a space in front of us
390 * and it will mess up our KERN_ERR.
391 */
0707ad30
CM
392 pr_err("\n");
393 pr_err("Starting stack dump of tid %d, pid %d (%s)"
867e359b
CM
394 " on cpu %d at cycle %lld\n",
395 kbt->task->pid, kbt->task->tgid, kbt->task->comm,
bc1a298f 396 raw_smp_processor_id(), get_cycles());
867e359b 397 }
867e359b
CM
398 kbt->verbose = 1;
399 i = 0;
400 for (; !KBacktraceIterator_end(kbt); KBacktraceIterator_next(kbt)) {
867e359b 401 char namebuf[KSYM_NAME_LEN+100];
5f639fdc 402 unsigned long address = kbt->it.pc;
867e359b 403
5f639fdc
CM
404 /* Try to acquire the mmap_sem as we pass into userspace. */
405 if (address < PAGE_OFFSET && !have_mmap_sem && kbt->task->mm)
406 have_mmap_sem =
407 down_read_trylock(&kbt->task->mm->mmap_sem);
408
409 describe_addr(kbt, address, have_mmap_sem,
410 namebuf, sizeof(namebuf));
867e359b 411
0707ad30 412 pr_err(" frame %d: 0x%lx %s(sp 0x%lx)\n",
867e359b
CM
413 i++, address, namebuf, (unsigned long)(kbt->it.sp));
414
415 if (i >= 100) {
0707ad30 416 pr_err("Stack dump truncated"
867e359b
CM
417 " (%d frames)\n", i);
418 break;
419 }
420 }
dabe98c9
CM
421 if (kbt->end == KBT_LOOP)
422 pr_err("Stack dump stopped; next frame identical to this one\n");
867e359b 423 if (headers)
0707ad30 424 pr_err("Stack dump complete\n");
5f639fdc
CM
425 if (have_mmap_sem)
426 up_read(&kbt->task->mm->mmap_sem);
3ef23111 427 end_backtrace();
867e359b
CM
428}
429EXPORT_SYMBOL(tile_show_stack);
430
431
432/* This is called from show_regs() and _dump_stack() */
433void dump_stack_regs(struct pt_regs *regs)
434{
435 struct KBacktraceIterator kbt;
436 KBacktraceIterator_init(&kbt, NULL, regs);
437 tile_show_stack(&kbt, 1);
438}
439EXPORT_SYMBOL(dump_stack_regs);
440
441static struct pt_regs *regs_to_pt_regs(struct pt_regs *regs,
442 ulong pc, ulong lr, ulong sp, ulong r52)
443{
444 memset(regs, 0, sizeof(struct pt_regs));
445 regs->pc = pc;
446 regs->lr = lr;
447 regs->sp = sp;
448 regs->regs[52] = r52;
449 return regs;
450}
451
452/* This is called from dump_stack() and just converts to pt_regs */
453void _dump_stack(int dummy, ulong pc, ulong lr, ulong sp, ulong r52)
454{
455 struct pt_regs regs;
456 dump_stack_regs(regs_to_pt_regs(&regs, pc, lr, sp, r52));
457}
458
459/* This is called from KBacktraceIterator_init_current() */
460void _KBacktraceIterator_init_current(struct KBacktraceIterator *kbt, ulong pc,
461 ulong lr, ulong sp, ulong r52)
462{
463 struct pt_regs regs;
464 KBacktraceIterator_init(kbt, NULL,
465 regs_to_pt_regs(&regs, pc, lr, sp, r52));
466}
467
0a0fca9d 468/* This is called only from kernel/sched/core.c, with esp == NULL */
867e359b
CM
469void show_stack(struct task_struct *task, unsigned long *esp)
470{
471 struct KBacktraceIterator kbt;
472 if (task == NULL || task == current)
473 KBacktraceIterator_init_current(&kbt);
474 else
475 KBacktraceIterator_init(&kbt, task, NULL);
476 tile_show_stack(&kbt, 0);
477}
478
479#ifdef CONFIG_STACKTRACE
480
481/* Support generic Linux stack API too */
482
483void save_stack_trace_tsk(struct task_struct *task, struct stack_trace *trace)
484{
485 struct KBacktraceIterator kbt;
486 int skip = trace->skip;
487 int i = 0;
488
3ef23111
CM
489 if (!start_backtrace())
490 goto done;
867e359b
CM
491 if (task == NULL || task == current)
492 KBacktraceIterator_init_current(&kbt);
493 else
494 KBacktraceIterator_init(&kbt, task, NULL);
495 for (; !KBacktraceIterator_end(&kbt); KBacktraceIterator_next(&kbt)) {
496 if (skip) {
497 --skip;
498 continue;
499 }
500 if (i >= trace->max_entries || kbt.it.pc < PAGE_OFFSET)
501 break;
502 trace->entries[i++] = kbt.it.pc;
503 }
3ef23111
CM
504 end_backtrace();
505done:
867e359b
CM
506 trace->nr_entries = i;
507}
508EXPORT_SYMBOL(save_stack_trace_tsk);
509
510void save_stack_trace(struct stack_trace *trace)
511{
512 save_stack_trace_tsk(NULL, trace);
513}
7c63e1ee 514EXPORT_SYMBOL_GPL(save_stack_trace);
867e359b
CM
515
516#endif
517
518/* In entry.S */
519EXPORT_SYMBOL(KBacktraceIterator_init_current);