Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 1 | #include <linux/module.h> |
| 2 | #include <linux/sort.h> |
| 3 | #include <asm/ptrace.h> |
| 4 | #include <asm/stacktrace.h> |
| 5 | #include <asm/unwind.h> |
| 6 | #include <asm/orc_types.h> |
| 7 | #include <asm/orc_lookup.h> |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 8 | |
| 9 | #define orc_warn(fmt, ...) \ |
| 10 | printk_deferred_once(KERN_WARNING pr_fmt("WARNING: " fmt), ##__VA_ARGS__) |
| 11 | |
| 12 | extern int __start_orc_unwind_ip[]; |
| 13 | extern int __stop_orc_unwind_ip[]; |
| 14 | extern struct orc_entry __start_orc_unwind[]; |
| 15 | extern struct orc_entry __stop_orc_unwind[]; |
| 16 | |
| 17 | static DEFINE_MUTEX(sort_mutex); |
| 18 | int *cur_orc_ip_table = __start_orc_unwind_ip; |
| 19 | struct orc_entry *cur_orc_table = __start_orc_unwind; |
| 20 | |
| 21 | unsigned int lookup_num_blocks; |
| 22 | bool orc_init; |
| 23 | |
| 24 | static inline unsigned long orc_ip(const int *ip) |
| 25 | { |
| 26 | return (unsigned long)ip + *ip; |
| 27 | } |
| 28 | |
| 29 | static struct orc_entry *__orc_find(int *ip_table, struct orc_entry *u_table, |
| 30 | unsigned int num_entries, unsigned long ip) |
| 31 | { |
| 32 | int *first = ip_table; |
| 33 | int *last = ip_table + num_entries - 1; |
| 34 | int *mid = first, *found = first; |
| 35 | |
| 36 | if (!num_entries) |
| 37 | return NULL; |
| 38 | |
| 39 | /* |
| 40 | * Do a binary range search to find the rightmost duplicate of a given |
| 41 | * starting address. Some entries are section terminators which are |
| 42 | * "weak" entries for ensuring there are no gaps. They should be |
| 43 | * ignored when they conflict with a real entry. |
| 44 | */ |
| 45 | while (first <= last) { |
| 46 | mid = first + ((last - first) / 2); |
| 47 | |
| 48 | if (orc_ip(mid) <= ip) { |
| 49 | found = mid; |
| 50 | first = mid + 1; |
| 51 | } else |
| 52 | last = mid - 1; |
| 53 | } |
| 54 | |
| 55 | return u_table + (found - ip_table); |
| 56 | } |
| 57 | |
| 58 | #ifdef CONFIG_MODULES |
| 59 | static struct orc_entry *orc_module_find(unsigned long ip) |
| 60 | { |
| 61 | struct module *mod; |
| 62 | |
| 63 | mod = __module_address(ip); |
| 64 | if (!mod || !mod->arch.orc_unwind || !mod->arch.orc_unwind_ip) |
| 65 | return NULL; |
| 66 | return __orc_find(mod->arch.orc_unwind_ip, mod->arch.orc_unwind, |
| 67 | mod->arch.num_orcs, ip); |
| 68 | } |
| 69 | #else |
| 70 | static struct orc_entry *orc_module_find(unsigned long ip) |
| 71 | { |
| 72 | return NULL; |
| 73 | } |
| 74 | #endif |
| 75 | |
Steven Rostedt (VMware) | 6be7fa3 | 2018-01-22 22:32:51 -0500 | [diff] [blame] | 76 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 77 | static struct orc_entry *orc_find(unsigned long ip); |
| 78 | |
| 79 | /* |
| 80 | * Ftrace dynamic trampolines do not have orc entries of their own. |
| 81 | * But they are copies of the ftrace entries that are static and |
| 82 | * defined in ftrace_*.S, which do have orc entries. |
| 83 | * |
| 84 | * If the undwinder comes across a ftrace trampoline, then find the |
| 85 | * ftrace function that was used to create it, and use that ftrace |
| 86 | * function's orc entrie, as the placement of the return code in |
| 87 | * the stack will be identical. |
| 88 | */ |
| 89 | static struct orc_entry *orc_ftrace_find(unsigned long ip) |
| 90 | { |
| 91 | struct ftrace_ops *ops; |
| 92 | unsigned long caller; |
| 93 | |
| 94 | ops = ftrace_ops_trampoline(ip); |
| 95 | if (!ops) |
| 96 | return NULL; |
| 97 | |
| 98 | if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) |
| 99 | caller = (unsigned long)ftrace_regs_call; |
| 100 | else |
| 101 | caller = (unsigned long)ftrace_call; |
| 102 | |
| 103 | /* Prevent unlikely recursion */ |
| 104 | if (ip == caller) |
| 105 | return NULL; |
| 106 | |
| 107 | return orc_find(caller); |
| 108 | } |
| 109 | #else |
| 110 | static struct orc_entry *orc_ftrace_find(unsigned long ip) |
| 111 | { |
| 112 | return NULL; |
| 113 | } |
| 114 | #endif |
| 115 | |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 116 | static struct orc_entry *orc_find(unsigned long ip) |
| 117 | { |
Steven Rostedt (VMware) | 6be7fa3 | 2018-01-22 22:32:51 -0500 | [diff] [blame] | 118 | static struct orc_entry *orc; |
| 119 | |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 120 | if (!orc_init) |
| 121 | return NULL; |
| 122 | |
| 123 | /* For non-init vmlinux addresses, use the fast lookup table: */ |
| 124 | if (ip >= LOOKUP_START_IP && ip < LOOKUP_STOP_IP) { |
| 125 | unsigned int idx, start, stop; |
| 126 | |
| 127 | idx = (ip - LOOKUP_START_IP) / LOOKUP_BLOCK_SIZE; |
| 128 | |
| 129 | if (unlikely((idx >= lookup_num_blocks-1))) { |
Josh Poimboeuf | 58c3862 | 2017-10-20 11:21:34 -0500 | [diff] [blame] | 130 | orc_warn("WARNING: bad lookup idx: idx=%u num=%u ip=%pB\n", |
| 131 | idx, lookup_num_blocks, (void *)ip); |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 132 | return NULL; |
| 133 | } |
| 134 | |
| 135 | start = orc_lookup[idx]; |
| 136 | stop = orc_lookup[idx + 1] + 1; |
| 137 | |
| 138 | if (unlikely((__start_orc_unwind + start >= __stop_orc_unwind) || |
| 139 | (__start_orc_unwind + stop > __stop_orc_unwind))) { |
Josh Poimboeuf | 58c3862 | 2017-10-20 11:21:34 -0500 | [diff] [blame] | 140 | orc_warn("WARNING: bad lookup value: idx=%u num=%u start=%u stop=%u ip=%pB\n", |
| 141 | idx, lookup_num_blocks, start, stop, (void *)ip); |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 142 | return NULL; |
| 143 | } |
| 144 | |
| 145 | return __orc_find(__start_orc_unwind_ip + start, |
| 146 | __start_orc_unwind + start, stop - start, ip); |
| 147 | } |
| 148 | |
| 149 | /* vmlinux .init slow lookup: */ |
Josh Poimboeuf | 9fbcc57 | 2018-02-20 11:37:53 -0600 | [diff] [blame] | 150 | if (init_kernel_text(ip)) |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 151 | return __orc_find(__start_orc_unwind_ip, __start_orc_unwind, |
| 152 | __stop_orc_unwind_ip - __start_orc_unwind_ip, ip); |
| 153 | |
| 154 | /* Module lookup: */ |
Steven Rostedt (VMware) | 6be7fa3 | 2018-01-22 22:32:51 -0500 | [diff] [blame] | 155 | orc = orc_module_find(ip); |
| 156 | if (orc) |
| 157 | return orc; |
| 158 | |
| 159 | return orc_ftrace_find(ip); |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static void orc_sort_swap(void *_a, void *_b, int size) |
| 163 | { |
| 164 | struct orc_entry *orc_a, *orc_b; |
| 165 | struct orc_entry orc_tmp; |
| 166 | int *a = _a, *b = _b, tmp; |
| 167 | int delta = _b - _a; |
| 168 | |
| 169 | /* Swap the .orc_unwind_ip entries: */ |
| 170 | tmp = *a; |
| 171 | *a = *b + delta; |
| 172 | *b = tmp - delta; |
| 173 | |
| 174 | /* Swap the corresponding .orc_unwind entries: */ |
| 175 | orc_a = cur_orc_table + (a - cur_orc_ip_table); |
| 176 | orc_b = cur_orc_table + (b - cur_orc_ip_table); |
| 177 | orc_tmp = *orc_a; |
| 178 | *orc_a = *orc_b; |
| 179 | *orc_b = orc_tmp; |
| 180 | } |
| 181 | |
| 182 | static int orc_sort_cmp(const void *_a, const void *_b) |
| 183 | { |
| 184 | struct orc_entry *orc_a; |
| 185 | const int *a = _a, *b = _b; |
| 186 | unsigned long a_val = orc_ip(a); |
| 187 | unsigned long b_val = orc_ip(b); |
| 188 | |
| 189 | if (a_val > b_val) |
| 190 | return 1; |
| 191 | if (a_val < b_val) |
| 192 | return -1; |
| 193 | |
| 194 | /* |
| 195 | * The "weak" section terminator entries need to always be on the left |
| 196 | * to ensure the lookup code skips them in favor of real entries. |
| 197 | * These terminator entries exist to handle any gaps created by |
| 198 | * whitelisted .o files which didn't get objtool generation. |
| 199 | */ |
| 200 | orc_a = cur_orc_table + (a - cur_orc_ip_table); |
Josh Poimboeuf | d31a580 | 2018-05-18 08:47:12 +0200 | [diff] [blame] | 201 | return orc_a->sp_reg == ORC_REG_UNDEFINED && !orc_a->end ? -1 : 1; |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | #ifdef CONFIG_MODULES |
| 205 | void unwind_module_init(struct module *mod, void *_orc_ip, size_t orc_ip_size, |
| 206 | void *_orc, size_t orc_size) |
| 207 | { |
| 208 | int *orc_ip = _orc_ip; |
| 209 | struct orc_entry *orc = _orc; |
| 210 | unsigned int num_entries = orc_ip_size / sizeof(int); |
| 211 | |
| 212 | WARN_ON_ONCE(orc_ip_size % sizeof(int) != 0 || |
| 213 | orc_size % sizeof(*orc) != 0 || |
| 214 | num_entries != orc_size / sizeof(*orc)); |
| 215 | |
| 216 | /* |
| 217 | * The 'cur_orc_*' globals allow the orc_sort_swap() callback to |
| 218 | * associate an .orc_unwind_ip table entry with its corresponding |
| 219 | * .orc_unwind entry so they can both be swapped. |
| 220 | */ |
| 221 | mutex_lock(&sort_mutex); |
| 222 | cur_orc_ip_table = orc_ip; |
| 223 | cur_orc_table = orc; |
| 224 | sort(orc_ip, num_entries, sizeof(int), orc_sort_cmp, orc_sort_swap); |
| 225 | mutex_unlock(&sort_mutex); |
| 226 | |
| 227 | mod->arch.orc_unwind_ip = orc_ip; |
| 228 | mod->arch.orc_unwind = orc; |
| 229 | mod->arch.num_orcs = num_entries; |
| 230 | } |
| 231 | #endif |
| 232 | |
| 233 | void __init unwind_init(void) |
| 234 | { |
| 235 | size_t orc_ip_size = (void *)__stop_orc_unwind_ip - (void *)__start_orc_unwind_ip; |
| 236 | size_t orc_size = (void *)__stop_orc_unwind - (void *)__start_orc_unwind; |
| 237 | size_t num_entries = orc_ip_size / sizeof(int); |
| 238 | struct orc_entry *orc; |
| 239 | int i; |
| 240 | |
| 241 | if (!num_entries || orc_ip_size % sizeof(int) != 0 || |
| 242 | orc_size % sizeof(struct orc_entry) != 0 || |
| 243 | num_entries != orc_size / sizeof(struct orc_entry)) { |
| 244 | orc_warn("WARNING: Bad or missing .orc_unwind table. Disabling unwinder.\n"); |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | /* Sort the .orc_unwind and .orc_unwind_ip tables: */ |
| 249 | sort(__start_orc_unwind_ip, num_entries, sizeof(int), orc_sort_cmp, |
| 250 | orc_sort_swap); |
| 251 | |
| 252 | /* Initialize the fast lookup table: */ |
| 253 | lookup_num_blocks = orc_lookup_end - orc_lookup; |
| 254 | for (i = 0; i < lookup_num_blocks-1; i++) { |
| 255 | orc = __orc_find(__start_orc_unwind_ip, __start_orc_unwind, |
| 256 | num_entries, |
| 257 | LOOKUP_START_IP + (LOOKUP_BLOCK_SIZE * i)); |
| 258 | if (!orc) { |
| 259 | orc_warn("WARNING: Corrupt .orc_unwind table. Disabling unwinder.\n"); |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | orc_lookup[i] = orc - __start_orc_unwind; |
| 264 | } |
| 265 | |
| 266 | /* Initialize the ending block: */ |
| 267 | orc = __orc_find(__start_orc_unwind_ip, __start_orc_unwind, num_entries, |
| 268 | LOOKUP_STOP_IP); |
| 269 | if (!orc) { |
| 270 | orc_warn("WARNING: Corrupt .orc_unwind table. Disabling unwinder.\n"); |
| 271 | return; |
| 272 | } |
| 273 | orc_lookup[lookup_num_blocks-1] = orc - __start_orc_unwind; |
| 274 | |
| 275 | orc_init = true; |
| 276 | } |
| 277 | |
| 278 | unsigned long unwind_get_return_address(struct unwind_state *state) |
| 279 | { |
| 280 | if (unwind_done(state)) |
| 281 | return 0; |
| 282 | |
| 283 | return __kernel_text_address(state->ip) ? state->ip : 0; |
| 284 | } |
| 285 | EXPORT_SYMBOL_GPL(unwind_get_return_address); |
| 286 | |
| 287 | unsigned long *unwind_get_return_address_ptr(struct unwind_state *state) |
| 288 | { |
| 289 | if (unwind_done(state)) |
| 290 | return NULL; |
| 291 | |
| 292 | if (state->regs) |
| 293 | return &state->regs->ip; |
| 294 | |
| 295 | if (state->sp) |
| 296 | return (unsigned long *)state->sp - 1; |
| 297 | |
| 298 | return NULL; |
| 299 | } |
| 300 | |
Josh Poimboeuf | b02fcf9 | 2017-12-04 15:07:09 +0100 | [diff] [blame] | 301 | static bool stack_access_ok(struct unwind_state *state, unsigned long _addr, |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 302 | size_t len) |
| 303 | { |
| 304 | struct stack_info *info = &state->stack_info; |
Josh Poimboeuf | b02fcf9 | 2017-12-04 15:07:09 +0100 | [diff] [blame] | 305 | void *addr = (void *)_addr; |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 306 | |
Josh Poimboeuf | b02fcf9 | 2017-12-04 15:07:09 +0100 | [diff] [blame] | 307 | if (!on_stack(info, addr, len) && |
| 308 | (get_stack_info(addr, state->task, info, &state->stack_mask))) |
| 309 | return false; |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 310 | |
| 311 | return true; |
| 312 | } |
| 313 | |
| 314 | static bool deref_stack_reg(struct unwind_state *state, unsigned long addr, |
| 315 | unsigned long *val) |
| 316 | { |
| 317 | if (!stack_access_ok(state, addr, sizeof(long))) |
| 318 | return false; |
| 319 | |
Josh Poimboeuf | 881125b | 2017-11-07 20:19:34 -0600 | [diff] [blame] | 320 | *val = READ_ONCE_NOCHECK(*(unsigned long *)addr); |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 321 | return true; |
| 322 | } |
| 323 | |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 324 | static bool deref_stack_regs(struct unwind_state *state, unsigned long addr, |
Josh Poimboeuf | b02fcf9 | 2017-12-04 15:07:09 +0100 | [diff] [blame] | 325 | unsigned long *ip, unsigned long *sp) |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 326 | { |
Josh Poimboeuf | b02fcf9 | 2017-12-04 15:07:09 +0100 | [diff] [blame] | 327 | struct pt_regs *regs = (struct pt_regs *)addr; |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 328 | |
Josh Poimboeuf | b02fcf9 | 2017-12-04 15:07:09 +0100 | [diff] [blame] | 329 | /* x86-32 support will be more complicated due to the ®s->sp hack */ |
| 330 | BUILD_BUG_ON(IS_ENABLED(CONFIG_X86_32)); |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 331 | |
Josh Poimboeuf | b02fcf9 | 2017-12-04 15:07:09 +0100 | [diff] [blame] | 332 | if (!stack_access_ok(state, addr, sizeof(struct pt_regs))) |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 333 | return false; |
| 334 | |
| 335 | *ip = regs->ip; |
Josh Poimboeuf | b02fcf9 | 2017-12-04 15:07:09 +0100 | [diff] [blame] | 336 | *sp = regs->sp; |
| 337 | return true; |
| 338 | } |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 339 | |
Josh Poimboeuf | b02fcf9 | 2017-12-04 15:07:09 +0100 | [diff] [blame] | 340 | static bool deref_stack_iret_regs(struct unwind_state *state, unsigned long addr, |
| 341 | unsigned long *ip, unsigned long *sp) |
| 342 | { |
| 343 | struct pt_regs *regs = (void *)addr - IRET_FRAME_OFFSET; |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 344 | |
Josh Poimboeuf | b02fcf9 | 2017-12-04 15:07:09 +0100 | [diff] [blame] | 345 | if (!stack_access_ok(state, addr, IRET_FRAME_SIZE)) |
| 346 | return false; |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 347 | |
Josh Poimboeuf | b02fcf9 | 2017-12-04 15:07:09 +0100 | [diff] [blame] | 348 | *ip = regs->ip; |
| 349 | *sp = regs->sp; |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 350 | return true; |
| 351 | } |
| 352 | |
| 353 | bool unwind_next_frame(struct unwind_state *state) |
| 354 | { |
Josh Poimboeuf | d31a580 | 2018-05-18 08:47:12 +0200 | [diff] [blame] | 355 | unsigned long ip_p, sp, orig_ip = state->ip, prev_sp = state->sp; |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 356 | enum stack_type prev_type = state->stack_info.type; |
| 357 | struct orc_entry *orc; |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 358 | bool indirect = false; |
| 359 | |
| 360 | if (unwind_done(state)) |
| 361 | return false; |
| 362 | |
| 363 | /* Don't let modules unload while we're reading their ORC data. */ |
| 364 | preempt_disable(); |
| 365 | |
Josh Poimboeuf | d31a580 | 2018-05-18 08:47:12 +0200 | [diff] [blame] | 366 | /* End-of-stack check for user tasks: */ |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 367 | if (state->regs && user_mode(state->regs)) |
Josh Poimboeuf | d31a580 | 2018-05-18 08:47:12 +0200 | [diff] [blame] | 368 | goto the_end; |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 369 | |
| 370 | /* |
| 371 | * Find the orc_entry associated with the text address. |
| 372 | * |
| 373 | * Decrement call return addresses by one so they work for sibling |
| 374 | * calls and calls to noreturn functions. |
| 375 | */ |
| 376 | orc = orc_find(state->signal ? state->ip : state->ip - 1); |
Josh Poimboeuf | d31a580 | 2018-05-18 08:47:12 +0200 | [diff] [blame] | 377 | if (!orc) |
| 378 | goto err; |
| 379 | |
| 380 | /* End-of-stack check for kernel threads: */ |
| 381 | if (orc->sp_reg == ORC_REG_UNDEFINED) { |
| 382 | if (!orc->end) |
| 383 | goto err; |
| 384 | |
| 385 | goto the_end; |
| 386 | } |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 387 | |
| 388 | /* Find the previous frame's stack: */ |
| 389 | switch (orc->sp_reg) { |
| 390 | case ORC_REG_SP: |
| 391 | sp = state->sp + orc->sp_offset; |
| 392 | break; |
| 393 | |
| 394 | case ORC_REG_BP: |
| 395 | sp = state->bp + orc->sp_offset; |
| 396 | break; |
| 397 | |
| 398 | case ORC_REG_SP_INDIRECT: |
| 399 | sp = state->sp + orc->sp_offset; |
| 400 | indirect = true; |
| 401 | break; |
| 402 | |
| 403 | case ORC_REG_BP_INDIRECT: |
| 404 | sp = state->bp + orc->sp_offset; |
| 405 | indirect = true; |
| 406 | break; |
| 407 | |
| 408 | case ORC_REG_R10: |
| 409 | if (!state->regs || !state->full_regs) { |
Josh Poimboeuf | 58c3862 | 2017-10-20 11:21:34 -0500 | [diff] [blame] | 410 | orc_warn("missing regs for base reg R10 at ip %pB\n", |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 411 | (void *)state->ip); |
Josh Poimboeuf | d31a580 | 2018-05-18 08:47:12 +0200 | [diff] [blame] | 412 | goto err; |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 413 | } |
| 414 | sp = state->regs->r10; |
| 415 | break; |
| 416 | |
| 417 | case ORC_REG_R13: |
| 418 | if (!state->regs || !state->full_regs) { |
Josh Poimboeuf | 58c3862 | 2017-10-20 11:21:34 -0500 | [diff] [blame] | 419 | orc_warn("missing regs for base reg R13 at ip %pB\n", |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 420 | (void *)state->ip); |
Josh Poimboeuf | d31a580 | 2018-05-18 08:47:12 +0200 | [diff] [blame] | 421 | goto err; |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 422 | } |
| 423 | sp = state->regs->r13; |
| 424 | break; |
| 425 | |
| 426 | case ORC_REG_DI: |
| 427 | if (!state->regs || !state->full_regs) { |
Josh Poimboeuf | 58c3862 | 2017-10-20 11:21:34 -0500 | [diff] [blame] | 428 | orc_warn("missing regs for base reg DI at ip %pB\n", |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 429 | (void *)state->ip); |
Josh Poimboeuf | d31a580 | 2018-05-18 08:47:12 +0200 | [diff] [blame] | 430 | goto err; |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 431 | } |
| 432 | sp = state->regs->di; |
| 433 | break; |
| 434 | |
| 435 | case ORC_REG_DX: |
| 436 | if (!state->regs || !state->full_regs) { |
Josh Poimboeuf | 58c3862 | 2017-10-20 11:21:34 -0500 | [diff] [blame] | 437 | orc_warn("missing regs for base reg DX at ip %pB\n", |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 438 | (void *)state->ip); |
Josh Poimboeuf | d31a580 | 2018-05-18 08:47:12 +0200 | [diff] [blame] | 439 | goto err; |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 440 | } |
| 441 | sp = state->regs->dx; |
| 442 | break; |
| 443 | |
| 444 | default: |
Josh Poimboeuf | 58c3862 | 2017-10-20 11:21:34 -0500 | [diff] [blame] | 445 | orc_warn("unknown SP base reg %d for ip %pB\n", |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 446 | orc->sp_reg, (void *)state->ip); |
Josh Poimboeuf | d31a580 | 2018-05-18 08:47:12 +0200 | [diff] [blame] | 447 | goto err; |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | if (indirect) { |
| 451 | if (!deref_stack_reg(state, sp, &sp)) |
Josh Poimboeuf | d31a580 | 2018-05-18 08:47:12 +0200 | [diff] [blame] | 452 | goto err; |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | /* Find IP, SP and possibly regs: */ |
| 456 | switch (orc->type) { |
| 457 | case ORC_TYPE_CALL: |
| 458 | ip_p = sp - sizeof(long); |
| 459 | |
| 460 | if (!deref_stack_reg(state, ip_p, &state->ip)) |
Josh Poimboeuf | d31a580 | 2018-05-18 08:47:12 +0200 | [diff] [blame] | 461 | goto err; |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 462 | |
| 463 | state->ip = ftrace_graph_ret_addr(state->task, &state->graph_idx, |
| 464 | state->ip, (void *)ip_p); |
| 465 | |
| 466 | state->sp = sp; |
| 467 | state->regs = NULL; |
| 468 | state->signal = false; |
| 469 | break; |
| 470 | |
| 471 | case ORC_TYPE_REGS: |
Josh Poimboeuf | b02fcf9 | 2017-12-04 15:07:09 +0100 | [diff] [blame] | 472 | if (!deref_stack_regs(state, sp, &state->ip, &state->sp)) { |
Josh Poimboeuf | 58c3862 | 2017-10-20 11:21:34 -0500 | [diff] [blame] | 473 | orc_warn("can't dereference registers at %p for ip %pB\n", |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 474 | (void *)sp, (void *)orig_ip); |
Josh Poimboeuf | d31a580 | 2018-05-18 08:47:12 +0200 | [diff] [blame] | 475 | goto err; |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | state->regs = (struct pt_regs *)sp; |
| 479 | state->full_regs = true; |
| 480 | state->signal = true; |
| 481 | break; |
| 482 | |
| 483 | case ORC_TYPE_REGS_IRET: |
Josh Poimboeuf | b02fcf9 | 2017-12-04 15:07:09 +0100 | [diff] [blame] | 484 | if (!deref_stack_iret_regs(state, sp, &state->ip, &state->sp)) { |
Josh Poimboeuf | 58c3862 | 2017-10-20 11:21:34 -0500 | [diff] [blame] | 485 | orc_warn("can't dereference iret registers at %p for ip %pB\n", |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 486 | (void *)sp, (void *)orig_ip); |
Josh Poimboeuf | d31a580 | 2018-05-18 08:47:12 +0200 | [diff] [blame] | 487 | goto err; |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 488 | } |
| 489 | |
Josh Poimboeuf | b02fcf9 | 2017-12-04 15:07:09 +0100 | [diff] [blame] | 490 | state->regs = (void *)sp - IRET_FRAME_OFFSET; |
| 491 | state->full_regs = false; |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 492 | state->signal = true; |
| 493 | break; |
| 494 | |
| 495 | default: |
Josh Poimboeuf | 58c3862 | 2017-10-20 11:21:34 -0500 | [diff] [blame] | 496 | orc_warn("unknown .orc_unwind entry type %d for ip %pB\n", |
| 497 | orc->type, (void *)orig_ip); |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 498 | break; |
| 499 | } |
| 500 | |
| 501 | /* Find BP: */ |
| 502 | switch (orc->bp_reg) { |
| 503 | case ORC_REG_UNDEFINED: |
| 504 | if (state->regs && state->full_regs) |
| 505 | state->bp = state->regs->bp; |
| 506 | break; |
| 507 | |
| 508 | case ORC_REG_PREV_SP: |
| 509 | if (!deref_stack_reg(state, sp + orc->bp_offset, &state->bp)) |
Josh Poimboeuf | d31a580 | 2018-05-18 08:47:12 +0200 | [diff] [blame] | 510 | goto err; |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 511 | break; |
| 512 | |
| 513 | case ORC_REG_BP: |
| 514 | if (!deref_stack_reg(state, state->bp + orc->bp_offset, &state->bp)) |
Josh Poimboeuf | d31a580 | 2018-05-18 08:47:12 +0200 | [diff] [blame] | 515 | goto err; |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 516 | break; |
| 517 | |
| 518 | default: |
Josh Poimboeuf | 58c3862 | 2017-10-20 11:21:34 -0500 | [diff] [blame] | 519 | orc_warn("unknown BP base reg %d for ip %pB\n", |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 520 | orc->bp_reg, (void *)orig_ip); |
Josh Poimboeuf | d31a580 | 2018-05-18 08:47:12 +0200 | [diff] [blame] | 521 | goto err; |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | /* Prevent a recursive loop due to bad ORC data: */ |
| 525 | if (state->stack_info.type == prev_type && |
| 526 | on_stack(&state->stack_info, (void *)state->sp, sizeof(long)) && |
| 527 | state->sp <= prev_sp) { |
Josh Poimboeuf | 58c3862 | 2017-10-20 11:21:34 -0500 | [diff] [blame] | 528 | orc_warn("stack going in the wrong direction? ip=%pB\n", |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 529 | (void *)orig_ip); |
Josh Poimboeuf | d31a580 | 2018-05-18 08:47:12 +0200 | [diff] [blame] | 530 | goto err; |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | preempt_enable(); |
| 534 | return true; |
| 535 | |
Josh Poimboeuf | d31a580 | 2018-05-18 08:47:12 +0200 | [diff] [blame] | 536 | err: |
| 537 | state->error = true; |
| 538 | |
| 539 | the_end: |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 540 | preempt_enable(); |
| 541 | state->stack_info.type = STACK_TYPE_UNKNOWN; |
| 542 | return false; |
| 543 | } |
| 544 | EXPORT_SYMBOL_GPL(unwind_next_frame); |
| 545 | |
| 546 | void __unwind_start(struct unwind_state *state, struct task_struct *task, |
| 547 | struct pt_regs *regs, unsigned long *first_frame) |
| 548 | { |
| 549 | memset(state, 0, sizeof(*state)); |
| 550 | state->task = task; |
| 551 | |
| 552 | /* |
| 553 | * Refuse to unwind the stack of a task while it's executing on another |
| 554 | * CPU. This check is racy, but that's ok: the unwinder has other |
| 555 | * checks to prevent it from going off the rails. |
| 556 | */ |
| 557 | if (task_on_another_cpu(task)) |
| 558 | goto done; |
| 559 | |
| 560 | if (regs) { |
| 561 | if (user_mode(regs)) |
| 562 | goto done; |
| 563 | |
| 564 | state->ip = regs->ip; |
| 565 | state->sp = kernel_stack_pointer(regs); |
| 566 | state->bp = regs->bp; |
| 567 | state->regs = regs; |
| 568 | state->full_regs = true; |
| 569 | state->signal = true; |
| 570 | |
| 571 | } else if (task == current) { |
| 572 | asm volatile("lea (%%rip), %0\n\t" |
| 573 | "mov %%rsp, %1\n\t" |
| 574 | "mov %%rbp, %2\n\t" |
| 575 | : "=r" (state->ip), "=r" (state->sp), |
| 576 | "=r" (state->bp)); |
| 577 | |
| 578 | } else { |
| 579 | struct inactive_task_frame *frame = (void *)task->thread.sp; |
| 580 | |
| 581 | state->sp = task->thread.sp; |
| 582 | state->bp = READ_ONCE_NOCHECK(frame->bp); |
| 583 | state->ip = READ_ONCE_NOCHECK(frame->ret_addr); |
| 584 | } |
| 585 | |
| 586 | if (get_stack_info((unsigned long *)state->sp, state->task, |
Andy Lutomirski | d3a0910 | 2017-12-04 15:07:08 +0100 | [diff] [blame] | 587 | &state->stack_info, &state->stack_mask)) { |
| 588 | /* |
| 589 | * We weren't on a valid stack. It's possible that |
| 590 | * we overflowed a valid stack into a guard page. |
| 591 | * See if the next page up is valid so that we can |
| 592 | * generate some kind of backtrace if this happens. |
| 593 | */ |
| 594 | void *next_page = (void *)PAGE_ALIGN((unsigned long)state->sp); |
| 595 | if (get_stack_info(next_page, state->task, &state->stack_info, |
| 596 | &state->stack_mask)) |
| 597 | return; |
| 598 | } |
Josh Poimboeuf | ee9f8fc | 2017-07-24 18:36:57 -0500 | [diff] [blame] | 599 | |
| 600 | /* |
| 601 | * The caller can provide the address of the first frame directly |
| 602 | * (first_frame) or indirectly (regs->sp) to indicate which stack frame |
| 603 | * to start unwinding at. Skip ahead until we reach it. |
| 604 | */ |
| 605 | |
| 606 | /* When starting from regs, skip the regs frame: */ |
| 607 | if (regs) { |
| 608 | unwind_next_frame(state); |
| 609 | return; |
| 610 | } |
| 611 | |
| 612 | /* Otherwise, skip ahead to the user-specified starting frame: */ |
| 613 | while (!unwind_done(state) && |
| 614 | (!on_stack(&state->stack_info, first_frame, sizeof(long)) || |
| 615 | state->sp <= (unsigned long)first_frame)) |
| 616 | unwind_next_frame(state); |
| 617 | |
| 618 | return; |
| 619 | |
| 620 | done: |
| 621 | state->stack_info.type = STACK_TYPE_UNKNOWN; |
| 622 | return; |
| 623 | } |
| 624 | EXPORT_SYMBOL_GPL(__unwind_start); |