^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/ftrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <asm/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <asm/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <asm/stacktrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <asm/unwind.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) unsigned long unwind_get_return_address(struct unwind_state *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) unsigned long addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) if (unwind_done(state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) addr = READ_ONCE_NOCHECK(*state->sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) return ftrace_graph_ret_addr(state->task, &state->graph_idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) addr, state->sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) EXPORT_SYMBOL_GPL(unwind_get_return_address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) unsigned long *unwind_get_return_address_ptr(struct unwind_state *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) bool unwind_next_frame(struct unwind_state *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct stack_info *info = &state->stack_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) if (unwind_done(state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) for (state->sp++; state->sp < info->end; state->sp++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) unsigned long addr = READ_ONCE_NOCHECK(*state->sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (__kernel_text_address(addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) state->sp = PTR_ALIGN(info->next_sp, sizeof(long));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) } while (!get_stack_info(state->sp, state->task, info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) &state->stack_mask));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) EXPORT_SYMBOL_GPL(unwind_next_frame);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) void __unwind_start(struct unwind_state *state, struct task_struct *task,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct pt_regs *regs, unsigned long *first_frame)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) memset(state, 0, sizeof(*state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) state->task = task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) state->sp = PTR_ALIGN(first_frame, sizeof(long));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) get_stack_info(first_frame, state->task, &state->stack_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) &state->stack_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * The caller can provide the address of the first frame directly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * (first_frame) or indirectly (regs->sp) to indicate which stack frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * to start unwinding at. Skip ahead until we reach it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (!unwind_done(state) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) !__kernel_text_address(*first_frame)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) unwind_next_frame(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) EXPORT_SYMBOL_GPL(__unwind_start);