common.h 3.6 KB

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