stack.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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/sched/debug.h>
  16. #include <linux/sched/task_stack.h>
  17. #include <linux/kernel.h>
  18. #include <linux/kprobes.h>
  19. #include <linux/module.h>
  20. #include <linux/pfn.h>
  21. #include <linux/kallsyms.h>
  22. #include <linux/stacktrace.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/mmzone.h>
  25. #include <linux/dcache.h>
  26. #include <linux/fs.h>
  27. #include <linux/hardirq.h>
  28. #include <linux/string.h>
  29. #include <asm/backtrace.h>
  30. #include <asm/page.h>
  31. #include <asm/ucontext.h>
  32. #include <asm/switch_to.h>
  33. #include <asm/sigframe.h>
  34. #include <asm/stack.h>
  35. #include <asm/vdso.h>
  36. #include <arch/abi.h>
  37. #include <arch/interrupts.h>
  38. #define KBT_ONGOING 0 /* Backtrace still ongoing */
  39. #define KBT_DONE 1 /* Backtrace cleanly completed */
  40. #define KBT_RUNNING 2 /* Can't run backtrace on a running task */
  41. #define KBT_LOOP 3 /* Backtrace entered a loop */
  42. /* Is address on the specified kernel stack? */
  43. static int in_kernel_stack(struct KBacktraceIterator *kbt, unsigned long sp)
  44. {
  45. ulong kstack_base = (ulong) kbt->task->stack;
  46. if (kstack_base == 0) /* corrupt task pointer; just follow stack... */
  47. return sp >= PAGE_OFFSET && sp < (unsigned long)high_memory;
  48. return sp >= kstack_base && sp < kstack_base + THREAD_SIZE;
  49. }
  50. /* Callback for backtracer; basically a glorified memcpy */
  51. static bool read_memory_func(void *result, unsigned long address,
  52. unsigned int size, void *vkbt)
  53. {
  54. int retval;
  55. struct KBacktraceIterator *kbt = (struct KBacktraceIterator *)vkbt;
  56. if (address == 0)
  57. return 0;
  58. if (__kernel_text_address(address)) {
  59. /* OK to read kernel code. */
  60. } else if (address >= PAGE_OFFSET) {
  61. /* We only tolerate kernel-space reads of this task's stack */
  62. if (!in_kernel_stack(kbt, address))
  63. return 0;
  64. } else if (!kbt->is_current) {
  65. return 0; /* can't read from other user address spaces */
  66. }
  67. pagefault_disable();
  68. retval = __copy_from_user_inatomic(result,
  69. (void __user __force *)address,
  70. size);
  71. pagefault_enable();
  72. return (retval == 0);
  73. }
  74. /* Return a pt_regs pointer for a valid fault handler frame */
  75. static struct pt_regs *valid_fault_handler(struct KBacktraceIterator* kbt)
  76. {
  77. char fault[64];
  78. unsigned long sp = kbt->it.sp;
  79. struct pt_regs *p;
  80. if (sp % sizeof(long) != 0)
  81. return NULL;
  82. if (!in_kernel_stack(kbt, sp))
  83. return NULL;
  84. if (!in_kernel_stack(kbt, sp + C_ABI_SAVE_AREA_SIZE + PTREGS_SIZE-1))
  85. return NULL;
  86. p = (struct pt_regs *)(sp + C_ABI_SAVE_AREA_SIZE);
  87. if (kbt->verbose) { /* else we aren't going to use it */
  88. if (p->faultnum == INT_SWINT_1 ||
  89. p->faultnum == INT_SWINT_1_SIGRETURN)
  90. snprintf(fault, sizeof(fault),
  91. "syscall %ld", p->regs[TREG_SYSCALL_NR]);
  92. else
  93. snprintf(fault, sizeof(fault),
  94. "interrupt %ld", p->faultnum);
  95. }
  96. if (EX1_PL(p->ex1) == KERNEL_PL &&
  97. __kernel_text_address(p->pc) &&
  98. in_kernel_stack(kbt, p->sp) &&
  99. p->sp >= sp) {
  100. if (kbt->verbose)
  101. pr_err(" <%s while in kernel mode>\n", fault);
  102. } else if (user_mode(p) &&
  103. p->sp < PAGE_OFFSET && p->sp != 0) {
  104. if (kbt->verbose)
  105. pr_err(" <%s while in user mode>\n", fault);
  106. } else {
  107. if (kbt->verbose && (p->pc != 0 || p->sp != 0 || p->ex1 != 0))
  108. pr_err(" (odd fault: pc %#lx, sp %#lx, ex1 %#lx?)\n",
  109. p->pc, p->sp, p->ex1);
  110. return NULL;
  111. }
  112. if (kbt->profile && ((1ULL << p->faultnum) & QUEUED_INTERRUPTS) != 0)
  113. return NULL;
  114. return p;
  115. }
  116. /* Is the iterator pointing to a sigreturn trampoline? */
  117. static int is_sigreturn(struct KBacktraceIterator *kbt)
  118. {
  119. return kbt->task->mm &&
  120. (kbt->it.pc == ((ulong)kbt->task->mm->context.vdso_base +
  121. (ulong)&__vdso_rt_sigreturn));
  122. }
  123. /* Return a pt_regs pointer for a valid signal handler frame */
  124. static struct pt_regs *valid_sigframe(struct KBacktraceIterator* kbt,
  125. struct rt_sigframe* kframe)
  126. {
  127. BacktraceIterator *b = &kbt->it;
  128. if (is_sigreturn(kbt) && b->sp < PAGE_OFFSET &&
  129. b->sp % sizeof(long) == 0) {
  130. int retval;
  131. pagefault_disable();
  132. retval = __copy_from_user_inatomic(
  133. kframe, (void __user __force *)b->sp,
  134. sizeof(*kframe));
  135. pagefault_enable();
  136. if (retval != 0 ||
  137. (unsigned int)(kframe->info.si_signo) >= _NSIG)
  138. return NULL;
  139. if (kbt->verbose) {
  140. pr_err(" <received signal %d>\n",
  141. kframe->info.si_signo);
  142. }
  143. return (struct pt_regs *)&kframe->uc.uc_mcontext;
  144. }
  145. return NULL;
  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 (!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 = file_path(vma->vm_file, 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_info()->in_backtrace) {
  329. pr_err("Backtrace requested while in backtrace!\n");
  330. return false;
  331. }
  332. current_thread_info()->in_backtrace = true;
  333. return true;
  334. }
  335. static void end_backtrace(void)
  336. {
  337. current_thread_info()->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() are architecture-independent entry points.
  343. */
  344. void tile_show_stack(struct KBacktraceIterator *kbt)
  345. {
  346. int i;
  347. int have_mmap_sem = 0;
  348. if (!start_backtrace())
  349. return;
  350. kbt->verbose = 1;
  351. i = 0;
  352. for (; !KBacktraceIterator_end(kbt); KBacktraceIterator_next(kbt)) {
  353. char namebuf[KSYM_NAME_LEN+100];
  354. unsigned long address = kbt->it.pc;
  355. /*
  356. * Try to acquire the mmap_sem as we pass into userspace.
  357. * If we're in an interrupt context, don't even try, since
  358. * it's not safe to call e.g. d_path() from an interrupt,
  359. * since it uses spin locks without disabling interrupts.
  360. * Note we test "kbt->task == current", not "kbt->is_current",
  361. * since we're checking that "current" will work in d_path().
  362. */
  363. if (kbt->task == current && address < PAGE_OFFSET &&
  364. !have_mmap_sem && kbt->task->mm && !in_interrupt()) {
  365. have_mmap_sem =
  366. down_read_trylock(&kbt->task->mm->mmap_sem);
  367. }
  368. describe_addr(kbt, address, have_mmap_sem,
  369. namebuf, sizeof(namebuf));
  370. pr_err(" frame %d: 0x%lx %s(sp 0x%lx)\n",
  371. i++, address, namebuf, (unsigned long)(kbt->it.sp));
  372. if (i >= 100) {
  373. pr_err("Stack dump truncated (%d frames)\n", i);
  374. break;
  375. }
  376. }
  377. if (kbt->end == KBT_LOOP)
  378. pr_err("Stack dump stopped; next frame identical to this one\n");
  379. if (have_mmap_sem)
  380. up_read(&kbt->task->mm->mmap_sem);
  381. end_backtrace();
  382. }
  383. EXPORT_SYMBOL(tile_show_stack);
  384. static struct pt_regs *regs_to_pt_regs(struct pt_regs *regs,
  385. ulong pc, ulong lr, ulong sp, ulong r52)
  386. {
  387. memset(regs, 0, sizeof(struct pt_regs));
  388. regs->pc = pc;
  389. regs->lr = lr;
  390. regs->sp = sp;
  391. regs->regs[52] = r52;
  392. return regs;
  393. }
  394. /* Deprecated function currently only used by kernel_double_fault(). */
  395. void _dump_stack(int dummy, ulong pc, ulong lr, ulong sp, ulong r52)
  396. {
  397. struct KBacktraceIterator kbt;
  398. struct pt_regs regs;
  399. regs_to_pt_regs(&regs, pc, lr, sp, r52);
  400. KBacktraceIterator_init(&kbt, NULL, &regs);
  401. tile_show_stack(&kbt);
  402. }
  403. /* This is called from KBacktraceIterator_init_current() */
  404. void _KBacktraceIterator_init_current(struct KBacktraceIterator *kbt, ulong pc,
  405. ulong lr, ulong sp, ulong r52)
  406. {
  407. struct pt_regs regs;
  408. KBacktraceIterator_init(kbt, NULL,
  409. regs_to_pt_regs(&regs, pc, lr, sp, r52));
  410. }
  411. /*
  412. * Called from sched_show_task() with task != NULL, or dump_stack()
  413. * with task == NULL. The esp argument is always NULL.
  414. */
  415. void show_stack(struct task_struct *task, unsigned long *esp)
  416. {
  417. struct KBacktraceIterator kbt;
  418. if (task == NULL || task == current) {
  419. KBacktraceIterator_init_current(&kbt);
  420. KBacktraceIterator_next(&kbt); /* don't show first frame */
  421. } else {
  422. KBacktraceIterator_init(&kbt, task, NULL);
  423. }
  424. tile_show_stack(&kbt);
  425. }
  426. #ifdef CONFIG_STACKTRACE
  427. /* Support generic Linux stack API too */
  428. static void save_stack_trace_common(struct task_struct *task,
  429. struct pt_regs *regs,
  430. bool user,
  431. 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 (regs != NULL) {
  439. KBacktraceIterator_init(&kbt, NULL, regs);
  440. } else if (task == NULL || task == current) {
  441. KBacktraceIterator_init_current(&kbt);
  442. skip++; /* don't show KBacktraceIterator_init_current */
  443. } else {
  444. KBacktraceIterator_init(&kbt, task, NULL);
  445. }
  446. for (; !KBacktraceIterator_end(&kbt); KBacktraceIterator_next(&kbt)) {
  447. if (skip) {
  448. --skip;
  449. continue;
  450. }
  451. if (i >= trace->max_entries ||
  452. (!user && kbt.it.pc < PAGE_OFFSET))
  453. break;
  454. trace->entries[i++] = kbt.it.pc;
  455. }
  456. end_backtrace();
  457. done:
  458. if (i < trace->max_entries)
  459. trace->entries[i++] = ULONG_MAX;
  460. trace->nr_entries = i;
  461. }
  462. void save_stack_trace_tsk(struct task_struct *task, struct stack_trace *trace)
  463. {
  464. save_stack_trace_common(task, NULL, false, trace);
  465. }
  466. EXPORT_SYMBOL(save_stack_trace_tsk);
  467. void save_stack_trace(struct stack_trace *trace)
  468. {
  469. save_stack_trace_common(NULL, NULL, false, trace);
  470. }
  471. EXPORT_SYMBOL_GPL(save_stack_trace);
  472. void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
  473. {
  474. save_stack_trace_common(NULL, regs, false, trace);
  475. }
  476. void save_stack_trace_user(struct stack_trace *trace)
  477. {
  478. /* Trace user stack if we are not a kernel thread. */
  479. if (current->mm)
  480. save_stack_trace_common(NULL, task_pt_regs(current),
  481. true, trace);
  482. else if (trace->nr_entries < trace->max_entries)
  483. trace->entries[trace->nr_entries++] = ULONG_MAX;
  484. }
  485. #endif
  486. /* In entry.S */
  487. EXPORT_SYMBOL(KBacktraceIterator_init_current);