1 // SPDX-License-Identifier: GPL-2.0-only 2 #include <linux/objtool.h> 3 #include <linux/module.h> 4 #include <linux/sort.h> 5 #include <asm/ptrace.h> 6 #include <asm/stacktrace.h> 7 #include <asm/unwind.h> 8 #include <asm/orc_types.h> 9 #include <asm/orc_lookup.h> 10 11 #define orc_warn(fmt, ...) \ 12 printk_deferred_once(KERN_WARNING "WARNING: " fmt, ##__VA_ARGS__) 13 14 #define orc_warn_current(args...) \ 15 ({ \ 16 static bool dumped_before; \ 17 if (state->task == current && !state->error) { \ 18 orc_warn(args); \ 19 if (unwind_debug && !dumped_before) { \ 20 dumped_before = true; \ 21 unwind_dump(state); \ 22 } \ 23 } \ 24 }) 25 26 extern int __start_orc_unwind_ip[]; 27 extern int __stop_orc_unwind_ip[]; 28 extern struct orc_entry __start_orc_unwind[]; 29 extern struct orc_entry __stop_orc_unwind[]; 30 31 static bool orc_init __ro_after_init; 32 static bool unwind_debug __ro_after_init; 33 static unsigned int lookup_num_blocks __ro_after_init; 34 35 static int __init unwind_debug_cmdline(char *str) 36 { 37 unwind_debug = true; 38 39 return 0; 40 } 41 early_param("unwind_debug", unwind_debug_cmdline); 42 43 static void unwind_dump(struct unwind_state *state) 44 { 45 static bool dumped_before; 46 unsigned long word, *sp; 47 struct stack_info stack_info = {0}; 48 unsigned long visit_mask = 0; 49 50 if (dumped_before) 51 return; 52 53 dumped_before = true; 54 55 printk_deferred("unwind stack type:%d next_sp:%p mask:0x%lx graph_idx:%d\n", 56 state->stack_info.type, state->stack_info.next_sp, 57 state->stack_mask, state->graph_idx); 58 59 for (sp = __builtin_frame_address(0); sp; 60 sp = PTR_ALIGN(stack_info.next_sp, sizeof(long))) { 61 if (get_stack_info(sp, state->task, &stack_info, &visit_mask)) 62 break; 63 64 for (; sp < stack_info.end; sp++) { 65 66 word = READ_ONCE_NOCHECK(*sp); 67 68 printk_deferred("%0*lx: %0*lx (%pB)\n", BITS_PER_LONG/4, 69 (unsigned long)sp, BITS_PER_LONG/4, 70 word, (void *)word); 71 } 72 } 73 } 74 75 static inline unsigned long orc_ip(const int *ip) 76 { 77 return (unsigned long)ip + *ip; 78 } 79 80 static struct orc_entry *__orc_find(int *ip_table, struct orc_entry *u_table, 81 unsigned int num_entries, unsigned long ip) 82 { 83 int *first = ip_table; 84 int *last = ip_table + num_entries - 1; 85 int *mid = first, *found = first; 86 87 if (!num_entries) 88 return NULL; 89 90 /* 91 * Do a binary range search to find the rightmost duplicate of a given 92 * starting address. Some entries are section terminators which are 93 * "weak" entries for ensuring there are no gaps. They should be 94 * ignored when they conflict with a real entry. 95 */ 96 while (first <= last) { 97 mid = first + ((last - first) / 2); 98 99 if (orc_ip(mid) <= ip) { 100 found = mid; 101 first = mid + 1; 102 } else 103 last = mid - 1; 104 } 105 106 return u_table + (found - ip_table); 107 } 108 109 #ifdef CONFIG_MODULES 110 static struct orc_entry *orc_module_find(unsigned long ip) 111 { 112 struct module *mod; 113 114 mod = __module_address(ip); 115 if (!mod || !mod->arch.orc_unwind || !mod->arch.orc_unwind_ip) 116 return NULL; 117 return __orc_find(mod->arch.orc_unwind_ip, mod->arch.orc_unwind, 118 mod->arch.num_orcs, ip); 119 } 120 #else 121 static struct orc_entry *orc_module_find(unsigned long ip) 122 { 123 return NULL; 124 } 125 #endif 126 127 #ifdef CONFIG_DYNAMIC_FTRACE 128 static struct orc_entry *orc_find(unsigned long ip); 129 130 /* 131 * Ftrace dynamic trampolines do not have orc entries of their own. 132 * But they are copies of the ftrace entries that are static and 133 * defined in ftrace_*.S, which do have orc entries. 134 * 135 * If the unwinder comes across a ftrace trampoline, then find the 136 * ftrace function that was used to create it, and use that ftrace 137 * function's orc entry, as the placement of the return code in 138 * the stack will be identical. 139 */ 140 static struct orc_entry *orc_ftrace_find(unsigned long ip) 141 { 142 struct ftrace_ops *ops; 143 unsigned long tramp_addr, offset; 144 145 ops = ftrace_ops_trampoline(ip); 146 if (!ops) 147 return NULL; 148 149 /* Set tramp_addr to the start of the code copied by the trampoline */ 150 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) 151 tramp_addr = (unsigned long)ftrace_regs_caller; 152 else 153 tramp_addr = (unsigned long)ftrace_caller; 154 155 /* Now place tramp_addr to the location within the trampoline ip is at */ 156 offset = ip - ops->trampoline; 157 tramp_addr += offset; 158 159 /* Prevent unlikely recursion */ 160 if (ip == tramp_addr) 161 return NULL; 162 163 return orc_find(tramp_addr); 164 } 165 #else 166 static struct orc_entry *orc_ftrace_find(unsigned long ip) 167 { 168 return NULL; 169 } 170 #endif 171 172 /* 173 * If we crash with IP==0, the last successfully executed instruction 174 * was probably an indirect function call with a NULL function pointer, 175 * and we don't have unwind information for NULL. 176 * This hardcoded ORC entry for IP==0 allows us to unwind from a NULL function 177 * pointer into its parent and then continue normally from there. 178 */ 179 static struct orc_entry null_orc_entry = { 180 .sp_offset = sizeof(long), 181 .sp_reg = ORC_REG_SP, 182 .bp_reg = ORC_REG_UNDEFINED, 183 .type = ORC_TYPE_CALL 184 }; 185 186 #ifdef CONFIG_CALL_THUNKS 187 static struct orc_entry *orc_callthunk_find(unsigned long ip) 188 { 189 if (!is_callthunk((void *)ip)) 190 return NULL; 191 192 return &null_orc_entry; 193 } 194 #else 195 static struct orc_entry *orc_callthunk_find(unsigned long ip) 196 { 197 return NULL; 198 } 199 #endif 200 201 /* Fake frame pointer entry -- used as a fallback for generated code */ 202 static struct orc_entry orc_fp_entry = { 203 .type = ORC_TYPE_CALL, 204 .sp_reg = ORC_REG_BP, 205 .sp_offset = 16, 206 .bp_reg = ORC_REG_PREV_SP, 207 .bp_offset = -16, 208 }; 209 210 static struct orc_entry *orc_find(unsigned long ip) 211 { 212 static struct orc_entry *orc; 213 214 if (ip == 0) 215 return &null_orc_entry; 216 217 /* For non-init vmlinux addresses, use the fast lookup table: */ 218 if (ip >= LOOKUP_START_IP && ip < LOOKUP_STOP_IP) { 219 unsigned int idx, start, stop; 220 221 idx = (ip - LOOKUP_START_IP) / LOOKUP_BLOCK_SIZE; 222 223 if (unlikely((idx >= lookup_num_blocks-1))) { 224 orc_warn("WARNING: bad lookup idx: idx=%u num=%u ip=%pB\n", 225 idx, lookup_num_blocks, (void *)ip); 226 return NULL; 227 } 228 229 start = orc_lookup[idx]; 230 stop = orc_lookup[idx + 1] + 1; 231 232 if (unlikely((__start_orc_unwind + start >= __stop_orc_unwind) || 233 (__start_orc_unwind + stop > __stop_orc_unwind))) { 234 orc_warn("WARNING: bad lookup value: idx=%u num=%u start=%u stop=%u ip=%pB\n", 235 idx, lookup_num_blocks, start, stop, (void *)ip); 236 return NULL; 237 } 238 239 return __orc_find(__start_orc_unwind_ip + start, 240 __start_orc_unwind + start, stop - start, ip); 241 } 242 243 /* vmlinux .init slow lookup: */ 244 if (is_kernel_inittext(ip)) 245 return __orc_find(__start_orc_unwind_ip, __start_orc_unwind, 246 __stop_orc_unwind_ip - __start_orc_unwind_ip, ip); 247 248 /* Module lookup: */ 249 orc = orc_module_find(ip); 250 if (orc) 251 return orc; 252 253 orc = orc_ftrace_find(ip); 254 if (orc) 255 return orc; 256 257 return orc_callthunk_find(ip); 258 } 259 260 #ifdef CONFIG_MODULES 261 262 static DEFINE_MUTEX(sort_mutex); 263 static int *cur_orc_ip_table = __start_orc_unwind_ip; 264 static struct orc_entry *cur_orc_table = __start_orc_unwind; 265 266 static void orc_sort_swap(void *_a, void *_b, int size) 267 { 268 struct orc_entry *orc_a, *orc_b; 269 int *a = _a, *b = _b, tmp; 270 int delta = _b - _a; 271 272 /* Swap the .orc_unwind_ip entries: */ 273 tmp = *a; 274 *a = *b + delta; 275 *b = tmp - delta; 276 277 /* Swap the corresponding .orc_unwind entries: */ 278 orc_a = cur_orc_table + (a - cur_orc_ip_table); 279 orc_b = cur_orc_table + (b - cur_orc_ip_table); 280 swap(*orc_a, *orc_b); 281 } 282 283 static int orc_sort_cmp(const void *_a, const void *_b) 284 { 285 struct orc_entry *orc_a; 286 const int *a = _a, *b = _b; 287 unsigned long a_val = orc_ip(a); 288 unsigned long b_val = orc_ip(b); 289 290 if (a_val > b_val) 291 return 1; 292 if (a_val < b_val) 293 return -1; 294 295 /* 296 * The "weak" section terminator entries need to always be first 297 * to ensure the lookup code skips them in favor of real entries. 298 * These terminator entries exist to handle any gaps created by 299 * whitelisted .o files which didn't get objtool generation. 300 */ 301 orc_a = cur_orc_table + (a - cur_orc_ip_table); 302 return orc_a->type == ORC_TYPE_UNDEFINED ? -1 : 1; 303 } 304 305 void unwind_module_init(struct module *mod, void *_orc_ip, size_t orc_ip_size, 306 void *_orc, size_t orc_size) 307 { 308 int *orc_ip = _orc_ip; 309 struct orc_entry *orc = _orc; 310 unsigned int num_entries = orc_ip_size / sizeof(int); 311 312 WARN_ON_ONCE(orc_ip_size % sizeof(int) != 0 || 313 orc_size % sizeof(*orc) != 0 || 314 num_entries != orc_size / sizeof(*orc)); 315 316 /* 317 * The 'cur_orc_*' globals allow the orc_sort_swap() callback to 318 * associate an .orc_unwind_ip table entry with its corresponding 319 * .orc_unwind entry so they can both be swapped. 320 */ 321 mutex_lock(&sort_mutex); 322 cur_orc_ip_table = orc_ip; 323 cur_orc_table = orc; 324 sort(orc_ip, num_entries, sizeof(int), orc_sort_cmp, orc_sort_swap); 325 mutex_unlock(&sort_mutex); 326 327 mod->arch.orc_unwind_ip = orc_ip; 328 mod->arch.orc_unwind = orc; 329 mod->arch.num_orcs = num_entries; 330 } 331 #endif 332 333 void __init unwind_init(void) 334 { 335 size_t orc_ip_size = (void *)__stop_orc_unwind_ip - (void *)__start_orc_unwind_ip; 336 size_t orc_size = (void *)__stop_orc_unwind - (void *)__start_orc_unwind; 337 size_t num_entries = orc_ip_size / sizeof(int); 338 struct orc_entry *orc; 339 int i; 340 341 if (!num_entries || orc_ip_size % sizeof(int) != 0 || 342 orc_size % sizeof(struct orc_entry) != 0 || 343 num_entries != orc_size / sizeof(struct orc_entry)) { 344 orc_warn("WARNING: Bad or missing .orc_unwind table. Disabling unwinder.\n"); 345 return; 346 } 347 348 /* 349 * Note, the orc_unwind and orc_unwind_ip tables were already 350 * sorted at build time via the 'sorttable' tool. 351 * It's ready for binary search straight away, no need to sort it. 352 */ 353 354 /* Initialize the fast lookup table: */ 355 lookup_num_blocks = orc_lookup_end - orc_lookup; 356 for (i = 0; i < lookup_num_blocks-1; i++) { 357 orc = __orc_find(__start_orc_unwind_ip, __start_orc_unwind, 358 num_entries, 359 LOOKUP_START_IP + (LOOKUP_BLOCK_SIZE * i)); 360 if (!orc) { 361 orc_warn("WARNING: Corrupt .orc_unwind table. Disabling unwinder.\n"); 362 return; 363 } 364 365 orc_lookup[i] = orc - __start_orc_unwind; 366 } 367 368 /* Initialize the ending block: */ 369 orc = __orc_find(__start_orc_unwind_ip, __start_orc_unwind, num_entries, 370 LOOKUP_STOP_IP); 371 if (!orc) { 372 orc_warn("WARNING: Corrupt .orc_unwind table. Disabling unwinder.\n"); 373 return; 374 } 375 orc_lookup[lookup_num_blocks-1] = orc - __start_orc_unwind; 376 377 orc_init = true; 378 } 379 380 unsigned long unwind_get_return_address(struct unwind_state *state) 381 { 382 if (unwind_done(state)) 383 return 0; 384 385 return __kernel_text_address(state->ip) ? state->ip : 0; 386 } 387 EXPORT_SYMBOL_GPL(unwind_get_return_address); 388 389 unsigned long *unwind_get_return_address_ptr(struct unwind_state *state) 390 { 391 if (unwind_done(state)) 392 return NULL; 393 394 if (state->regs) 395 return &state->regs->ip; 396 397 if (state->sp) 398 return (unsigned long *)state->sp - 1; 399 400 return NULL; 401 } 402 403 static bool stack_access_ok(struct unwind_state *state, unsigned long _addr, 404 size_t len) 405 { 406 struct stack_info *info = &state->stack_info; 407 void *addr = (void *)_addr; 408 409 if (on_stack(info, addr, len)) 410 return true; 411 412 return !get_stack_info(addr, state->task, info, &state->stack_mask) && 413 on_stack(info, addr, len); 414 } 415 416 static bool deref_stack_reg(struct unwind_state *state, unsigned long addr, 417 unsigned long *val) 418 { 419 if (!stack_access_ok(state, addr, sizeof(long))) 420 return false; 421 422 *val = READ_ONCE_NOCHECK(*(unsigned long *)addr); 423 return true; 424 } 425 426 static bool deref_stack_regs(struct unwind_state *state, unsigned long addr, 427 unsigned long *ip, unsigned long *sp) 428 { 429 struct pt_regs *regs = (struct pt_regs *)addr; 430 431 /* x86-32 support will be more complicated due to the ®s->sp hack */ 432 BUILD_BUG_ON(IS_ENABLED(CONFIG_X86_32)); 433 434 if (!stack_access_ok(state, addr, sizeof(struct pt_regs))) 435 return false; 436 437 *ip = READ_ONCE_NOCHECK(regs->ip); 438 *sp = READ_ONCE_NOCHECK(regs->sp); 439 return true; 440 } 441 442 static bool deref_stack_iret_regs(struct unwind_state *state, unsigned long addr, 443 unsigned long *ip, unsigned long *sp) 444 { 445 struct pt_regs *regs = (void *)addr - IRET_FRAME_OFFSET; 446 447 if (!stack_access_ok(state, addr, IRET_FRAME_SIZE)) 448 return false; 449 450 *ip = READ_ONCE_NOCHECK(regs->ip); 451 *sp = READ_ONCE_NOCHECK(regs->sp); 452 return true; 453 } 454 455 /* 456 * If state->regs is non-NULL, and points to a full pt_regs, just get the reg 457 * value from state->regs. 458 * 459 * Otherwise, if state->regs just points to IRET regs, and the previous frame 460 * had full regs, it's safe to get the value from the previous regs. This can 461 * happen when early/late IRQ entry code gets interrupted by an NMI. 462 */ 463 static bool get_reg(struct unwind_state *state, unsigned int reg_off, 464 unsigned long *val) 465 { 466 unsigned int reg = reg_off/8; 467 468 if (!state->regs) 469 return false; 470 471 if (state->full_regs) { 472 *val = READ_ONCE_NOCHECK(((unsigned long *)state->regs)[reg]); 473 return true; 474 } 475 476 if (state->prev_regs) { 477 *val = READ_ONCE_NOCHECK(((unsigned long *)state->prev_regs)[reg]); 478 return true; 479 } 480 481 return false; 482 } 483 484 bool unwind_next_frame(struct unwind_state *state) 485 { 486 unsigned long ip_p, sp, tmp, orig_ip = state->ip, prev_sp = state->sp; 487 enum stack_type prev_type = state->stack_info.type; 488 struct orc_entry *orc; 489 bool indirect = false; 490 491 if (unwind_done(state)) 492 return false; 493 494 /* Don't let modules unload while we're reading their ORC data. */ 495 preempt_disable(); 496 497 /* End-of-stack check for user tasks: */ 498 if (state->regs && user_mode(state->regs)) 499 goto the_end; 500 501 /* 502 * Find the orc_entry associated with the text address. 503 * 504 * For a call frame (as opposed to a signal frame), state->ip points to 505 * the instruction after the call. That instruction's stack layout 506 * could be different from the call instruction's layout, for example 507 * if the call was to a noreturn function. So get the ORC data for the 508 * call instruction itself. 509 */ 510 orc = orc_find(state->signal ? state->ip : state->ip - 1); 511 if (!orc) { 512 /* 513 * As a fallback, try to assume this code uses a frame pointer. 514 * This is useful for generated code, like BPF, which ORC 515 * doesn't know about. This is just a guess, so the rest of 516 * the unwind is no longer considered reliable. 517 */ 518 orc = &orc_fp_entry; 519 state->error = true; 520 } else { 521 if (orc->type == ORC_TYPE_UNDEFINED) 522 goto err; 523 524 if (orc->type == ORC_TYPE_END_OF_STACK) 525 goto the_end; 526 } 527 528 state->signal = orc->signal; 529 530 /* Find the previous frame's stack: */ 531 switch (orc->sp_reg) { 532 case ORC_REG_SP: 533 sp = state->sp + orc->sp_offset; 534 break; 535 536 case ORC_REG_BP: 537 sp = state->bp + orc->sp_offset; 538 break; 539 540 case ORC_REG_SP_INDIRECT: 541 sp = state->sp; 542 indirect = true; 543 break; 544 545 case ORC_REG_BP_INDIRECT: 546 sp = state->bp + orc->sp_offset; 547 indirect = true; 548 break; 549 550 case ORC_REG_R10: 551 if (!get_reg(state, offsetof(struct pt_regs, r10), &sp)) { 552 orc_warn_current("missing R10 value at %pB\n", 553 (void *)state->ip); 554 goto err; 555 } 556 break; 557 558 case ORC_REG_R13: 559 if (!get_reg(state, offsetof(struct pt_regs, r13), &sp)) { 560 orc_warn_current("missing R13 value at %pB\n", 561 (void *)state->ip); 562 goto err; 563 } 564 break; 565 566 case ORC_REG_DI: 567 if (!get_reg(state, offsetof(struct pt_regs, di), &sp)) { 568 orc_warn_current("missing RDI value at %pB\n", 569 (void *)state->ip); 570 goto err; 571 } 572 break; 573 574 case ORC_REG_DX: 575 if (!get_reg(state, offsetof(struct pt_regs, dx), &sp)) { 576 orc_warn_current("missing DX value at %pB\n", 577 (void *)state->ip); 578 goto err; 579 } 580 break; 581 582 default: 583 orc_warn("unknown SP base reg %d at %pB\n", 584 orc->sp_reg, (void *)state->ip); 585 goto err; 586 } 587 588 if (indirect) { 589 if (!deref_stack_reg(state, sp, &sp)) 590 goto err; 591 592 if (orc->sp_reg == ORC_REG_SP_INDIRECT) 593 sp += orc->sp_offset; 594 } 595 596 /* Find IP, SP and possibly regs: */ 597 switch (orc->type) { 598 case ORC_TYPE_CALL: 599 ip_p = sp - sizeof(long); 600 601 if (!deref_stack_reg(state, ip_p, &state->ip)) 602 goto err; 603 604 state->ip = unwind_recover_ret_addr(state, state->ip, 605 (unsigned long *)ip_p); 606 state->sp = sp; 607 state->regs = NULL; 608 state->prev_regs = NULL; 609 break; 610 611 case ORC_TYPE_REGS: 612 if (!deref_stack_regs(state, sp, &state->ip, &state->sp)) { 613 orc_warn_current("can't access registers at %pB\n", 614 (void *)orig_ip); 615 goto err; 616 } 617 /* 618 * There is a small chance to interrupt at the entry of 619 * arch_rethook_trampoline() where the ORC info doesn't exist. 620 * That point is right after the RET to arch_rethook_trampoline() 621 * which was modified return address. 622 * At that point, the @addr_p of the unwind_recover_rethook() 623 * (this has to point the address of the stack entry storing 624 * the modified return address) must be "SP - (a stack entry)" 625 * because SP is incremented by the RET. 626 */ 627 state->ip = unwind_recover_rethook(state, state->ip, 628 (unsigned long *)(state->sp - sizeof(long))); 629 state->regs = (struct pt_regs *)sp; 630 state->prev_regs = NULL; 631 state->full_regs = true; 632 break; 633 634 case ORC_TYPE_REGS_PARTIAL: 635 if (!deref_stack_iret_regs(state, sp, &state->ip, &state->sp)) { 636 orc_warn_current("can't access iret registers at %pB\n", 637 (void *)orig_ip); 638 goto err; 639 } 640 /* See ORC_TYPE_REGS case comment. */ 641 state->ip = unwind_recover_rethook(state, state->ip, 642 (unsigned long *)(state->sp - sizeof(long))); 643 644 if (state->full_regs) 645 state->prev_regs = state->regs; 646 state->regs = (void *)sp - IRET_FRAME_OFFSET; 647 state->full_regs = false; 648 break; 649 650 default: 651 orc_warn("unknown .orc_unwind entry type %d at %pB\n", 652 orc->type, (void *)orig_ip); 653 goto err; 654 } 655 656 /* Find BP: */ 657 switch (orc->bp_reg) { 658 case ORC_REG_UNDEFINED: 659 if (get_reg(state, offsetof(struct pt_regs, bp), &tmp)) 660 state->bp = tmp; 661 break; 662 663 case ORC_REG_PREV_SP: 664 if (!deref_stack_reg(state, sp + orc->bp_offset, &state->bp)) 665 goto err; 666 break; 667 668 case ORC_REG_BP: 669 if (!deref_stack_reg(state, state->bp + orc->bp_offset, &state->bp)) 670 goto err; 671 break; 672 673 default: 674 orc_warn("unknown BP base reg %d for ip %pB\n", 675 orc->bp_reg, (void *)orig_ip); 676 goto err; 677 } 678 679 /* Prevent a recursive loop due to bad ORC data: */ 680 if (state->stack_info.type == prev_type && 681 on_stack(&state->stack_info, (void *)state->sp, sizeof(long)) && 682 state->sp <= prev_sp) { 683 orc_warn_current("stack going in the wrong direction? at %pB\n", 684 (void *)orig_ip); 685 goto err; 686 } 687 688 preempt_enable(); 689 return true; 690 691 err: 692 state->error = true; 693 694 the_end: 695 preempt_enable(); 696 state->stack_info.type = STACK_TYPE_UNKNOWN; 697 return false; 698 } 699 EXPORT_SYMBOL_GPL(unwind_next_frame); 700 701 void __unwind_start(struct unwind_state *state, struct task_struct *task, 702 struct pt_regs *regs, unsigned long *first_frame) 703 { 704 memset(state, 0, sizeof(*state)); 705 state->task = task; 706 707 if (!orc_init) 708 goto err; 709 710 /* 711 * Refuse to unwind the stack of a task while it's executing on another 712 * CPU. This check is racy, but that's ok: the unwinder has other 713 * checks to prevent it from going off the rails. 714 */ 715 if (task_on_another_cpu(task)) 716 goto err; 717 718 if (regs) { 719 if (user_mode(regs)) 720 goto the_end; 721 722 state->ip = regs->ip; 723 state->sp = regs->sp; 724 state->bp = regs->bp; 725 state->regs = regs; 726 state->full_regs = true; 727 state->signal = true; 728 729 } else if (task == current) { 730 asm volatile("lea (%%rip), %0\n\t" 731 "mov %%rsp, %1\n\t" 732 "mov %%rbp, %2\n\t" 733 : "=r" (state->ip), "=r" (state->sp), 734 "=r" (state->bp)); 735 736 } else { 737 struct inactive_task_frame *frame = (void *)task->thread.sp; 738 739 state->sp = task->thread.sp + sizeof(*frame); 740 state->bp = READ_ONCE_NOCHECK(frame->bp); 741 state->ip = READ_ONCE_NOCHECK(frame->ret_addr); 742 state->signal = (void *)state->ip == ret_from_fork; 743 } 744 745 if (get_stack_info((unsigned long *)state->sp, state->task, 746 &state->stack_info, &state->stack_mask)) { 747 /* 748 * We weren't on a valid stack. It's possible that 749 * we overflowed a valid stack into a guard page. 750 * See if the next page up is valid so that we can 751 * generate some kind of backtrace if this happens. 752 */ 753 void *next_page = (void *)PAGE_ALIGN((unsigned long)state->sp); 754 state->error = true; 755 if (get_stack_info(next_page, state->task, &state->stack_info, 756 &state->stack_mask)) 757 return; 758 } 759 760 /* 761 * The caller can provide the address of the first frame directly 762 * (first_frame) or indirectly (regs->sp) to indicate which stack frame 763 * to start unwinding at. Skip ahead until we reach it. 764 */ 765 766 /* When starting from regs, skip the regs frame: */ 767 if (regs) { 768 unwind_next_frame(state); 769 return; 770 } 771 772 /* Otherwise, skip ahead to the user-specified starting frame: */ 773 while (!unwind_done(state) && 774 (!on_stack(&state->stack_info, first_frame, sizeof(long)) || 775 state->sp <= (unsigned long)first_frame)) 776 unwind_next_frame(state); 777 778 return; 779 780 err: 781 state->error = true; 782 the_end: 783 state->stack_info.type = STACK_TYPE_UNKNOWN; 784 } 785 EXPORT_SYMBOL_GPL(__unwind_start); 786