dumpstack_64.c 8.2 KB

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