GNU Linux-libre 6.1.90-gnu
[releases.git] / arch / loongarch / kernel / unwind_guess.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2022 Loongson Technology Corporation Limited
4  */
5 #include <linux/kernel.h>
6
7 #include <asm/unwind.h>
8
9 unsigned long unwind_get_return_address(struct unwind_state *state)
10 {
11         if (unwind_done(state))
12                 return 0;
13         else if (state->first)
14                 return state->pc;
15
16         return *(unsigned long *)(state->sp);
17 }
18 EXPORT_SYMBOL_GPL(unwind_get_return_address);
19
20 void unwind_start(struct unwind_state *state, struct task_struct *task,
21                     struct pt_regs *regs)
22 {
23         memset(state, 0, sizeof(*state));
24
25         if (regs) {
26                 state->sp = regs->regs[3];
27                 state->pc = regs->csr_era;
28         } else if (task && task != current) {
29                 state->sp = thread_saved_fp(task);
30                 state->pc = thread_saved_ra(task);
31         } else {
32                 state->sp = (unsigned long)__builtin_frame_address(0);
33                 state->pc = (unsigned long)__builtin_return_address(0);
34         }
35
36         state->task = task;
37         state->first = true;
38
39         get_stack_info(state->sp, state->task, &state->stack_info);
40
41         if (!unwind_done(state) && !__kernel_text_address(state->pc))
42                 unwind_next_frame(state);
43 }
44 EXPORT_SYMBOL_GPL(unwind_start);
45
46 bool unwind_next_frame(struct unwind_state *state)
47 {
48         struct stack_info *info = &state->stack_info;
49         unsigned long addr;
50
51         if (unwind_done(state))
52                 return false;
53
54         if (state->first)
55                 state->first = false;
56
57         do {
58                 for (state->sp += sizeof(unsigned long);
59                      state->sp < info->end;
60                      state->sp += sizeof(unsigned long)) {
61                         addr = *(unsigned long *)(state->sp);
62
63                         if (__kernel_text_address(addr))
64                                 return true;
65                 }
66
67                 state->sp = info->next_sp;
68
69         } while (!get_stack_info(state->sp, state->task, info));
70
71         return false;
72 }
73 EXPORT_SYMBOL_GPL(unwind_next_frame);