dumpstack_64.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. * We are returning from the irq stack and go to the previous one.
  100. * If the previous stack is also in the irq stack, then bp in the first
  101. * frame of the irq stack points to the previous, interrupted one.
  102. * Otherwise we have another level of indirection: We first save
  103. * the bp of the previous stack, then we switch the stack to the irq one
  104. * and save a new bp that links to the previous one.
  105. * (See save_args())
  106. */
  107. static inline unsigned long
  108. fixup_bp_irq_link(unsigned long bp, unsigned long *stack,
  109. unsigned long *irq_stack, unsigned long *irq_stack_end)
  110. {
  111. #ifdef CONFIG_FRAME_POINTER
  112. struct stack_frame *frame = (struct stack_frame *)bp;
  113. unsigned long next;
  114. if (!in_irq_stack(stack, irq_stack, irq_stack_end)) {
  115. if (!probe_kernel_address(&frame->next_frame, next))
  116. return next;
  117. else
  118. WARN_ONCE(1, "Perf: bad frame pointer = %p in "
  119. "callchain\n", &frame->next_frame);
  120. }
  121. #endif
  122. return bp;
  123. }
  124. /*
  125. * x86-64 can have up to three kernel stacks:
  126. * process stack
  127. * interrupt stack
  128. * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
  129. */
  130. void dump_trace(struct task_struct *task, struct pt_regs *regs,
  131. unsigned long *stack, unsigned long bp,
  132. const struct stacktrace_ops *ops, void *data)
  133. {
  134. const unsigned cpu = get_cpu();
  135. unsigned long *irq_stack_end =
  136. (unsigned long *)per_cpu(irq_stack_ptr, cpu);
  137. unsigned used = 0;
  138. struct thread_info *tinfo;
  139. int graph = 0;
  140. unsigned long dummy;
  141. if (!task)
  142. task = current;
  143. if (!stack) {
  144. stack = &dummy;
  145. if (task && task != current)
  146. stack = (unsigned long *)task->thread.sp;
  147. }
  148. if (!bp)
  149. bp = stack_frame(task, regs);
  150. /*
  151. * Print function call entries in all stacks, starting at the
  152. * current stack address. If the stacks consist of nested
  153. * exceptions
  154. */
  155. tinfo = task_thread_info(task);
  156. for (;;) {
  157. char *id;
  158. unsigned long *estack_end;
  159. estack_end = in_exception_stack(cpu, (unsigned long)stack,
  160. &used, &id);
  161. if (estack_end) {
  162. if (ops->stack(data, id) < 0)
  163. break;
  164. bp = ops->walk_stack(tinfo, stack, bp, ops,
  165. data, estack_end, &graph);
  166. ops->stack(data, "<EOE>");
  167. /*
  168. * We link to the next stack via the
  169. * second-to-last pointer (index -2 to end) in the
  170. * exception stack:
  171. */
  172. stack = (unsigned long *) estack_end[-2];
  173. continue;
  174. }
  175. if (irq_stack_end) {
  176. unsigned long *irq_stack;
  177. irq_stack = irq_stack_end -
  178. (IRQ_STACK_SIZE - 64) / sizeof(*irq_stack);
  179. if (in_irq_stack(stack, irq_stack, irq_stack_end)) {
  180. if (ops->stack(data, "IRQ") < 0)
  181. break;
  182. bp = ops->walk_stack(tinfo, stack, bp,
  183. ops, data, irq_stack_end, &graph);
  184. /*
  185. * We link to the next stack (which would be
  186. * the process stack normally) the last
  187. * pointer (index -1 to end) in the IRQ stack:
  188. */
  189. stack = (unsigned long *) (irq_stack_end[-1]);
  190. bp = fixup_bp_irq_link(bp, stack, irq_stack,
  191. irq_stack_end);
  192. irq_stack_end = NULL;
  193. ops->stack(data, "EOI");
  194. continue;
  195. }
  196. }
  197. break;
  198. }
  199. /*
  200. * This handles the process stack:
  201. */
  202. bp = ops->walk_stack(tinfo, stack, bp, ops, data, NULL, &graph);
  203. put_cpu();
  204. }
  205. EXPORT_SYMBOL(dump_trace);
  206. void
  207. show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
  208. unsigned long *sp, unsigned long bp, char *log_lvl)
  209. {
  210. unsigned long *irq_stack_end;
  211. unsigned long *irq_stack;
  212. unsigned long *stack;
  213. int cpu;
  214. int i;
  215. preempt_disable();
  216. cpu = smp_processor_id();
  217. irq_stack_end = (unsigned long *)(per_cpu(irq_stack_ptr, cpu));
  218. irq_stack = (unsigned long *)(per_cpu(irq_stack_ptr, cpu) - IRQ_STACK_SIZE);
  219. /*
  220. * Debugging aid: "show_stack(NULL, NULL);" prints the
  221. * back trace for this cpu:
  222. */
  223. if (sp == NULL) {
  224. if (task)
  225. sp = (unsigned long *)task->thread.sp;
  226. else
  227. sp = (unsigned long *)&sp;
  228. }
  229. stack = sp;
  230. for (i = 0; i < kstack_depth_to_print; i++) {
  231. if (stack >= irq_stack && stack <= irq_stack_end) {
  232. if (stack == irq_stack_end) {
  233. stack = (unsigned long *) (irq_stack_end[-1]);
  234. printk(KERN_CONT " <EOI> ");
  235. }
  236. } else {
  237. if (((long) stack & (THREAD_SIZE-1)) == 0)
  238. break;
  239. }
  240. if (i && ((i % STACKSLOTS_PER_LINE) == 0))
  241. printk(KERN_CONT "\n");
  242. printk(KERN_CONT " %016lx", *stack++);
  243. touch_nmi_watchdog();
  244. }
  245. preempt_enable();
  246. printk(KERN_CONT "\n");
  247. show_trace_log_lvl(task, regs, sp, bp, log_lvl);
  248. }
  249. void show_registers(struct pt_regs *regs)
  250. {
  251. int i;
  252. unsigned long sp;
  253. const int cpu = smp_processor_id();
  254. struct task_struct *cur = current;
  255. sp = regs->sp;
  256. printk("CPU %d ", cpu);
  257. print_modules();
  258. __show_regs(regs, 1);
  259. printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
  260. cur->comm, cur->pid, task_thread_info(cur), cur);
  261. /*
  262. * When in-kernel, we also print out the stack and code at the
  263. * time of the fault..
  264. */
  265. if (!user_mode(regs)) {
  266. unsigned int code_prologue = code_bytes * 43 / 64;
  267. unsigned int code_len = code_bytes;
  268. unsigned char c;
  269. u8 *ip;
  270. printk(KERN_EMERG "Stack:\n");
  271. show_stack_log_lvl(NULL, regs, (unsigned long *)sp,
  272. 0, KERN_EMERG);
  273. printk(KERN_EMERG "Code: ");
  274. ip = (u8 *)regs->ip - code_prologue;
  275. if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
  276. /* try starting at IP */
  277. ip = (u8 *)regs->ip;
  278. code_len = code_len - code_prologue + 1;
  279. }
  280. for (i = 0; i < code_len; i++, ip++) {
  281. if (ip < (u8 *)PAGE_OFFSET ||
  282. probe_kernel_address(ip, c)) {
  283. printk(" Bad RIP value.");
  284. break;
  285. }
  286. if (ip == (u8 *)regs->ip)
  287. printk("<%02x> ", c);
  288. else
  289. printk("%02x ", c);
  290. }
  291. }
  292. printk("\n");
  293. }
  294. int is_valid_bugaddr(unsigned long ip)
  295. {
  296. unsigned short ud2;
  297. if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
  298. return 0;
  299. return ud2 == 0x0b0f;
  300. }