stack.c 15 KB

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