stack.c 14 KB

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