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