dumpstack_64.c 13 KB

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