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