dumpstack.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 <linux/kasan.h>
  21. #include <asm/cpu_entry_area.h>
  22. #include <asm/stacktrace.h>
  23. #include <asm/unwind.h>
  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(struct pt_regs *regs, const char *loglvl)
  80. {
  81. #define PROLOGUE_SIZE 42
  82. #define EPILOGUE_SIZE 21
  83. #define OPCODE_BUFSIZE (PROLOGUE_SIZE + 1 + EPILOGUE_SIZE)
  84. u8 opcodes[OPCODE_BUFSIZE];
  85. unsigned long prologue = regs->ip - PROLOGUE_SIZE;
  86. bool bad_ip;
  87. /*
  88. * Make sure userspace isn't trying to trick us into dumping kernel
  89. * memory by pointing the userspace instruction pointer at it.
  90. */
  91. bad_ip = user_mode(regs) &&
  92. __chk_range_not_ok(prologue, OPCODE_BUFSIZE, TASK_SIZE_MAX);
  93. if (bad_ip || probe_kernel_read(opcodes, (u8 *)prologue,
  94. OPCODE_BUFSIZE)) {
  95. printk("%sCode: Bad RIP value.\n", loglvl);
  96. } else {
  97. printk("%sCode: %" __stringify(PROLOGUE_SIZE) "ph <%02x> %"
  98. __stringify(EPILOGUE_SIZE) "ph\n", loglvl, opcodes,
  99. opcodes[PROLOGUE_SIZE], opcodes + PROLOGUE_SIZE + 1);
  100. }
  101. }
  102. void show_ip(struct pt_regs *regs, const char *loglvl)
  103. {
  104. #ifdef CONFIG_X86_32
  105. printk("%sEIP: %pS\n", loglvl, (void *)regs->ip);
  106. #else
  107. printk("%sRIP: %04x:%pS\n", loglvl, (int)regs->cs, (void *)regs->ip);
  108. #endif
  109. show_opcodes(regs, loglvl);
  110. }
  111. void show_iret_regs(struct pt_regs *regs)
  112. {
  113. show_ip(regs, KERN_DEFAULT);
  114. printk(KERN_DEFAULT "RSP: %04x:%016lx EFLAGS: %08lx", (int)regs->ss,
  115. regs->sp, regs->flags);
  116. }
  117. static void show_regs_if_on_stack(struct stack_info *info, struct pt_regs *regs,
  118. bool partial)
  119. {
  120. /*
  121. * These on_stack() checks aren't strictly necessary: the unwind code
  122. * has already validated the 'regs' pointer. The checks are done for
  123. * ordering reasons: if the registers are on the next stack, we don't
  124. * want to print them out yet. Otherwise they'll be shown as part of
  125. * the wrong stack. Later, when show_trace_log_lvl() switches to the
  126. * next stack, this function will be called again with the same regs so
  127. * they can be printed in the right context.
  128. */
  129. if (!partial && on_stack(info, regs, sizeof(*regs))) {
  130. __show_regs(regs, SHOW_REGS_SHORT);
  131. } else if (partial && on_stack(info, (void *)regs + IRET_FRAME_OFFSET,
  132. IRET_FRAME_SIZE)) {
  133. /*
  134. * When an interrupt or exception occurs in entry code, the
  135. * full pt_regs might not have been saved yet. In that case
  136. * just print the iret frame.
  137. */
  138. show_iret_regs(regs);
  139. }
  140. }
  141. void show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
  142. unsigned long *stack, char *log_lvl)
  143. {
  144. struct unwind_state state;
  145. struct stack_info stack_info = {0};
  146. unsigned long visit_mask = 0;
  147. int graph_idx = 0;
  148. bool partial = false;
  149. printk("%sCall Trace:\n", log_lvl);
  150. unwind_start(&state, task, regs, stack);
  151. stack = stack ? : get_stack_pointer(task, regs);
  152. regs = unwind_get_entry_regs(&state, &partial);
  153. /*
  154. * Iterate through the stacks, starting with the current stack pointer.
  155. * Each stack has a pointer to the next one.
  156. *
  157. * x86-64 can have several stacks:
  158. * - task stack
  159. * - interrupt stack
  160. * - HW exception stacks (double fault, nmi, debug, mce)
  161. * - entry stack
  162. *
  163. * x86-32 can have up to four stacks:
  164. * - task stack
  165. * - softirq stack
  166. * - hardirq stack
  167. * - entry stack
  168. */
  169. for ( ; stack; stack = PTR_ALIGN(stack_info.next_sp, sizeof(long))) {
  170. const char *stack_name;
  171. if (get_stack_info(stack, task, &stack_info, &visit_mask)) {
  172. /*
  173. * We weren't on a valid stack. It's possible that
  174. * we overflowed a valid stack into a guard page.
  175. * See if the next page up is valid so that we can
  176. * generate some kind of backtrace if this happens.
  177. */
  178. stack = (unsigned long *)PAGE_ALIGN((unsigned long)stack);
  179. if (get_stack_info(stack, task, &stack_info, &visit_mask))
  180. break;
  181. }
  182. stack_name = stack_type_name(stack_info.type);
  183. if (stack_name)
  184. printk("%s <%s>\n", log_lvl, stack_name);
  185. if (regs)
  186. show_regs_if_on_stack(&stack_info, regs, partial);
  187. /*
  188. * Scan the stack, printing any text addresses we find. At the
  189. * same time, follow proper stack frames with the unwinder.
  190. *
  191. * Addresses found during the scan which are not reported by
  192. * the unwinder are considered to be additional clues which are
  193. * sometimes useful for debugging and are prefixed with '?'.
  194. * This also serves as a failsafe option in case the unwinder
  195. * goes off in the weeds.
  196. */
  197. for (; stack < stack_info.end; stack++) {
  198. unsigned long real_addr;
  199. int reliable = 0;
  200. unsigned long addr = READ_ONCE_NOCHECK(*stack);
  201. unsigned long *ret_addr_p =
  202. unwind_get_return_address_ptr(&state);
  203. if (!__kernel_text_address(addr))
  204. continue;
  205. /*
  206. * Don't print regs->ip again if it was already printed
  207. * by show_regs_if_on_stack().
  208. */
  209. if (regs && stack == &regs->ip)
  210. goto next;
  211. if (stack == ret_addr_p)
  212. reliable = 1;
  213. /*
  214. * When function graph tracing is enabled for a
  215. * function, its return address on the stack is
  216. * replaced with the address of an ftrace handler
  217. * (return_to_handler). In that case, before printing
  218. * the "real" address, we want to print the handler
  219. * address as an "unreliable" hint that function graph
  220. * tracing was involved.
  221. */
  222. real_addr = ftrace_graph_ret_addr(task, &graph_idx,
  223. addr, stack);
  224. if (real_addr != addr)
  225. printk_stack_address(addr, 0, log_lvl);
  226. printk_stack_address(real_addr, reliable, log_lvl);
  227. if (!reliable)
  228. continue;
  229. next:
  230. /*
  231. * Get the next frame from the unwinder. No need to
  232. * check for an error: if anything goes wrong, the rest
  233. * of the addresses will just be printed as unreliable.
  234. */
  235. unwind_next_frame(&state);
  236. /* if the frame has entry regs, print them */
  237. regs = unwind_get_entry_regs(&state, &partial);
  238. if (regs)
  239. show_regs_if_on_stack(&stack_info, regs, partial);
  240. }
  241. if (stack_name)
  242. printk("%s </%s>\n", log_lvl, stack_name);
  243. }
  244. }
  245. void show_stack(struct task_struct *task, unsigned long *sp)
  246. {
  247. task = task ? : current;
  248. /*
  249. * Stack frames below this one aren't interesting. Don't show them
  250. * if we're printing for %current.
  251. */
  252. if (!sp && task == current)
  253. sp = get_stack_pointer(current, NULL);
  254. show_trace_log_lvl(task, NULL, sp, KERN_DEFAULT);
  255. }
  256. void show_stack_regs(struct pt_regs *regs)
  257. {
  258. show_trace_log_lvl(current, regs, NULL, KERN_DEFAULT);
  259. }
  260. static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
  261. static int die_owner = -1;
  262. static unsigned int die_nest_count;
  263. unsigned long oops_begin(void)
  264. {
  265. int cpu;
  266. unsigned long flags;
  267. oops_enter();
  268. /* racy, but better than risking deadlock. */
  269. raw_local_irq_save(flags);
  270. cpu = smp_processor_id();
  271. if (!arch_spin_trylock(&die_lock)) {
  272. if (cpu == die_owner)
  273. /* nested oops. should stop eventually */;
  274. else
  275. arch_spin_lock(&die_lock);
  276. }
  277. die_nest_count++;
  278. die_owner = cpu;
  279. console_verbose();
  280. bust_spinlocks(1);
  281. return flags;
  282. }
  283. NOKPROBE_SYMBOL(oops_begin);
  284. void __noreturn rewind_stack_do_exit(int signr);
  285. void oops_end(unsigned long flags, struct pt_regs *regs, int signr)
  286. {
  287. if (regs && kexec_should_crash(current))
  288. crash_kexec(regs);
  289. bust_spinlocks(0);
  290. die_owner = -1;
  291. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  292. die_nest_count--;
  293. if (!die_nest_count)
  294. /* Nest count reaches zero, release the lock. */
  295. arch_spin_unlock(&die_lock);
  296. raw_local_irq_restore(flags);
  297. oops_exit();
  298. /* Executive summary in case the oops scrolled away */
  299. __show_regs(&exec_summary_regs, SHOW_REGS_ALL);
  300. if (!signr)
  301. return;
  302. if (in_interrupt())
  303. panic("Fatal exception in interrupt");
  304. if (panic_on_oops)
  305. panic("Fatal exception");
  306. /*
  307. * We're not going to return, but we might be on an IST stack or
  308. * have very little stack space left. Rewind the stack and kill
  309. * the task.
  310. * Before we rewind the stack, we have to tell KASAN that we're going to
  311. * reuse the task stack and that existing poisons are invalid.
  312. */
  313. kasan_unpoison_task_stack(current);
  314. rewind_stack_do_exit(signr);
  315. }
  316. NOKPROBE_SYMBOL(oops_end);
  317. int __die(const char *str, struct pt_regs *regs, long err)
  318. {
  319. /* Save the regs of the first oops for the executive summary later. */
  320. if (!die_counter)
  321. exec_summary_regs = *regs;
  322. printk(KERN_DEFAULT
  323. "%s: %04lx [#%d]%s%s%s%s%s\n", str, err & 0xffff, ++die_counter,
  324. IS_ENABLED(CONFIG_PREEMPT) ? " PREEMPT" : "",
  325. IS_ENABLED(CONFIG_SMP) ? " SMP" : "",
  326. debug_pagealloc_enabled() ? " DEBUG_PAGEALLOC" : "",
  327. IS_ENABLED(CONFIG_KASAN) ? " KASAN" : "",
  328. IS_ENABLED(CONFIG_PAGE_TABLE_ISOLATION) ?
  329. (boot_cpu_has(X86_FEATURE_PTI) ? " PTI" : " NOPTI") : "");
  330. show_regs(regs);
  331. print_modules();
  332. if (notify_die(DIE_OOPS, str, regs, err,
  333. current->thread.trap_nr, SIGSEGV) == NOTIFY_STOP)
  334. return 1;
  335. return 0;
  336. }
  337. NOKPROBE_SYMBOL(__die);
  338. /*
  339. * This is gone through when something in the kernel has done something bad
  340. * and is about to be terminated:
  341. */
  342. void die(const char *str, struct pt_regs *regs, long err)
  343. {
  344. unsigned long flags = oops_begin();
  345. int sig = SIGSEGV;
  346. if (__die(str, regs, err))
  347. sig = 0;
  348. oops_end(flags, regs, sig);
  349. }
  350. void show_regs(struct pt_regs *regs)
  351. {
  352. show_regs_print_info(KERN_DEFAULT);
  353. __show_regs(regs, user_mode(regs) ? SHOW_REGS_USER : SHOW_REGS_ALL);
  354. /*
  355. * When in-kernel, we also print out the stack at the time of the fault..
  356. */
  357. if (!user_mode(regs))
  358. show_trace_log_lvl(current, regs, NULL, KERN_DEFAULT);
  359. }