dumpstack_64.c 8.4 KB

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