common.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Common arm64 stack unwinder code.
  4. *
  5. * See: arch/arm64/kernel/stacktrace.c for the reference implementation.
  6. *
  7. * Copyright (C) 2012 ARM Ltd.
  8. */
  9. #ifndef __ASM_STACKTRACE_COMMON_H
  10. #define __ASM_STACKTRACE_COMMON_H
  11. #include <linux/kprobes.h>
  12. #include <linux/types.h>
  13. struct stack_info {
  14. unsigned long low;
  15. unsigned long high;
  16. };
  17. /**
  18. * struct unwind_state - state used for robust unwinding.
  19. *
  20. * @fp: The fp value in the frame record (or the real fp)
  21. * @pc: The lr value in the frame record (or the real lr)
  22. *
  23. * @kr_cur: When KRETPROBES is selected, holds the kretprobe instance
  24. * associated with the most recently encountered replacement lr
  25. * value.
  26. *
  27. * @task: The task being unwound.
  28. *
  29. * @stack: The stack currently being unwound.
  30. * @stacks: An array of stacks which can be unwound.
  31. * @nr_stacks: The number of stacks in @stacks.
  32. */
  33. struct unwind_state {
  34. unsigned long fp;
  35. unsigned long pc;
  36. #ifdef CONFIG_KRETPROBES
  37. struct llist_node *kr_cur;
  38. #endif
  39. struct task_struct *task;
  40. struct stack_info stack;
  41. struct stack_info *stacks;
  42. int nr_stacks;
  43. };
  44. static inline struct stack_info stackinfo_get_unknown(void)
  45. {
  46. return (struct stack_info) {
  47. .low = 0,
  48. .high = 0,
  49. };
  50. }
  51. static inline bool stackinfo_on_stack(const struct stack_info *info,
  52. unsigned long sp, unsigned long size)
  53. {
  54. if (!info->low)
  55. return false;
  56. if (sp < info->low || sp + size < sp || sp + size > info->high)
  57. return false;
  58. return true;
  59. }
  60. static inline void unwind_init_common(struct unwind_state *state,
  61. struct task_struct *task)
  62. {
  63. state->task = task;
  64. #ifdef CONFIG_KRETPROBES
  65. state->kr_cur = NULL;
  66. #endif
  67. state->stack = stackinfo_get_unknown();
  68. }
  69. static struct stack_info *unwind_find_next_stack(const struct unwind_state *state,
  70. unsigned long sp,
  71. unsigned long size)
  72. {
  73. for (int i = 0; i < state->nr_stacks; i++) {
  74. struct stack_info *info = &state->stacks[i];
  75. if (stackinfo_on_stack(info, sp, size))
  76. return info;
  77. }
  78. return NULL;
  79. }
  80. /**
  81. * unwind_consume_stack() - Check if an object is on an accessible stack,
  82. * updating stack boundaries so that future unwind steps cannot consume this
  83. * object again.
  84. *
  85. * @state: the current unwind state.
  86. * @sp: the base address of the object.
  87. * @size: the size of the object.
  88. *
  89. * Return: 0 upon success, an error code otherwise.
  90. */
  91. static inline int unwind_consume_stack(struct unwind_state *state,
  92. unsigned long sp,
  93. unsigned long size)
  94. {
  95. struct stack_info *next;
  96. if (stackinfo_on_stack(&state->stack, sp, size))
  97. goto found;
  98. next = unwind_find_next_stack(state, sp, size);
  99. if (!next)
  100. return -EINVAL;
  101. /*
  102. * Stack transitions are strictly one-way, and once we've
  103. * transitioned from one stack to another, it's never valid to
  104. * unwind back to the old stack.
  105. *
  106. * Remove the current stack from the list of stacks so that it cannot
  107. * be found on a subsequent transition.
  108. *
  109. * Note that stacks can nest in several valid orders, e.g.
  110. *
  111. * TASK -> IRQ -> OVERFLOW -> SDEI_NORMAL
  112. * TASK -> SDEI_NORMAL -> SDEI_CRITICAL -> OVERFLOW
  113. * HYP -> OVERFLOW
  114. *
  115. * ... so we do not check the specific order of stack
  116. * transitions.
  117. */
  118. state->stack = *next;
  119. *next = stackinfo_get_unknown();
  120. found:
  121. /*
  122. * Future unwind steps can only consume stack above this frame record.
  123. * Update the current stack to start immediately above it.
  124. */
  125. state->stack.low = sp + size;
  126. return 0;
  127. }
  128. /**
  129. * unwind_next_frame_record() - Unwind to the next frame record.
  130. *
  131. * @state: the current unwind state.
  132. *
  133. * Return: 0 upon success, an error code otherwise.
  134. */
  135. static inline int
  136. unwind_next_frame_record(struct unwind_state *state)
  137. {
  138. unsigned long fp = state->fp;
  139. int err;
  140. if (fp & 0x7)
  141. return -EINVAL;
  142. err = unwind_consume_stack(state, fp, 16);
  143. if (err)
  144. return err;
  145. /*
  146. * Record this frame record's values.
  147. */
  148. state->fp = READ_ONCE(*(unsigned long *)(fp));
  149. state->pc = READ_ONCE(*(unsigned long *)(fp + 8));
  150. return 0;
  151. }
  152. #endif /* __ASM_STACKTRACE_COMMON_H */