dumpstack.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*
  2. * Copyright (C) 1991, 1992 Linus Torvalds
  3. * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
  4. */
  5. #include <linux/kallsyms.h>
  6. #include <linux/kprobes.h>
  7. #include <linux/uaccess.h>
  8. #include <linux/utsname.h>
  9. #include <linux/hardirq.h>
  10. #include <linux/kdebug.h>
  11. #include <linux/module.h>
  12. #include <linux/ptrace.h>
  13. #include <linux/sched/debug.h>
  14. #include <linux/sched/task_stack.h>
  15. #include <linux/ftrace.h>
  16. #include <linux/kexec.h>
  17. #include <linux/bug.h>
  18. #include <linux/nmi.h>
  19. #include <linux/sysfs.h>
  20. #include <asm/cpu_entry_area.h>
  21. #include <asm/stacktrace.h>
  22. #include <asm/unwind.h>
  23. #define OPCODE_BUFSIZE 64
  24. int panic_on_unrecovered_nmi;
  25. int panic_on_io_nmi;
  26. static int die_counter;
  27. static struct pt_regs exec_summary_regs;
  28. bool in_task_stack(unsigned long *stack, struct task_struct *task,
  29. struct stack_info *info)
  30. {
  31. unsigned long *begin = task_stack_page(task);
  32. unsigned long *end = task_stack_page(task) + THREAD_SIZE;
  33. if (stack < begin || stack >= end)
  34. return false;
  35. info->type = STACK_TYPE_TASK;
  36. info->begin = begin;
  37. info->end = end;
  38. info->next_sp = NULL;
  39. return true;
  40. }
  41. bool in_entry_stack(unsigned long *stack, struct stack_info *info)
  42. {
  43. struct entry_stack *ss = cpu_entry_stack(smp_processor_id());
  44. void *begin = ss;
  45. void *end = ss + 1;
  46. if ((void *)stack < begin || (void *)stack >= end)
  47. return false;
  48. info->type = STACK_TYPE_ENTRY;
  49. info->begin = begin;
  50. info->end = end;
  51. info->next_sp = NULL;
  52. return true;
  53. }
  54. static void printk_stack_address(unsigned long address, int reliable,
  55. char *log_lvl)
  56. {
  57. touch_nmi_watchdog();
  58. printk("%s %s%pB\n", log_lvl, reliable ? "" : "? ", (void *)address);
  59. }
  60. /*
  61. * There are a couple of reasons for the 2/3rd prologue, courtesy of Linus:
  62. *
  63. * In case where we don't have the exact kernel image (which, if we did, we can
  64. * simply disassemble and navigate to the RIP), the purpose of the bigger
  65. * prologue is to have more context and to be able to correlate the code from
  66. * the different toolchains better.
  67. *
  68. * In addition, it helps in recreating the register allocation of the failing
  69. * kernel and thus make sense of the register dump.
  70. *
  71. * What is more, the additional complication of a variable length insn arch like
  72. * x86 warrants having longer byte sequence before rIP so that the disassembler
  73. * can "sync" up properly and find instruction boundaries when decoding the
  74. * opcode bytes.
  75. *
  76. * Thus, the 2/3rds prologue and 64 byte OPCODE_BUFSIZE is just a random
  77. * guesstimate in attempt to achieve all of the above.
  78. */
  79. void show_opcodes(u8 *rip, const char *loglvl)
  80. {
  81. unsigned int code_prologue = OPCODE_BUFSIZE * 2 / 3;
  82. u8 opcodes[OPCODE_BUFSIZE];
  83. u8 *ip;
  84. int i;
  85. printk("%sCode: ", loglvl);
  86. ip = (u8 *)rip - code_prologue;
  87. if (probe_kernel_read(opcodes, ip, OPCODE_BUFSIZE)) {
  88. pr_cont("Bad RIP value.\n");
  89. return;
  90. }
  91. for (i = 0; i < OPCODE_BUFSIZE; i++, ip++) {
  92. if (ip == rip)
  93. pr_cont("<%02x> ", opcodes[i]);
  94. else
  95. pr_cont("%02x ", opcodes[i]);
  96. }
  97. pr_cont("\n");
  98. }
  99. void show_ip(struct pt_regs *regs, const char *loglvl)
  100. {
  101. #ifdef CONFIG_X86_32
  102. printk("%sEIP: %pS\n", loglvl, (void *)regs->ip);
  103. #else
  104. printk("%sRIP: %04x:%pS\n", loglvl, (int)regs->cs, (void *)regs->ip);
  105. #endif
  106. show_opcodes((u8 *)regs->ip, loglvl);
  107. }
  108. void show_iret_regs(struct pt_regs *regs)
  109. {
  110. show_ip(regs, KERN_DEFAULT);
  111. printk(KERN_DEFAULT "RSP: %04x:%016lx EFLAGS: %08lx", (int)regs->ss,
  112. regs->sp, regs->flags);
  113. }
  114. static void show_regs_if_on_stack(struct stack_info *info, struct pt_regs *regs,
  115. bool partial)
  116. {
  117. /*
  118. * These on_stack() checks aren't strictly necessary: the unwind code
  119. * has already validated the 'regs' pointer. The checks are done for
  120. * ordering reasons: if the registers are on the next stack, we don't
  121. * want to print them out yet. Otherwise they'll be shown as part of
  122. * the wrong stack. Later, when show_trace_log_lvl() switches to the
  123. * next stack, this function will be called again with the same regs so
  124. * they can be printed in the right context.
  125. */
  126. if (!partial && on_stack(info, regs, sizeof(*regs))) {
  127. __show_regs(regs, 0);
  128. } else if (partial && on_stack(info, (void *)regs + IRET_FRAME_OFFSET,
  129. IRET_FRAME_SIZE)) {
  130. /*
  131. * When an interrupt or exception occurs in entry code, the
  132. * full pt_regs might not have been saved yet. In that case
  133. * just print the iret frame.
  134. */
  135. show_iret_regs(regs);
  136. }
  137. }
  138. void show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
  139. unsigned long *stack, char *log_lvl)
  140. {
  141. struct unwind_state state;
  142. struct stack_info stack_info = {0};
  143. unsigned long visit_mask = 0;
  144. int graph_idx = 0;
  145. bool partial = false;
  146. printk("%sCall Trace:\n", log_lvl);
  147. unwind_start(&state, task, regs, stack);
  148. stack = stack ? : get_stack_pointer(task, regs);
  149. regs = unwind_get_entry_regs(&state, &partial);
  150. /*
  151. * Iterate through the stacks, starting with the current stack pointer.
  152. * Each stack has a pointer to the next one.
  153. *
  154. * x86-64 can have several stacks:
  155. * - task stack
  156. * - interrupt stack
  157. * - HW exception stacks (double fault, nmi, debug, mce)
  158. * - entry stack
  159. *
  160. * x86-32 can have up to four stacks:
  161. * - task stack
  162. * - softirq stack
  163. * - hardirq stack
  164. * - entry stack
  165. */
  166. for ( ; stack; stack = PTR_ALIGN(stack_info.next_sp, sizeof(long))) {
  167. const char *stack_name;
  168. if (get_stack_info(stack, task, &stack_info, &visit_mask)) {
  169. /*
  170. * We weren't on a valid stack. It's possible that
  171. * we overflowed a valid stack into a guard page.
  172. * See if the next page up is valid so that we can
  173. * generate some kind of backtrace if this happens.
  174. */
  175. stack = (unsigned long *)PAGE_ALIGN((unsigned long)stack);
  176. if (get_stack_info(stack, task, &stack_info, &visit_mask))
  177. break;
  178. }
  179. stack_name = stack_type_name(stack_info.type);
  180. if (stack_name)
  181. printk("%s <%s>\n", log_lvl, stack_name);
  182. if (regs)
  183. show_regs_if_on_stack(&stack_info, regs, partial);
  184. /*
  185. * Scan the stack, printing any text addresses we find. At the
  186. * same time, follow proper stack frames with the unwinder.
  187. *
  188. * Addresses found during the scan which are not reported by
  189. * the unwinder are considered to be additional clues which are
  190. * sometimes useful for debugging and are prefixed with '?'.
  191. * This also serves as a failsafe option in case the unwinder
  192. * goes off in the weeds.
  193. */
  194. for (; stack < stack_info.end; stack++) {
  195. unsigned long real_addr;
  196. int reliable = 0;
  197. unsigned long addr = READ_ONCE_NOCHECK(*stack);
  198. unsigned long *ret_addr_p =
  199. unwind_get_return_address_ptr(&state);
  200. if (!__kernel_text_address(addr))
  201. continue;
  202. /*
  203. * Don't print regs->ip again if it was already printed
  204. * by show_regs_if_on_stack().
  205. */
  206. if (regs && stack == &regs->ip)
  207. goto next;
  208. if (stack == ret_addr_p)
  209. reliable = 1;
  210. /*
  211. * When function graph tracing is enabled for a
  212. * function, its return address on the stack is
  213. * replaced with the address of an ftrace handler
  214. * (return_to_handler). In that case, before printing
  215. * the "real" address, we want to print the handler
  216. * address as an "unreliable" hint that function graph
  217. * tracing was involved.
  218. */
  219. real_addr = ftrace_graph_ret_addr(task, &graph_idx,
  220. addr, stack);
  221. if (real_addr != addr)
  222. printk_stack_address(addr, 0, log_lvl);
  223. printk_stack_address(real_addr, reliable, log_lvl);
  224. if (!reliable)
  225. continue;
  226. next:
  227. /*
  228. * Get the next frame from the unwinder. No need to
  229. * check for an error: if anything goes wrong, the rest
  230. * of the addresses will just be printed as unreliable.
  231. */
  232. unwind_next_frame(&state);
  233. /* if the frame has entry regs, print them */
  234. regs = unwind_get_entry_regs(&state, &partial);
  235. if (regs)
  236. show_regs_if_on_stack(&stack_info, regs, partial);
  237. }
  238. if (stack_name)
  239. printk("%s </%s>\n", log_lvl, stack_name);
  240. }
  241. }
  242. void show_stack(struct task_struct *task, unsigned long *sp)
  243. {
  244. task = task ? : current;
  245. /*
  246. * Stack frames below this one aren't interesting. Don't show them
  247. * if we're printing for %current.
  248. */
  249. if (!sp && task == current)
  250. sp = get_stack_pointer(current, NULL);
  251. show_trace_log_lvl(task, NULL, sp, KERN_DEFAULT);
  252. }
  253. void show_stack_regs(struct pt_regs *regs)
  254. {
  255. show_trace_log_lvl(current, regs, NULL, KERN_DEFAULT);
  256. }
  257. static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
  258. static int die_owner = -1;
  259. static unsigned int die_nest_count;
  260. unsigned long oops_begin(void)
  261. {
  262. int cpu;
  263. unsigned long flags;
  264. oops_enter();
  265. /* racy, but better than risking deadlock. */
  266. raw_local_irq_save(flags);
  267. cpu = smp_processor_id();
  268. if (!arch_spin_trylock(&die_lock)) {
  269. if (cpu == die_owner)
  270. /* nested oops. should stop eventually */;
  271. else
  272. arch_spin_lock(&die_lock);
  273. }
  274. die_nest_count++;
  275. die_owner = cpu;
  276. console_verbose();
  277. bust_spinlocks(1);
  278. return flags;
  279. }
  280. NOKPROBE_SYMBOL(oops_begin);
  281. void __noreturn rewind_stack_do_exit(int signr);
  282. void oops_end(unsigned long flags, struct pt_regs *regs, int signr)
  283. {
  284. if (regs && kexec_should_crash(current))
  285. crash_kexec(regs);
  286. bust_spinlocks(0);
  287. die_owner = -1;
  288. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  289. die_nest_count--;
  290. if (!die_nest_count)
  291. /* Nest count reaches zero, release the lock. */
  292. arch_spin_unlock(&die_lock);
  293. raw_local_irq_restore(flags);
  294. oops_exit();
  295. /* Executive summary in case the oops scrolled away */
  296. __show_regs(&exec_summary_regs, true);
  297. if (!signr)
  298. return;
  299. if (in_interrupt())
  300. panic("Fatal exception in interrupt");
  301. if (panic_on_oops)
  302. panic("Fatal exception");
  303. /*
  304. * We're not going to return, but we might be on an IST stack or
  305. * have very little stack space left. Rewind the stack and kill
  306. * the task.
  307. */
  308. rewind_stack_do_exit(signr);
  309. }
  310. NOKPROBE_SYMBOL(oops_end);
  311. int __die(const char *str, struct pt_regs *regs, long err)
  312. {
  313. /* Save the regs of the first oops for the executive summary later. */
  314. if (!die_counter)
  315. exec_summary_regs = *regs;
  316. printk(KERN_DEFAULT
  317. "%s: %04lx [#%d]%s%s%s%s%s\n", str, err & 0xffff, ++die_counter,
  318. IS_ENABLED(CONFIG_PREEMPT) ? " PREEMPT" : "",
  319. IS_ENABLED(CONFIG_SMP) ? " SMP" : "",
  320. debug_pagealloc_enabled() ? " DEBUG_PAGEALLOC" : "",
  321. IS_ENABLED(CONFIG_KASAN) ? " KASAN" : "",
  322. IS_ENABLED(CONFIG_PAGE_TABLE_ISOLATION) ?
  323. (boot_cpu_has(X86_FEATURE_PTI) ? " PTI" : " NOPTI") : "");
  324. show_regs(regs);
  325. print_modules();
  326. if (notify_die(DIE_OOPS, str, regs, err,
  327. current->thread.trap_nr, SIGSEGV) == NOTIFY_STOP)
  328. return 1;
  329. return 0;
  330. }
  331. NOKPROBE_SYMBOL(__die);
  332. /*
  333. * This is gone through when something in the kernel has done something bad
  334. * and is about to be terminated:
  335. */
  336. void die(const char *str, struct pt_regs *regs, long err)
  337. {
  338. unsigned long flags = oops_begin();
  339. int sig = SIGSEGV;
  340. if (__die(str, regs, err))
  341. sig = 0;
  342. oops_end(flags, regs, sig);
  343. }
  344. void show_regs(struct pt_regs *regs)
  345. {
  346. bool all = true;
  347. show_regs_print_info(KERN_DEFAULT);
  348. if (IS_ENABLED(CONFIG_X86_32))
  349. all = !user_mode(regs);
  350. __show_regs(regs, all);
  351. /*
  352. * When in-kernel, we also print out the stack at the time of the fault..
  353. */
  354. if (!user_mode(regs))
  355. show_trace_log_lvl(current, regs, NULL, KERN_DEFAULT);
  356. }