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