dumpstack_64.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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/cpu_entry_area.h>
  19. #include <asm/stacktrace.h>
  20. static const char * const exception_stack_names[] = {
  21. [ ESTACK_DF ] = "#DF",
  22. [ ESTACK_NMI ] = "NMI",
  23. [ ESTACK_DB ] = "#DB",
  24. [ ESTACK_MCE ] = "#MC",
  25. [ ESTACK_VC ] = "#VC",
  26. [ ESTACK_VC2 ] = "#VC2",
  27. };
  28. const char *stack_type_name(enum stack_type type)
  29. {
  30. BUILD_BUG_ON(N_EXCEPTION_STACKS != 6);
  31. if (type == STACK_TYPE_IRQ)
  32. return "IRQ";
  33. if (type == STACK_TYPE_ENTRY) {
  34. /*
  35. * On 64-bit, we have a generic entry stack that we
  36. * use for all the kernel entry points, including
  37. * SYSENTER.
  38. */
  39. return "ENTRY_TRAMPOLINE";
  40. }
  41. if (type >= STACK_TYPE_EXCEPTION && type <= STACK_TYPE_EXCEPTION_LAST)
  42. return exception_stack_names[type - STACK_TYPE_EXCEPTION];
  43. return NULL;
  44. }
  45. /**
  46. * struct estack_pages - Page descriptor for exception stacks
  47. * @offs: Offset from the start of the exception stack area
  48. * @size: Size of the exception stack
  49. * @type: Type to store in the stack_info struct
  50. */
  51. struct estack_pages {
  52. u32 offs;
  53. u16 size;
  54. u16 type;
  55. };
  56. #define EPAGERANGE(st) \
  57. [PFN_DOWN(CEA_ESTACK_OFFS(st)) ... \
  58. PFN_DOWN(CEA_ESTACK_OFFS(st) + CEA_ESTACK_SIZE(st) - 1)] = { \
  59. .offs = CEA_ESTACK_OFFS(st), \
  60. .size = CEA_ESTACK_SIZE(st), \
  61. .type = STACK_TYPE_EXCEPTION + ESTACK_ ##st, }
  62. /*
  63. * Array of exception stack page descriptors. If the stack is larger than
  64. * PAGE_SIZE, all pages covering a particular stack will have the same
  65. * info. The guard pages including the not mapped DB2 stack are zeroed
  66. * out.
  67. */
  68. static const
  69. struct estack_pages estack_pages[CEA_ESTACK_PAGES] ____cacheline_aligned = {
  70. EPAGERANGE(DF),
  71. EPAGERANGE(NMI),
  72. EPAGERANGE(DB),
  73. EPAGERANGE(MCE),
  74. EPAGERANGE(VC),
  75. EPAGERANGE(VC2),
  76. };
  77. static __always_inline bool in_exception_stack(unsigned long *stack, struct stack_info *info)
  78. {
  79. unsigned long begin, end, stk = (unsigned long)stack;
  80. const struct estack_pages *ep;
  81. struct pt_regs *regs;
  82. unsigned int k;
  83. BUILD_BUG_ON(N_EXCEPTION_STACKS != 6);
  84. begin = (unsigned long)__this_cpu_read(cea_exception_stacks);
  85. /*
  86. * Handle the case where stack trace is collected _before_
  87. * cea_exception_stacks had been initialized.
  88. */
  89. if (!begin)
  90. return false;
  91. end = begin + sizeof(struct cea_exception_stacks);
  92. /* Bail if @stack is outside the exception stack area. */
  93. if (stk < begin || stk >= end)
  94. return false;
  95. /* Calc page offset from start of exception stacks */
  96. k = (stk - begin) >> PAGE_SHIFT;
  97. /* Lookup the page descriptor */
  98. ep = &estack_pages[k];
  99. /* Guard page? */
  100. if (!ep->size)
  101. return false;
  102. begin += (unsigned long)ep->offs;
  103. end = begin + (unsigned long)ep->size;
  104. regs = (struct pt_regs *)end - 1;
  105. info->type = ep->type;
  106. info->begin = (unsigned long *)begin;
  107. info->end = (unsigned long *)end;
  108. info->next_sp = (unsigned long *)regs->sp;
  109. return true;
  110. }
  111. static __always_inline bool in_irq_stack(unsigned long *stack, struct stack_info *info)
  112. {
  113. unsigned long *end = (unsigned long *)this_cpu_read(hardirq_stack_ptr);
  114. unsigned long *begin = end - (IRQ_STACK_SIZE / sizeof(long));
  115. /*
  116. * This is a software stack, so 'end' can be a valid stack pointer.
  117. * It just means the stack is empty.
  118. */
  119. if (stack < begin || stack >= end)
  120. return false;
  121. info->type = STACK_TYPE_IRQ;
  122. info->begin = begin;
  123. info->end = end;
  124. /*
  125. * The next stack pointer is the first thing pushed by the entry code
  126. * after switching to the irq stack.
  127. */
  128. info->next_sp = (unsigned long *)*(end - 1);
  129. return true;
  130. }
  131. bool noinstr get_stack_info_noinstr(unsigned long *stack, struct task_struct *task,
  132. struct stack_info *info)
  133. {
  134. if (in_task_stack(stack, task, info))
  135. return true;
  136. if (task != current)
  137. return false;
  138. if (in_exception_stack(stack, info))
  139. return true;
  140. if (in_irq_stack(stack, info))
  141. return true;
  142. if (in_entry_stack(stack, info))
  143. return true;
  144. return false;
  145. }
  146. int get_stack_info(unsigned long *stack, struct task_struct *task,
  147. struct stack_info *info, unsigned long *visit_mask)
  148. {
  149. task = task ? : current;
  150. if (!stack)
  151. goto unknown;
  152. if (!get_stack_info_noinstr(stack, task, info))
  153. goto unknown;
  154. /*
  155. * Make sure we don't iterate through any given stack more than once.
  156. * If it comes up a second time then there's something wrong going on:
  157. * just break out and report an unknown stack type.
  158. */
  159. if (visit_mask) {
  160. if (*visit_mask & (1UL << info->type)) {
  161. if (task == current)
  162. printk_deferred_once(KERN_WARNING "WARNING: stack recursion on stack type %d\n", info->type);
  163. goto unknown;
  164. }
  165. *visit_mask |= 1UL << info->type;
  166. }
  167. return 0;
  168. unknown:
  169. info->type = STACK_TYPE_UNKNOWN;
  170. return -EINVAL;
  171. }