dumpstack_32.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. const char *stack_type_name(enum stack_type type)
  20. {
  21. if (type == STACK_TYPE_IRQ)
  22. return "IRQ";
  23. if (type == STACK_TYPE_SOFTIRQ)
  24. return "SOFTIRQ";
  25. if (type == STACK_TYPE_ENTRY)
  26. return "ENTRY_TRAMPOLINE";
  27. if (type == STACK_TYPE_EXCEPTION)
  28. return "#DF";
  29. return NULL;
  30. }
  31. static bool in_hardirq_stack(unsigned long *stack, struct stack_info *info)
  32. {
  33. unsigned long *begin = (unsigned long *)this_cpu_read(hardirq_stack_ptr);
  34. unsigned long *end = begin + (THREAD_SIZE / sizeof(long));
  35. /*
  36. * This is a software stack, so 'end' can be a valid stack pointer.
  37. * It just means the stack is empty.
  38. */
  39. if (stack < begin || stack > end)
  40. return false;
  41. info->type = STACK_TYPE_IRQ;
  42. info->begin = begin;
  43. info->end = end;
  44. /*
  45. * See irq_32.c -- the next stack pointer is stored at the beginning of
  46. * the stack.
  47. */
  48. info->next_sp = (unsigned long *)*begin;
  49. return true;
  50. }
  51. static bool in_softirq_stack(unsigned long *stack, struct stack_info *info)
  52. {
  53. unsigned long *begin = (unsigned long *)this_cpu_read(softirq_stack_ptr);
  54. unsigned long *end = begin + (THREAD_SIZE / sizeof(long));
  55. /*
  56. * This is a software stack, so 'end' can be a valid stack pointer.
  57. * It just means the stack is empty.
  58. */
  59. if (stack < begin || stack > end)
  60. return false;
  61. info->type = STACK_TYPE_SOFTIRQ;
  62. info->begin = begin;
  63. info->end = end;
  64. /*
  65. * The next stack pointer is stored at the beginning of the stack.
  66. * See irq_32.c.
  67. */
  68. info->next_sp = (unsigned long *)*begin;
  69. return true;
  70. }
  71. static bool in_doublefault_stack(unsigned long *stack, struct stack_info *info)
  72. {
  73. struct cpu_entry_area *cea = get_cpu_entry_area(raw_smp_processor_id());
  74. struct doublefault_stack *ss = &cea->doublefault_stack;
  75. void *begin = ss->stack;
  76. void *end = begin + sizeof(ss->stack);
  77. if ((void *)stack < begin || (void *)stack >= end)
  78. return false;
  79. info->type = STACK_TYPE_EXCEPTION;
  80. info->begin = begin;
  81. info->end = end;
  82. info->next_sp = (unsigned long *)this_cpu_read(cpu_tss_rw.x86_tss.sp);
  83. return true;
  84. }
  85. int get_stack_info(unsigned long *stack, struct task_struct *task,
  86. struct stack_info *info, unsigned long *visit_mask)
  87. {
  88. if (!stack)
  89. goto unknown;
  90. task = task ? : current;
  91. if (in_task_stack(stack, task, info))
  92. goto recursion_check;
  93. if (task != current)
  94. goto unknown;
  95. if (in_entry_stack(stack, info))
  96. goto recursion_check;
  97. if (in_hardirq_stack(stack, info))
  98. goto recursion_check;
  99. if (in_softirq_stack(stack, info))
  100. goto recursion_check;
  101. if (in_doublefault_stack(stack, info))
  102. goto recursion_check;
  103. goto unknown;
  104. recursion_check:
  105. /*
  106. * Make sure we don't iterate through any given stack more than once.
  107. * If it comes up a second time then there's something wrong going on:
  108. * just break out and report an unknown stack type.
  109. */
  110. if (visit_mask) {
  111. if (*visit_mask & (1UL << info->type)) {
  112. printk_deferred_once(KERN_WARNING "WARNING: stack recursion on stack type %d\n", info->type);
  113. goto unknown;
  114. }
  115. *visit_mask |= 1UL << info->type;
  116. }
  117. return 0;
  118. unknown:
  119. info->type = STACK_TYPE_UNKNOWN;
  120. return -EINVAL;
  121. }