unwind_bc.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #include <linux/sched.h>
  3. #include <linux/sched/task.h>
  4. #include <linux/sched/task_stack.h>
  5. #include <linux/interrupt.h>
  6. #include <asm/sections.h>
  7. #include <asm/ptrace.h>
  8. #include <asm/bitops.h>
  9. #include <asm/stacktrace.h>
  10. #include <asm/unwind.h>
  11. unsigned long unwind_get_return_address(struct unwind_state *state)
  12. {
  13. if (unwind_done(state))
  14. return 0;
  15. return __kernel_text_address(state->ip) ? state->ip : 0;
  16. }
  17. EXPORT_SYMBOL_GPL(unwind_get_return_address);
  18. static bool outside_of_stack(struct unwind_state *state, unsigned long sp)
  19. {
  20. return (sp <= state->sp) ||
  21. (sp > state->stack_info.end - sizeof(struct stack_frame));
  22. }
  23. static bool update_stack_info(struct unwind_state *state, unsigned long sp)
  24. {
  25. struct stack_info *info = &state->stack_info;
  26. unsigned long *mask = &state->stack_mask;
  27. /* New stack pointer leaves the current stack */
  28. if (get_stack_info(sp, state->task, info, mask) != 0 ||
  29. !on_stack(info, sp, sizeof(struct stack_frame)))
  30. /* 'sp' does not point to a valid stack */
  31. return false;
  32. return true;
  33. }
  34. static inline bool is_final_pt_regs(struct unwind_state *state,
  35. struct pt_regs *regs)
  36. {
  37. /* user mode or kernel thread pt_regs at the bottom of task stack */
  38. if (task_pt_regs(state->task) == regs)
  39. return true;
  40. /* user mode pt_regs at the bottom of irq stack */
  41. return state->stack_info.type == STACK_TYPE_IRQ &&
  42. state->stack_info.end - sizeof(struct pt_regs) == (unsigned long)regs &&
  43. READ_ONCE_NOCHECK(regs->psw.mask) & PSW_MASK_PSTATE;
  44. }
  45. /* Avoid KMSAN false positives from touching uninitialized frames. */
  46. __no_kmsan_checks
  47. bool unwind_next_frame(struct unwind_state *state)
  48. {
  49. struct stack_info *info = &state->stack_info;
  50. struct stack_frame *sf;
  51. struct pt_regs *regs;
  52. unsigned long sp, ip;
  53. bool reliable;
  54. regs = state->regs;
  55. if (unlikely(regs)) {
  56. sp = state->sp;
  57. sf = (struct stack_frame *) sp;
  58. ip = READ_ONCE_NOCHECK(sf->gprs[8]);
  59. reliable = false;
  60. regs = NULL;
  61. /* skip bogus %r14 or if is the same as regs->psw.addr */
  62. if (!__kernel_text_address(ip) || state->ip == unwind_recover_ret_addr(state, ip)) {
  63. state->regs = NULL;
  64. return unwind_next_frame(state);
  65. }
  66. } else {
  67. sf = (struct stack_frame *) state->sp;
  68. sp = READ_ONCE_NOCHECK(sf->back_chain);
  69. if (likely(sp)) {
  70. /* Non-zero back-chain points to the previous frame */
  71. if (unlikely(outside_of_stack(state, sp))) {
  72. if (!update_stack_info(state, sp))
  73. goto out_err;
  74. }
  75. sf = (struct stack_frame *) sp;
  76. ip = READ_ONCE_NOCHECK(sf->gprs[8]);
  77. reliable = true;
  78. } else {
  79. /* No back-chain, look for a pt_regs structure */
  80. sp = state->sp + STACK_FRAME_OVERHEAD;
  81. if (!on_stack(info, sp, sizeof(struct pt_regs)))
  82. goto out_err;
  83. regs = (struct pt_regs *) sp;
  84. if (is_final_pt_regs(state, regs))
  85. goto out_stop;
  86. ip = READ_ONCE_NOCHECK(regs->psw.addr);
  87. sp = READ_ONCE_NOCHECK(regs->gprs[15]);
  88. if (unlikely(outside_of_stack(state, sp))) {
  89. if (!update_stack_info(state, sp))
  90. goto out_err;
  91. }
  92. reliable = true;
  93. }
  94. }
  95. /* Sanity check: ABI requires SP to be aligned 8 bytes. */
  96. if (sp & 0x7)
  97. goto out_err;
  98. /* Update unwind state */
  99. state->sp = sp;
  100. state->regs = regs;
  101. state->reliable = reliable;
  102. state->ip = unwind_recover_ret_addr(state, ip);
  103. return true;
  104. out_err:
  105. state->error = true;
  106. out_stop:
  107. state->stack_info.type = STACK_TYPE_UNKNOWN;
  108. return false;
  109. }
  110. EXPORT_SYMBOL_GPL(unwind_next_frame);
  111. /* Avoid KMSAN false positives from touching uninitialized frames. */
  112. __no_kmsan_checks
  113. void __unwind_start(struct unwind_state *state, struct task_struct *task,
  114. struct pt_regs *regs, unsigned long first_frame)
  115. {
  116. struct stack_info *info = &state->stack_info;
  117. struct stack_frame *sf;
  118. unsigned long ip, sp;
  119. memset(state, 0, sizeof(*state));
  120. state->task = task;
  121. state->regs = regs;
  122. /* Don't even attempt to start from user mode regs: */
  123. if (regs && user_mode(regs)) {
  124. info->type = STACK_TYPE_UNKNOWN;
  125. return;
  126. }
  127. /* Get the instruction pointer from pt_regs or the stack frame */
  128. if (regs) {
  129. ip = regs->psw.addr;
  130. sp = regs->gprs[15];
  131. } else if (task == current) {
  132. sp = current_frame_address();
  133. } else {
  134. sp = task->thread.ksp;
  135. }
  136. /* Get current stack pointer and initialize stack info */
  137. if (!update_stack_info(state, sp)) {
  138. /* Something is wrong with the stack pointer */
  139. info->type = STACK_TYPE_UNKNOWN;
  140. state->error = true;
  141. return;
  142. }
  143. if (!regs) {
  144. /* Stack frame is within valid stack */
  145. sf = (struct stack_frame *)sp;
  146. ip = READ_ONCE_NOCHECK(sf->gprs[8]);
  147. }
  148. /* Update unwind state */
  149. state->sp = sp;
  150. state->reliable = true;
  151. state->ip = unwind_recover_ret_addr(state, ip);
  152. if (!first_frame)
  153. return;
  154. /* Skip through the call chain to the specified starting frame */
  155. while (!unwind_done(state)) {
  156. if (on_stack(&state->stack_info, first_frame, sizeof(struct stack_frame))) {
  157. if (state->sp >= first_frame)
  158. break;
  159. }
  160. unwind_next_frame(state);
  161. }
  162. }
  163. EXPORT_SYMBOL_GPL(__unwind_start);