1 // SPDX-License-Identifier: GPL-2.0
3 * Stack trace management functions
5 * Copyright (C) 2022 Loongson Technology Corporation Limited
7 #include <linux/sched.h>
8 #include <linux/stacktrace.h>
9 #include <linux/uaccess.h>
11 #include <asm/stacktrace.h>
12 #include <asm/unwind.h>
14 void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie,
15 struct task_struct *task, struct pt_regs *regs)
18 struct pt_regs dummyregs;
19 struct unwind_state state;
24 if (task == current) {
25 regs->regs[3] = (unsigned long)__builtin_frame_address(0);
26 regs->csr_era = (unsigned long)__builtin_return_address(0);
28 regs->regs[3] = thread_saved_fp(task);
29 regs->csr_era = thread_saved_ra(task);
35 for (unwind_start(&state, task, regs);
36 !unwind_done(&state); unwind_next_frame(&state)) {
37 addr = unwind_get_return_address(&state);
38 if (!addr || !consume_entry(cookie, addr))
43 int arch_stack_walk_reliable(stack_trace_consume_fn consume_entry,
44 void *cookie, struct task_struct *task)
47 struct pt_regs dummyregs;
48 struct pt_regs *regs = &dummyregs;
49 struct unwind_state state;
51 if (task == current) {
52 regs->regs[3] = (unsigned long)__builtin_frame_address(0);
53 regs->csr_era = (unsigned long)__builtin_return_address(0);
55 regs->regs[3] = thread_saved_fp(task);
56 regs->csr_era = thread_saved_ra(task);
61 for (unwind_start(&state, task, regs);
62 !unwind_done(&state) && !unwind_error(&state); unwind_next_frame(&state)) {
63 addr = unwind_get_return_address(&state);
66 * A NULL or invalid return address probably means there's some
67 * generated code which __kernel_text_address() doesn't know about.
72 if (!consume_entry(cookie, addr))
76 /* Check for stack corruption */
77 if (unwind_error(&state))
84 copy_stack_frame(unsigned long fp, struct stack_frame *frame)
88 unsigned long __user *user_frame_tail;
90 user_frame_tail = (unsigned long *)(fp - sizeof(struct stack_frame));
91 if (!access_ok(user_frame_tail, sizeof(*frame)))
95 err = (__copy_from_user_inatomic(frame, user_frame_tail, sizeof(*frame)));
96 if (err || (unsigned long)user_frame_tail >= frame->fp)
103 void arch_stack_walk_user(stack_trace_consume_fn consume_entry, void *cookie,
104 const struct pt_regs *regs)
106 unsigned long fp = regs->regs[22];
108 while (fp && !((unsigned long)fp & 0xf)) {
109 struct stack_frame frame;
113 if (!copy_stack_frame(fp, &frame))
117 if (!consume_entry(cookie, frame.ra))