Josh Poimboeuf | 7c7900f | 2016-09-16 14:18:12 -0500 | [diff] [blame] | 1 | #include <linux/sched.h> |
Ingo Molnar | 2993002 | 2017-02-08 18:51:36 +0100 | [diff] [blame] | 2 | #include <linux/sched/task.h> |
Ingo Molnar | 68db0cf | 2017-02-08 18:51:37 +0100 | [diff] [blame] | 3 | #include <linux/sched/task_stack.h> |
Josh Poimboeuf | a8b7a92 | 2017-04-12 13:47:12 -0500 | [diff] [blame] | 4 | #include <linux/interrupt.h> |
| 5 | #include <asm/sections.h> |
Josh Poimboeuf | 7c7900f | 2016-09-16 14:18:12 -0500 | [diff] [blame] | 6 | #include <asm/ptrace.h> |
| 7 | #include <asm/bitops.h> |
| 8 | #include <asm/stacktrace.h> |
| 9 | #include <asm/unwind.h> |
| 10 | |
| 11 | #define FRAME_HEADER_SIZE (sizeof(long) * 2) |
| 12 | |
Josh Poimboeuf | 8493611 | 2017-01-09 12:00:23 -0600 | [diff] [blame] | 13 | /* |
| 14 | * This disables KASAN checking when reading a value from another task's stack, |
| 15 | * since the other task could be running on another CPU and could have poisoned |
| 16 | * the stack in the meantime. |
| 17 | */ |
| 18 | #define READ_ONCE_TASK_STACK(task, x) \ |
| 19 | ({ \ |
| 20 | unsigned long val; \ |
| 21 | if (task == current) \ |
| 22 | val = READ_ONCE(x); \ |
| 23 | else \ |
| 24 | val = READ_ONCE_NOCHECK(x); \ |
| 25 | val; \ |
| 26 | }) |
| 27 | |
Josh Poimboeuf | aa4f853 | 2017-04-18 08:12:58 -0500 | [diff] [blame] | 28 | static void unwind_dump(struct unwind_state *state) |
Josh Poimboeuf | 8b5e99f | 2016-12-16 10:05:06 -0600 | [diff] [blame] | 29 | { |
| 30 | static bool dumped_before = false; |
| 31 | bool prev_zero, zero = false; |
Josh Poimboeuf | aa4f853 | 2017-04-18 08:12:58 -0500 | [diff] [blame] | 32 | unsigned long word, *sp; |
Josh Poimboeuf | 262fa73 | 2017-04-25 20:48:52 -0500 | [diff] [blame^] | 33 | struct stack_info stack_info = {0}; |
| 34 | unsigned long visit_mask = 0; |
Josh Poimboeuf | 8b5e99f | 2016-12-16 10:05:06 -0600 | [diff] [blame] | 35 | |
| 36 | if (dumped_before) |
| 37 | return; |
| 38 | |
| 39 | dumped_before = true; |
| 40 | |
Josh Poimboeuf | 4ea3d74 | 2017-04-18 08:12:57 -0500 | [diff] [blame] | 41 | printk_deferred("unwind stack type:%d next_sp:%p mask:0x%lx graph_idx:%d\n", |
Josh Poimboeuf | 8b5e99f | 2016-12-16 10:05:06 -0600 | [diff] [blame] | 42 | state->stack_info.type, state->stack_info.next_sp, |
| 43 | state->stack_mask, state->graph_idx); |
| 44 | |
Josh Poimboeuf | 262fa73 | 2017-04-25 20:48:52 -0500 | [diff] [blame^] | 45 | for (sp = state->orig_sp; sp; sp = PTR_ALIGN(stack_info.next_sp, sizeof(long))) { |
| 46 | if (get_stack_info(sp, state->task, &stack_info, &visit_mask)) |
| 47 | break; |
Josh Poimboeuf | 8b5e99f | 2016-12-16 10:05:06 -0600 | [diff] [blame] | 48 | |
Josh Poimboeuf | 262fa73 | 2017-04-25 20:48:52 -0500 | [diff] [blame^] | 49 | for (; sp < stack_info.end; sp++) { |
Josh Poimboeuf | 8b5e99f | 2016-12-16 10:05:06 -0600 | [diff] [blame] | 50 | |
Josh Poimboeuf | 262fa73 | 2017-04-25 20:48:52 -0500 | [diff] [blame^] | 51 | word = READ_ONCE_NOCHECK(*sp); |
| 52 | |
| 53 | prev_zero = zero; |
| 54 | zero = word == 0; |
| 55 | |
| 56 | if (zero) { |
| 57 | if (!prev_zero) |
| 58 | printk_deferred("%p: %0*x ...\n", |
| 59 | sp, BITS_PER_LONG/4, 0); |
| 60 | continue; |
| 61 | } |
| 62 | |
| 63 | printk_deferred("%p: %0*lx (%pB)\n", |
| 64 | sp, BITS_PER_LONG/4, word, (void *)word); |
Josh Poimboeuf | 8b5e99f | 2016-12-16 10:05:06 -0600 | [diff] [blame] | 65 | } |
Josh Poimboeuf | 8b5e99f | 2016-12-16 10:05:06 -0600 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
Josh Poimboeuf | 7c7900f | 2016-09-16 14:18:12 -0500 | [diff] [blame] | 69 | unsigned long unwind_get_return_address(struct unwind_state *state) |
| 70 | { |
Josh Poimboeuf | 7c7900f | 2016-09-16 14:18:12 -0500 | [diff] [blame] | 71 | if (unwind_done(state)) |
| 72 | return 0; |
| 73 | |
Josh Poimboeuf | 6bcdf9d | 2017-04-12 13:47:11 -0500 | [diff] [blame] | 74 | return __kernel_text_address(state->ip) ? state->ip : 0; |
Josh Poimboeuf | 7c7900f | 2016-09-16 14:18:12 -0500 | [diff] [blame] | 75 | } |
| 76 | EXPORT_SYMBOL_GPL(unwind_get_return_address); |
| 77 | |
Josh Poimboeuf | 24d86f5 | 2016-10-27 08:10:58 -0500 | [diff] [blame] | 78 | static size_t regs_size(struct pt_regs *regs) |
| 79 | { |
| 80 | /* x86_32 regs from kernel mode are two words shorter: */ |
| 81 | if (IS_ENABLED(CONFIG_X86_32) && !user_mode(regs)) |
| 82 | return sizeof(*regs) - 2*sizeof(long); |
| 83 | |
| 84 | return sizeof(*regs); |
| 85 | } |
| 86 | |
Josh Poimboeuf | a8b7a92 | 2017-04-12 13:47:12 -0500 | [diff] [blame] | 87 | static bool in_entry_code(unsigned long ip) |
| 88 | { |
| 89 | char *addr = (char *)ip; |
| 90 | |
| 91 | if (addr >= __entry_text_start && addr < __entry_text_end) |
| 92 | return true; |
| 93 | |
| 94 | #if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN) |
| 95 | if (addr >= __irqentry_text_start && addr < __irqentry_text_end) |
| 96 | return true; |
| 97 | #endif |
| 98 | |
| 99 | return false; |
| 100 | } |
| 101 | |
Josh Poimboeuf | b0d50c7 | 2017-04-25 20:48:51 -0500 | [diff] [blame] | 102 | static inline unsigned long *last_frame(struct unwind_state *state) |
| 103 | { |
| 104 | return (unsigned long *)task_pt_regs(state->task) - 2; |
| 105 | } |
| 106 | |
Josh Poimboeuf | 87a6b29 | 2017-03-13 23:27:47 -0500 | [diff] [blame] | 107 | #ifdef CONFIG_X86_32 |
| 108 | #define GCC_REALIGN_WORDS 3 |
| 109 | #else |
| 110 | #define GCC_REALIGN_WORDS 1 |
| 111 | #endif |
| 112 | |
Josh Poimboeuf | b0d50c7 | 2017-04-25 20:48:51 -0500 | [diff] [blame] | 113 | static inline unsigned long *last_aligned_frame(struct unwind_state *state) |
| 114 | { |
| 115 | return last_frame(state) - GCC_REALIGN_WORDS; |
| 116 | } |
| 117 | |
Josh Poimboeuf | acb4608 | 2016-10-20 11:34:41 -0500 | [diff] [blame] | 118 | static bool is_last_task_frame(struct unwind_state *state) |
| 119 | { |
Josh Poimboeuf | b0d50c7 | 2017-04-25 20:48:51 -0500 | [diff] [blame] | 120 | unsigned long *last_bp = last_frame(state); |
| 121 | unsigned long *aligned_bp = last_aligned_frame(state); |
Josh Poimboeuf | acb4608 | 2016-10-20 11:34:41 -0500 | [diff] [blame] | 122 | |
Josh Poimboeuf | 8023e0e | 2016-12-16 10:05:05 -0600 | [diff] [blame] | 123 | /* |
| 124 | * We have to check for the last task frame at two different locations |
| 125 | * because gcc can occasionally decide to realign the stack pointer and |
Josh Poimboeuf | 87a6b29 | 2017-03-13 23:27:47 -0500 | [diff] [blame] | 126 | * change the offset of the stack frame in the prologue of a function |
| 127 | * called by head/entry code. Examples: |
| 128 | * |
| 129 | * <start_secondary>: |
| 130 | * push %edi |
| 131 | * lea 0x8(%esp),%edi |
| 132 | * and $0xfffffff8,%esp |
| 133 | * pushl -0x4(%edi) |
| 134 | * push %ebp |
| 135 | * mov %esp,%ebp |
| 136 | * |
| 137 | * <x86_64_start_kernel>: |
| 138 | * lea 0x8(%rsp),%r10 |
| 139 | * and $0xfffffffffffffff0,%rsp |
| 140 | * pushq -0x8(%r10) |
| 141 | * push %rbp |
| 142 | * mov %rsp,%rbp |
| 143 | * |
| 144 | * Note that after aligning the stack, it pushes a duplicate copy of |
| 145 | * the return address before pushing the frame pointer. |
Josh Poimboeuf | 8023e0e | 2016-12-16 10:05:05 -0600 | [diff] [blame] | 146 | */ |
Josh Poimboeuf | 87a6b29 | 2017-03-13 23:27:47 -0500 | [diff] [blame] | 147 | return (state->bp == last_bp || |
| 148 | (state->bp == aligned_bp && *(aligned_bp+1) == *(last_bp+1))); |
Josh Poimboeuf | acb4608 | 2016-10-20 11:34:41 -0500 | [diff] [blame] | 149 | } |
| 150 | |
Josh Poimboeuf | 946c191 | 2016-10-20 11:34:40 -0500 | [diff] [blame] | 151 | /* |
| 152 | * This determines if the frame pointer actually contains an encoded pointer to |
| 153 | * pt_regs on the stack. See ENCODE_FRAME_POINTER. |
| 154 | */ |
| 155 | static struct pt_regs *decode_frame_pointer(unsigned long *bp) |
| 156 | { |
| 157 | unsigned long regs = (unsigned long)bp; |
| 158 | |
| 159 | if (!(regs & 0x1)) |
| 160 | return NULL; |
| 161 | |
| 162 | return (struct pt_regs *)(regs & ~0x1); |
| 163 | } |
| 164 | |
Josh Poimboeuf | 5ed8d8b | 2017-04-12 13:47:10 -0500 | [diff] [blame] | 165 | static bool update_stack_state(struct unwind_state *state, |
| 166 | unsigned long *next_bp) |
Josh Poimboeuf | 7c7900f | 2016-09-16 14:18:12 -0500 | [diff] [blame] | 167 | { |
| 168 | struct stack_info *info = &state->stack_info; |
Josh Poimboeuf | 5ed8d8b | 2017-04-12 13:47:10 -0500 | [diff] [blame] | 169 | enum stack_type prev_type = info->type; |
| 170 | struct pt_regs *regs; |
Josh Poimboeuf | 6bcdf9d | 2017-04-12 13:47:11 -0500 | [diff] [blame] | 171 | unsigned long *frame, *prev_frame_end, *addr_p, addr; |
Josh Poimboeuf | 5ed8d8b | 2017-04-12 13:47:10 -0500 | [diff] [blame] | 172 | size_t len; |
| 173 | |
| 174 | if (state->regs) |
| 175 | prev_frame_end = (void *)state->regs + regs_size(state->regs); |
| 176 | else |
| 177 | prev_frame_end = (void *)state->bp + FRAME_HEADER_SIZE; |
| 178 | |
| 179 | /* Is the next frame pointer an encoded pointer to pt_regs? */ |
| 180 | regs = decode_frame_pointer(next_bp); |
| 181 | if (regs) { |
| 182 | frame = (unsigned long *)regs; |
| 183 | len = regs_size(regs); |
Josh Poimboeuf | a8b7a92 | 2017-04-12 13:47:12 -0500 | [diff] [blame] | 184 | state->got_irq = true; |
Josh Poimboeuf | 5ed8d8b | 2017-04-12 13:47:10 -0500 | [diff] [blame] | 185 | } else { |
| 186 | frame = next_bp; |
| 187 | len = FRAME_HEADER_SIZE; |
| 188 | } |
Josh Poimboeuf | 7c7900f | 2016-09-16 14:18:12 -0500 | [diff] [blame] | 189 | |
| 190 | /* |
Josh Poimboeuf | 5ed8d8b | 2017-04-12 13:47:10 -0500 | [diff] [blame] | 191 | * If the next bp isn't on the current stack, switch to the next one. |
Josh Poimboeuf | 7c7900f | 2016-09-16 14:18:12 -0500 | [diff] [blame] | 192 | * |
| 193 | * We may have to traverse multiple stacks to deal with the possibility |
Josh Poimboeuf | 5ed8d8b | 2017-04-12 13:47:10 -0500 | [diff] [blame] | 194 | * that info->next_sp could point to an empty stack and the next bp |
| 195 | * could be on a subsequent stack. |
Josh Poimboeuf | 7c7900f | 2016-09-16 14:18:12 -0500 | [diff] [blame] | 196 | */ |
Josh Poimboeuf | 5ed8d8b | 2017-04-12 13:47:10 -0500 | [diff] [blame] | 197 | while (!on_stack(info, frame, len)) |
Josh Poimboeuf | 7c7900f | 2016-09-16 14:18:12 -0500 | [diff] [blame] | 198 | if (get_stack_info(info->next_sp, state->task, info, |
| 199 | &state->stack_mask)) |
| 200 | return false; |
| 201 | |
Josh Poimboeuf | 5ed8d8b | 2017-04-12 13:47:10 -0500 | [diff] [blame] | 202 | /* Make sure it only unwinds up and doesn't overlap the prev frame: */ |
| 203 | if (state->orig_sp && state->stack_info.type == prev_type && |
| 204 | frame < prev_frame_end) |
| 205 | return false; |
| 206 | |
| 207 | /* Move state to the next frame: */ |
| 208 | if (regs) { |
| 209 | state->regs = regs; |
| 210 | state->bp = NULL; |
| 211 | } else { |
| 212 | state->bp = next_bp; |
| 213 | state->regs = NULL; |
| 214 | } |
| 215 | |
Josh Poimboeuf | 6bcdf9d | 2017-04-12 13:47:11 -0500 | [diff] [blame] | 216 | /* Save the return address: */ |
| 217 | if (state->regs && user_mode(state->regs)) |
| 218 | state->ip = 0; |
| 219 | else { |
| 220 | addr_p = unwind_get_return_address_ptr(state); |
| 221 | addr = READ_ONCE_TASK_STACK(state->task, *addr_p); |
| 222 | state->ip = ftrace_graph_ret_addr(state->task, &state->graph_idx, |
| 223 | addr, addr_p); |
| 224 | } |
| 225 | |
Josh Poimboeuf | 5ed8d8b | 2017-04-12 13:47:10 -0500 | [diff] [blame] | 226 | /* Save the original stack pointer for unwind_dump(): */ |
Josh Poimboeuf | 262fa73 | 2017-04-25 20:48:52 -0500 | [diff] [blame^] | 227 | if (!state->orig_sp) |
Josh Poimboeuf | 5ed8d8b | 2017-04-12 13:47:10 -0500 | [diff] [blame] | 228 | state->orig_sp = frame; |
Josh Poimboeuf | 8b5e99f | 2016-12-16 10:05:06 -0600 | [diff] [blame] | 229 | |
Josh Poimboeuf | 7c7900f | 2016-09-16 14:18:12 -0500 | [diff] [blame] | 230 | return true; |
| 231 | } |
| 232 | |
| 233 | bool unwind_next_frame(struct unwind_state *state) |
| 234 | { |
Josh Poimboeuf | 946c191 | 2016-10-20 11:34:40 -0500 | [diff] [blame] | 235 | struct pt_regs *regs; |
Josh Poimboeuf | 5ed8d8b | 2017-04-12 13:47:10 -0500 | [diff] [blame] | 236 | unsigned long *next_bp; |
Josh Poimboeuf | 7c7900f | 2016-09-16 14:18:12 -0500 | [diff] [blame] | 237 | |
| 238 | if (unwind_done(state)) |
| 239 | return false; |
| 240 | |
Josh Poimboeuf | 5ed8d8b | 2017-04-12 13:47:10 -0500 | [diff] [blame] | 241 | /* Have we reached the end? */ |
Josh Poimboeuf | 946c191 | 2016-10-20 11:34:40 -0500 | [diff] [blame] | 242 | if (state->regs && user_mode(state->regs)) |
| 243 | goto the_end; |
| 244 | |
Josh Poimboeuf | acb4608 | 2016-10-20 11:34:41 -0500 | [diff] [blame] | 245 | if (is_last_task_frame(state)) { |
| 246 | regs = task_pt_regs(state->task); |
| 247 | |
| 248 | /* |
| 249 | * kthreads (other than the boot CPU's idle thread) have some |
| 250 | * partial regs at the end of their stack which were placed |
| 251 | * there by copy_thread_tls(). But the regs don't have any |
| 252 | * useful information, so we can skip them. |
| 253 | * |
| 254 | * This user_mode() check is slightly broader than a PF_KTHREAD |
| 255 | * check because it also catches the awkward situation where a |
| 256 | * newly forked kthread transitions into a user task by calling |
| 257 | * do_execve(), which eventually clears PF_KTHREAD. |
| 258 | */ |
| 259 | if (!user_mode(regs)) |
| 260 | goto the_end; |
| 261 | |
| 262 | /* |
| 263 | * We're almost at the end, but not quite: there's still the |
| 264 | * syscall regs frame. Entry code doesn't encode the regs |
| 265 | * pointer for syscalls, so we have to set it manually. |
| 266 | */ |
| 267 | state->regs = regs; |
| 268 | state->bp = NULL; |
Josh Poimboeuf | 6bcdf9d | 2017-04-12 13:47:11 -0500 | [diff] [blame] | 269 | state->ip = 0; |
Josh Poimboeuf | acb4608 | 2016-10-20 11:34:41 -0500 | [diff] [blame] | 270 | return true; |
| 271 | } |
| 272 | |
Josh Poimboeuf | 5ed8d8b | 2017-04-12 13:47:10 -0500 | [diff] [blame] | 273 | /* Get the next frame pointer: */ |
Josh Poimboeuf | 946c191 | 2016-10-20 11:34:40 -0500 | [diff] [blame] | 274 | if (state->regs) |
| 275 | next_bp = (unsigned long *)state->regs->bp; |
| 276 | else |
Josh Poimboeuf | 5ed8d8b | 2017-04-12 13:47:10 -0500 | [diff] [blame] | 277 | next_bp = (unsigned long *)READ_ONCE_TASK_STACK(state->task, *state->bp); |
Josh Poimboeuf | 946c191 | 2016-10-20 11:34:40 -0500 | [diff] [blame] | 278 | |
Josh Poimboeuf | 5ed8d8b | 2017-04-12 13:47:10 -0500 | [diff] [blame] | 279 | /* Move to the next frame if it's safe: */ |
Josh Poimboeuf | a8b7a92 | 2017-04-12 13:47:12 -0500 | [diff] [blame] | 280 | if (!update_stack_state(state, next_bp)) |
Josh Poimboeuf | c32c47c | 2016-10-26 10:41:48 -0500 | [diff] [blame] | 281 | goto bad_address; |
Josh Poimboeuf | c32c47c | 2016-10-26 10:41:48 -0500 | [diff] [blame] | 282 | |
Josh Poimboeuf | 7c7900f | 2016-09-16 14:18:12 -0500 | [diff] [blame] | 283 | return true; |
Josh Poimboeuf | 946c191 | 2016-10-20 11:34:40 -0500 | [diff] [blame] | 284 | |
Josh Poimboeuf | c32c47c | 2016-10-26 10:41:48 -0500 | [diff] [blame] | 285 | bad_address: |
Josh Poimboeuf | 900742d | 2017-01-09 12:00:22 -0600 | [diff] [blame] | 286 | /* |
| 287 | * When unwinding a non-current task, the task might actually be |
| 288 | * running on another CPU, in which case it could be modifying its |
| 289 | * stack while we're reading it. This is generally not a problem and |
| 290 | * can be ignored as long as the caller understands that unwinding |
| 291 | * another task will not always succeed. |
| 292 | */ |
| 293 | if (state->task != current) |
| 294 | goto the_end; |
| 295 | |
Josh Poimboeuf | a8b7a92 | 2017-04-12 13:47:12 -0500 | [diff] [blame] | 296 | /* |
| 297 | * Don't warn if the unwinder got lost due to an interrupt in entry |
Josh Poimboeuf | b0d50c7 | 2017-04-25 20:48:51 -0500 | [diff] [blame] | 298 | * code or in the C handler before the first frame pointer got set up: |
Josh Poimboeuf | a8b7a92 | 2017-04-12 13:47:12 -0500 | [diff] [blame] | 299 | */ |
| 300 | if (state->got_irq && in_entry_code(state->ip)) |
| 301 | goto the_end; |
Josh Poimboeuf | b0d50c7 | 2017-04-25 20:48:51 -0500 | [diff] [blame] | 302 | if (state->regs && |
| 303 | state->regs->sp >= (unsigned long)last_aligned_frame(state) && |
| 304 | state->regs->sp < (unsigned long)task_pt_regs(state->task)) |
| 305 | goto the_end; |
Josh Poimboeuf | a8b7a92 | 2017-04-12 13:47:12 -0500 | [diff] [blame] | 306 | |
Josh Poimboeuf | 24d86f5 | 2016-10-27 08:10:58 -0500 | [diff] [blame] | 307 | if (state->regs) { |
| 308 | printk_deferred_once(KERN_WARNING |
| 309 | "WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n", |
| 310 | state->regs, state->task->comm, |
Josh Poimboeuf | 5ed8d8b | 2017-04-12 13:47:10 -0500 | [diff] [blame] | 311 | state->task->pid, next_bp); |
Josh Poimboeuf | aa4f853 | 2017-04-18 08:12:58 -0500 | [diff] [blame] | 312 | unwind_dump(state); |
Josh Poimboeuf | 24d86f5 | 2016-10-27 08:10:58 -0500 | [diff] [blame] | 313 | } else { |
| 314 | printk_deferred_once(KERN_WARNING |
| 315 | "WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n", |
| 316 | state->bp, state->task->comm, |
Josh Poimboeuf | 5ed8d8b | 2017-04-12 13:47:10 -0500 | [diff] [blame] | 317 | state->task->pid, next_bp); |
Josh Poimboeuf | aa4f853 | 2017-04-18 08:12:58 -0500 | [diff] [blame] | 318 | unwind_dump(state); |
Josh Poimboeuf | 24d86f5 | 2016-10-27 08:10:58 -0500 | [diff] [blame] | 319 | } |
Josh Poimboeuf | 946c191 | 2016-10-20 11:34:40 -0500 | [diff] [blame] | 320 | the_end: |
| 321 | state->stack_info.type = STACK_TYPE_UNKNOWN; |
| 322 | return false; |
Josh Poimboeuf | 7c7900f | 2016-09-16 14:18:12 -0500 | [diff] [blame] | 323 | } |
| 324 | EXPORT_SYMBOL_GPL(unwind_next_frame); |
| 325 | |
| 326 | void __unwind_start(struct unwind_state *state, struct task_struct *task, |
| 327 | struct pt_regs *regs, unsigned long *first_frame) |
| 328 | { |
Josh Poimboeuf | 5ed8d8b | 2017-04-12 13:47:10 -0500 | [diff] [blame] | 329 | unsigned long *bp; |
Josh Poimboeuf | 946c191 | 2016-10-20 11:34:40 -0500 | [diff] [blame] | 330 | |
Josh Poimboeuf | 7c7900f | 2016-09-16 14:18:12 -0500 | [diff] [blame] | 331 | memset(state, 0, sizeof(*state)); |
| 332 | state->task = task; |
Josh Poimboeuf | a8b7a92 | 2017-04-12 13:47:12 -0500 | [diff] [blame] | 333 | state->got_irq = (regs); |
Josh Poimboeuf | 7c7900f | 2016-09-16 14:18:12 -0500 | [diff] [blame] | 334 | |
Josh Poimboeuf | 5ed8d8b | 2017-04-12 13:47:10 -0500 | [diff] [blame] | 335 | /* Don't even attempt to start from user mode regs: */ |
Josh Poimboeuf | 7c7900f | 2016-09-16 14:18:12 -0500 | [diff] [blame] | 336 | if (regs && user_mode(regs)) { |
| 337 | state->stack_info.type = STACK_TYPE_UNKNOWN; |
| 338 | return; |
| 339 | } |
| 340 | |
Josh Poimboeuf | 946c191 | 2016-10-20 11:34:40 -0500 | [diff] [blame] | 341 | bp = get_frame_pointer(task, regs); |
Josh Poimboeuf | 7c7900f | 2016-09-16 14:18:12 -0500 | [diff] [blame] | 342 | |
Josh Poimboeuf | 5ed8d8b | 2017-04-12 13:47:10 -0500 | [diff] [blame] | 343 | /* Initialize stack info and make sure the frame data is accessible: */ |
| 344 | get_stack_info(bp, state->task, &state->stack_info, |
Josh Poimboeuf | 7c7900f | 2016-09-16 14:18:12 -0500 | [diff] [blame] | 345 | &state->stack_mask); |
Josh Poimboeuf | 5ed8d8b | 2017-04-12 13:47:10 -0500 | [diff] [blame] | 346 | update_stack_state(state, bp); |
Josh Poimboeuf | 7c7900f | 2016-09-16 14:18:12 -0500 | [diff] [blame] | 347 | |
| 348 | /* |
| 349 | * The caller can provide the address of the first frame directly |
| 350 | * (first_frame) or indirectly (regs->sp) to indicate which stack frame |
| 351 | * to start unwinding at. Skip ahead until we reach it. |
| 352 | */ |
| 353 | while (!unwind_done(state) && |
| 354 | (!on_stack(&state->stack_info, first_frame, sizeof(long)) || |
| 355 | state->bp < first_frame)) |
| 356 | unwind_next_frame(state); |
| 357 | } |
| 358 | EXPORT_SYMBOL_GPL(__unwind_start); |