dumpstack_64.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. const char *stack_type_name(enum stack_type type)
  28. {
  29. BUILD_BUG_ON(N_EXCEPTION_STACKS != 4);
  30. if (type == STACK_TYPE_IRQ)
  31. return "IRQ";
  32. if (type >= STACK_TYPE_EXCEPTION && type <= STACK_TYPE_EXCEPTION_LAST)
  33. return exception_stack_names[type - STACK_TYPE_EXCEPTION];
  34. return NULL;
  35. }
  36. static bool in_exception_stack(unsigned long *stack, struct stack_info *info)
  37. {
  38. unsigned long *begin, *end;
  39. struct pt_regs *regs;
  40. unsigned k;
  41. BUILD_BUG_ON(N_EXCEPTION_STACKS != 4);
  42. for (k = 0; k < N_EXCEPTION_STACKS; k++) {
  43. end = (unsigned long *)raw_cpu_ptr(&orig_ist)->ist[k];
  44. begin = end - (exception_stack_sizes[k] / sizeof(long));
  45. regs = (struct pt_regs *)end - 1;
  46. if (stack < begin || stack >= end)
  47. continue;
  48. info->type = STACK_TYPE_EXCEPTION + k;
  49. info->begin = begin;
  50. info->end = end;
  51. info->next_sp = (unsigned long *)regs->sp;
  52. return true;
  53. }
  54. return false;
  55. }
  56. static bool in_irq_stack(unsigned long *stack, struct stack_info *info)
  57. {
  58. unsigned long *end = (unsigned long *)this_cpu_read(irq_stack_ptr);
  59. unsigned long *begin = end - (IRQ_STACK_SIZE / sizeof(long));
  60. /*
  61. * This is a software stack, so 'end' can be a valid stack pointer.
  62. * It just means the stack is empty.
  63. */
  64. if (stack < begin || stack > end)
  65. return false;
  66. info->type = STACK_TYPE_IRQ;
  67. info->begin = begin;
  68. info->end = end;
  69. /*
  70. * The next stack pointer is the first thing pushed by the entry code
  71. * after switching to the irq stack.
  72. */
  73. info->next_sp = (unsigned long *)*(end - 1);
  74. return true;
  75. }
  76. int get_stack_info(unsigned long *stack, struct task_struct *task,
  77. struct stack_info *info, unsigned long *visit_mask)
  78. {
  79. if (!stack)
  80. goto unknown;
  81. task = task ? : current;
  82. if (in_task_stack(stack, task, info))
  83. goto recursion_check;
  84. if (task != current)
  85. goto unknown;
  86. if (in_exception_stack(stack, info))
  87. goto recursion_check;
  88. if (in_irq_stack(stack, info))
  89. goto recursion_check;
  90. goto unknown;
  91. recursion_check:
  92. /*
  93. * Make sure we don't iterate through any given stack more than once.
  94. * If it comes up a second time then there's something wrong going on:
  95. * just break out and report an unknown stack type.
  96. */
  97. if (visit_mask) {
  98. if (*visit_mask & (1UL << info->type)) {
  99. printk_deferred_once(KERN_WARNING "WARNING: stack recursion on stack type %d\n", info->type);
  100. goto unknown;
  101. }
  102. *visit_mask |= 1UL << info->type;
  103. }
  104. return 0;
  105. unknown:
  106. info->type = STACK_TYPE_UNKNOWN;
  107. return -EINVAL;
  108. }
  109. void show_regs(struct pt_regs *regs)
  110. {
  111. int i;
  112. show_regs_print_info(KERN_DEFAULT);
  113. __show_regs(regs, 1);
  114. /*
  115. * When in-kernel, we also print out the stack and code at the
  116. * time of the fault..
  117. */
  118. if (!user_mode(regs)) {
  119. unsigned int code_prologue = code_bytes * 43 / 64;
  120. unsigned int code_len = code_bytes;
  121. unsigned char c;
  122. u8 *ip;
  123. show_trace_log_lvl(current, regs, NULL, KERN_DEFAULT);
  124. printk(KERN_DEFAULT "Code: ");
  125. ip = (u8 *)regs->ip - code_prologue;
  126. if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
  127. /* try starting at IP */
  128. ip = (u8 *)regs->ip;
  129. code_len = code_len - code_prologue + 1;
  130. }
  131. for (i = 0; i < code_len; i++, ip++) {
  132. if (ip < (u8 *)PAGE_OFFSET ||
  133. probe_kernel_address(ip, c)) {
  134. pr_cont(" Bad RIP value.");
  135. break;
  136. }
  137. if (ip == (u8 *)regs->ip)
  138. pr_cont("<%02x> ", c);
  139. else
  140. pr_cont("%02x ", c);
  141. }
  142. }
  143. pr_cont("\n");
  144. }
  145. int is_valid_bugaddr(unsigned long ip)
  146. {
  147. unsigned short ud2;
  148. if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
  149. return 0;
  150. return ud2 == 0x0b0f;
  151. }