dumpstack.c 6.2 KB

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