stacktrace.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (C) 2012 ARM Ltd.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef __ASM_STACKTRACE_H
  17. #define __ASM_STACKTRACE_H
  18. #include <linux/percpu.h>
  19. #include <linux/sched.h>
  20. #include <linux/sched/task_stack.h>
  21. #include <asm/memory.h>
  22. #include <asm/ptrace.h>
  23. #include <asm/sdei.h>
  24. struct stackframe {
  25. unsigned long fp;
  26. unsigned long pc;
  27. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  28. int graph;
  29. #endif
  30. };
  31. enum stack_type {
  32. STACK_TYPE_UNKNOWN,
  33. STACK_TYPE_TASK,
  34. STACK_TYPE_IRQ,
  35. STACK_TYPE_OVERFLOW,
  36. STACK_TYPE_SDEI_NORMAL,
  37. STACK_TYPE_SDEI_CRITICAL,
  38. };
  39. struct stack_info {
  40. unsigned long low;
  41. unsigned long high;
  42. enum stack_type type;
  43. };
  44. extern int unwind_frame(struct task_struct *tsk, struct stackframe *frame);
  45. extern void walk_stackframe(struct task_struct *tsk, struct stackframe *frame,
  46. int (*fn)(struct stackframe *, void *), void *data);
  47. extern void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk);
  48. DECLARE_PER_CPU(unsigned long *, irq_stack_ptr);
  49. static inline bool on_irq_stack(unsigned long sp,
  50. struct stack_info *info)
  51. {
  52. unsigned long low = (unsigned long)raw_cpu_read(irq_stack_ptr);
  53. unsigned long high = low + IRQ_STACK_SIZE;
  54. if (!low)
  55. return false;
  56. if (sp < low || sp >= high)
  57. return false;
  58. if (info) {
  59. info->low = low;
  60. info->high = high;
  61. info->type = STACK_TYPE_IRQ;
  62. }
  63. return true;
  64. }
  65. static inline bool on_task_stack(struct task_struct *tsk, unsigned long sp,
  66. struct stack_info *info)
  67. {
  68. unsigned long low = (unsigned long)task_stack_page(tsk);
  69. unsigned long high = low + THREAD_SIZE;
  70. if (sp < low || sp >= high)
  71. return false;
  72. if (info) {
  73. info->low = low;
  74. info->high = high;
  75. info->type = STACK_TYPE_TASK;
  76. }
  77. return true;
  78. }
  79. #ifdef CONFIG_VMAP_STACK
  80. DECLARE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)], overflow_stack);
  81. static inline bool on_overflow_stack(unsigned long sp,
  82. struct stack_info *info)
  83. {
  84. unsigned long low = (unsigned long)raw_cpu_ptr(overflow_stack);
  85. unsigned long high = low + OVERFLOW_STACK_SIZE;
  86. if (sp < low || sp >= high)
  87. return false;
  88. if (info) {
  89. info->low = low;
  90. info->high = high;
  91. info->type = STACK_TYPE_OVERFLOW;
  92. }
  93. return true;
  94. }
  95. #else
  96. static inline bool on_overflow_stack(unsigned long sp,
  97. struct stack_info *info) { return false; }
  98. #endif
  99. /*
  100. * We can only safely access per-cpu stacks from current in a non-preemptible
  101. * context.
  102. */
  103. static inline bool on_accessible_stack(struct task_struct *tsk,
  104. unsigned long sp,
  105. struct stack_info *info)
  106. {
  107. if (on_task_stack(tsk, sp, info))
  108. return true;
  109. if (tsk != current || preemptible())
  110. return false;
  111. if (on_irq_stack(sp, info))
  112. return true;
  113. if (on_overflow_stack(sp, info))
  114. return true;
  115. if (on_sdei_stack(sp, info))
  116. return true;
  117. return false;
  118. }
  119. #endif /* __ASM_STACKTRACE_H */