dumpstack_64.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. 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. unsigned long *irq_stack = (unsigned long *)per_cpu(irq_stack_ptr, cpu);
  138. unsigned long dummy;
  139. unsigned used = 0;
  140. int graph = 0;
  141. int done = 0;
  142. if (!task)
  143. task = current;
  144. if (!stack) {
  145. if (regs)
  146. stack = (unsigned long *)regs->sp;
  147. else if (task != current)
  148. stack = (unsigned long *)task->thread.sp;
  149. else
  150. stack = &dummy;
  151. }
  152. if (!bp)
  153. bp = stack_frame(task, regs);
  154. /*
  155. * Print function call entries in all stacks, starting at the
  156. * current stack address. If the stacks consist of nested
  157. * exceptions
  158. */
  159. while (!done) {
  160. unsigned long *stack_end;
  161. enum stack_type stype;
  162. char *id;
  163. stype = analyze_stack(cpu, task, stack, &stack_end,
  164. irq_stack, &used, &id);
  165. /* Default finish unless specified to continue */
  166. done = 1;
  167. switch (stype) {
  168. /* Break out early if we are on the thread stack */
  169. case STACK_IS_NORMAL:
  170. break;
  171. case STACK_IS_EXCEPTION:
  172. if (ops->stack(data, id) < 0)
  173. break;
  174. bp = ops->walk_stack(task, stack, bp, ops,
  175. data, stack_end, &graph);
  176. ops->stack(data, "<EOE>");
  177. /*
  178. * We link to the next stack via the
  179. * second-to-last pointer (index -2 to end) in the
  180. * exception stack:
  181. */
  182. stack = (unsigned long *) stack_end[-2];
  183. done = 0;
  184. break;
  185. case STACK_IS_IRQ:
  186. if (ops->stack(data, "IRQ") < 0)
  187. break;
  188. bp = ops->walk_stack(task, stack, bp,
  189. ops, data, 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 *) (stack_end[-1]);
  196. irq_stack = NULL;
  197. ops->stack(data, "EOI");
  198. done = 0;
  199. break;
  200. case STACK_IS_UNKNOWN:
  201. ops->stack(data, "UNK");
  202. break;
  203. }
  204. }
  205. /*
  206. * This handles the process stack:
  207. */
  208. bp = ops->walk_stack(task, 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 *irq_stack_end;
  217. unsigned long *irq_stack;
  218. unsigned long *stack;
  219. int cpu;
  220. int i;
  221. preempt_disable();
  222. cpu = smp_processor_id();
  223. irq_stack_end = (unsigned long *)(per_cpu(irq_stack_ptr, cpu));
  224. irq_stack = (unsigned long *)(per_cpu(irq_stack_ptr, cpu) - IRQ_STACK_SIZE);
  225. /*
  226. * Debugging aid: "show_stack(NULL, NULL);" prints the
  227. * back trace for this cpu:
  228. */
  229. if (sp == NULL) {
  230. if (regs)
  231. sp = (unsigned long *)regs->sp;
  232. else 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. unsigned long word;
  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 (kstack_end(stack))
  247. break;
  248. }
  249. if (probe_kernel_address(stack, word))
  250. break;
  251. if ((i % STACKSLOTS_PER_LINE) == 0) {
  252. if (i != 0)
  253. pr_cont("\n");
  254. printk("%s %016lx", log_lvl, word);
  255. } else
  256. pr_cont(" %016lx", word);
  257. stack++;
  258. touch_nmi_watchdog();
  259. }
  260. preempt_enable();
  261. pr_cont("\n");
  262. show_trace_log_lvl(task, regs, sp, bp, log_lvl);
  263. }
  264. void show_regs(struct pt_regs *regs)
  265. {
  266. int i;
  267. unsigned long sp;
  268. sp = regs->sp;
  269. show_regs_print_info(KERN_DEFAULT);
  270. __show_regs(regs, 1);
  271. /*
  272. * When in-kernel, we also print out the stack and code at the
  273. * time of the fault..
  274. */
  275. if (!user_mode(regs)) {
  276. unsigned int code_prologue = code_bytes * 43 / 64;
  277. unsigned int code_len = code_bytes;
  278. unsigned char c;
  279. u8 *ip;
  280. printk(KERN_DEFAULT "Stack:\n");
  281. show_stack_log_lvl(NULL, regs, (unsigned long *)sp,
  282. 0, KERN_DEFAULT);
  283. printk(KERN_DEFAULT "Code: ");
  284. ip = (u8 *)regs->ip - code_prologue;
  285. if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
  286. /* try starting at IP */
  287. ip = (u8 *)regs->ip;
  288. code_len = code_len - code_prologue + 1;
  289. }
  290. for (i = 0; i < code_len; i++, ip++) {
  291. if (ip < (u8 *)PAGE_OFFSET ||
  292. probe_kernel_address(ip, c)) {
  293. pr_cont(" Bad RIP value.");
  294. break;
  295. }
  296. if (ip == (u8 *)regs->ip)
  297. pr_cont("<%02x> ", c);
  298. else
  299. pr_cont("%02x ", c);
  300. }
  301. }
  302. pr_cont("\n");
  303. }
  304. int is_valid_bugaddr(unsigned long ip)
  305. {
  306. unsigned short ud2;
  307. if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
  308. return 0;
  309. return ud2 == 0x0b0f;
  310. }