dumpstack_64.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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/hardirq.h>
  9. #include <linux/kdebug.h>
  10. #include <linux/module.h>
  11. #include <linux/ptrace.h>
  12. #include <linux/kexec.h>
  13. #include <linux/sysfs.h>
  14. #include <linux/bug.h>
  15. #include <linux/nmi.h>
  16. #include <asm/stacktrace.h>
  17. #define N_EXCEPTION_STACKS_END \
  18. (N_EXCEPTION_STACKS + DEBUG_STKSZ/EXCEPTION_STKSZ - 2)
  19. static char x86_stack_ids[][8] = {
  20. [ DEBUG_STACK-1 ] = "#DB",
  21. [ NMI_STACK-1 ] = "NMI",
  22. [ DOUBLEFAULT_STACK-1 ] = "#DF",
  23. [ STACKFAULT_STACK-1 ] = "#SS",
  24. [ MCE_STACK-1 ] = "#MC",
  25. #if DEBUG_STKSZ > EXCEPTION_STKSZ
  26. [ N_EXCEPTION_STACKS ...
  27. N_EXCEPTION_STACKS_END ] = "#DB[?]"
  28. #endif
  29. };
  30. static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
  31. unsigned *usedp, char **idp)
  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 = x86_stack_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. x86_stack_ids[j][4] = '1' +
  80. (j - N_EXCEPTION_STACKS);
  81. } while (stack < end - EXCEPTION_STKSZ);
  82. if (*usedp & (1U << j))
  83. break;
  84. *usedp |= 1U << j;
  85. *idp = x86_stack_ids[j];
  86. return (unsigned long *)end;
  87. }
  88. #endif
  89. }
  90. return NULL;
  91. }
  92. static inline int
  93. in_irq_stack(unsigned long *stack, unsigned long *irq_stack,
  94. unsigned long *irq_stack_end)
  95. {
  96. return (stack >= irq_stack && stack < irq_stack_end);
  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. void dump_trace(struct task_struct *task, struct pt_regs *regs,
  105. unsigned long *stack, unsigned long bp,
  106. const struct stacktrace_ops *ops, void *data)
  107. {
  108. const unsigned cpu = get_cpu();
  109. unsigned long *irq_stack_end =
  110. (unsigned long *)per_cpu(irq_stack_ptr, cpu);
  111. unsigned used = 0;
  112. struct thread_info *tinfo;
  113. int graph = 0;
  114. unsigned long dummy;
  115. if (!task)
  116. task = current;
  117. if (!stack) {
  118. if (regs)
  119. stack = (unsigned long *)regs->sp;
  120. else if (task && task != current)
  121. stack = (unsigned long *)task->thread.sp;
  122. else
  123. stack = &dummy;
  124. }
  125. if (!bp)
  126. bp = stack_frame(task, regs);
  127. /*
  128. * Print function call entries in all stacks, starting at the
  129. * current stack address. If the stacks consist of nested
  130. * exceptions
  131. */
  132. tinfo = task_thread_info(task);
  133. for (;;) {
  134. char *id;
  135. unsigned long *estack_end;
  136. estack_end = in_exception_stack(cpu, (unsigned long)stack,
  137. &used, &id);
  138. if (estack_end) {
  139. if (ops->stack(data, id) < 0)
  140. break;
  141. bp = ops->walk_stack(tinfo, stack, bp, ops,
  142. data, estack_end, &graph);
  143. ops->stack(data, "<EOE>");
  144. /*
  145. * We link to the next stack via the
  146. * second-to-last pointer (index -2 to end) in the
  147. * exception stack:
  148. */
  149. stack = (unsigned long *) estack_end[-2];
  150. continue;
  151. }
  152. if (irq_stack_end) {
  153. unsigned long *irq_stack;
  154. irq_stack = irq_stack_end -
  155. (IRQ_STACK_SIZE - 64) / sizeof(*irq_stack);
  156. if (in_irq_stack(stack, irq_stack, irq_stack_end)) {
  157. if (ops->stack(data, "IRQ") < 0)
  158. break;
  159. bp = ops->walk_stack(tinfo, stack, bp,
  160. ops, data, irq_stack_end, &graph);
  161. /*
  162. * We link to the next stack (which would be
  163. * the process stack normally) the last
  164. * pointer (index -1 to end) in the IRQ stack:
  165. */
  166. stack = (unsigned long *) (irq_stack_end[-1]);
  167. irq_stack_end = NULL;
  168. ops->stack(data, "EOI");
  169. continue;
  170. }
  171. }
  172. break;
  173. }
  174. /*
  175. * This handles the process stack:
  176. */
  177. bp = ops->walk_stack(tinfo, stack, bp, ops, data, NULL, &graph);
  178. put_cpu();
  179. }
  180. EXPORT_SYMBOL(dump_trace);
  181. void
  182. show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
  183. unsigned long *sp, unsigned long bp, char *log_lvl)
  184. {
  185. unsigned long *irq_stack_end;
  186. unsigned long *irq_stack;
  187. unsigned long *stack;
  188. int cpu;
  189. int i;
  190. preempt_disable();
  191. cpu = smp_processor_id();
  192. irq_stack_end = (unsigned long *)(per_cpu(irq_stack_ptr, cpu));
  193. irq_stack = (unsigned long *)(per_cpu(irq_stack_ptr, cpu) - IRQ_STACK_SIZE);
  194. /*
  195. * Debugging aid: "show_stack(NULL, NULL);" prints the
  196. * back trace for this cpu:
  197. */
  198. if (sp == NULL) {
  199. if (task)
  200. sp = (unsigned long *)task->thread.sp;
  201. else
  202. sp = (unsigned long *)&sp;
  203. }
  204. stack = sp;
  205. for (i = 0; i < kstack_depth_to_print; i++) {
  206. if (stack >= irq_stack && stack <= irq_stack_end) {
  207. if (stack == irq_stack_end) {
  208. stack = (unsigned long *) (irq_stack_end[-1]);
  209. printk(KERN_CONT " <EOI> ");
  210. }
  211. } else {
  212. if (((long) stack & (THREAD_SIZE-1)) == 0)
  213. break;
  214. }
  215. if (i && ((i % STACKSLOTS_PER_LINE) == 0))
  216. printk(KERN_CONT "\n");
  217. printk(KERN_CONT " %016lx", *stack++);
  218. touch_nmi_watchdog();
  219. }
  220. preempt_enable();
  221. printk(KERN_CONT "\n");
  222. show_trace_log_lvl(task, regs, sp, bp, log_lvl);
  223. }
  224. void show_registers(struct pt_regs *regs)
  225. {
  226. int i;
  227. unsigned long sp;
  228. const int cpu = smp_processor_id();
  229. struct task_struct *cur = current;
  230. sp = regs->sp;
  231. printk("CPU %d ", cpu);
  232. print_modules();
  233. __show_regs(regs, 1);
  234. printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
  235. cur->comm, cur->pid, task_thread_info(cur), cur);
  236. /*
  237. * When in-kernel, we also print out the stack and code at the
  238. * time of the fault..
  239. */
  240. if (!user_mode(regs)) {
  241. unsigned int code_prologue = code_bytes * 43 / 64;
  242. unsigned int code_len = code_bytes;
  243. unsigned char c;
  244. u8 *ip;
  245. printk(KERN_EMERG "Stack:\n");
  246. show_stack_log_lvl(NULL, regs, (unsigned long *)sp,
  247. 0, KERN_EMERG);
  248. printk(KERN_EMERG "Code: ");
  249. ip = (u8 *)regs->ip - code_prologue;
  250. if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
  251. /* try starting at IP */
  252. ip = (u8 *)regs->ip;
  253. code_len = code_len - code_prologue + 1;
  254. }
  255. for (i = 0; i < code_len; i++, ip++) {
  256. if (ip < (u8 *)PAGE_OFFSET ||
  257. probe_kernel_address(ip, c)) {
  258. printk(" Bad RIP value.");
  259. break;
  260. }
  261. if (ip == (u8 *)regs->ip)
  262. printk("<%02x> ", c);
  263. else
  264. printk("%02x ", c);
  265. }
  266. }
  267. printk("\n");
  268. }
  269. int is_valid_bugaddr(unsigned long ip)
  270. {
  271. unsigned short ud2;
  272. if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
  273. return 0;
  274. return ud2 == 0x0b0f;
  275. }