dumpstack_64.c 7.3 KB

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