dumpstack_64.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 1991, 1992 Linus Torvalds
  4. * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
  5. */
  6. #include <linux/sched/debug.h>
  7. #include <linux/kallsyms.h>
  8. #include <linux/kprobes.h>
  9. #include <linux/uaccess.h>
  10. #include <linux/hardirq.h>
  11. #include <linux/kdebug.h>
  12. #include <linux/export.h>
  13. #include <linux/ptrace.h>
  14. #include <linux/kexec.h>
  15. #include <linux/sysfs.h>
  16. #include <linux/bug.h>
  17. #include <linux/nmi.h>
  18. #include <asm/stacktrace.h>
  19. static char *exception_stack_names[N_EXCEPTION_STACKS] = {
  20. [ DOUBLEFAULT_STACK-1 ] = "#DF",
  21. [ NMI_STACK-1 ] = "NMI",
  22. [ DEBUG_STACK-1 ] = "#DB",
  23. [ MCE_STACK-1 ] = "#MC",
  24. };
  25. static unsigned long exception_stack_sizes[N_EXCEPTION_STACKS] = {
  26. [0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STKSZ,
  27. [DEBUG_STACK - 1] = DEBUG_STKSZ
  28. };
  29. const char *stack_type_name(enum stack_type type)
  30. {
  31. BUILD_BUG_ON(N_EXCEPTION_STACKS != 4);
  32. if (type == STACK_TYPE_IRQ)
  33. return "IRQ";
  34. if (type == STACK_TYPE_ENTRY) {
  35. /*
  36. * On 64-bit, we have a generic entry stack that we
  37. * use for all the kernel entry points, including
  38. * SYSENTER.
  39. */
  40. return "ENTRY_TRAMPOLINE";
  41. }
  42. if (type >= STACK_TYPE_EXCEPTION && type <= STACK_TYPE_EXCEPTION_LAST)
  43. return exception_stack_names[type - STACK_TYPE_EXCEPTION];
  44. return NULL;
  45. }
  46. static bool in_exception_stack(unsigned long *stack, struct stack_info *info)
  47. {
  48. unsigned long *begin, *end;
  49. struct pt_regs *regs;
  50. unsigned k;
  51. BUILD_BUG_ON(N_EXCEPTION_STACKS != 4);
  52. for (k = 0; k < N_EXCEPTION_STACKS; k++) {
  53. end = (unsigned long *)raw_cpu_ptr(&orig_ist)->ist[k];
  54. begin = end - (exception_stack_sizes[k] / sizeof(long));
  55. regs = (struct pt_regs *)end - 1;
  56. if (stack <= begin || stack >= end)
  57. continue;
  58. info->type = STACK_TYPE_EXCEPTION + k;
  59. info->begin = begin;
  60. info->end = end;
  61. info->next_sp = (unsigned long *)regs->sp;
  62. return true;
  63. }
  64. return false;
  65. }
  66. static bool in_irq_stack(unsigned long *stack, struct stack_info *info)
  67. {
  68. unsigned long *end = (unsigned long *)this_cpu_read(irq_stack_ptr);
  69. unsigned long *begin = end - (IRQ_STACK_SIZE / sizeof(long));
  70. /*
  71. * This is a software stack, so 'end' can be a valid stack pointer.
  72. * It just means the stack is empty.
  73. */
  74. if (stack <= begin || stack > end)
  75. return false;
  76. info->type = STACK_TYPE_IRQ;
  77. info->begin = begin;
  78. info->end = end;
  79. /*
  80. * The next stack pointer is the first thing pushed by the entry code
  81. * after switching to the irq stack.
  82. */
  83. info->next_sp = (unsigned long *)*(end - 1);
  84. return true;
  85. }
  86. int get_stack_info(unsigned long *stack, struct task_struct *task,
  87. struct stack_info *info, unsigned long *visit_mask)
  88. {
  89. if (!stack)
  90. goto unknown;
  91. task = task ? : current;
  92. if (in_task_stack(stack, task, info))
  93. goto recursion_check;
  94. if (task != current)
  95. goto unknown;
  96. if (in_exception_stack(stack, info))
  97. goto recursion_check;
  98. if (in_irq_stack(stack, info))
  99. goto recursion_check;
  100. if (in_entry_stack(stack, info))
  101. goto recursion_check;
  102. goto unknown;
  103. recursion_check:
  104. /*
  105. * Make sure we don't iterate through any given stack more than once.
  106. * If it comes up a second time then there's something wrong going on:
  107. * just break out and report an unknown stack type.
  108. */
  109. if (visit_mask) {
  110. if (*visit_mask & (1UL << info->type)) {
  111. printk_deferred_once(KERN_WARNING "WARNING: stack recursion on stack type %d\n", info->type);
  112. goto unknown;
  113. }
  114. *visit_mask |= 1UL << info->type;
  115. }
  116. return 0;
  117. unknown:
  118. info->type = STACK_TYPE_UNKNOWN;
  119. return -EINVAL;
  120. }