18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Most of this ideas comes from x86.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2020 Loongson Technology Corporation Limited
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci#ifndef _ASM_UNWIND_H
88c2ecf20Sopenharmony_ci#define _ASM_UNWIND_H
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci#include <linux/ftrace.h>
128c2ecf20Sopenharmony_ci#include <linux/sched.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <asm/ptrace.h>
158c2ecf20Sopenharmony_ci#include <asm/stacktrace.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_cistruct unwind_state {
188c2ecf20Sopenharmony_ci	struct stack_info stack_info;
198c2ecf20Sopenharmony_ci	struct task_struct *task;
208c2ecf20Sopenharmony_ci	int graph_idx;
218c2ecf20Sopenharmony_ci#if defined(CONFIG_UNWINDER_PROLOGUE)
228c2ecf20Sopenharmony_ci	unsigned long sp, pc, ra;
238c2ecf20Sopenharmony_ci	bool enable;
248c2ecf20Sopenharmony_ci	bool first;
258c2ecf20Sopenharmony_ci#elif defined(CONFIG_UNWINDER_ORC)
268c2ecf20Sopenharmony_ci	unsigned long sp, pc, fp, ra;
278c2ecf20Sopenharmony_ci#else /* CONFIG_UNWINDER_GUESS */
288c2ecf20Sopenharmony_ci	unsigned long sp, pc;
298c2ecf20Sopenharmony_ci	bool first;
308c2ecf20Sopenharmony_ci#endif
318c2ecf20Sopenharmony_ci	bool error, is_ftrace;
328c2ecf20Sopenharmony_ci};
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_civoid unwind_start(struct unwind_state *state, struct task_struct *task,
358c2ecf20Sopenharmony_ci		      struct pt_regs *regs);
368c2ecf20Sopenharmony_cibool unwind_next_frame(struct unwind_state *state);
378c2ecf20Sopenharmony_ciunsigned long unwind_get_return_address(struct unwind_state *state);
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistatic inline bool unwind_done(struct unwind_state *state)
408c2ecf20Sopenharmony_ci{
418c2ecf20Sopenharmony_ci	return state->stack_info.type == STACK_TYPE_UNKNOWN;
428c2ecf20Sopenharmony_ci}
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_cistatic inline bool unwind_error(struct unwind_state *state)
458c2ecf20Sopenharmony_ci{
468c2ecf20Sopenharmony_ci	return state->error;
478c2ecf20Sopenharmony_ci}
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci#ifdef CONFIG_UNWINDER_ORC
508c2ecf20Sopenharmony_civoid unwind_init(void);
518c2ecf20Sopenharmony_civoid unwind_module_init(struct module *mod, void *orc_ip, size_t orc_ip_size, void *orc, size_t orc_size);
528c2ecf20Sopenharmony_ci#else
538c2ecf20Sopenharmony_cistatic inline void unwind_init(void) {}
548c2ecf20Sopenharmony_cistatic inline void unwind_module_init(struct module *mod, void *orc_ip, size_t orc_ip_size, void *orc, size_t orc_size) {}
558c2ecf20Sopenharmony_ci#endif /* CONFIG_UNWINDER_ORC */
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci#define GRAPH_FAKE_OFFSET (sizeof(struct pt_regs) - offsetof(struct pt_regs, regs[1]))
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_cistatic inline unsigned long unwind_graph_addr(struct unwind_state *state,
608c2ecf20Sopenharmony_ci					unsigned long pc, unsigned long cfa)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	return ftrace_graph_ret_addr(state->task, &state->graph_idx,
638c2ecf20Sopenharmony_ci				     pc, (unsigned long *)(cfa - GRAPH_FAKE_OFFSET));
648c2ecf20Sopenharmony_ci}
658c2ecf20Sopenharmony_ci#endif /* _ASM_UNWIND_H */
66