dumpstack_64.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. static const unsigned long irq_stack_size =
  99. (IRQ_STACK_SIZE - 64) / sizeof(unsigned long);
  100. enum stack_type {
  101. STACK_IS_UNKNOWN,
  102. STACK_IS_NORMAL,
  103. STACK_IS_EXCEPTION,
  104. STACK_IS_IRQ,
  105. };
  106. static enum stack_type
  107. analyze_stack(int cpu, struct task_struct *task, unsigned long *stack,
  108. unsigned long **stack_end, unsigned long *irq_stack,
  109. unsigned *used, char **id)
  110. {
  111. unsigned long addr;
  112. addr = ((unsigned long)stack & (~(THREAD_SIZE - 1)));
  113. if ((unsigned long)task_stack_page(task) == addr)
  114. return STACK_IS_NORMAL;
  115. *stack_end = in_exception_stack(cpu, (unsigned long)stack,
  116. used, id);
  117. if (*stack_end)
  118. return STACK_IS_EXCEPTION;
  119. if (!irq_stack)
  120. return STACK_IS_NORMAL;
  121. *stack_end = irq_stack;
  122. irq_stack = irq_stack - irq_stack_size;
  123. if (in_irq_stack(stack, irq_stack, *stack_end))
  124. return STACK_IS_IRQ;
  125. return STACK_IS_UNKNOWN;
  126. }
  127. /*
  128. * x86-64 can have up to three kernel stacks:
  129. * process stack
  130. * interrupt stack
  131. * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
  132. */
  133. void dump_trace(struct task_struct *task, struct pt_regs *regs,
  134. unsigned long *stack, unsigned long bp,
  135. const struct stacktrace_ops *ops, void *data)
  136. {
  137. const unsigned cpu = get_cpu();
  138. struct thread_info *tinfo;
  139. unsigned long *irq_stack = (unsigned long *)per_cpu(irq_stack_ptr, cpu);
  140. unsigned long dummy;
  141. unsigned used = 0;
  142. int graph = 0;
  143. int done = 0;
  144. if (!task)
  145. task = current;
  146. if (!stack) {
  147. if (regs)
  148. stack = (unsigned long *)regs->sp;
  149. else if (task != current)
  150. stack = (unsigned long *)task->thread.sp;
  151. else
  152. stack = &dummy;
  153. }
  154. if (!bp)
  155. bp = stack_frame(task, regs);
  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. while (!done) {
  163. unsigned long *stack_end;
  164. enum stack_type stype;
  165. char *id;
  166. stype = analyze_stack(cpu, task, stack, &stack_end,
  167. irq_stack, &used, &id);
  168. /* Default finish unless specified to continue */
  169. done = 1;
  170. switch (stype) {
  171. /* Break out early if we are on the thread stack */
  172. case STACK_IS_NORMAL:
  173. break;
  174. case STACK_IS_EXCEPTION:
  175. if (ops->stack(data, id) < 0)
  176. break;
  177. bp = ops->walk_stack(tinfo, stack, bp, ops,
  178. data, stack_end, &graph);
  179. ops->stack(data, "<EOE>");
  180. /*
  181. * We link to the next stack via the
  182. * second-to-last pointer (index -2 to end) in the
  183. * exception stack:
  184. */
  185. stack = (unsigned long *) stack_end[-2];
  186. done = 0;
  187. break;
  188. case STACK_IS_IRQ:
  189. if (ops->stack(data, "IRQ") < 0)
  190. break;
  191. bp = ops->walk_stack(tinfo, stack, bp,
  192. ops, data, 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 *) (stack_end[-1]);
  199. irq_stack = NULL;
  200. ops->stack(data, "EOI");
  201. done = 0;
  202. break;
  203. case STACK_IS_UNKNOWN:
  204. ops->stack(data, "UNK");
  205. break;
  206. }
  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. pr_cont(" <EOI> ");
  244. }
  245. } else {
  246. if (((long) stack & (THREAD_SIZE-1)) == 0)
  247. break;
  248. }
  249. if (i && ((i % STACKSLOTS_PER_LINE) == 0))
  250. pr_cont("\n");
  251. pr_cont(" %016lx", *stack++);
  252. touch_nmi_watchdog();
  253. }
  254. preempt_enable();
  255. pr_cont("\n");
  256. show_trace_log_lvl(task, regs, sp, bp, log_lvl);
  257. }
  258. void show_regs(struct pt_regs *regs)
  259. {
  260. int i;
  261. unsigned long sp;
  262. sp = regs->sp;
  263. show_regs_print_info(KERN_DEFAULT);
  264. __show_regs(regs, 1);
  265. /*
  266. * When in-kernel, we also print out the stack and code at the
  267. * time of the fault..
  268. */
  269. if (!user_mode(regs)) {
  270. unsigned int code_prologue = code_bytes * 43 / 64;
  271. unsigned int code_len = code_bytes;
  272. unsigned char c;
  273. u8 *ip;
  274. printk(KERN_DEFAULT "Stack:\n");
  275. show_stack_log_lvl(NULL, regs, (unsigned long *)sp,
  276. 0, KERN_DEFAULT);
  277. printk(KERN_DEFAULT "Code: ");
  278. ip = (u8 *)regs->ip - code_prologue;
  279. if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
  280. /* try starting at IP */
  281. ip = (u8 *)regs->ip;
  282. code_len = code_len - code_prologue + 1;
  283. }
  284. for (i = 0; i < code_len; i++, ip++) {
  285. if (ip < (u8 *)PAGE_OFFSET ||
  286. probe_kernel_address(ip, c)) {
  287. pr_cont(" Bad RIP value.");
  288. break;
  289. }
  290. if (ip == (u8 *)regs->ip)
  291. pr_cont("<%02x> ", c);
  292. else
  293. pr_cont("%02x ", c);
  294. }
  295. }
  296. pr_cont("\n");
  297. }
  298. int is_valid_bugaddr(unsigned long ip)
  299. {
  300. unsigned short ud2;
  301. if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
  302. return 0;
  303. return ud2 == 0x0b0f;
  304. }