stack.c 14 KB

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