dumpstack_64.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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/kexec.h>
  14. #include <linux/bug.h>
  15. #include <linux/nmi.h>
  16. #include <asm/stacktrace.h>
  17. int panic_on_unrecovered_nmi;
  18. int kstack_depth_to_print = 12;
  19. static unsigned int code_bytes = 64;
  20. static int die_counter;
  21. void printk_address(unsigned long address, int reliable)
  22. {
  23. printk(" [<%016lx>] %s%pS\n",
  24. address, reliable ? "" : "? ", (void *) address);
  25. }
  26. static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
  27. unsigned *usedp, char **idp)
  28. {
  29. static char ids[][8] = {
  30. [DEBUG_STACK - 1] = "#DB",
  31. [NMI_STACK - 1] = "NMI",
  32. [DOUBLEFAULT_STACK - 1] = "#DF",
  33. [STACKFAULT_STACK - 1] = "#SS",
  34. [MCE_STACK - 1] = "#MC",
  35. #if DEBUG_STKSZ > EXCEPTION_STKSZ
  36. [N_EXCEPTION_STACKS ...
  37. N_EXCEPTION_STACKS + DEBUG_STKSZ / EXCEPTION_STKSZ - 2] = "#DB[?]"
  38. #endif
  39. };
  40. unsigned k;
  41. /*
  42. * Iterate over all exception stacks, and figure out whether
  43. * 'stack' is in one of them:
  44. */
  45. for (k = 0; k < N_EXCEPTION_STACKS; k++) {
  46. unsigned long end = per_cpu(orig_ist, cpu).ist[k];
  47. /*
  48. * Is 'stack' above this exception frame's end?
  49. * If yes then skip to the next frame.
  50. */
  51. if (stack >= end)
  52. continue;
  53. /*
  54. * Is 'stack' above this exception frame's start address?
  55. * If yes then we found the right frame.
  56. */
  57. if (stack >= end - EXCEPTION_STKSZ) {
  58. /*
  59. * Make sure we only iterate through an exception
  60. * stack once. If it comes up for the second time
  61. * then there's something wrong going on - just
  62. * break out and return NULL:
  63. */
  64. if (*usedp & (1U << k))
  65. break;
  66. *usedp |= 1U << k;
  67. *idp = ids[k];
  68. return (unsigned long *)end;
  69. }
  70. /*
  71. * If this is a debug stack, and if it has a larger size than
  72. * the usual exception stacks, then 'stack' might still
  73. * be within the lower portion of the debug stack:
  74. */
  75. #if DEBUG_STKSZ > EXCEPTION_STKSZ
  76. if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
  77. unsigned j = N_EXCEPTION_STACKS - 1;
  78. /*
  79. * Black magic. A large debug stack is composed of
  80. * multiple exception stack entries, which we
  81. * iterate through now. Dont look:
  82. */
  83. do {
  84. ++j;
  85. end -= EXCEPTION_STKSZ;
  86. ids[j][4] = '1' + (j - N_EXCEPTION_STACKS);
  87. } while (stack < end - EXCEPTION_STKSZ);
  88. if (*usedp & (1U << j))
  89. break;
  90. *usedp |= 1U << j;
  91. *idp = ids[j];
  92. return (unsigned long *)end;
  93. }
  94. #endif
  95. }
  96. return NULL;
  97. }
  98. /*
  99. * x86-64 can have up to three kernel stacks:
  100. * process stack
  101. * interrupt stack
  102. * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
  103. */
  104. static inline int valid_stack_ptr(struct thread_info *tinfo,
  105. void *p, unsigned int size, void *end)
  106. {
  107. void *t = tinfo;
  108. if (end) {
  109. if (p < end && p >= (end-THREAD_SIZE))
  110. return 1;
  111. else
  112. return 0;
  113. }
  114. return p > t && p < t + THREAD_SIZE - size;
  115. }
  116. /* The form of the top of the frame on the stack */
  117. struct stack_frame {
  118. struct stack_frame *next_frame;
  119. unsigned long return_address;
  120. };
  121. static inline unsigned long
  122. print_context_stack(struct thread_info *tinfo,
  123. unsigned long *stack, unsigned long bp,
  124. const struct stacktrace_ops *ops, void *data,
  125. unsigned long *end)
  126. {
  127. struct stack_frame *frame = (struct stack_frame *)bp;
  128. while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) {
  129. unsigned long addr;
  130. addr = *stack;
  131. if (__kernel_text_address(addr)) {
  132. if ((unsigned long) stack == bp + 8) {
  133. ops->address(data, addr, 1);
  134. frame = frame->next_frame;
  135. bp = (unsigned long) frame;
  136. } else {
  137. ops->address(data, addr, bp == 0);
  138. }
  139. }
  140. stack++;
  141. }
  142. return bp;
  143. }
  144. void dump_trace(struct task_struct *task, struct pt_regs *regs,
  145. unsigned long *stack, unsigned long bp,
  146. const struct stacktrace_ops *ops, void *data)
  147. {
  148. const unsigned cpu = get_cpu();
  149. unsigned long *irqstack_end = (unsigned long *)cpu_pda(cpu)->irqstackptr;
  150. unsigned used = 0;
  151. struct thread_info *tinfo;
  152. if (!task)
  153. task = current;
  154. if (!stack) {
  155. unsigned long dummy;
  156. stack = &dummy;
  157. if (task && task != current)
  158. stack = (unsigned long *)task->thread.sp;
  159. }
  160. #ifdef CONFIG_FRAME_POINTER
  161. if (!bp) {
  162. if (task == current) {
  163. /* Grab bp right from our regs */
  164. asm("movq %%rbp, %0" : "=r" (bp) : );
  165. } else {
  166. /* bp is the last reg pushed by switch_to */
  167. bp = *(unsigned long *) task->thread.sp;
  168. }
  169. }
  170. #endif
  171. /*
  172. * Print function call entries in all stacks, starting at the
  173. * current stack address. If the stacks consist of nested
  174. * exceptions
  175. */
  176. tinfo = task_thread_info(task);
  177. for (;;) {
  178. char *id;
  179. unsigned long *estack_end;
  180. estack_end = in_exception_stack(cpu, (unsigned long)stack,
  181. &used, &id);
  182. if (estack_end) {
  183. if (ops->stack(data, id) < 0)
  184. break;
  185. bp = print_context_stack(tinfo, stack, bp, ops,
  186. data, estack_end);
  187. ops->stack(data, "<EOE>");
  188. /*
  189. * We link to the next stack via the
  190. * second-to-last pointer (index -2 to end) in the
  191. * exception stack:
  192. */
  193. stack = (unsigned long *) estack_end[-2];
  194. continue;
  195. }
  196. if (irqstack_end) {
  197. unsigned long *irqstack;
  198. irqstack = irqstack_end -
  199. (IRQSTACKSIZE - 64) / sizeof(*irqstack);
  200. if (stack >= irqstack && stack < irqstack_end) {
  201. if (ops->stack(data, "IRQ") < 0)
  202. break;
  203. bp = print_context_stack(tinfo, stack, bp,
  204. ops, data, irqstack_end);
  205. /*
  206. * We link to the next stack (which would be
  207. * the process stack normally) the last
  208. * pointer (index -1 to end) in the IRQ stack:
  209. */
  210. stack = (unsigned long *) (irqstack_end[-1]);
  211. irqstack_end = NULL;
  212. ops->stack(data, "EOI");
  213. continue;
  214. }
  215. }
  216. break;
  217. }
  218. /*
  219. * This handles the process stack:
  220. */
  221. bp = print_context_stack(tinfo, stack, bp, ops, data, NULL);
  222. put_cpu();
  223. }
  224. EXPORT_SYMBOL(dump_trace);
  225. static void
  226. print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
  227. {
  228. print_symbol(msg, symbol);
  229. printk("\n");
  230. }
  231. static void print_trace_warning(void *data, char *msg)
  232. {
  233. printk("%s\n", msg);
  234. }
  235. static int print_trace_stack(void *data, char *name)
  236. {
  237. printk(" <%s> ", name);
  238. return 0;
  239. }
  240. static void print_trace_address(void *data, unsigned long addr, int reliable)
  241. {
  242. touch_nmi_watchdog();
  243. printk_address(addr, reliable);
  244. }
  245. static const struct stacktrace_ops print_trace_ops = {
  246. .warning = print_trace_warning,
  247. .warning_symbol = print_trace_warning_symbol,
  248. .stack = print_trace_stack,
  249. .address = print_trace_address,
  250. };
  251. static void
  252. show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
  253. unsigned long *stack, unsigned long bp, char *log_lvl)
  254. {
  255. printk("Call Trace:\n");
  256. dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl);
  257. }
  258. void show_trace(struct task_struct *task, struct pt_regs *regs,
  259. unsigned long *stack, unsigned long bp)
  260. {
  261. show_trace_log_lvl(task, regs, stack, bp, "");
  262. }
  263. static void
  264. show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
  265. unsigned long *sp, unsigned long bp, char *log_lvl)
  266. {
  267. unsigned long *stack;
  268. int i;
  269. const int cpu = smp_processor_id();
  270. unsigned long *irqstack_end =
  271. (unsigned long *) (cpu_pda(cpu)->irqstackptr);
  272. unsigned long *irqstack =
  273. (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
  274. /*
  275. * debugging aid: "show_stack(NULL, NULL);" prints the
  276. * back trace for this cpu.
  277. */
  278. if (sp == NULL) {
  279. if (task)
  280. sp = (unsigned long *)task->thread.sp;
  281. else
  282. sp = (unsigned long *)&sp;
  283. }
  284. stack = sp;
  285. for (i = 0; i < kstack_depth_to_print; i++) {
  286. if (stack >= irqstack && stack <= irqstack_end) {
  287. if (stack == irqstack_end) {
  288. stack = (unsigned long *) (irqstack_end[-1]);
  289. printk(" <EOI> ");
  290. }
  291. } else {
  292. if (((long) stack & (THREAD_SIZE-1)) == 0)
  293. break;
  294. }
  295. if (i && ((i % 4) == 0))
  296. printk("\n");
  297. printk(" %016lx", *stack++);
  298. touch_nmi_watchdog();
  299. }
  300. printk("\n");
  301. show_trace_log_lvl(task, regs, sp, bp, log_lvl);
  302. }
  303. void show_stack(struct task_struct *task, unsigned long *sp)
  304. {
  305. show_stack_log_lvl(task, NULL, sp, 0, "");
  306. }
  307. /*
  308. * The architecture-independent dump_stack generator
  309. */
  310. void dump_stack(void)
  311. {
  312. unsigned long bp = 0;
  313. unsigned long stack;
  314. #ifdef CONFIG_FRAME_POINTER
  315. if (!bp)
  316. asm("movq %%rbp, %0" : "=r" (bp) : );
  317. #endif
  318. printk("Pid: %d, comm: %.20s %s %s %.*s\n",
  319. current->pid, current->comm, print_tainted(),
  320. init_utsname()->release,
  321. (int)strcspn(init_utsname()->version, " "),
  322. init_utsname()->version);
  323. show_trace(NULL, NULL, &stack, bp);
  324. }
  325. EXPORT_SYMBOL(dump_stack);
  326. void show_registers(struct pt_regs *regs)
  327. {
  328. int i;
  329. unsigned long sp;
  330. const int cpu = smp_processor_id();
  331. struct task_struct *cur = cpu_pda(cpu)->pcurrent;
  332. sp = regs->sp;
  333. printk("CPU %d ", cpu);
  334. __show_regs(regs, 1);
  335. printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
  336. cur->comm, cur->pid, task_thread_info(cur), cur);
  337. /*
  338. * When in-kernel, we also print out the stack and code at the
  339. * time of the fault..
  340. */
  341. if (!user_mode(regs)) {
  342. unsigned int code_prologue = code_bytes * 43 / 64;
  343. unsigned int code_len = code_bytes;
  344. unsigned char c;
  345. u8 *ip;
  346. printk("Stack: ");
  347. show_stack_log_lvl(NULL, regs, (unsigned long *)sp,
  348. regs->bp, "");
  349. printk(KERN_EMERG "Code: ");
  350. ip = (u8 *)regs->ip - code_prologue;
  351. if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
  352. /* try starting at RIP */
  353. ip = (u8 *)regs->ip;
  354. code_len = code_len - code_prologue + 1;
  355. }
  356. for (i = 0; i < code_len; i++, ip++) {
  357. if (ip < (u8 *)PAGE_OFFSET ||
  358. probe_kernel_address(ip, c)) {
  359. printk(" Bad RIP value.");
  360. break;
  361. }
  362. if (ip == (u8 *)regs->ip)
  363. printk("<%02x> ", c);
  364. else
  365. printk("%02x ", c);
  366. }
  367. }
  368. printk("\n");
  369. }
  370. int is_valid_bugaddr(unsigned long ip)
  371. {
  372. unsigned short ud2;
  373. if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
  374. return 0;
  375. return ud2 == 0x0b0f;
  376. }
  377. static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED;
  378. static int die_owner = -1;
  379. static unsigned int die_nest_count;
  380. unsigned __kprobes long oops_begin(void)
  381. {
  382. int cpu;
  383. unsigned long flags;
  384. oops_enter();
  385. /* racy, but better than risking deadlock. */
  386. raw_local_irq_save(flags);
  387. cpu = smp_processor_id();
  388. if (!__raw_spin_trylock(&die_lock)) {
  389. if (cpu == die_owner)
  390. /* nested oops. should stop eventually */;
  391. else
  392. __raw_spin_lock(&die_lock);
  393. }
  394. die_nest_count++;
  395. die_owner = cpu;
  396. console_verbose();
  397. bust_spinlocks(1);
  398. return flags;
  399. }
  400. void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
  401. {
  402. die_owner = -1;
  403. bust_spinlocks(0);
  404. die_nest_count--;
  405. if (!die_nest_count)
  406. /* Nest count reaches zero, release the lock. */
  407. __raw_spin_unlock(&die_lock);
  408. raw_local_irq_restore(flags);
  409. if (!regs) {
  410. oops_exit();
  411. return;
  412. }
  413. if (in_interrupt())
  414. panic("Fatal exception in interrupt");
  415. if (panic_on_oops)
  416. panic("Fatal exception");
  417. oops_exit();
  418. do_exit(signr);
  419. }
  420. int __kprobes __die(const char *str, struct pt_regs *regs, long err)
  421. {
  422. printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff, ++die_counter);
  423. #ifdef CONFIG_PREEMPT
  424. printk("PREEMPT ");
  425. #endif
  426. #ifdef CONFIG_SMP
  427. printk("SMP ");
  428. #endif
  429. #ifdef CONFIG_DEBUG_PAGEALLOC
  430. printk("DEBUG_PAGEALLOC");
  431. #endif
  432. printk("\n");
  433. if (notify_die(DIE_OOPS, str, regs, err,
  434. current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
  435. return 1;
  436. show_registers(regs);
  437. add_taint(TAINT_DIE);
  438. /* Executive summary in case the oops scrolled away */
  439. printk(KERN_ALERT "RIP ");
  440. printk_address(regs->ip, 1);
  441. printk(" RSP <%016lx>\n", regs->sp);
  442. if (kexec_should_crash(current))
  443. crash_kexec(regs);
  444. return 0;
  445. }
  446. void die(const char *str, struct pt_regs *regs, long err)
  447. {
  448. unsigned long flags = oops_begin();
  449. if (!user_mode(regs))
  450. report_bug(regs->ip, regs);
  451. if (__die(str, regs, err))
  452. regs = NULL;
  453. oops_end(flags, regs, SIGSEGV);
  454. }
  455. notrace __kprobes void
  456. die_nmi(char *str, struct pt_regs *regs, int do_panic)
  457. {
  458. unsigned long flags;
  459. if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) == NOTIFY_STOP)
  460. return;
  461. flags = oops_begin();
  462. /*
  463. * We are in trouble anyway, lets at least try
  464. * to get a message out.
  465. */
  466. printk(KERN_EMERG "%s", str);
  467. printk(" on CPU%d, ip %08lx, registers:\n",
  468. smp_processor_id(), regs->ip);
  469. show_registers(regs);
  470. if (kexec_should_crash(current))
  471. crash_kexec(regs);
  472. if (do_panic || panic_on_oops)
  473. panic("Non maskable interrupt");
  474. oops_end(flags, NULL, SIGBUS);
  475. nmi_exit();
  476. local_irq_enable();
  477. do_exit(SIGBUS);
  478. }
  479. static int __init oops_setup(char *s)
  480. {
  481. if (!s)
  482. return -EINVAL;
  483. if (!strcmp(s, "panic"))
  484. panic_on_oops = 1;
  485. return 0;
  486. }
  487. early_param("oops", oops_setup);
  488. static int __init kstack_setup(char *s)
  489. {
  490. if (!s)
  491. return -EINVAL;
  492. kstack_depth_to_print = simple_strtoul(s, NULL, 0);
  493. return 0;
  494. }
  495. early_param("kstack", kstack_setup);
  496. static int __init code_bytes_setup(char *s)
  497. {
  498. code_bytes = simple_strtoul(s, NULL, 0);
  499. if (code_bytes > 8192)
  500. code_bytes = 8192;
  501. return 1;
  502. }
  503. __setup("code_bytes=", code_bytes_setup);