dumpstack_64.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. [ 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. static const unsigned long irq_stack_size =
  98. (IRQ_STACK_SIZE - 64) / sizeof(unsigned long);
  99. enum stack_type {
  100. STACK_IS_UNKNOWN,
  101. STACK_IS_NORMAL,
  102. STACK_IS_EXCEPTION,
  103. STACK_IS_IRQ,
  104. };
  105. static enum stack_type
  106. analyze_stack(int cpu, struct task_struct *task, unsigned long *stack,
  107. unsigned long **stack_end, unsigned long *irq_stack,
  108. unsigned *used, char **id)
  109. {
  110. unsigned long addr;
  111. addr = ((unsigned long)stack & (~(THREAD_SIZE - 1)));
  112. if ((unsigned long)task_stack_page(task) == addr)
  113. return STACK_IS_NORMAL;
  114. *stack_end = in_exception_stack(cpu, (unsigned long)stack,
  115. used, id);
  116. if (*stack_end)
  117. return STACK_IS_EXCEPTION;
  118. if (!irq_stack)
  119. return STACK_IS_NORMAL;
  120. *stack_end = irq_stack;
  121. irq_stack = irq_stack - irq_stack_size;
  122. if (in_irq_stack(stack, irq_stack, *stack_end))
  123. return STACK_IS_IRQ;
  124. return STACK_IS_UNKNOWN;
  125. }
  126. /*
  127. * x86-64 can have up to three kernel stacks:
  128. * process stack
  129. * interrupt stack
  130. * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
  131. */
  132. void dump_trace(struct task_struct *task, struct pt_regs *regs,
  133. unsigned long *stack, unsigned long bp,
  134. const struct stacktrace_ops *ops, void *data)
  135. {
  136. const unsigned cpu = get_cpu();
  137. struct thread_info *tinfo;
  138. unsigned long *irq_stack = (unsigned long *)per_cpu(irq_stack_ptr, cpu);
  139. unsigned long dummy;
  140. unsigned used = 0;
  141. int graph = 0;
  142. int done = 0;
  143. if (!task)
  144. task = current;
  145. if (!stack) {
  146. if (regs)
  147. stack = (unsigned long *)regs->sp;
  148. else if (task != current)
  149. stack = (unsigned long *)task->thread.sp;
  150. else
  151. stack = &dummy;
  152. }
  153. if (!bp)
  154. bp = stack_frame(task, regs);
  155. /*
  156. * Print function call entries in all stacks, starting at the
  157. * current stack address. If the stacks consist of nested
  158. * exceptions
  159. */
  160. tinfo = task_thread_info(task);
  161. while (!done) {
  162. unsigned long *stack_end;
  163. enum stack_type stype;
  164. char *id;
  165. stype = analyze_stack(cpu, task, stack, &stack_end,
  166. irq_stack, &used, &id);
  167. /* Default finish unless specified to continue */
  168. done = 1;
  169. switch (stype) {
  170. /* Break out early if we are on the thread stack */
  171. case STACK_IS_NORMAL:
  172. break;
  173. case STACK_IS_EXCEPTION:
  174. if (ops->stack(data, id) < 0)
  175. break;
  176. bp = ops->walk_stack(tinfo, stack, bp, ops,
  177. data, stack_end, &graph);
  178. ops->stack(data, "<EOE>");
  179. /*
  180. * We link to the next stack via the
  181. * second-to-last pointer (index -2 to end) in the
  182. * exception stack:
  183. */
  184. stack = (unsigned long *) stack_end[-2];
  185. done = 0;
  186. break;
  187. case STACK_IS_IRQ:
  188. if (ops->stack(data, "IRQ") < 0)
  189. break;
  190. bp = ops->walk_stack(tinfo, stack, bp,
  191. ops, data, stack_end, &graph);
  192. /*
  193. * We link to the next stack (which would be
  194. * the process stack normally) the last
  195. * pointer (index -1 to end) in the IRQ stack:
  196. */
  197. stack = (unsigned long *) (stack_end[-1]);
  198. irq_stack = NULL;
  199. ops->stack(data, "EOI");
  200. done = 0;
  201. break;
  202. case STACK_IS_UNKNOWN:
  203. ops->stack(data, "UNK");
  204. break;
  205. }
  206. }
  207. /*
  208. * This handles the process stack:
  209. */
  210. bp = ops->walk_stack(tinfo, stack, bp, ops, data, NULL, &graph);
  211. put_cpu();
  212. }
  213. EXPORT_SYMBOL(dump_trace);
  214. void
  215. show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
  216. unsigned long *sp, unsigned long bp, char *log_lvl)
  217. {
  218. unsigned long *irq_stack_end;
  219. unsigned long *irq_stack;
  220. unsigned long *stack;
  221. int cpu;
  222. int i;
  223. preempt_disable();
  224. cpu = smp_processor_id();
  225. irq_stack_end = (unsigned long *)(per_cpu(irq_stack_ptr, cpu));
  226. irq_stack = (unsigned long *)(per_cpu(irq_stack_ptr, cpu) - IRQ_STACK_SIZE);
  227. /*
  228. * Debugging aid: "show_stack(NULL, NULL);" prints the
  229. * back trace for this cpu:
  230. */
  231. if (sp == NULL) {
  232. if (task)
  233. sp = (unsigned long *)task->thread.sp;
  234. else
  235. sp = (unsigned long *)&sp;
  236. }
  237. stack = sp;
  238. for (i = 0; i < kstack_depth_to_print; i++) {
  239. if (stack >= irq_stack && stack <= irq_stack_end) {
  240. if (stack == irq_stack_end) {
  241. stack = (unsigned long *) (irq_stack_end[-1]);
  242. pr_cont(" <EOI> ");
  243. }
  244. } else {
  245. if (kstack_end(stack))
  246. break;
  247. }
  248. if ((i % STACKSLOTS_PER_LINE) == 0) {
  249. if (i != 0)
  250. pr_cont("\n");
  251. printk("%s %016lx", log_lvl, *stack++);
  252. } else
  253. pr_cont(" %016lx", *stack++);
  254. touch_nmi_watchdog();
  255. }
  256. preempt_enable();
  257. pr_cont("\n");
  258. show_trace_log_lvl(task, regs, sp, bp, log_lvl);
  259. }
  260. void show_regs(struct pt_regs *regs)
  261. {
  262. int i;
  263. unsigned long sp;
  264. sp = regs->sp;
  265. show_regs_print_info(KERN_DEFAULT);
  266. __show_regs(regs, 1);
  267. /*
  268. * When in-kernel, we also print out the stack and code at the
  269. * time of the fault..
  270. */
  271. if (!user_mode(regs)) {
  272. unsigned int code_prologue = code_bytes * 43 / 64;
  273. unsigned int code_len = code_bytes;
  274. unsigned char c;
  275. u8 *ip;
  276. printk(KERN_DEFAULT "Stack:\n");
  277. show_stack_log_lvl(NULL, regs, (unsigned long *)sp,
  278. 0, KERN_DEFAULT);
  279. printk(KERN_DEFAULT "Code: ");
  280. ip = (u8 *)regs->ip - code_prologue;
  281. if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
  282. /* try starting at IP */
  283. ip = (u8 *)regs->ip;
  284. code_len = code_len - code_prologue + 1;
  285. }
  286. for (i = 0; i < code_len; i++, ip++) {
  287. if (ip < (u8 *)PAGE_OFFSET ||
  288. probe_kernel_address(ip, c)) {
  289. pr_cont(" Bad RIP value.");
  290. break;
  291. }
  292. if (ip == (u8 *)regs->ip)
  293. pr_cont("<%02x> ", c);
  294. else
  295. pr_cont("%02x ", c);
  296. }
  297. }
  298. pr_cont("\n");
  299. }
  300. int is_valid_bugaddr(unsigned long ip)
  301. {
  302. unsigned short ud2;
  303. if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
  304. return 0;
  305. return ud2 == 0x0b0f;
  306. }