dumpstack_64.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (C) 1991, 1992 Linus Torvalds
  3. * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
  4. */
  5. #include <linux/sched/debug.h>
  6. #include <linux/kallsyms.h>
  7. #include <linux/kprobes.h>
  8. #include <linux/uaccess.h>
  9. #include <linux/hardirq.h>
  10. #include <linux/kdebug.h>
  11. #include <linux/export.h>
  12. #include <linux/ptrace.h>
  13. #include <linux/kexec.h>
  14. #include <linux/sysfs.h>
  15. #include <linux/bug.h>
  16. #include <linux/nmi.h>
  17. #include <asm/stacktrace.h>
  18. static char *exception_stack_names[N_EXCEPTION_STACKS] = {
  19. [ DOUBLEFAULT_STACK-1 ] = "#DF",
  20. [ NMI_STACK-1 ] = "NMI",
  21. [ DEBUG_STACK-1 ] = "#DB",
  22. [ MCE_STACK-1 ] = "#MC",
  23. };
  24. static unsigned long exception_stack_sizes[N_EXCEPTION_STACKS] = {
  25. [0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STKSZ,
  26. [DEBUG_STACK - 1] = DEBUG_STKSZ
  27. };
  28. const char *stack_type_name(enum stack_type type)
  29. {
  30. BUILD_BUG_ON(N_EXCEPTION_STACKS != 4);
  31. if (type == STACK_TYPE_IRQ)
  32. return "IRQ";
  33. if (type >= STACK_TYPE_EXCEPTION && type <= STACK_TYPE_EXCEPTION_LAST)
  34. return exception_stack_names[type - STACK_TYPE_EXCEPTION];
  35. return NULL;
  36. }
  37. static bool in_exception_stack(unsigned long *stack, struct stack_info *info)
  38. {
  39. unsigned long *begin, *end;
  40. struct pt_regs *regs;
  41. unsigned k;
  42. BUILD_BUG_ON(N_EXCEPTION_STACKS != 4);
  43. for (k = 0; k < N_EXCEPTION_STACKS; k++) {
  44. end = (unsigned long *)raw_cpu_ptr(&orig_ist)->ist[k];
  45. begin = end - (exception_stack_sizes[k] / sizeof(long));
  46. regs = (struct pt_regs *)end - 1;
  47. if (stack < begin || stack >= end)
  48. continue;
  49. info->type = STACK_TYPE_EXCEPTION + k;
  50. info->begin = begin;
  51. info->end = end;
  52. info->next_sp = (unsigned long *)regs->sp;
  53. return true;
  54. }
  55. return false;
  56. }
  57. static bool in_irq_stack(unsigned long *stack, struct stack_info *info)
  58. {
  59. unsigned long *end = (unsigned long *)this_cpu_read(irq_stack_ptr);
  60. unsigned long *begin = end - (IRQ_STACK_SIZE / sizeof(long));
  61. /*
  62. * This is a software stack, so 'end' can be a valid stack pointer.
  63. * It just means the stack is empty.
  64. */
  65. if (stack < begin || stack > end)
  66. return false;
  67. info->type = STACK_TYPE_IRQ;
  68. info->begin = begin;
  69. info->end = end;
  70. /*
  71. * The next stack pointer is the first thing pushed by the entry code
  72. * after switching to the irq stack.
  73. */
  74. info->next_sp = (unsigned long *)*(end - 1);
  75. return true;
  76. }
  77. int get_stack_info(unsigned long *stack, struct task_struct *task,
  78. struct stack_info *info, unsigned long *visit_mask)
  79. {
  80. if (!stack)
  81. goto unknown;
  82. task = task ? : current;
  83. if (in_task_stack(stack, task, info))
  84. goto recursion_check;
  85. if (task != current)
  86. goto unknown;
  87. if (in_exception_stack(stack, info))
  88. goto recursion_check;
  89. if (in_irq_stack(stack, info))
  90. goto recursion_check;
  91. goto unknown;
  92. recursion_check:
  93. /*
  94. * Make sure we don't iterate through any given stack more than once.
  95. * If it comes up a second time then there's something wrong going on:
  96. * just break out and report an unknown stack type.
  97. */
  98. if (visit_mask) {
  99. if (*visit_mask & (1UL << info->type)) {
  100. printk_deferred_once(KERN_WARNING "WARNING: stack recursion on stack type %d\n", info->type);
  101. goto unknown;
  102. }
  103. *visit_mask |= 1UL << info->type;
  104. }
  105. return 0;
  106. unknown:
  107. info->type = STACK_TYPE_UNKNOWN;
  108. return -EINVAL;
  109. }
  110. void show_regs(struct pt_regs *regs)
  111. {
  112. int i;
  113. show_regs_print_info(KERN_DEFAULT);
  114. __show_regs(regs, 1);
  115. /*
  116. * When in-kernel, we also print out the stack and code at the
  117. * time of the fault..
  118. */
  119. if (!user_mode(regs)) {
  120. unsigned int code_prologue = code_bytes * 43 / 64;
  121. unsigned int code_len = code_bytes;
  122. unsigned char c;
  123. u8 *ip;
  124. show_trace_log_lvl(current, regs, NULL, KERN_DEFAULT);
  125. printk(KERN_DEFAULT "Code: ");
  126. ip = (u8 *)regs->ip - code_prologue;
  127. if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
  128. /* try starting at IP */
  129. ip = (u8 *)regs->ip;
  130. code_len = code_len - code_prologue + 1;
  131. }
  132. for (i = 0; i < code_len; i++, ip++) {
  133. if (ip < (u8 *)PAGE_OFFSET ||
  134. probe_kernel_address(ip, c)) {
  135. pr_cont(" Bad RIP value.");
  136. break;
  137. }
  138. if (ip == (u8 *)regs->ip)
  139. pr_cont("<%02x> ", c);
  140. else
  141. pr_cont("%02x ", c);
  142. }
  143. }
  144. pr_cont("\n");
  145. }