dumpstack_64.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. static char *exception_stack_names[N_EXCEPTION_STACKS] = {
  18. [ DOUBLEFAULT_STACK-1 ] = "#DF",
  19. [ NMI_STACK-1 ] = "NMI",
  20. [ DEBUG_STACK-1 ] = "#DB",
  21. [ MCE_STACK-1 ] = "#MC",
  22. };
  23. static unsigned long exception_stack_sizes[N_EXCEPTION_STACKS] = {
  24. [0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STKSZ,
  25. [DEBUG_STACK - 1] = DEBUG_STKSZ
  26. };
  27. void stack_type_str(enum stack_type type, const char **begin, const char **end)
  28. {
  29. BUILD_BUG_ON(N_EXCEPTION_STACKS != 4);
  30. switch (type) {
  31. case STACK_TYPE_IRQ:
  32. *begin = "IRQ";
  33. *end = "EOI";
  34. break;
  35. case STACK_TYPE_EXCEPTION ... STACK_TYPE_EXCEPTION_LAST:
  36. *begin = exception_stack_names[type - STACK_TYPE_EXCEPTION];
  37. *end = "EOE";
  38. break;
  39. default:
  40. *begin = NULL;
  41. *end = NULL;
  42. }
  43. }
  44. static bool in_exception_stack(unsigned long *stack, struct stack_info *info)
  45. {
  46. unsigned long *begin, *end;
  47. struct pt_regs *regs;
  48. unsigned k;
  49. BUILD_BUG_ON(N_EXCEPTION_STACKS != 4);
  50. for (k = 0; k < N_EXCEPTION_STACKS; k++) {
  51. end = (unsigned long *)raw_cpu_ptr(&orig_ist)->ist[k];
  52. begin = end - (exception_stack_sizes[k] / sizeof(long));
  53. regs = (struct pt_regs *)end - 1;
  54. if (stack < begin || stack >= end)
  55. continue;
  56. info->type = STACK_TYPE_EXCEPTION + k;
  57. info->begin = begin;
  58. info->end = end;
  59. info->next_sp = (unsigned long *)regs->sp;
  60. return true;
  61. }
  62. return false;
  63. }
  64. static bool in_irq_stack(unsigned long *stack, struct stack_info *info)
  65. {
  66. unsigned long *end = (unsigned long *)this_cpu_read(irq_stack_ptr);
  67. unsigned long *begin = end - (IRQ_STACK_SIZE / sizeof(long));
  68. /*
  69. * This is a software stack, so 'end' can be a valid stack pointer.
  70. * It just means the stack is empty.
  71. */
  72. if (stack < begin || stack > end)
  73. return false;
  74. info->type = STACK_TYPE_IRQ;
  75. info->begin = begin;
  76. info->end = end;
  77. /*
  78. * The next stack pointer is the first thing pushed by the entry code
  79. * after switching to the irq stack.
  80. */
  81. info->next_sp = (unsigned long *)*(end - 1);
  82. return true;
  83. }
  84. int get_stack_info(unsigned long *stack, struct task_struct *task,
  85. struct stack_info *info, unsigned long *visit_mask)
  86. {
  87. if (!stack)
  88. goto unknown;
  89. task = task ? : current;
  90. if (in_task_stack(stack, task, info))
  91. goto recursion_check;
  92. if (task != current)
  93. goto unknown;
  94. if (in_exception_stack(stack, info))
  95. goto recursion_check;
  96. if (in_irq_stack(stack, info))
  97. goto recursion_check;
  98. goto unknown;
  99. recursion_check:
  100. /*
  101. * Make sure we don't iterate through any given stack more than once.
  102. * If it comes up a second time then there's something wrong going on:
  103. * just break out and report an unknown stack type.
  104. */
  105. if (visit_mask) {
  106. if (*visit_mask & (1UL << info->type))
  107. goto unknown;
  108. *visit_mask |= 1UL << info->type;
  109. }
  110. return 0;
  111. unknown:
  112. info->type = STACK_TYPE_UNKNOWN;
  113. return -EINVAL;
  114. }
  115. void show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
  116. unsigned long *sp, char *log_lvl)
  117. {
  118. unsigned long *irq_stack_end;
  119. unsigned long *irq_stack;
  120. unsigned long *stack;
  121. int i;
  122. if (!try_get_task_stack(task))
  123. return;
  124. irq_stack_end = (unsigned long *)this_cpu_read(irq_stack_ptr);
  125. irq_stack = irq_stack_end - (IRQ_STACK_SIZE / sizeof(long));
  126. sp = sp ? : get_stack_pointer(task, regs);
  127. stack = sp;
  128. for (i = 0; i < kstack_depth_to_print; i++) {
  129. unsigned long word;
  130. if (stack >= irq_stack && stack <= irq_stack_end) {
  131. if (stack == irq_stack_end) {
  132. stack = (unsigned long *) (irq_stack_end[-1]);
  133. pr_cont(" <EOI> ");
  134. }
  135. } else {
  136. if (kstack_end(stack))
  137. break;
  138. }
  139. if (probe_kernel_address(stack, word))
  140. break;
  141. if ((i % STACKSLOTS_PER_LINE) == 0) {
  142. if (i != 0)
  143. pr_cont("\n");
  144. printk("%s %016lx", log_lvl, word);
  145. } else
  146. pr_cont(" %016lx", word);
  147. stack++;
  148. touch_nmi_watchdog();
  149. }
  150. pr_cont("\n");
  151. show_trace_log_lvl(task, regs, sp, log_lvl);
  152. put_task_stack(task);
  153. }
  154. void show_regs(struct pt_regs *regs)
  155. {
  156. int i;
  157. show_regs_print_info(KERN_DEFAULT);
  158. __show_regs(regs, 1);
  159. /*
  160. * When in-kernel, we also print out the stack and code at the
  161. * time of the fault..
  162. */
  163. if (!user_mode(regs)) {
  164. unsigned int code_prologue = code_bytes * 43 / 64;
  165. unsigned int code_len = code_bytes;
  166. unsigned char c;
  167. u8 *ip;
  168. printk(KERN_DEFAULT "Stack:\n");
  169. show_stack_log_lvl(current, regs, NULL, KERN_DEFAULT);
  170. printk(KERN_DEFAULT "Code: ");
  171. ip = (u8 *)regs->ip - code_prologue;
  172. if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
  173. /* try starting at IP */
  174. ip = (u8 *)regs->ip;
  175. code_len = code_len - code_prologue + 1;
  176. }
  177. for (i = 0; i < code_len; i++, ip++) {
  178. if (ip < (u8 *)PAGE_OFFSET ||
  179. probe_kernel_address(ip, c)) {
  180. pr_cont(" Bad RIP value.");
  181. break;
  182. }
  183. if (ip == (u8 *)regs->ip)
  184. pr_cont("<%02x> ", c);
  185. else
  186. pr_cont("%02x ", c);
  187. }
  188. }
  189. pr_cont("\n");
  190. }
  191. int is_valid_bugaddr(unsigned long ip)
  192. {
  193. unsigned short ud2;
  194. if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
  195. return 0;
  196. return ud2 == 0x0b0f;
  197. }