dumpstack_64.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 *irq_stack_end =
  103. (unsigned long *)per_cpu(irq_stack_ptr, cpu);
  104. unsigned used = 0;
  105. struct thread_info *tinfo;
  106. int graph = 0;
  107. if (!task)
  108. task = current;
  109. if (!stack) {
  110. unsigned long dummy;
  111. stack = &dummy;
  112. if (task && task != current)
  113. stack = (unsigned long *)task->thread.sp;
  114. }
  115. #ifdef CONFIG_FRAME_POINTER
  116. if (!bp) {
  117. if (task == current) {
  118. /* Grab bp right from our regs */
  119. get_bp(bp);
  120. } else {
  121. /* bp is the last reg pushed by switch_to */
  122. bp = *(unsigned long *) task->thread.sp;
  123. }
  124. }
  125. #endif
  126. /*
  127. * Print function call entries in all stacks, starting at the
  128. * current stack address. If the stacks consist of nested
  129. * exceptions
  130. */
  131. tinfo = task_thread_info(task);
  132. for (;;) {
  133. char *id;
  134. unsigned long *estack_end;
  135. estack_end = in_exception_stack(cpu, (unsigned long)stack,
  136. &used, &id);
  137. if (estack_end) {
  138. if (ops->stack(data, id) < 0)
  139. break;
  140. bp = print_context_stack(tinfo, stack, bp, ops,
  141. data, estack_end, &graph);
  142. ops->stack(data, "<EOE>");
  143. /*
  144. * We link to the next stack via the
  145. * second-to-last pointer (index -2 to end) in the
  146. * exception stack:
  147. */
  148. stack = (unsigned long *) estack_end[-2];
  149. continue;
  150. }
  151. if (irq_stack_end) {
  152. unsigned long *irq_stack;
  153. irq_stack = irq_stack_end -
  154. (IRQ_STACK_SIZE - 64) / sizeof(*irq_stack);
  155. if (stack >= irq_stack && stack < irq_stack_end) {
  156. if (ops->stack(data, "IRQ") < 0)
  157. break;
  158. bp = print_context_stack(tinfo, stack, bp,
  159. ops, data, irq_stack_end, &graph);
  160. /*
  161. * We link to the next stack (which would be
  162. * the process stack normally) the last
  163. * pointer (index -1 to end) in the IRQ stack:
  164. */
  165. stack = (unsigned long *) (irq_stack_end[-1]);
  166. irq_stack_end = NULL;
  167. ops->stack(data, "EOI");
  168. continue;
  169. }
  170. }
  171. break;
  172. }
  173. /*
  174. * This handles the process stack:
  175. */
  176. bp = print_context_stack(tinfo, stack, bp, ops, data, NULL, &graph);
  177. put_cpu();
  178. }
  179. EXPORT_SYMBOL(dump_trace);
  180. void
  181. show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
  182. unsigned long *sp, unsigned long bp, char *log_lvl)
  183. {
  184. unsigned long *stack;
  185. int i;
  186. const int cpu = smp_processor_id();
  187. unsigned long *irq_stack_end =
  188. (unsigned long *)(per_cpu(irq_stack_ptr, cpu));
  189. unsigned long *irq_stack =
  190. (unsigned long *)(per_cpu(irq_stack_ptr, cpu) - IRQ_STACK_SIZE);
  191. /*
  192. * debugging aid: "show_stack(NULL, NULL);" prints the
  193. * back trace for this cpu.
  194. */
  195. if (sp == NULL) {
  196. if (task)
  197. sp = (unsigned long *)task->thread.sp;
  198. else
  199. sp = (unsigned long *)&sp;
  200. }
  201. stack = sp;
  202. for (i = 0; i < kstack_depth_to_print; i++) {
  203. if (stack >= irq_stack && stack <= irq_stack_end) {
  204. if (stack == irq_stack_end) {
  205. stack = (unsigned long *) (irq_stack_end[-1]);
  206. printk(" <EOI> ");
  207. }
  208. } else {
  209. if (((long) stack & (THREAD_SIZE-1)) == 0)
  210. break;
  211. }
  212. if (i && ((i % STACKSLOTS_PER_LINE) == 0))
  213. printk("\n%s", log_lvl);
  214. printk(" %016lx", *stack++);
  215. touch_nmi_watchdog();
  216. }
  217. printk("\n");
  218. show_trace_log_lvl(task, regs, sp, bp, log_lvl);
  219. }
  220. void show_registers(struct pt_regs *regs)
  221. {
  222. int i;
  223. unsigned long sp;
  224. const int cpu = smp_processor_id();
  225. struct task_struct *cur = current;
  226. sp = regs->sp;
  227. printk("CPU %d ", cpu);
  228. __show_regs(regs, 1);
  229. printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
  230. cur->comm, cur->pid, task_thread_info(cur), cur);
  231. /*
  232. * When in-kernel, we also print out the stack and code at the
  233. * time of the fault..
  234. */
  235. if (!user_mode(regs)) {
  236. unsigned int code_prologue = code_bytes * 43 / 64;
  237. unsigned int code_len = code_bytes;
  238. unsigned char c;
  239. u8 *ip;
  240. printk(KERN_EMERG "Stack:\n");
  241. show_stack_log_lvl(NULL, regs, (unsigned long *)sp,
  242. regs->bp, KERN_EMERG);
  243. printk(KERN_EMERG "Code: ");
  244. ip = (u8 *)regs->ip - code_prologue;
  245. if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
  246. /* try starting at IP */
  247. ip = (u8 *)regs->ip;
  248. code_len = code_len - code_prologue + 1;
  249. }
  250. for (i = 0; i < code_len; i++, ip++) {
  251. if (ip < (u8 *)PAGE_OFFSET ||
  252. probe_kernel_address(ip, c)) {
  253. printk(" Bad RIP value.");
  254. break;
  255. }
  256. if (ip == (u8 *)regs->ip)
  257. printk("<%02x> ", c);
  258. else
  259. printk("%02x ", c);
  260. }
  261. }
  262. printk("\n");
  263. }
  264. int is_valid_bugaddr(unsigned long ip)
  265. {
  266. unsigned short ud2;
  267. if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
  268. return 0;
  269. return ud2 == 0x0b0f;
  270. }