dumpstack_64.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. if (!task)
  141. task = current;
  142. if (!stack) {
  143. unsigned long dummy;
  144. stack = &dummy;
  145. if (task && task != current)
  146. stack = (unsigned long *)task->thread.sp;
  147. }
  148. #ifdef CONFIG_FRAME_POINTER
  149. if (!bp) {
  150. if (task == current) {
  151. /* Grab bp right from our regs */
  152. get_bp(bp);
  153. } else {
  154. /* bp is the last reg pushed by switch_to */
  155. bp = *(unsigned long *) task->thread.sp;
  156. }
  157. }
  158. #endif
  159. /*
  160. * Print function call entries in all stacks, starting at the
  161. * current stack address. If the stacks consist of nested
  162. * exceptions
  163. */
  164. tinfo = task_thread_info(task);
  165. for (;;) {
  166. char *id;
  167. unsigned long *estack_end;
  168. estack_end = in_exception_stack(cpu, (unsigned long)stack,
  169. &used, &id);
  170. if (estack_end) {
  171. if (ops->stack(data, id) < 0)
  172. break;
  173. bp = ops->walk_stack(tinfo, stack, bp, ops,
  174. data, estack_end, &graph);
  175. ops->stack(data, "<EOE>");
  176. /*
  177. * We link to the next stack via the
  178. * second-to-last pointer (index -2 to end) in the
  179. * exception stack:
  180. */
  181. stack = (unsigned long *) estack_end[-2];
  182. continue;
  183. }
  184. if (irq_stack_end) {
  185. unsigned long *irq_stack;
  186. irq_stack = irq_stack_end -
  187. (IRQ_STACK_SIZE - 64) / sizeof(*irq_stack);
  188. if (in_irq_stack(stack, irq_stack, irq_stack_end)) {
  189. if (ops->stack(data, "IRQ") < 0)
  190. break;
  191. bp = ops->walk_stack(tinfo, stack, bp,
  192. ops, data, irq_stack_end, &graph);
  193. /*
  194. * We link to the next stack (which would be
  195. * the process stack normally) the last
  196. * pointer (index -1 to end) in the IRQ stack:
  197. */
  198. stack = (unsigned long *) (irq_stack_end[-1]);
  199. bp = fixup_bp_irq_link(bp, stack, irq_stack,
  200. irq_stack_end);
  201. irq_stack_end = NULL;
  202. ops->stack(data, "EOI");
  203. continue;
  204. }
  205. }
  206. break;
  207. }
  208. /*
  209. * This handles the process stack:
  210. */
  211. bp = ops->walk_stack(tinfo, stack, bp, ops, data, NULL, &graph);
  212. put_cpu();
  213. }
  214. EXPORT_SYMBOL(dump_trace);
  215. void
  216. show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
  217. unsigned long *sp, unsigned long bp, char *log_lvl)
  218. {
  219. unsigned long *irq_stack_end;
  220. unsigned long *irq_stack;
  221. unsigned long *stack;
  222. int cpu;
  223. int i;
  224. preempt_disable();
  225. cpu = smp_processor_id();
  226. irq_stack_end = (unsigned long *)(per_cpu(irq_stack_ptr, cpu));
  227. irq_stack = (unsigned long *)(per_cpu(irq_stack_ptr, cpu) - IRQ_STACK_SIZE);
  228. /*
  229. * Debugging aid: "show_stack(NULL, NULL);" prints the
  230. * back trace for this cpu:
  231. */
  232. if (sp == NULL) {
  233. if (task)
  234. sp = (unsigned long *)task->thread.sp;
  235. else
  236. sp = (unsigned long *)&sp;
  237. }
  238. stack = sp;
  239. for (i = 0; i < kstack_depth_to_print; i++) {
  240. if (stack >= irq_stack && stack <= irq_stack_end) {
  241. if (stack == irq_stack_end) {
  242. stack = (unsigned long *) (irq_stack_end[-1]);
  243. printk(" <EOI> ");
  244. }
  245. } else {
  246. if (((long) stack & (THREAD_SIZE-1)) == 0)
  247. break;
  248. }
  249. if (i && ((i % STACKSLOTS_PER_LINE) == 0))
  250. printk("\n%s", log_lvl);
  251. printk(" %016lx", *stack++);
  252. touch_nmi_watchdog();
  253. }
  254. preempt_enable();
  255. printk("\n");
  256. show_trace_log_lvl(task, regs, sp, bp, log_lvl);
  257. }
  258. void show_registers(struct pt_regs *regs)
  259. {
  260. int i;
  261. unsigned long sp;
  262. const int cpu = smp_processor_id();
  263. struct task_struct *cur = current;
  264. sp = regs->sp;
  265. printk("CPU %d ", cpu);
  266. print_modules();
  267. __show_regs(regs, 1);
  268. printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
  269. cur->comm, cur->pid, task_thread_info(cur), cur);
  270. /*
  271. * When in-kernel, we also print out the stack and code at the
  272. * time of the fault..
  273. */
  274. if (!user_mode(regs)) {
  275. unsigned int code_prologue = code_bytes * 43 / 64;
  276. unsigned int code_len = code_bytes;
  277. unsigned char c;
  278. u8 *ip;
  279. printk(KERN_EMERG "Stack:\n");
  280. show_stack_log_lvl(NULL, regs, (unsigned long *)sp,
  281. regs->bp, KERN_EMERG);
  282. printk(KERN_EMERG "Code: ");
  283. ip = (u8 *)regs->ip - code_prologue;
  284. if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
  285. /* try starting at IP */
  286. ip = (u8 *)regs->ip;
  287. code_len = code_len - code_prologue + 1;
  288. }
  289. for (i = 0; i < code_len; i++, ip++) {
  290. if (ip < (u8 *)PAGE_OFFSET ||
  291. probe_kernel_address(ip, c)) {
  292. printk(" Bad RIP value.");
  293. break;
  294. }
  295. if (ip == (u8 *)regs->ip)
  296. printk("<%02x> ", c);
  297. else
  298. printk("%02x ", c);
  299. }
  300. }
  301. printk("\n");
  302. }
  303. int is_valid_bugaddr(unsigned long ip)
  304. {
  305. unsigned short ud2;
  306. if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
  307. return 0;
  308. return ud2 == 0x0b0f;
  309. }