Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _ASM_X86_UNWIND_H
0003 #define _ASM_X86_UNWIND_H
0004 
0005 #include <linux/sched.h>
0006 #include <linux/ftrace.h>
0007 #include <linux/rethook.h>
0008 #include <asm/ptrace.h>
0009 #include <asm/stacktrace.h>
0010 
0011 #define IRET_FRAME_OFFSET (offsetof(struct pt_regs, ip))
0012 #define IRET_FRAME_SIZE   (sizeof(struct pt_regs) - IRET_FRAME_OFFSET)
0013 
0014 struct unwind_state {
0015     struct stack_info stack_info;
0016     unsigned long stack_mask;
0017     struct task_struct *task;
0018     int graph_idx;
0019 #if defined(CONFIG_RETHOOK)
0020     struct llist_node *kr_cur;
0021 #endif
0022     bool error;
0023 #if defined(CONFIG_UNWINDER_ORC)
0024     bool signal, full_regs;
0025     unsigned long sp, bp, ip;
0026     struct pt_regs *regs, *prev_regs;
0027 #elif defined(CONFIG_UNWINDER_FRAME_POINTER)
0028     bool got_irq;
0029     unsigned long *bp, *orig_sp, ip;
0030     /*
0031      * If non-NULL: The current frame is incomplete and doesn't contain a
0032      * valid BP. When looking for the next frame, use this instead of the
0033      * non-existent saved BP.
0034      */
0035     unsigned long *next_bp;
0036     struct pt_regs *regs;
0037 #else
0038     unsigned long *sp;
0039 #endif
0040 };
0041 
0042 void __unwind_start(struct unwind_state *state, struct task_struct *task,
0043             struct pt_regs *regs, unsigned long *first_frame);
0044 bool unwind_next_frame(struct unwind_state *state);
0045 unsigned long unwind_get_return_address(struct unwind_state *state);
0046 unsigned long *unwind_get_return_address_ptr(struct unwind_state *state);
0047 
0048 static inline bool unwind_done(struct unwind_state *state)
0049 {
0050     return state->stack_info.type == STACK_TYPE_UNKNOWN;
0051 }
0052 
0053 static inline bool unwind_error(struct unwind_state *state)
0054 {
0055     return state->error;
0056 }
0057 
0058 static inline
0059 void unwind_start(struct unwind_state *state, struct task_struct *task,
0060           struct pt_regs *regs, unsigned long *first_frame)
0061 {
0062     first_frame = first_frame ? : get_stack_pointer(task, regs);
0063 
0064     __unwind_start(state, task, regs, first_frame);
0065 }
0066 
0067 #if defined(CONFIG_UNWINDER_ORC) || defined(CONFIG_UNWINDER_FRAME_POINTER)
0068 /*
0069  * If 'partial' returns true, only the iret frame registers are valid.
0070  */
0071 static inline struct pt_regs *unwind_get_entry_regs(struct unwind_state *state,
0072                             bool *partial)
0073 {
0074     if (unwind_done(state))
0075         return NULL;
0076 
0077     if (partial) {
0078 #ifdef CONFIG_UNWINDER_ORC
0079         *partial = !state->full_regs;
0080 #else
0081         *partial = false;
0082 #endif
0083     }
0084 
0085     return state->regs;
0086 }
0087 #else
0088 static inline struct pt_regs *unwind_get_entry_regs(struct unwind_state *state,
0089                             bool *partial)
0090 {
0091     return NULL;
0092 }
0093 #endif
0094 
0095 #ifdef CONFIG_UNWINDER_ORC
0096 void unwind_init(void);
0097 void unwind_module_init(struct module *mod, void *orc_ip, size_t orc_ip_size,
0098             void *orc, size_t orc_size);
0099 #else
0100 static inline void unwind_init(void) {}
0101 static inline
0102 void unwind_module_init(struct module *mod, void *orc_ip, size_t orc_ip_size,
0103             void *orc, size_t orc_size) {}
0104 #endif
0105 
0106 static inline
0107 unsigned long unwind_recover_rethook(struct unwind_state *state,
0108                      unsigned long addr, unsigned long *addr_p)
0109 {
0110 #ifdef CONFIG_RETHOOK
0111     if (is_rethook_trampoline(addr))
0112         return rethook_find_ret_addr(state->task, (unsigned long)addr_p,
0113                          &state->kr_cur);
0114 #endif
0115     return addr;
0116 }
0117 
0118 /* Recover the return address modified by rethook and ftrace_graph. */
0119 static inline
0120 unsigned long unwind_recover_ret_addr(struct unwind_state *state,
0121                      unsigned long addr, unsigned long *addr_p)
0122 {
0123     unsigned long ret;
0124 
0125     ret = ftrace_graph_ret_addr(state->task, &state->graph_idx,
0126                     addr, addr_p);
0127     return unwind_recover_rethook(state, ret, addr_p);
0128 }
0129 
0130 /*
0131  * This disables KASAN checking when reading a value from another task's stack,
0132  * since the other task could be running on another CPU and could have poisoned
0133  * the stack in the meantime.
0134  */
0135 #define READ_ONCE_TASK_STACK(task, x)           \
0136 ({                          \
0137     unsigned long val;              \
0138     if (task == current)                \
0139         val = READ_ONCE(x);         \
0140     else                        \
0141         val = READ_ONCE_NOCHECK(x);     \
0142     val;                        \
0143 })
0144 
0145 static inline bool task_on_another_cpu(struct task_struct *task)
0146 {
0147 #ifdef CONFIG_SMP
0148     return task != current && task->on_cpu;
0149 #else
0150     return false;
0151 #endif
0152 }
0153 
0154 #endif /* _ASM_X86_UNWIND_H */