dumpstack_64.c 13 KB

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