xref: /linux/arch/loongarch/kernel/unwind_guess.c (revision 2da68a77)
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 	}
29 
30 	state->task = task;
31 	state->first = true;
32 
33 	get_stack_info(state->sp, state->task, &state->stack_info);
34 
35 	if (!unwind_done(state) && !__kernel_text_address(state->pc))
36 		unwind_next_frame(state);
37 }
38 EXPORT_SYMBOL_GPL(unwind_start);
39 
40 bool unwind_next_frame(struct unwind_state *state)
41 {
42 	struct stack_info *info = &state->stack_info;
43 	unsigned long addr;
44 
45 	if (unwind_done(state))
46 		return false;
47 
48 	if (state->first)
49 		state->first = false;
50 
51 	do {
52 		for (state->sp += sizeof(unsigned long);
53 		     state->sp < info->end;
54 		     state->sp += sizeof(unsigned long)) {
55 			addr = *(unsigned long *)(state->sp);
56 
57 			if (__kernel_text_address(addr))
58 				return true;
59 		}
60 
61 		state->sp = info->next_sp;
62 
63 	} while (!get_stack_info(state->sp, state->task, info));
64 
65 	return false;
66 }
67 EXPORT_SYMBOL_GPL(unwind_next_frame);
68