dumpstack_64.c 8.0 KB

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