dumpstack_64.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. #include "dumpstack.h"
  19. static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
  20. unsigned *usedp, char **idp)
  21. {
  22. static char ids[][8] = {
  23. [DEBUG_STACK - 1] = "#DB",
  24. [NMI_STACK - 1] = "NMI",
  25. [DOUBLEFAULT_STACK - 1] = "#DF",
  26. [STACKFAULT_STACK - 1] = "#SS",
  27. [MCE_STACK - 1] = "#MC",
  28. #if DEBUG_STKSZ > EXCEPTION_STKSZ
  29. [N_EXCEPTION_STACKS ...
  30. N_EXCEPTION_STACKS + DEBUG_STKSZ / EXCEPTION_STKSZ - 2] = "#DB[?]"
  31. #endif
  32. };
  33. unsigned k;
  34. /*
  35. * Iterate over all exception stacks, and figure out whether
  36. * 'stack' is in one of them:
  37. */
  38. for (k = 0; k < N_EXCEPTION_STACKS; k++) {
  39. unsigned long end = per_cpu(orig_ist, cpu).ist[k];
  40. /*
  41. * Is 'stack' above this exception frame's end?
  42. * If yes then skip to the next frame.
  43. */
  44. if (stack >= end)
  45. continue;
  46. /*
  47. * Is 'stack' above this exception frame's start address?
  48. * If yes then we found the right frame.
  49. */
  50. if (stack >= end - EXCEPTION_STKSZ) {
  51. /*
  52. * Make sure we only iterate through an exception
  53. * stack once. If it comes up for the second time
  54. * then there's something wrong going on - just
  55. * break out and return NULL:
  56. */
  57. if (*usedp & (1U << k))
  58. break;
  59. *usedp |= 1U << k;
  60. *idp = ids[k];
  61. return (unsigned long *)end;
  62. }
  63. /*
  64. * If this is a debug stack, and if it has a larger size than
  65. * the usual exception stacks, then 'stack' might still
  66. * be within the lower portion of the debug stack:
  67. */
  68. #if DEBUG_STKSZ > EXCEPTION_STKSZ
  69. if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
  70. unsigned j = N_EXCEPTION_STACKS - 1;
  71. /*
  72. * Black magic. A large debug stack is composed of
  73. * multiple exception stack entries, which we
  74. * iterate through now. Dont look:
  75. */
  76. do {
  77. ++j;
  78. end -= EXCEPTION_STKSZ;
  79. ids[j][4] = '1' + (j - N_EXCEPTION_STACKS);
  80. } while (stack < end - EXCEPTION_STKSZ);
  81. if (*usedp & (1U << j))
  82. break;
  83. *usedp |= 1U << j;
  84. *idp = ids[j];
  85. return (unsigned long *)end;
  86. }
  87. #endif
  88. }
  89. return NULL;
  90. }
  91. /*
  92. * x86-64 can have up to three kernel stacks:
  93. * process stack
  94. * interrupt stack
  95. * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
  96. */
  97. void dump_trace(struct task_struct *task, struct pt_regs *regs,
  98. unsigned long *stack, unsigned long bp,
  99. const struct stacktrace_ops *ops, void *data)
  100. {
  101. const unsigned cpu = get_cpu();
  102. unsigned long *irqstack_end = (unsigned long *)cpu_pda(cpu)->irqstackptr;
  103. unsigned used = 0;
  104. struct thread_info *tinfo;
  105. if (!task)
  106. task = current;
  107. if (!stack) {
  108. unsigned long dummy;
  109. stack = &dummy;
  110. if (task && task != current)
  111. stack = (unsigned long *)task->thread.sp;
  112. }
  113. #ifdef CONFIG_FRAME_POINTER
  114. if (!bp) {
  115. if (task == current) {
  116. /* Grab bp right from our regs */
  117. get_bp(bp);
  118. } else {
  119. /* bp is the last reg pushed by switch_to */
  120. bp = *(unsigned long *) task->thread.sp;
  121. }
  122. }
  123. #endif
  124. /*
  125. * Print function call entries in all stacks, starting at the
  126. * current stack address. If the stacks consist of nested
  127. * exceptions
  128. */
  129. tinfo = task_thread_info(task);
  130. for (;;) {
  131. char *id;
  132. unsigned long *estack_end;
  133. estack_end = in_exception_stack(cpu, (unsigned long)stack,
  134. &used, &id);
  135. if (estack_end) {
  136. if (ops->stack(data, id) < 0)
  137. break;
  138. bp = print_context_stack(tinfo, stack, bp, ops,
  139. data, estack_end);
  140. ops->stack(data, "<EOE>");
  141. /*
  142. * We link to the next stack via the
  143. * second-to-last pointer (index -2 to end) in the
  144. * exception stack:
  145. */
  146. stack = (unsigned long *) estack_end[-2];
  147. continue;
  148. }
  149. if (irqstack_end) {
  150. unsigned long *irqstack;
  151. irqstack = irqstack_end -
  152. (IRQSTACKSIZE - 64) / sizeof(*irqstack);
  153. if (stack >= irqstack && stack < irqstack_end) {
  154. if (ops->stack(data, "IRQ") < 0)
  155. break;
  156. bp = print_context_stack(tinfo, stack, bp,
  157. ops, data, irqstack_end);
  158. /*
  159. * We link to the next stack (which would be
  160. * the process stack normally) the last
  161. * pointer (index -1 to end) in the IRQ stack:
  162. */
  163. stack = (unsigned long *) (irqstack_end[-1]);
  164. irqstack_end = NULL;
  165. ops->stack(data, "EOI");
  166. continue;
  167. }
  168. }
  169. break;
  170. }
  171. /*
  172. * This handles the process stack:
  173. */
  174. bp = print_context_stack(tinfo, stack, bp, ops, data, NULL);
  175. put_cpu();
  176. }
  177. EXPORT_SYMBOL(dump_trace);
  178. void
  179. show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
  180. unsigned long *sp, unsigned long bp, char *log_lvl)
  181. {
  182. unsigned long *stack;
  183. int i;
  184. const int cpu = smp_processor_id();
  185. unsigned long *irqstack_end =
  186. (unsigned long *) (cpu_pda(cpu)->irqstackptr);
  187. unsigned long *irqstack =
  188. (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
  189. /*
  190. * debugging aid: "show_stack(NULL, NULL);" prints the
  191. * back trace for this cpu.
  192. */
  193. if (sp == NULL) {
  194. if (task)
  195. sp = (unsigned long *)task->thread.sp;
  196. else
  197. sp = (unsigned long *)&sp;
  198. }
  199. stack = sp;
  200. for (i = 0; i < kstack_depth_to_print; i++) {
  201. if (stack >= irqstack && stack <= irqstack_end) {
  202. if (stack == irqstack_end) {
  203. stack = (unsigned long *) (irqstack_end[-1]);
  204. printk(" <EOI> ");
  205. }
  206. } else {
  207. if (((long) stack & (THREAD_SIZE-1)) == 0)
  208. break;
  209. }
  210. if (i && ((i % STACKSLOTS_PER_LINE) == 0))
  211. printk("\n%s", log_lvl);
  212. printk(" %016lx", *stack++);
  213. touch_nmi_watchdog();
  214. }
  215. printk("\n");
  216. show_trace_log_lvl(task, regs, sp, bp, log_lvl);
  217. }
  218. void show_registers(struct pt_regs *regs)
  219. {
  220. int i;
  221. unsigned long sp;
  222. const int cpu = smp_processor_id();
  223. struct task_struct *cur = cpu_pda(cpu)->pcurrent;
  224. sp = regs->sp;
  225. printk("CPU %d ", cpu);
  226. __show_regs(regs, 1);
  227. printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
  228. cur->comm, cur->pid, task_thread_info(cur), cur);
  229. /*
  230. * When in-kernel, we also print out the stack and code at the
  231. * time of the fault..
  232. */
  233. if (!user_mode(regs)) {
  234. unsigned int code_prologue = code_bytes * 43 / 64;
  235. unsigned int code_len = code_bytes;
  236. unsigned char c;
  237. u8 *ip;
  238. printk(KERN_EMERG "Stack:\n");
  239. show_stack_log_lvl(NULL, regs, (unsigned long *)sp,
  240. regs->bp, KERN_EMERG);
  241. printk(KERN_EMERG "Code: ");
  242. ip = (u8 *)regs->ip - code_prologue;
  243. if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
  244. /* try starting at IP */
  245. ip = (u8 *)regs->ip;
  246. code_len = code_len - code_prologue + 1;
  247. }
  248. for (i = 0; i < code_len; i++, ip++) {
  249. if (ip < (u8 *)PAGE_OFFSET ||
  250. probe_kernel_address(ip, c)) {
  251. printk(" Bad RIP value.");
  252. break;
  253. }
  254. if (ip == (u8 *)regs->ip)
  255. printk("<%02x> ", c);
  256. else
  257. printk("%02x ", c);
  258. }
  259. }
  260. printk("\n");
  261. }
  262. int is_valid_bugaddr(unsigned long ip)
  263. {
  264. unsigned short ud2;
  265. if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
  266. return 0;
  267. return ud2 == 0x0b0f;
  268. }