stack.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. pte = l1_pgtable[HV_L1_INDEX(address)];
  54. if (!hv_pte_get_present(pte))
  55. return 0;
  56. pfn = hv_pte_get_pfn(pte);
  57. if (pte_huge(pte)) {
  58. if (!pfn_valid(pfn)) {
  59. printk(KERN_ERR "huge page has bad pfn %#lx\n", pfn);
  60. return 0;
  61. }
  62. return hv_pte_get_present(pte) && hv_pte_get_readable(pte);
  63. }
  64. page = pfn_to_page(pfn);
  65. if (PageHighMem(page)) {
  66. printk(KERN_ERR "L2 page table not in LOWMEM (%#llx)\n",
  67. HV_PFN_TO_CPA(pfn));
  68. return 0;
  69. }
  70. l2_pgtable = (HV_PTE *)pfn_to_kaddr(pfn);
  71. pte = l2_pgtable[HV_L2_INDEX(address)];
  72. return hv_pte_get_present(pte) && hv_pte_get_readable(pte);
  73. }
  74. /* Callback for backtracer; basically a glorified memcpy */
  75. static bool read_memory_func(void *result, VirtualAddress address,
  76. unsigned int size, void *vkbt)
  77. {
  78. int retval;
  79. struct KBacktraceIterator *kbt = (struct KBacktraceIterator *)vkbt;
  80. if (in_kernel_text(address)) {
  81. /* OK to read kernel code. */
  82. } else if (address >= PAGE_OFFSET) {
  83. /* We only tolerate kernel-space reads of this task's stack */
  84. if (!in_kernel_stack(kbt, address))
  85. return 0;
  86. } else if (kbt->pgtable == NULL) {
  87. return 0; /* can't read user space in other tasks */
  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, (const void *)address,
  93. size);
  94. pagefault_enable();
  95. return (retval == 0);
  96. }
  97. /* Return a pt_regs pointer for a valid fault handler frame */
  98. static struct pt_regs *valid_fault_handler(struct KBacktraceIterator* kbt)
  99. {
  100. #ifndef __tilegx__
  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. printk(KERN_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. printk(KERN_ERR " <%s while in user mode>\n", fault);
  130. } else if (kbt->verbose) {
  131. printk(KERN_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. #endif
  138. return NULL;
  139. }
  140. /* Is the pc pointing to a sigreturn trampoline? */
  141. static int is_sigreturn(VirtualAddress pc)
  142. {
  143. return (pc == VDSO_BASE);
  144. }
  145. /* Return a pt_regs pointer for a valid signal handler frame */
  146. static struct pt_regs *valid_sigframe(struct KBacktraceIterator* kbt)
  147. {
  148. BacktraceIterator *b = &kbt->it;
  149. if (b->pc == VDSO_BASE) {
  150. struct rt_sigframe *frame;
  151. unsigned long sigframe_top =
  152. b->sp + sizeof(struct rt_sigframe) - 1;
  153. if (!valid_address(kbt, b->sp) ||
  154. !valid_address(kbt, sigframe_top)) {
  155. if (kbt->verbose)
  156. printk(" (odd signal: sp %#lx?)\n",
  157. (unsigned long)(b->sp));
  158. return NULL;
  159. }
  160. frame = (struct rt_sigframe *)b->sp;
  161. if (kbt->verbose) {
  162. printk(KERN_ERR " <received signal %d>\n",
  163. frame->info.si_signo);
  164. }
  165. return &frame->uc.uc_mcontext.regs;
  166. }
  167. return NULL;
  168. }
  169. int KBacktraceIterator_is_sigreturn(struct KBacktraceIterator *kbt)
  170. {
  171. return is_sigreturn(kbt->it.pc);
  172. }
  173. static int KBacktraceIterator_restart(struct KBacktraceIterator *kbt)
  174. {
  175. struct pt_regs *p;
  176. p = valid_fault_handler(kbt);
  177. if (p == NULL)
  178. p = valid_sigframe(kbt);
  179. if (p == NULL)
  180. return 0;
  181. backtrace_init(&kbt->it, read_memory_func, kbt,
  182. p->pc, p->lr, p->sp, p->regs[52]);
  183. kbt->new_context = 1;
  184. return 1;
  185. }
  186. /* Find a frame that isn't a sigreturn, if there is one. */
  187. static int KBacktraceIterator_next_item_inclusive(
  188. struct KBacktraceIterator *kbt)
  189. {
  190. for (;;) {
  191. do {
  192. if (!KBacktraceIterator_is_sigreturn(kbt))
  193. return 1;
  194. } while (backtrace_next(&kbt->it));
  195. if (!KBacktraceIterator_restart(kbt))
  196. return 0;
  197. }
  198. }
  199. /*
  200. * If the current sp is on a page different than what we recorded
  201. * as the top-of-kernel-stack last time we context switched, we have
  202. * probably blown the stack, and nothing is going to work out well.
  203. * If we can at least get out a warning, that may help the debug,
  204. * though we probably won't be able to backtrace into the code that
  205. * actually did the recursive damage.
  206. */
  207. static void validate_stack(struct pt_regs *regs)
  208. {
  209. int cpu = smp_processor_id();
  210. unsigned long ksp0 = get_current_ksp0();
  211. unsigned long ksp0_base = ksp0 - THREAD_SIZE;
  212. unsigned long sp = stack_pointer;
  213. if (EX1_PL(regs->ex1) == KERNEL_PL && regs->sp >= ksp0) {
  214. printk("WARNING: cpu %d: kernel stack page %#lx underrun!\n"
  215. " sp %#lx (%#lx in caller), caller pc %#lx, lr %#lx\n",
  216. cpu, ksp0_base, sp, regs->sp, regs->pc, regs->lr);
  217. }
  218. else if (sp < ksp0_base + sizeof(struct thread_info)) {
  219. printk("WARNING: cpu %d: kernel stack page %#lx overrun!\n"
  220. " sp %#lx (%#lx in caller), caller pc %#lx, lr %#lx\n",
  221. cpu, ksp0_base, sp, regs->sp, regs->pc, regs->lr);
  222. }
  223. }
  224. void KBacktraceIterator_init(struct KBacktraceIterator *kbt,
  225. struct task_struct *t, struct pt_regs *regs)
  226. {
  227. VirtualAddress pc, lr, sp, r52;
  228. int is_current;
  229. /*
  230. * Set up callback information. We grab the kernel stack base
  231. * so we will allow reads of that address range, and if we're
  232. * asking about the current process we grab the page table
  233. * so we can check user accesses before trying to read them.
  234. * We flush the TLB to avoid any weird skew issues.
  235. */
  236. is_current = (t == NULL);
  237. kbt->is_current = is_current;
  238. if (is_current)
  239. t = validate_current();
  240. kbt->task = t;
  241. kbt->pgtable = NULL;
  242. kbt->verbose = 0; /* override in caller if desired */
  243. kbt->profile = 0; /* override in caller if desired */
  244. kbt->end = 0;
  245. kbt->new_context = 0;
  246. if (is_current) {
  247. HV_PhysAddr pgdir_pa = hv_inquire_context().page_table;
  248. if (pgdir_pa == (unsigned long)swapper_pg_dir - PAGE_OFFSET) {
  249. /*
  250. * Not just an optimization: this also allows
  251. * this to work at all before va/pa mappings
  252. * are set up.
  253. */
  254. kbt->pgtable = swapper_pg_dir;
  255. } else {
  256. struct page *page = pfn_to_page(PFN_DOWN(pgdir_pa));
  257. if (!PageHighMem(page))
  258. kbt->pgtable = __va(pgdir_pa);
  259. else
  260. printk(KERN_ERR "page table not in LOWMEM"
  261. " (%#llx)\n", pgdir_pa);
  262. }
  263. local_flush_tlb_all();
  264. validate_stack(regs);
  265. }
  266. if (regs == NULL) {
  267. extern const void *get_switch_to_pc(void);
  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 = (ulong) 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. printk("\n");
  318. printk(KERN_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. printk(KERN_ERR " frame %d: 0x%lx %s(sp 0x%lx)\n",
  356. i++, address, namebuf, (unsigned long)(kbt->it.sp));
  357. if (i >= 100) {
  358. printk(KERN_ERR "Stack dump truncated"
  359. " (%d frames)\n", i);
  360. break;
  361. }
  362. }
  363. if (headers)
  364. printk(KERN_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);