stack.c 13 KB

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