dumpstack.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Stack dumping functions
  4. *
  5. * Copyright IBM Corp. 1999, 2013
  6. */
  7. #include <linux/kallsyms.h>
  8. #include <linux/hardirq.h>
  9. #include <linux/kprobes.h>
  10. #include <linux/utsname.h>
  11. #include <linux/export.h>
  12. #include <linux/kdebug.h>
  13. #include <linux/ptrace.h>
  14. #include <linux/mm.h>
  15. #include <linux/module.h>
  16. #include <linux/sched.h>
  17. #include <linux/sched/debug.h>
  18. #include <linux/sched/task_stack.h>
  19. #include <asm/processor.h>
  20. #include <asm/debug.h>
  21. #include <asm/dis.h>
  22. #include <asm/ipl.h>
  23. #include <asm/unwind.h>
  24. const char *stack_type_name(enum stack_type type)
  25. {
  26. switch (type) {
  27. case STACK_TYPE_TASK:
  28. return "task";
  29. case STACK_TYPE_IRQ:
  30. return "irq";
  31. case STACK_TYPE_NODAT:
  32. return "nodat";
  33. case STACK_TYPE_RESTART:
  34. return "restart";
  35. default:
  36. return "unknown";
  37. }
  38. }
  39. EXPORT_SYMBOL_GPL(stack_type_name);
  40. static inline bool in_stack(unsigned long sp, struct stack_info *info,
  41. enum stack_type type, unsigned long stack)
  42. {
  43. if (sp < stack || sp >= stack + THREAD_SIZE)
  44. return false;
  45. info->type = type;
  46. info->begin = stack;
  47. info->end = stack + THREAD_SIZE;
  48. return true;
  49. }
  50. static bool in_task_stack(unsigned long sp, struct task_struct *task,
  51. struct stack_info *info)
  52. {
  53. unsigned long stack = (unsigned long)task_stack_page(task);
  54. return in_stack(sp, info, STACK_TYPE_TASK, stack);
  55. }
  56. static bool in_irq_stack(unsigned long sp, struct stack_info *info)
  57. {
  58. unsigned long stack = get_lowcore()->async_stack - STACK_INIT_OFFSET;
  59. return in_stack(sp, info, STACK_TYPE_IRQ, stack);
  60. }
  61. static bool in_nodat_stack(unsigned long sp, struct stack_info *info)
  62. {
  63. unsigned long stack = get_lowcore()->nodat_stack - STACK_INIT_OFFSET;
  64. return in_stack(sp, info, STACK_TYPE_NODAT, stack);
  65. }
  66. static bool in_mcck_stack(unsigned long sp, struct stack_info *info)
  67. {
  68. unsigned long stack = get_lowcore()->mcck_stack - STACK_INIT_OFFSET;
  69. return in_stack(sp, info, STACK_TYPE_MCCK, stack);
  70. }
  71. static bool in_restart_stack(unsigned long sp, struct stack_info *info)
  72. {
  73. unsigned long stack = get_lowcore()->restart_stack - STACK_INIT_OFFSET;
  74. return in_stack(sp, info, STACK_TYPE_RESTART, stack);
  75. }
  76. int get_stack_info(unsigned long sp, struct task_struct *task,
  77. struct stack_info *info, unsigned long *visit_mask)
  78. {
  79. if (!sp)
  80. goto unknown;
  81. /* Sanity check: ABI requires SP to be aligned 8 bytes. */
  82. if (sp & 0x7)
  83. goto unknown;
  84. /* Check per-task stack */
  85. if (in_task_stack(sp, task, info))
  86. goto recursion_check;
  87. if (task != current)
  88. goto unknown;
  89. /* Check per-cpu stacks */
  90. if (!in_irq_stack(sp, info) &&
  91. !in_nodat_stack(sp, info) &&
  92. !in_restart_stack(sp, info) &&
  93. !in_mcck_stack(sp, info))
  94. goto unknown;
  95. recursion_check:
  96. /*
  97. * Make sure we don't iterate through any given stack more than once.
  98. * If it comes up a second time then there's something wrong going on:
  99. * just break out and report an unknown stack type.
  100. */
  101. if (*visit_mask & (1UL << info->type))
  102. goto unknown;
  103. *visit_mask |= 1UL << info->type;
  104. return 0;
  105. unknown:
  106. info->type = STACK_TYPE_UNKNOWN;
  107. return -EINVAL;
  108. }
  109. void show_stack(struct task_struct *task, unsigned long *stack,
  110. const char *loglvl)
  111. {
  112. struct unwind_state state;
  113. printk("%sCall Trace:\n", loglvl);
  114. unwind_for_each_frame(&state, task, NULL, (unsigned long) stack)
  115. printk(state.reliable ? "%s [<%016lx>] %pSR \n" :
  116. "%s([<%016lx>] %pSR)\n",
  117. loglvl, state.ip, (void *) state.ip);
  118. debug_show_held_locks(task ? : current);
  119. }
  120. static void show_last_breaking_event(struct pt_regs *regs)
  121. {
  122. printk("Last Breaking-Event-Address:\n");
  123. printk(" [<%016lx>] ", regs->last_break);
  124. if (user_mode(regs)) {
  125. print_vma_addr(KERN_CONT, regs->last_break);
  126. pr_cont("\n");
  127. } else {
  128. pr_cont("%pSR\n", (void *)regs->last_break);
  129. }
  130. }
  131. void show_registers(struct pt_regs *regs)
  132. {
  133. struct psw_bits *psw = &psw_bits(regs->psw);
  134. char *mode;
  135. mode = user_mode(regs) ? "User" : "Krnl";
  136. printk("%s PSW : %px %px", mode, (void *)regs->psw.mask, (void *)regs->psw.addr);
  137. if (!user_mode(regs))
  138. pr_cont(" (%pSR)", (void *)regs->psw.addr);
  139. pr_cont("\n");
  140. printk(" R:%x T:%x IO:%x EX:%x Key:%x M:%x W:%x "
  141. "P:%x AS:%x CC:%x PM:%x", psw->per, psw->dat, psw->io, psw->ext,
  142. psw->key, psw->mcheck, psw->wait, psw->pstate, psw->as, psw->cc, psw->pm);
  143. pr_cont(" RI:%x EA:%x\n", psw->ri, psw->eaba);
  144. printk("%s GPRS: %016lx %016lx %016lx %016lx\n", mode,
  145. regs->gprs[0], regs->gprs[1], regs->gprs[2], regs->gprs[3]);
  146. printk(" %016lx %016lx %016lx %016lx\n",
  147. regs->gprs[4], regs->gprs[5], regs->gprs[6], regs->gprs[7]);
  148. printk(" %016lx %016lx %016lx %016lx\n",
  149. regs->gprs[8], regs->gprs[9], regs->gprs[10], regs->gprs[11]);
  150. printk(" %016lx %016lx %016lx %016lx\n",
  151. regs->gprs[12], regs->gprs[13], regs->gprs[14], regs->gprs[15]);
  152. show_code(regs);
  153. }
  154. void show_regs(struct pt_regs *regs)
  155. {
  156. show_regs_print_info(KERN_DEFAULT);
  157. show_registers(regs);
  158. /* Show stack backtrace if pt_regs is from kernel mode */
  159. if (!user_mode(regs))
  160. show_stack(NULL, (unsigned long *) regs->gprs[15], KERN_DEFAULT);
  161. show_last_breaking_event(regs);
  162. }
  163. static DEFINE_SPINLOCK(die_lock);
  164. void __noreturn die(struct pt_regs *regs, const char *str)
  165. {
  166. static int die_counter;
  167. oops_enter();
  168. lgr_info_log();
  169. debug_stop_all();
  170. console_verbose();
  171. spin_lock_irq(&die_lock);
  172. bust_spinlocks(1);
  173. printk("%s: %04x ilc:%d [#%d] ", str, regs->int_code & 0xffff,
  174. regs->int_code >> 17, ++die_counter);
  175. #ifdef CONFIG_PREEMPT
  176. pr_cont("PREEMPT ");
  177. #elif defined(CONFIG_PREEMPT_RT)
  178. pr_cont("PREEMPT_RT ");
  179. #endif
  180. pr_cont("SMP ");
  181. if (debug_pagealloc_enabled())
  182. pr_cont("DEBUG_PAGEALLOC");
  183. pr_cont("\n");
  184. notify_die(DIE_OOPS, str, regs, 0, regs->int_code & 0xffff, SIGSEGV);
  185. print_modules();
  186. show_regs(regs);
  187. bust_spinlocks(0);
  188. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  189. spin_unlock_irq(&die_lock);
  190. if (in_interrupt())
  191. panic("Fatal exception in interrupt");
  192. if (panic_on_oops)
  193. panic("Fatal exception: panic_on_oops");
  194. oops_exit();
  195. make_task_dead(SIGSEGV);
  196. }