1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_UNWIND_H
3 #define _ASM_X86_UNWIND_H
5 #include <linux/sched.h>
6 #include <linux/ftrace.h>
7 #include <linux/rethook.h>
8 #include <asm/ptrace.h>
9 #include <asm/stacktrace.h>
11 #define IRET_FRAME_OFFSET (offsetof(struct pt_regs, ip))
12 #define IRET_FRAME_SIZE (sizeof(struct pt_regs) - IRET_FRAME_OFFSET)
15 struct stack_info stack_info;
16 unsigned long stack_mask;
17 struct task_struct *task;
19 #if defined(CONFIG_RETHOOK)
20 struct llist_node *kr_cur;
23 #if defined(CONFIG_UNWINDER_ORC)
24 bool signal, full_regs;
25 unsigned long sp, bp, ip;
26 struct pt_regs *regs, *prev_regs;
27 #elif defined(CONFIG_UNWINDER_FRAME_POINTER)
29 unsigned long *bp, *orig_sp, ip;
31 * If non-NULL: The current frame is incomplete and doesn't contain a
32 * valid BP. When looking for the next frame, use this instead of the
33 * non-existent saved BP.
35 unsigned long *next_bp;
42 void __unwind_start(struct unwind_state *state, struct task_struct *task,
43 struct pt_regs *regs, unsigned long *first_frame);
44 bool unwind_next_frame(struct unwind_state *state);
45 unsigned long unwind_get_return_address(struct unwind_state *state);
46 unsigned long *unwind_get_return_address_ptr(struct unwind_state *state);
48 static inline bool unwind_done(struct unwind_state *state)
50 return state->stack_info.type == STACK_TYPE_UNKNOWN;
53 static inline bool unwind_error(struct unwind_state *state)
59 void unwind_start(struct unwind_state *state, struct task_struct *task,
60 struct pt_regs *regs, unsigned long *first_frame)
62 first_frame = first_frame ? : get_stack_pointer(task, regs);
64 __unwind_start(state, task, regs, first_frame);
67 #if defined(CONFIG_UNWINDER_ORC) || defined(CONFIG_UNWINDER_FRAME_POINTER)
69 * If 'partial' returns true, only the iret frame registers are valid.
71 static inline struct pt_regs *unwind_get_entry_regs(struct unwind_state *state,
74 if (unwind_done(state))
78 #ifdef CONFIG_UNWINDER_ORC
79 *partial = !state->full_regs;
88 static inline struct pt_regs *unwind_get_entry_regs(struct unwind_state *state,
95 #ifdef CONFIG_UNWINDER_ORC
96 void unwind_init(void);
97 void unwind_module_init(struct module *mod, void *orc_ip, size_t orc_ip_size,
98 void *orc, size_t orc_size);
100 static inline void unwind_init(void) {}
102 void unwind_module_init(struct module *mod, void *orc_ip, size_t orc_ip_size,
103 void *orc, size_t orc_size) {}
107 unsigned long unwind_recover_rethook(struct unwind_state *state,
108 unsigned long addr, unsigned long *addr_p)
110 #ifdef CONFIG_RETHOOK
111 if (is_rethook_trampoline(addr))
112 return rethook_find_ret_addr(state->task, (unsigned long)addr_p,
118 /* Recover the return address modified by rethook and ftrace_graph. */
120 unsigned long unwind_recover_ret_addr(struct unwind_state *state,
121 unsigned long addr, unsigned long *addr_p)
125 ret = ftrace_graph_ret_addr(state->task, &state->graph_idx,
127 return unwind_recover_rethook(state, ret, addr_p);
131 * This disables KASAN checking when reading a value from another task's stack,
132 * since the other task could be running on another CPU and could have poisoned
133 * the stack in the meantime.
135 #define READ_ONCE_TASK_STACK(task, x) \
138 if (task == current) \
139 val = READ_ONCE(x); \
141 val = READ_ONCE_NOCHECK(x); \
145 static inline bool task_on_another_cpu(struct task_struct *task)
148 return task != current && task->on_cpu;
154 #endif /* _ASM_X86_UNWIND_H */