target/cxgbit: Use T6 specific macros to get ETH/IP hdr len
[linux/fpc-iii.git] / arch / x86 / kernel / unwind_frame.c
blob23d15565d02ad780529091e5004557081bb62844
1 #include <linux/sched.h>
2 #include <asm/ptrace.h>
3 #include <asm/bitops.h>
4 #include <asm/stacktrace.h>
5 #include <asm/unwind.h>
7 #define FRAME_HEADER_SIZE (sizeof(long) * 2)
9 /*
10 * This disables KASAN checking when reading a value from another task's stack,
11 * since the other task could be running on another CPU and could have poisoned
12 * the stack in the meantime.
14 #define READ_ONCE_TASK_STACK(task, x) \
15 ({ \
16 unsigned long val; \
17 if (task == current) \
18 val = READ_ONCE(x); \
19 else \
20 val = READ_ONCE_NOCHECK(x); \
21 val; \
24 static void unwind_dump(struct unwind_state *state, unsigned long *sp)
26 static bool dumped_before = false;
27 bool prev_zero, zero = false;
28 unsigned long word;
30 if (dumped_before)
31 return;
33 dumped_before = true;
35 printk_deferred("unwind stack type:%d next_sp:%p mask:%lx graph_idx:%d\n",
36 state->stack_info.type, state->stack_info.next_sp,
37 state->stack_mask, state->graph_idx);
39 for (sp = state->orig_sp; sp < state->stack_info.end; sp++) {
40 word = READ_ONCE_NOCHECK(*sp);
42 prev_zero = zero;
43 zero = word == 0;
45 if (zero) {
46 if (!prev_zero)
47 printk_deferred("%p: %016x ...\n", sp, 0);
48 continue;
51 printk_deferred("%p: %016lx (%pB)\n", sp, word, (void *)word);
55 unsigned long unwind_get_return_address(struct unwind_state *state)
57 unsigned long addr;
58 unsigned long *addr_p = unwind_get_return_address_ptr(state);
60 if (unwind_done(state))
61 return 0;
63 if (state->regs && user_mode(state->regs))
64 return 0;
66 addr = READ_ONCE_TASK_STACK(state->task, *addr_p);
67 addr = ftrace_graph_ret_addr(state->task, &state->graph_idx, addr,
68 addr_p);
70 return __kernel_text_address(addr) ? addr : 0;
72 EXPORT_SYMBOL_GPL(unwind_get_return_address);
74 static size_t regs_size(struct pt_regs *regs)
76 /* x86_32 regs from kernel mode are two words shorter: */
77 if (IS_ENABLED(CONFIG_X86_32) && !user_mode(regs))
78 return sizeof(*regs) - 2*sizeof(long);
80 return sizeof(*regs);
83 static bool is_last_task_frame(struct unwind_state *state)
85 unsigned long bp = (unsigned long)state->bp;
86 unsigned long regs = (unsigned long)task_pt_regs(state->task);
89 * We have to check for the last task frame at two different locations
90 * because gcc can occasionally decide to realign the stack pointer and
91 * change the offset of the stack frame by a word in the prologue of a
92 * function called by head/entry code.
94 return bp == regs - FRAME_HEADER_SIZE ||
95 bp == regs - FRAME_HEADER_SIZE - sizeof(long);
99 * This determines if the frame pointer actually contains an encoded pointer to
100 * pt_regs on the stack. See ENCODE_FRAME_POINTER.
102 static struct pt_regs *decode_frame_pointer(unsigned long *bp)
104 unsigned long regs = (unsigned long)bp;
106 if (!(regs & 0x1))
107 return NULL;
109 return (struct pt_regs *)(regs & ~0x1);
112 static bool update_stack_state(struct unwind_state *state, void *addr,
113 size_t len)
115 struct stack_info *info = &state->stack_info;
116 enum stack_type orig_type = info->type;
119 * If addr isn't on the current stack, switch to the next one.
121 * We may have to traverse multiple stacks to deal with the possibility
122 * that 'info->next_sp' could point to an empty stack and 'addr' could
123 * be on a subsequent stack.
125 while (!on_stack(info, addr, len))
126 if (get_stack_info(info->next_sp, state->task, info,
127 &state->stack_mask))
128 return false;
130 if (!state->orig_sp || info->type != orig_type)
131 state->orig_sp = addr;
133 return true;
136 bool unwind_next_frame(struct unwind_state *state)
138 struct pt_regs *regs;
139 unsigned long *next_bp, *next_frame;
140 size_t next_len;
141 enum stack_type prev_type = state->stack_info.type;
143 if (unwind_done(state))
144 return false;
146 /* have we reached the end? */
147 if (state->regs && user_mode(state->regs))
148 goto the_end;
150 if (is_last_task_frame(state)) {
151 regs = task_pt_regs(state->task);
154 * kthreads (other than the boot CPU's idle thread) have some
155 * partial regs at the end of their stack which were placed
156 * there by copy_thread_tls(). But the regs don't have any
157 * useful information, so we can skip them.
159 * This user_mode() check is slightly broader than a PF_KTHREAD
160 * check because it also catches the awkward situation where a
161 * newly forked kthread transitions into a user task by calling
162 * do_execve(), which eventually clears PF_KTHREAD.
164 if (!user_mode(regs))
165 goto the_end;
168 * We're almost at the end, but not quite: there's still the
169 * syscall regs frame. Entry code doesn't encode the regs
170 * pointer for syscalls, so we have to set it manually.
172 state->regs = regs;
173 state->bp = NULL;
174 return true;
177 /* get the next frame pointer */
178 if (state->regs)
179 next_bp = (unsigned long *)state->regs->bp;
180 else
181 next_bp = (unsigned long *)READ_ONCE_TASK_STACK(state->task,*state->bp);
183 /* is the next frame pointer an encoded pointer to pt_regs? */
184 regs = decode_frame_pointer(next_bp);
185 if (regs) {
186 next_frame = (unsigned long *)regs;
187 next_len = sizeof(*regs);
188 } else {
189 next_frame = next_bp;
190 next_len = FRAME_HEADER_SIZE;
193 /* make sure the next frame's data is accessible */
194 if (!update_stack_state(state, next_frame, next_len)) {
196 * Don't warn on bad regs->bp. An interrupt in entry code
197 * might cause a false positive warning.
199 if (state->regs)
200 goto the_end;
202 goto bad_address;
205 /* Make sure it only unwinds up and doesn't overlap the last frame: */
206 if (state->stack_info.type == prev_type) {
207 if (state->regs && (void *)next_frame < (void *)state->regs + regs_size(state->regs))
208 goto bad_address;
210 if (state->bp && (void *)next_frame < (void *)state->bp + FRAME_HEADER_SIZE)
211 goto bad_address;
214 /* move to the next frame */
215 if (regs) {
216 state->regs = regs;
217 state->bp = NULL;
218 } else {
219 state->bp = next_bp;
220 state->regs = NULL;
223 return true;
225 bad_address:
227 * When unwinding a non-current task, the task might actually be
228 * running on another CPU, in which case it could be modifying its
229 * stack while we're reading it. This is generally not a problem and
230 * can be ignored as long as the caller understands that unwinding
231 * another task will not always succeed.
233 if (state->task != current)
234 goto the_end;
236 if (state->regs) {
237 printk_deferred_once(KERN_WARNING
238 "WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n",
239 state->regs, state->task->comm,
240 state->task->pid, next_frame);
241 unwind_dump(state, (unsigned long *)state->regs);
242 } else {
243 printk_deferred_once(KERN_WARNING
244 "WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n",
245 state->bp, state->task->comm,
246 state->task->pid, next_frame);
247 unwind_dump(state, state->bp);
249 the_end:
250 state->stack_info.type = STACK_TYPE_UNKNOWN;
251 return false;
253 EXPORT_SYMBOL_GPL(unwind_next_frame);
255 void __unwind_start(struct unwind_state *state, struct task_struct *task,
256 struct pt_regs *regs, unsigned long *first_frame)
258 unsigned long *bp, *frame;
259 size_t len;
261 memset(state, 0, sizeof(*state));
262 state->task = task;
264 /* don't even attempt to start from user mode regs */
265 if (regs && user_mode(regs)) {
266 state->stack_info.type = STACK_TYPE_UNKNOWN;
267 return;
270 /* set up the starting stack frame */
271 bp = get_frame_pointer(task, regs);
272 regs = decode_frame_pointer(bp);
273 if (regs) {
274 state->regs = regs;
275 frame = (unsigned long *)regs;
276 len = sizeof(*regs);
277 } else {
278 state->bp = bp;
279 frame = bp;
280 len = FRAME_HEADER_SIZE;
283 /* initialize stack info and make sure the frame data is accessible */
284 get_stack_info(frame, state->task, &state->stack_info,
285 &state->stack_mask);
286 update_stack_state(state, frame, len);
289 * The caller can provide the address of the first frame directly
290 * (first_frame) or indirectly (regs->sp) to indicate which stack frame
291 * to start unwinding at. Skip ahead until we reach it.
293 while (!unwind_done(state) &&
294 (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
295 state->bp < first_frame))
296 unwind_next_frame(state);
298 EXPORT_SYMBOL_GPL(__unwind_start);