dumpstack_64.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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(" [<%p>] %s%pS\n", (void *) address,
  24. 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 + sizeof(long)) {
  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. printk(data);
  229. print_symbol(msg, symbol);
  230. printk("\n");
  231. }
  232. static void print_trace_warning(void *data, char *msg)
  233. {
  234. printk("%s%s\n", (char *)data, msg);
  235. }
  236. static int print_trace_stack(void *data, char *name)
  237. {
  238. printk("%s <%s> ", (char *)data, name);
  239. return 0;
  240. }
  241. /*
  242. * Print one address/symbol entries per line.
  243. */
  244. static void print_trace_address(void *data, unsigned long addr, int reliable)
  245. {
  246. touch_nmi_watchdog();
  247. printk(data);
  248. printk_address(addr, reliable);
  249. }
  250. static const struct stacktrace_ops print_trace_ops = {
  251. .warning = print_trace_warning,
  252. .warning_symbol = print_trace_warning_symbol,
  253. .stack = print_trace_stack,
  254. .address = print_trace_address,
  255. };
  256. static void
  257. show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
  258. unsigned long *stack, unsigned long bp, char *log_lvl)
  259. {
  260. printk("%sCall Trace:\n", log_lvl);
  261. dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl);
  262. }
  263. void show_trace(struct task_struct *task, struct pt_regs *regs,
  264. unsigned long *stack, unsigned long bp)
  265. {
  266. show_trace_log_lvl(task, regs, stack, bp, "");
  267. }
  268. static void
  269. show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
  270. unsigned long *sp, unsigned long bp, char *log_lvl)
  271. {
  272. unsigned long *stack;
  273. int i;
  274. const int cpu = smp_processor_id();
  275. unsigned long *irqstack_end =
  276. (unsigned long *) (cpu_pda(cpu)->irqstackptr);
  277. unsigned long *irqstack =
  278. (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
  279. /*
  280. * debugging aid: "show_stack(NULL, NULL);" prints the
  281. * back trace for this cpu.
  282. */
  283. if (sp == NULL) {
  284. if (task)
  285. sp = (unsigned long *)task->thread.sp;
  286. else
  287. sp = (unsigned long *)&sp;
  288. }
  289. stack = sp;
  290. for (i = 0; i < kstack_depth_to_print; i++) {
  291. if (stack >= irqstack && stack <= irqstack_end) {
  292. if (stack == irqstack_end) {
  293. stack = (unsigned long *) (irqstack_end[-1]);
  294. printk(" <EOI> ");
  295. }
  296. } else {
  297. if (((long) stack & (THREAD_SIZE-1)) == 0)
  298. break;
  299. }
  300. if (i && ((i % 4) == 0))
  301. printk("\n%s", log_lvl);
  302. printk(" %016lx", *stack++);
  303. touch_nmi_watchdog();
  304. }
  305. printk("\n");
  306. show_trace_log_lvl(task, regs, sp, bp, log_lvl);
  307. }
  308. void show_stack(struct task_struct *task, unsigned long *sp)
  309. {
  310. show_stack_log_lvl(task, NULL, sp, 0, "");
  311. }
  312. /*
  313. * The architecture-independent dump_stack generator
  314. */
  315. void dump_stack(void)
  316. {
  317. unsigned long bp = 0;
  318. unsigned long stack;
  319. #ifdef CONFIG_FRAME_POINTER
  320. if (!bp)
  321. asm("movq %%rbp, %0" : "=r" (bp) : );
  322. #endif
  323. printk("Pid: %d, comm: %.20s %s %s %.*s\n",
  324. current->pid, current->comm, print_tainted(),
  325. init_utsname()->release,
  326. (int)strcspn(init_utsname()->version, " "),
  327. init_utsname()->version);
  328. show_trace(NULL, NULL, &stack, bp);
  329. }
  330. EXPORT_SYMBOL(dump_stack);
  331. void show_registers(struct pt_regs *regs)
  332. {
  333. int i;
  334. unsigned long sp;
  335. const int cpu = smp_processor_id();
  336. struct task_struct *cur = cpu_pda(cpu)->pcurrent;
  337. sp = regs->sp;
  338. printk("CPU %d ", cpu);
  339. __show_regs(regs, 1);
  340. printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
  341. cur->comm, cur->pid, task_thread_info(cur), cur);
  342. /*
  343. * When in-kernel, we also print out the stack and code at the
  344. * time of the fault..
  345. */
  346. if (!user_mode(regs)) {
  347. unsigned int code_prologue = code_bytes * 43 / 64;
  348. unsigned int code_len = code_bytes;
  349. unsigned char c;
  350. u8 *ip;
  351. printk(KERN_EMERG "Stack:\n");
  352. show_stack_log_lvl(NULL, regs, (unsigned long *)sp,
  353. regs->bp, KERN_EMERG);
  354. printk(KERN_EMERG "Code: ");
  355. ip = (u8 *)regs->ip - code_prologue;
  356. if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
  357. /* try starting at RIP */
  358. ip = (u8 *)regs->ip;
  359. code_len = code_len - code_prologue + 1;
  360. }
  361. for (i = 0; i < code_len; i++, ip++) {
  362. if (ip < (u8 *)PAGE_OFFSET ||
  363. probe_kernel_address(ip, c)) {
  364. printk(" Bad RIP value.");
  365. break;
  366. }
  367. if (ip == (u8 *)regs->ip)
  368. printk("<%02x> ", c);
  369. else
  370. printk("%02x ", c);
  371. }
  372. }
  373. printk("\n");
  374. }
  375. int is_valid_bugaddr(unsigned long ip)
  376. {
  377. unsigned short ud2;
  378. if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
  379. return 0;
  380. return ud2 == 0x0b0f;
  381. }
  382. static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED;
  383. static int die_owner = -1;
  384. static unsigned int die_nest_count;
  385. unsigned __kprobes long oops_begin(void)
  386. {
  387. int cpu;
  388. unsigned long flags;
  389. oops_enter();
  390. /* racy, but better than risking deadlock. */
  391. raw_local_irq_save(flags);
  392. cpu = smp_processor_id();
  393. if (!__raw_spin_trylock(&die_lock)) {
  394. if (cpu == die_owner)
  395. /* nested oops. should stop eventually */;
  396. else
  397. __raw_spin_lock(&die_lock);
  398. }
  399. die_nest_count++;
  400. die_owner = cpu;
  401. console_verbose();
  402. bust_spinlocks(1);
  403. return flags;
  404. }
  405. void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
  406. {
  407. die_owner = -1;
  408. bust_spinlocks(0);
  409. die_nest_count--;
  410. if (!die_nest_count)
  411. /* Nest count reaches zero, release the lock. */
  412. __raw_spin_unlock(&die_lock);
  413. raw_local_irq_restore(flags);
  414. if (!regs) {
  415. oops_exit();
  416. return;
  417. }
  418. if (in_interrupt())
  419. panic("Fatal exception in interrupt");
  420. if (panic_on_oops)
  421. panic("Fatal exception");
  422. oops_exit();
  423. do_exit(signr);
  424. }
  425. int __kprobes __die(const char *str, struct pt_regs *regs, long err)
  426. {
  427. printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff, ++die_counter);
  428. #ifdef CONFIG_PREEMPT
  429. printk("PREEMPT ");
  430. #endif
  431. #ifdef CONFIG_SMP
  432. printk("SMP ");
  433. #endif
  434. #ifdef CONFIG_DEBUG_PAGEALLOC
  435. printk("DEBUG_PAGEALLOC");
  436. #endif
  437. printk("\n");
  438. if (notify_die(DIE_OOPS, str, regs, err,
  439. current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
  440. return 1;
  441. show_registers(regs);
  442. add_taint(TAINT_DIE);
  443. /* Executive summary in case the oops scrolled away */
  444. printk(KERN_ALERT "RIP ");
  445. printk_address(regs->ip, 1);
  446. printk(" RSP <%016lx>\n", regs->sp);
  447. if (kexec_should_crash(current))
  448. crash_kexec(regs);
  449. return 0;
  450. }
  451. void die(const char *str, struct pt_regs *regs, long err)
  452. {
  453. unsigned long flags = oops_begin();
  454. if (!user_mode(regs))
  455. report_bug(regs->ip, regs);
  456. if (__die(str, regs, err))
  457. regs = NULL;
  458. oops_end(flags, regs, SIGSEGV);
  459. }
  460. notrace __kprobes void
  461. die_nmi(char *str, struct pt_regs *regs, int do_panic)
  462. {
  463. unsigned long flags;
  464. if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) == NOTIFY_STOP)
  465. return;
  466. flags = oops_begin();
  467. /*
  468. * We are in trouble anyway, lets at least try
  469. * to get a message out.
  470. */
  471. printk(KERN_EMERG "%s", str);
  472. printk(" on CPU%d, ip %08lx, registers:\n",
  473. smp_processor_id(), regs->ip);
  474. show_registers(regs);
  475. if (kexec_should_crash(current))
  476. crash_kexec(regs);
  477. if (do_panic || panic_on_oops)
  478. panic("Non maskable interrupt");
  479. oops_end(flags, NULL, SIGBUS);
  480. nmi_exit();
  481. local_irq_enable();
  482. do_exit(SIGBUS);
  483. }
  484. static int __init oops_setup(char *s)
  485. {
  486. if (!s)
  487. return -EINVAL;
  488. if (!strcmp(s, "panic"))
  489. panic_on_oops = 1;
  490. return 0;
  491. }
  492. early_param("oops", oops_setup);
  493. static int __init kstack_setup(char *s)
  494. {
  495. if (!s)
  496. return -EINVAL;
  497. kstack_depth_to_print = simple_strtoul(s, NULL, 0);
  498. return 0;
  499. }
  500. early_param("kstack", kstack_setup);
  501. static int __init code_bytes_setup(char *s)
  502. {
  503. code_bytes = simple_strtoul(s, NULL, 0);
  504. if (code_bytes > 8192)
  505. code_bytes = 8192;
  506. return 1;
  507. }
  508. __setup("code_bytes=", code_bytes_setup);