stack.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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 <asm/backtrace.h>
  24. #include <asm/page.h>
  25. #include <asm/tlbflush.h>
  26. #include <asm/ucontext.h>
  27. #include <asm/sigframe.h>
  28. #include <asm/stack.h>
  29. #include <arch/abi.h>
  30. #include <arch/interrupts.h>
  31. /* Is address on the specified kernel stack? */
  32. static int in_kernel_stack(struct KBacktraceIterator *kbt, VirtualAddress sp)
  33. {
  34. ulong kstack_base = (ulong) kbt->task->stack;
  35. if (kstack_base == 0) /* corrupt task pointer; just follow stack... */
  36. return sp >= PAGE_OFFSET && sp < (unsigned long)high_memory;
  37. return sp >= kstack_base && sp < kstack_base + THREAD_SIZE;
  38. }
  39. /* Is address in the specified kernel code? */
  40. static int in_kernel_text(VirtualAddress address)
  41. {
  42. return (address >= MEM_SV_INTRPT &&
  43. address < MEM_SV_INTRPT + HPAGE_SIZE);
  44. }
  45. /* Is address valid for reading? */
  46. static int valid_address(struct KBacktraceIterator *kbt, VirtualAddress address)
  47. {
  48. HV_PTE *l1_pgtable = kbt->pgtable;
  49. HV_PTE *l2_pgtable;
  50. unsigned long pfn;
  51. HV_PTE pte;
  52. struct page *page;
  53. if (l1_pgtable == NULL)
  54. return 0; /* can't read user space in other tasks */
  55. pte = l1_pgtable[HV_L1_INDEX(address)];
  56. if (!hv_pte_get_present(pte))
  57. return 0;
  58. pfn = hv_pte_get_pfn(pte);
  59. if (pte_huge(pte)) {
  60. if (!pfn_valid(pfn)) {
  61. pr_err("huge page has bad pfn %#lx\n", pfn);
  62. return 0;
  63. }
  64. return hv_pte_get_present(pte) && hv_pte_get_readable(pte);
  65. }
  66. page = pfn_to_page(pfn);
  67. if (PageHighMem(page)) {
  68. pr_err("L2 page table not in LOWMEM (%#llx)\n",
  69. HV_PFN_TO_CPA(pfn));
  70. return 0;
  71. }
  72. l2_pgtable = (HV_PTE *)pfn_to_kaddr(pfn);
  73. pte = l2_pgtable[HV_L2_INDEX(address)];
  74. return hv_pte_get_present(pte) && hv_pte_get_readable(pte);
  75. }
  76. /* Callback for backtracer; basically a glorified memcpy */
  77. static bool read_memory_func(void *result, VirtualAddress address,
  78. unsigned int size, void *vkbt)
  79. {
  80. int retval;
  81. struct KBacktraceIterator *kbt = (struct KBacktraceIterator *)vkbt;
  82. if (in_kernel_text(address)) {
  83. /* OK to read kernel code. */
  84. } else if (address >= PAGE_OFFSET) {
  85. /* We only tolerate kernel-space reads of this task's stack */
  86. if (!in_kernel_stack(kbt, address))
  87. return 0;
  88. } else if (!valid_address(kbt, address)) {
  89. return 0; /* invalid user-space address */
  90. }
  91. pagefault_disable();
  92. retval = __copy_from_user_inatomic(result,
  93. (void __user __force *)address,
  94. size);
  95. pagefault_enable();
  96. return (retval == 0);
  97. }
  98. /* Return a pt_regs pointer for a valid fault handler frame */
  99. static struct pt_regs *valid_fault_handler(struct KBacktraceIterator* kbt)
  100. {
  101. const char *fault = NULL; /* happy compiler */
  102. char fault_buf[64];
  103. VirtualAddress sp = kbt->it.sp;
  104. struct pt_regs *p;
  105. if (!in_kernel_stack(kbt, sp))
  106. return NULL;
  107. if (!in_kernel_stack(kbt, sp + C_ABI_SAVE_AREA_SIZE + PTREGS_SIZE-1))
  108. return NULL;
  109. p = (struct pt_regs *)(sp + C_ABI_SAVE_AREA_SIZE);
  110. if (p->faultnum == INT_SWINT_1 || p->faultnum == INT_SWINT_1_SIGRETURN)
  111. fault = "syscall";
  112. else {
  113. if (kbt->verbose) { /* else we aren't going to use it */
  114. snprintf(fault_buf, sizeof(fault_buf),
  115. "interrupt %ld", p->faultnum);
  116. fault = fault_buf;
  117. }
  118. }
  119. if (EX1_PL(p->ex1) == KERNEL_PL &&
  120. in_kernel_text(p->pc) &&
  121. in_kernel_stack(kbt, p->sp) &&
  122. p->sp >= sp) {
  123. if (kbt->verbose)
  124. pr_err(" <%s while in kernel mode>\n", fault);
  125. } else if (EX1_PL(p->ex1) == USER_PL &&
  126. p->pc < PAGE_OFFSET &&
  127. p->sp < PAGE_OFFSET) {
  128. if (kbt->verbose)
  129. pr_err(" <%s while in user mode>\n", fault);
  130. } else if (kbt->verbose) {
  131. pr_err(" (odd fault: pc %#lx, sp %#lx, ex1 %#lx?)\n",
  132. p->pc, p->sp, p->ex1);
  133. p = NULL;
  134. }
  135. if (!kbt->profile || (INT_MASK(p->faultnum) & QUEUED_INTERRUPTS) == 0)
  136. return p;
  137. return NULL;
  138. }
  139. /* Is the pc pointing to a sigreturn trampoline? */
  140. static int is_sigreturn(VirtualAddress pc)
  141. {
  142. return (pc == VDSO_BASE);
  143. }
  144. /* Return a pt_regs pointer for a valid signal handler frame */
  145. static struct pt_regs *valid_sigframe(struct KBacktraceIterator* kbt)
  146. {
  147. BacktraceIterator *b = &kbt->it;
  148. if (b->pc == VDSO_BASE) {
  149. struct rt_sigframe *frame;
  150. unsigned long sigframe_top =
  151. b->sp + sizeof(struct rt_sigframe) - 1;
  152. if (!valid_address(kbt, b->sp) ||
  153. !valid_address(kbt, sigframe_top)) {
  154. if (kbt->verbose)
  155. pr_err(" (odd signal: sp %#lx?)\n",
  156. (unsigned long)(b->sp));
  157. return NULL;
  158. }
  159. frame = (struct rt_sigframe *)b->sp;
  160. if (kbt->verbose) {
  161. pr_err(" <received signal %d>\n",
  162. frame->info.si_signo);
  163. }
  164. return (struct pt_regs *)&frame->uc.uc_mcontext;
  165. }
  166. return NULL;
  167. }
  168. static int KBacktraceIterator_is_sigreturn(struct KBacktraceIterator *kbt)
  169. {
  170. return is_sigreturn(kbt->it.pc);
  171. }
  172. static int KBacktraceIterator_restart(struct KBacktraceIterator *kbt)
  173. {
  174. struct pt_regs *p;
  175. p = valid_fault_handler(kbt);
  176. if (p == NULL)
  177. p = valid_sigframe(kbt);
  178. if (p == NULL)
  179. return 0;
  180. backtrace_init(&kbt->it, read_memory_func, kbt,
  181. p->pc, p->lr, p->sp, p->regs[52]);
  182. kbt->new_context = 1;
  183. return 1;
  184. }
  185. /* Find a frame that isn't a sigreturn, if there is one. */
  186. static int KBacktraceIterator_next_item_inclusive(
  187. struct KBacktraceIterator *kbt)
  188. {
  189. for (;;) {
  190. do {
  191. if (!KBacktraceIterator_is_sigreturn(kbt))
  192. return 1;
  193. } while (backtrace_next(&kbt->it));
  194. if (!KBacktraceIterator_restart(kbt))
  195. return 0;
  196. }
  197. }
  198. /*
  199. * If the current sp is on a page different than what we recorded
  200. * as the top-of-kernel-stack last time we context switched, we have
  201. * probably blown the stack, and nothing is going to work out well.
  202. * If we can at least get out a warning, that may help the debug,
  203. * though we probably won't be able to backtrace into the code that
  204. * actually did the recursive damage.
  205. */
  206. static void validate_stack(struct pt_regs *regs)
  207. {
  208. int cpu = smp_processor_id();
  209. unsigned long ksp0 = get_current_ksp0();
  210. unsigned long ksp0_base = ksp0 - THREAD_SIZE;
  211. unsigned long sp = stack_pointer;
  212. if (EX1_PL(regs->ex1) == KERNEL_PL && regs->sp >= ksp0) {
  213. pr_err("WARNING: cpu %d: kernel stack page %#lx underrun!\n"
  214. " sp %#lx (%#lx in caller), caller pc %#lx, lr %#lx\n",
  215. cpu, ksp0_base, sp, regs->sp, regs->pc, regs->lr);
  216. }
  217. else if (sp < ksp0_base + sizeof(struct thread_info)) {
  218. pr_err("WARNING: cpu %d: kernel stack page %#lx overrun!\n"
  219. " sp %#lx (%#lx in caller), caller pc %#lx, lr %#lx\n",
  220. cpu, ksp0_base, sp, regs->sp, regs->pc, regs->lr);
  221. }
  222. }
  223. void KBacktraceIterator_init(struct KBacktraceIterator *kbt,
  224. struct task_struct *t, struct pt_regs *regs)
  225. {
  226. VirtualAddress pc, lr, sp, r52;
  227. int is_current;
  228. /*
  229. * Set up callback information. We grab the kernel stack base
  230. * so we will allow reads of that address range, and if we're
  231. * asking about the current process we grab the page table
  232. * so we can check user accesses before trying to read them.
  233. * We flush the TLB to avoid any weird skew issues.
  234. */
  235. is_current = (t == NULL);
  236. kbt->is_current = is_current;
  237. if (is_current)
  238. t = validate_current();
  239. kbt->task = t;
  240. kbt->pgtable = NULL;
  241. kbt->verbose = 0; /* override in caller if desired */
  242. kbt->profile = 0; /* override in caller if desired */
  243. kbt->end = 0;
  244. kbt->new_context = 0;
  245. if (is_current) {
  246. HV_PhysAddr pgdir_pa = hv_inquire_context().page_table;
  247. if (pgdir_pa == (unsigned long)swapper_pg_dir - PAGE_OFFSET) {
  248. /*
  249. * Not just an optimization: this also allows
  250. * this to work at all before va/pa mappings
  251. * are set up.
  252. */
  253. kbt->pgtable = swapper_pg_dir;
  254. } else {
  255. struct page *page = pfn_to_page(PFN_DOWN(pgdir_pa));
  256. if (!PageHighMem(page))
  257. kbt->pgtable = __va(pgdir_pa);
  258. else
  259. pr_err("page table not in LOWMEM"
  260. " (%#llx)\n", pgdir_pa);
  261. }
  262. local_flush_tlb_all();
  263. validate_stack(regs);
  264. }
  265. if (regs == NULL) {
  266. if (is_current || t->state == TASK_RUNNING) {
  267. /* Can't do this; we need registers */
  268. kbt->end = 1;
  269. return;
  270. }
  271. pc = get_switch_to_pc();
  272. lr = t->thread.pc;
  273. sp = t->thread.ksp;
  274. r52 = 0;
  275. } else {
  276. pc = regs->pc;
  277. lr = regs->lr;
  278. sp = regs->sp;
  279. r52 = regs->regs[52];
  280. }
  281. backtrace_init(&kbt->it, read_memory_func, kbt, pc, lr, sp, r52);
  282. kbt->end = !KBacktraceIterator_next_item_inclusive(kbt);
  283. }
  284. EXPORT_SYMBOL(KBacktraceIterator_init);
  285. int KBacktraceIterator_end(struct KBacktraceIterator *kbt)
  286. {
  287. return kbt->end;
  288. }
  289. EXPORT_SYMBOL(KBacktraceIterator_end);
  290. void KBacktraceIterator_next(struct KBacktraceIterator *kbt)
  291. {
  292. kbt->new_context = 0;
  293. if (!backtrace_next(&kbt->it) &&
  294. !KBacktraceIterator_restart(kbt)) {
  295. kbt->end = 1;
  296. return;
  297. }
  298. kbt->end = !KBacktraceIterator_next_item_inclusive(kbt);
  299. }
  300. EXPORT_SYMBOL(KBacktraceIterator_next);
  301. /*
  302. * This method wraps the backtracer's more generic support.
  303. * It is only invoked from the architecture-specific code; show_stack()
  304. * and dump_stack() (in entry.S) are architecture-independent entry points.
  305. */
  306. void tile_show_stack(struct KBacktraceIterator *kbt, int headers)
  307. {
  308. int i;
  309. if (headers) {
  310. /*
  311. * Add a blank line since if we are called from panic(),
  312. * then bust_spinlocks() spit out a space in front of us
  313. * and it will mess up our KERN_ERR.
  314. */
  315. pr_err("\n");
  316. pr_err("Starting stack dump of tid %d, pid %d (%s)"
  317. " on cpu %d at cycle %lld\n",
  318. kbt->task->pid, kbt->task->tgid, kbt->task->comm,
  319. smp_processor_id(), get_cycles());
  320. }
  321. kbt->verbose = 1;
  322. i = 0;
  323. for (; !KBacktraceIterator_end(kbt); KBacktraceIterator_next(kbt)) {
  324. char *modname;
  325. const char *name;
  326. unsigned long address = kbt->it.pc;
  327. unsigned long offset, size;
  328. char namebuf[KSYM_NAME_LEN+100];
  329. if (address >= PAGE_OFFSET)
  330. name = kallsyms_lookup(address, &size, &offset,
  331. &modname, namebuf);
  332. else
  333. name = NULL;
  334. if (!name)
  335. namebuf[0] = '\0';
  336. else {
  337. size_t namelen = strlen(namebuf);
  338. size_t remaining = (sizeof(namebuf) - 1) - namelen;
  339. char *p = namebuf + namelen;
  340. int rc = snprintf(p, remaining, "+%#lx/%#lx ",
  341. offset, size);
  342. if (modname && rc < remaining)
  343. snprintf(p + rc, remaining - rc,
  344. "[%s] ", modname);
  345. namebuf[sizeof(namebuf)-1] = '\0';
  346. }
  347. pr_err(" frame %d: 0x%lx %s(sp 0x%lx)\n",
  348. i++, address, namebuf, (unsigned long)(kbt->it.sp));
  349. if (i >= 100) {
  350. pr_err("Stack dump truncated"
  351. " (%d frames)\n", i);
  352. break;
  353. }
  354. }
  355. if (headers)
  356. pr_err("Stack dump complete\n");
  357. }
  358. EXPORT_SYMBOL(tile_show_stack);
  359. /* This is called from show_regs() and _dump_stack() */
  360. void dump_stack_regs(struct pt_regs *regs)
  361. {
  362. struct KBacktraceIterator kbt;
  363. KBacktraceIterator_init(&kbt, NULL, regs);
  364. tile_show_stack(&kbt, 1);
  365. }
  366. EXPORT_SYMBOL(dump_stack_regs);
  367. static struct pt_regs *regs_to_pt_regs(struct pt_regs *regs,
  368. ulong pc, ulong lr, ulong sp, ulong r52)
  369. {
  370. memset(regs, 0, sizeof(struct pt_regs));
  371. regs->pc = pc;
  372. regs->lr = lr;
  373. regs->sp = sp;
  374. regs->regs[52] = r52;
  375. return regs;
  376. }
  377. /* This is called from dump_stack() and just converts to pt_regs */
  378. void _dump_stack(int dummy, ulong pc, ulong lr, ulong sp, ulong r52)
  379. {
  380. struct pt_regs regs;
  381. dump_stack_regs(regs_to_pt_regs(&regs, pc, lr, sp, r52));
  382. }
  383. /* This is called from KBacktraceIterator_init_current() */
  384. void _KBacktraceIterator_init_current(struct KBacktraceIterator *kbt, ulong pc,
  385. ulong lr, ulong sp, ulong r52)
  386. {
  387. struct pt_regs regs;
  388. KBacktraceIterator_init(kbt, NULL,
  389. regs_to_pt_regs(&regs, pc, lr, sp, r52));
  390. }
  391. /* This is called only from kernel/sched.c, with esp == NULL */
  392. void show_stack(struct task_struct *task, unsigned long *esp)
  393. {
  394. struct KBacktraceIterator kbt;
  395. if (task == NULL || task == current)
  396. KBacktraceIterator_init_current(&kbt);
  397. else
  398. KBacktraceIterator_init(&kbt, task, NULL);
  399. tile_show_stack(&kbt, 0);
  400. }
  401. #ifdef CONFIG_STACKTRACE
  402. /* Support generic Linux stack API too */
  403. void save_stack_trace_tsk(struct task_struct *task, struct stack_trace *trace)
  404. {
  405. struct KBacktraceIterator kbt;
  406. int skip = trace->skip;
  407. int i = 0;
  408. if (task == NULL || task == current)
  409. KBacktraceIterator_init_current(&kbt);
  410. else
  411. KBacktraceIterator_init(&kbt, task, NULL);
  412. for (; !KBacktraceIterator_end(&kbt); KBacktraceIterator_next(&kbt)) {
  413. if (skip) {
  414. --skip;
  415. continue;
  416. }
  417. if (i >= trace->max_entries || kbt.it.pc < PAGE_OFFSET)
  418. break;
  419. trace->entries[i++] = kbt.it.pc;
  420. }
  421. trace->nr_entries = i;
  422. }
  423. EXPORT_SYMBOL(save_stack_trace_tsk);
  424. void save_stack_trace(struct stack_trace *trace)
  425. {
  426. save_stack_trace_tsk(NULL, trace);
  427. }
  428. #endif
  429. /* In entry.S */
  430. EXPORT_SYMBOL(KBacktraceIterator_init_current);