^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #ifndef _ASM_X86_UNWIND_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #define _ASM_X86_UNWIND_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/ftrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <asm/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <asm/stacktrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #define IRET_FRAME_OFFSET (offsetof(struct pt_regs, ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #define IRET_FRAME_SIZE (sizeof(struct pt_regs) - IRET_FRAME_OFFSET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) struct unwind_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) struct stack_info stack_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) unsigned long stack_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) struct task_struct *task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) int graph_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) bool error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #if defined(CONFIG_UNWINDER_ORC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) bool signal, full_regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) unsigned long sp, bp, ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct pt_regs *regs, *prev_regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #elif defined(CONFIG_UNWINDER_FRAME_POINTER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) bool got_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) unsigned long *bp, *orig_sp, ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * If non-NULL: The current frame is incomplete and doesn't contain a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * valid BP. When looking for the next frame, use this instead of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * non-existent saved BP.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) unsigned long *next_bp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct pt_regs *regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) unsigned long *sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) void __unwind_start(struct unwind_state *state, struct task_struct *task,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct pt_regs *regs, unsigned long *first_frame);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) bool unwind_next_frame(struct unwind_state *state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) unsigned long unwind_get_return_address(struct unwind_state *state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) unsigned long *unwind_get_return_address_ptr(struct unwind_state *state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static inline bool unwind_done(struct unwind_state *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return state->stack_info.type == STACK_TYPE_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static inline bool unwind_error(struct unwind_state *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return state->error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) static inline
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) void unwind_start(struct unwind_state *state, struct task_struct *task,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) struct pt_regs *regs, unsigned long *first_frame)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) first_frame = first_frame ? : get_stack_pointer(task, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) __unwind_start(state, task, regs, first_frame);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) #if defined(CONFIG_UNWINDER_ORC) || defined(CONFIG_UNWINDER_FRAME_POINTER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * If 'partial' returns true, only the iret frame registers are valid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static inline struct pt_regs *unwind_get_entry_regs(struct unwind_state *state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) bool *partial)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (unwind_done(state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (partial) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) #ifdef CONFIG_UNWINDER_ORC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) *partial = !state->full_regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) *partial = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return state->regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) static inline struct pt_regs *unwind_get_entry_regs(struct unwind_state *state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) bool *partial)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) #ifdef CONFIG_UNWINDER_ORC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) void unwind_init(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) void unwind_module_init(struct module *mod, void *orc_ip, size_t orc_ip_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) void *orc, size_t orc_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static inline void unwind_init(void) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static inline
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) void unwind_module_init(struct module *mod, void *orc_ip, size_t orc_ip_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) void *orc, size_t orc_size) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * This disables KASAN checking when reading a value from another task's stack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * since the other task could be running on another CPU and could have poisoned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * the stack in the meantime.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) #define READ_ONCE_TASK_STACK(task, x) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) ({ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) unsigned long val; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (task == current) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) val = READ_ONCE(x); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) else \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) val = READ_ONCE_NOCHECK(x); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) val; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) })
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static inline bool task_on_another_cpu(struct task_struct *task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) #ifdef CONFIG_SMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return task != current && task->on_cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) #endif /* _ASM_X86_UNWIND_H */