dumpstack.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. /*
  24. * For dump_trace we have tree different stack to consider:
  25. * - the panic stack which is used if the kernel stack has overflown
  26. * - the asynchronous interrupt stack (cpu related)
  27. * - the synchronous kernel stack (process related)
  28. * The stack trace can start at any of the three stacks and can potentially
  29. * touch all of them. The order is: panic stack, async stack, sync stack.
  30. */
  31. static unsigned long
  32. __dump_trace(dump_trace_func_t func, void *data, unsigned long sp,
  33. unsigned long low, unsigned long high)
  34. {
  35. struct stack_frame *sf;
  36. struct pt_regs *regs;
  37. while (1) {
  38. if (sp < low || sp > high - sizeof(*sf))
  39. return sp;
  40. sf = (struct stack_frame *) sp;
  41. if (func(data, sf->gprs[8], 0))
  42. return sp;
  43. /* Follow the backchain. */
  44. while (1) {
  45. low = sp;
  46. sp = sf->back_chain;
  47. if (!sp)
  48. break;
  49. if (sp <= low || sp > high - sizeof(*sf))
  50. return sp;
  51. sf = (struct stack_frame *) sp;
  52. if (func(data, sf->gprs[8], 1))
  53. return sp;
  54. }
  55. /* Zero backchain detected, check for interrupt frame. */
  56. sp = (unsigned long) (sf + 1);
  57. if (sp <= low || sp > high - sizeof(*regs))
  58. return sp;
  59. regs = (struct pt_regs *) sp;
  60. if (!user_mode(regs)) {
  61. if (func(data, regs->psw.addr, 1))
  62. return sp;
  63. }
  64. low = sp;
  65. sp = regs->gprs[15];
  66. }
  67. }
  68. void dump_trace(dump_trace_func_t func, void *data, struct task_struct *task,
  69. unsigned long sp)
  70. {
  71. unsigned long frame_size;
  72. frame_size = STACK_FRAME_OVERHEAD + sizeof(struct pt_regs);
  73. #ifdef CONFIG_CHECK_STACK
  74. sp = __dump_trace(func, data, sp,
  75. S390_lowcore.panic_stack + frame_size - PAGE_SIZE,
  76. S390_lowcore.panic_stack + frame_size);
  77. #endif
  78. sp = __dump_trace(func, data, sp,
  79. S390_lowcore.async_stack + frame_size - ASYNC_SIZE,
  80. S390_lowcore.async_stack + frame_size);
  81. task = task ?: current;
  82. __dump_trace(func, data, sp,
  83. (unsigned long)task_stack_page(task),
  84. (unsigned long)task_stack_page(task) + THREAD_SIZE);
  85. }
  86. EXPORT_SYMBOL_GPL(dump_trace);
  87. static int show_address(void *data, unsigned long address, int reliable)
  88. {
  89. if (reliable)
  90. printk(" [<%016lx>] %pSR \n", address, (void *)address);
  91. else
  92. printk("([<%016lx>] %pSR)\n", address, (void *)address);
  93. return 0;
  94. }
  95. void show_stack(struct task_struct *task, unsigned long *stack)
  96. {
  97. unsigned long sp = (unsigned long) stack;
  98. if (!sp)
  99. sp = task ? task->thread.ksp : current_stack_pointer();
  100. printk("Call Trace:\n");
  101. dump_trace(show_address, NULL, task, sp);
  102. if (!task)
  103. task = current;
  104. debug_show_held_locks(task);
  105. }
  106. static void show_last_breaking_event(struct pt_regs *regs)
  107. {
  108. printk("Last Breaking-Event-Address:\n");
  109. printk(" [<%016lx>] %pSR\n", regs->args[0], (void *)regs->args[0]);
  110. }
  111. void show_registers(struct pt_regs *regs)
  112. {
  113. struct psw_bits *psw = &psw_bits(regs->psw);
  114. char *mode;
  115. mode = user_mode(regs) ? "User" : "Krnl";
  116. printk("%s PSW : %p %p", mode, (void *)regs->psw.mask, (void *)regs->psw.addr);
  117. if (!user_mode(regs))
  118. pr_cont(" (%pSR)", (void *)regs->psw.addr);
  119. pr_cont("\n");
  120. printk(" R:%x T:%x IO:%x EX:%x Key:%x M:%x W:%x "
  121. "P:%x AS:%x CC:%x PM:%x", psw->per, psw->dat, psw->io, psw->ext,
  122. psw->key, psw->mcheck, psw->wait, psw->pstate, psw->as, psw->cc, psw->pm);
  123. pr_cont(" RI:%x EA:%x\n", psw->ri, psw->eaba);
  124. printk("%s GPRS: %016lx %016lx %016lx %016lx\n", mode,
  125. regs->gprs[0], regs->gprs[1], regs->gprs[2], regs->gprs[3]);
  126. printk(" %016lx %016lx %016lx %016lx\n",
  127. regs->gprs[4], regs->gprs[5], regs->gprs[6], regs->gprs[7]);
  128. printk(" %016lx %016lx %016lx %016lx\n",
  129. regs->gprs[8], regs->gprs[9], regs->gprs[10], regs->gprs[11]);
  130. printk(" %016lx %016lx %016lx %016lx\n",
  131. regs->gprs[12], regs->gprs[13], regs->gprs[14], regs->gprs[15]);
  132. show_code(regs);
  133. }
  134. void show_regs(struct pt_regs *regs)
  135. {
  136. show_regs_print_info(KERN_DEFAULT);
  137. show_registers(regs);
  138. /* Show stack backtrace if pt_regs is from kernel mode */
  139. if (!user_mode(regs))
  140. show_stack(NULL, (unsigned long *) regs->gprs[15]);
  141. show_last_breaking_event(regs);
  142. }
  143. static DEFINE_SPINLOCK(die_lock);
  144. void die(struct pt_regs *regs, const char *str)
  145. {
  146. static int die_counter;
  147. oops_enter();
  148. lgr_info_log();
  149. debug_stop_all();
  150. console_verbose();
  151. spin_lock_irq(&die_lock);
  152. bust_spinlocks(1);
  153. printk("%s: %04x ilc:%d [#%d] ", str, regs->int_code & 0xffff,
  154. regs->int_code >> 17, ++die_counter);
  155. #ifdef CONFIG_PREEMPT
  156. pr_cont("PREEMPT ");
  157. #endif
  158. #ifdef CONFIG_SMP
  159. pr_cont("SMP ");
  160. #endif
  161. if (debug_pagealloc_enabled())
  162. pr_cont("DEBUG_PAGEALLOC");
  163. pr_cont("\n");
  164. notify_die(DIE_OOPS, str, regs, 0, regs->int_code & 0xffff, SIGSEGV);
  165. print_modules();
  166. show_regs(regs);
  167. bust_spinlocks(0);
  168. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  169. spin_unlock_irq(&die_lock);
  170. if (in_interrupt())
  171. panic("Fatal exception in interrupt");
  172. if (panic_on_oops)
  173. panic("Fatal exception: panic_on_oops");
  174. oops_exit();
  175. do_exit(SIGSEGV);
  176. }