17c7900f8SJosh Poimboeuf #include <linux/sched.h> 27c7900f8SJosh Poimboeuf #include <asm/ptrace.h> 37c7900f8SJosh Poimboeuf #include <asm/bitops.h> 47c7900f8SJosh Poimboeuf #include <asm/stacktrace.h> 57c7900f8SJosh Poimboeuf #include <asm/unwind.h> 67c7900f8SJosh Poimboeuf 77c7900f8SJosh Poimboeuf #define FRAME_HEADER_SIZE (sizeof(long) * 2) 87c7900f8SJosh Poimboeuf 97c7900f8SJosh Poimboeuf unsigned long unwind_get_return_address(struct unwind_state *state) 107c7900f8SJosh Poimboeuf { 117c7900f8SJosh Poimboeuf unsigned long addr; 127c7900f8SJosh Poimboeuf unsigned long *addr_p = unwind_get_return_address_ptr(state); 137c7900f8SJosh Poimboeuf 147c7900f8SJosh Poimboeuf if (unwind_done(state)) 157c7900f8SJosh Poimboeuf return 0; 167c7900f8SJosh Poimboeuf 17946c1911SJosh Poimboeuf if (state->regs && user_mode(state->regs)) 18946c1911SJosh Poimboeuf return 0; 19946c1911SJosh Poimboeuf 207c7900f8SJosh Poimboeuf addr = ftrace_graph_ret_addr(state->task, &state->graph_idx, *addr_p, 217c7900f8SJosh Poimboeuf addr_p); 227c7900f8SJosh Poimboeuf 23b6959a36SJosh Poimboeuf if (!__kernel_text_address(addr)) { 24b6959a36SJosh Poimboeuf printk_deferred_once(KERN_WARNING 25b6959a36SJosh Poimboeuf "WARNING: unrecognized kernel stack return address %p at %p in %s:%d\n", 26b6959a36SJosh Poimboeuf (void *)addr, addr_p, state->task->comm, 27b6959a36SJosh Poimboeuf state->task->pid); 28b6959a36SJosh Poimboeuf return 0; 29b6959a36SJosh Poimboeuf } 30b6959a36SJosh Poimboeuf 31b6959a36SJosh Poimboeuf return addr; 327c7900f8SJosh Poimboeuf } 337c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(unwind_get_return_address); 347c7900f8SJosh Poimboeuf 3524d86f59SJosh Poimboeuf static size_t regs_size(struct pt_regs *regs) 3624d86f59SJosh Poimboeuf { 3724d86f59SJosh Poimboeuf /* x86_32 regs from kernel mode are two words shorter: */ 3824d86f59SJosh Poimboeuf if (IS_ENABLED(CONFIG_X86_32) && !user_mode(regs)) 3924d86f59SJosh Poimboeuf return sizeof(*regs) - 2*sizeof(long); 4024d86f59SJosh Poimboeuf 4124d86f59SJosh Poimboeuf return sizeof(*regs); 4224d86f59SJosh Poimboeuf } 4324d86f59SJosh Poimboeuf 44acb4608aSJosh Poimboeuf static bool is_last_task_frame(struct unwind_state *state) 45acb4608aSJosh Poimboeuf { 46acb4608aSJosh Poimboeuf unsigned long bp = (unsigned long)state->bp; 47acb4608aSJosh Poimboeuf unsigned long regs = (unsigned long)task_pt_regs(state->task); 48acb4608aSJosh Poimboeuf 49*8023e0e2SJosh Poimboeuf /* 50*8023e0e2SJosh Poimboeuf * We have to check for the last task frame at two different locations 51*8023e0e2SJosh Poimboeuf * because gcc can occasionally decide to realign the stack pointer and 52*8023e0e2SJosh Poimboeuf * change the offset of the stack frame by a word in the prologue of a 53*8023e0e2SJosh Poimboeuf * function called by head/entry code. 54*8023e0e2SJosh Poimboeuf */ 55*8023e0e2SJosh Poimboeuf return bp == regs - FRAME_HEADER_SIZE || 56*8023e0e2SJosh Poimboeuf bp == regs - FRAME_HEADER_SIZE - sizeof(long); 57acb4608aSJosh Poimboeuf } 58acb4608aSJosh Poimboeuf 59946c1911SJosh Poimboeuf /* 60946c1911SJosh Poimboeuf * This determines if the frame pointer actually contains an encoded pointer to 61946c1911SJosh Poimboeuf * pt_regs on the stack. See ENCODE_FRAME_POINTER. 62946c1911SJosh Poimboeuf */ 63946c1911SJosh Poimboeuf static struct pt_regs *decode_frame_pointer(unsigned long *bp) 64946c1911SJosh Poimboeuf { 65946c1911SJosh Poimboeuf unsigned long regs = (unsigned long)bp; 66946c1911SJosh Poimboeuf 67946c1911SJosh Poimboeuf if (!(regs & 0x1)) 68946c1911SJosh Poimboeuf return NULL; 69946c1911SJosh Poimboeuf 70946c1911SJosh Poimboeuf return (struct pt_regs *)(regs & ~0x1); 71946c1911SJosh Poimboeuf } 72946c1911SJosh Poimboeuf 737c7900f8SJosh Poimboeuf static bool update_stack_state(struct unwind_state *state, void *addr, 747c7900f8SJosh Poimboeuf size_t len) 757c7900f8SJosh Poimboeuf { 767c7900f8SJosh Poimboeuf struct stack_info *info = &state->stack_info; 777c7900f8SJosh Poimboeuf 787c7900f8SJosh Poimboeuf /* 797c7900f8SJosh Poimboeuf * If addr isn't on the current stack, switch to the next one. 807c7900f8SJosh Poimboeuf * 817c7900f8SJosh Poimboeuf * We may have to traverse multiple stacks to deal with the possibility 827c7900f8SJosh Poimboeuf * that 'info->next_sp' could point to an empty stack and 'addr' could 837c7900f8SJosh Poimboeuf * be on a subsequent stack. 847c7900f8SJosh Poimboeuf */ 857c7900f8SJosh Poimboeuf while (!on_stack(info, addr, len)) 867c7900f8SJosh Poimboeuf if (get_stack_info(info->next_sp, state->task, info, 877c7900f8SJosh Poimboeuf &state->stack_mask)) 887c7900f8SJosh Poimboeuf return false; 897c7900f8SJosh Poimboeuf 907c7900f8SJosh Poimboeuf return true; 917c7900f8SJosh Poimboeuf } 927c7900f8SJosh Poimboeuf 937c7900f8SJosh Poimboeuf bool unwind_next_frame(struct unwind_state *state) 947c7900f8SJosh Poimboeuf { 95946c1911SJosh Poimboeuf struct pt_regs *regs; 96946c1911SJosh Poimboeuf unsigned long *next_bp, *next_frame; 97946c1911SJosh Poimboeuf size_t next_len; 9824d86f59SJosh Poimboeuf enum stack_type prev_type = state->stack_info.type; 997c7900f8SJosh Poimboeuf 1007c7900f8SJosh Poimboeuf if (unwind_done(state)) 1017c7900f8SJosh Poimboeuf return false; 1027c7900f8SJosh Poimboeuf 103946c1911SJosh Poimboeuf /* have we reached the end? */ 104946c1911SJosh Poimboeuf if (state->regs && user_mode(state->regs)) 105946c1911SJosh Poimboeuf goto the_end; 106946c1911SJosh Poimboeuf 107acb4608aSJosh Poimboeuf if (is_last_task_frame(state)) { 108acb4608aSJosh Poimboeuf regs = task_pt_regs(state->task); 109acb4608aSJosh Poimboeuf 110acb4608aSJosh Poimboeuf /* 111acb4608aSJosh Poimboeuf * kthreads (other than the boot CPU's idle thread) have some 112acb4608aSJosh Poimboeuf * partial regs at the end of their stack which were placed 113acb4608aSJosh Poimboeuf * there by copy_thread_tls(). But the regs don't have any 114acb4608aSJosh Poimboeuf * useful information, so we can skip them. 115acb4608aSJosh Poimboeuf * 116acb4608aSJosh Poimboeuf * This user_mode() check is slightly broader than a PF_KTHREAD 117acb4608aSJosh Poimboeuf * check because it also catches the awkward situation where a 118acb4608aSJosh Poimboeuf * newly forked kthread transitions into a user task by calling 119acb4608aSJosh Poimboeuf * do_execve(), which eventually clears PF_KTHREAD. 120acb4608aSJosh Poimboeuf */ 121acb4608aSJosh Poimboeuf if (!user_mode(regs)) 122acb4608aSJosh Poimboeuf goto the_end; 123acb4608aSJosh Poimboeuf 124acb4608aSJosh Poimboeuf /* 125acb4608aSJosh Poimboeuf * We're almost at the end, but not quite: there's still the 126acb4608aSJosh Poimboeuf * syscall regs frame. Entry code doesn't encode the regs 127acb4608aSJosh Poimboeuf * pointer for syscalls, so we have to set it manually. 128acb4608aSJosh Poimboeuf */ 129acb4608aSJosh Poimboeuf state->regs = regs; 130acb4608aSJosh Poimboeuf state->bp = NULL; 131acb4608aSJosh Poimboeuf return true; 132acb4608aSJosh Poimboeuf } 133acb4608aSJosh Poimboeuf 134946c1911SJosh Poimboeuf /* get the next frame pointer */ 135946c1911SJosh Poimboeuf if (state->regs) 136946c1911SJosh Poimboeuf next_bp = (unsigned long *)state->regs->bp; 137946c1911SJosh Poimboeuf else 1387c7900f8SJosh Poimboeuf next_bp = (unsigned long *)*state->bp; 1397c7900f8SJosh Poimboeuf 140946c1911SJosh Poimboeuf /* is the next frame pointer an encoded pointer to pt_regs? */ 141946c1911SJosh Poimboeuf regs = decode_frame_pointer(next_bp); 142946c1911SJosh Poimboeuf if (regs) { 143946c1911SJosh Poimboeuf next_frame = (unsigned long *)regs; 144946c1911SJosh Poimboeuf next_len = sizeof(*regs); 145946c1911SJosh Poimboeuf } else { 146946c1911SJosh Poimboeuf next_frame = next_bp; 147946c1911SJosh Poimboeuf next_len = FRAME_HEADER_SIZE; 148946c1911SJosh Poimboeuf } 1497c7900f8SJosh Poimboeuf 150946c1911SJosh Poimboeuf /* make sure the next frame's data is accessible */ 151c32c47c6SJosh Poimboeuf if (!update_stack_state(state, next_frame, next_len)) { 152c32c47c6SJosh Poimboeuf /* 153c32c47c6SJosh Poimboeuf * Don't warn on bad regs->bp. An interrupt in entry code 154c32c47c6SJosh Poimboeuf * might cause a false positive warning. 155c32c47c6SJosh Poimboeuf */ 156c32c47c6SJosh Poimboeuf if (state->regs) 157c32c47c6SJosh Poimboeuf goto the_end; 158c32c47c6SJosh Poimboeuf 159c32c47c6SJosh Poimboeuf goto bad_address; 160c32c47c6SJosh Poimboeuf } 161c32c47c6SJosh Poimboeuf 16224d86f59SJosh Poimboeuf /* Make sure it only unwinds up and doesn't overlap the last frame: */ 16324d86f59SJosh Poimboeuf if (state->stack_info.type == prev_type) { 16424d86f59SJosh Poimboeuf if (state->regs && (void *)next_frame < (void *)state->regs + regs_size(state->regs)) 16524d86f59SJosh Poimboeuf goto bad_address; 16624d86f59SJosh Poimboeuf 16724d86f59SJosh Poimboeuf if (state->bp && (void *)next_frame < (void *)state->bp + FRAME_HEADER_SIZE) 16824d86f59SJosh Poimboeuf goto bad_address; 16924d86f59SJosh Poimboeuf } 17024d86f59SJosh Poimboeuf 1717c7900f8SJosh Poimboeuf /* move to the next frame */ 172946c1911SJosh Poimboeuf if (regs) { 173946c1911SJosh Poimboeuf state->regs = regs; 174946c1911SJosh Poimboeuf state->bp = NULL; 175946c1911SJosh Poimboeuf } else { 1767c7900f8SJosh Poimboeuf state->bp = next_bp; 177946c1911SJosh Poimboeuf state->regs = NULL; 178946c1911SJosh Poimboeuf } 179946c1911SJosh Poimboeuf 1807c7900f8SJosh Poimboeuf return true; 181946c1911SJosh Poimboeuf 182c32c47c6SJosh Poimboeuf bad_address: 18324d86f59SJosh Poimboeuf if (state->regs) { 18424d86f59SJosh Poimboeuf printk_deferred_once(KERN_WARNING 18524d86f59SJosh Poimboeuf "WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n", 18624d86f59SJosh Poimboeuf state->regs, state->task->comm, 18724d86f59SJosh Poimboeuf state->task->pid, next_frame); 18824d86f59SJosh Poimboeuf } else { 189c32c47c6SJosh Poimboeuf printk_deferred_once(KERN_WARNING 190c32c47c6SJosh Poimboeuf "WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n", 191c32c47c6SJosh Poimboeuf state->bp, state->task->comm, 19224d86f59SJosh Poimboeuf state->task->pid, next_frame); 19324d86f59SJosh Poimboeuf } 194946c1911SJosh Poimboeuf the_end: 195946c1911SJosh Poimboeuf state->stack_info.type = STACK_TYPE_UNKNOWN; 196946c1911SJosh Poimboeuf return false; 1977c7900f8SJosh Poimboeuf } 1987c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(unwind_next_frame); 1997c7900f8SJosh Poimboeuf 2007c7900f8SJosh Poimboeuf void __unwind_start(struct unwind_state *state, struct task_struct *task, 2017c7900f8SJosh Poimboeuf struct pt_regs *regs, unsigned long *first_frame) 2027c7900f8SJosh Poimboeuf { 203946c1911SJosh Poimboeuf unsigned long *bp, *frame; 204946c1911SJosh Poimboeuf size_t len; 205946c1911SJosh Poimboeuf 2067c7900f8SJosh Poimboeuf memset(state, 0, sizeof(*state)); 2077c7900f8SJosh Poimboeuf state->task = task; 2087c7900f8SJosh Poimboeuf 2097c7900f8SJosh Poimboeuf /* don't even attempt to start from user mode regs */ 2107c7900f8SJosh Poimboeuf if (regs && user_mode(regs)) { 2117c7900f8SJosh Poimboeuf state->stack_info.type = STACK_TYPE_UNKNOWN; 2127c7900f8SJosh Poimboeuf return; 2137c7900f8SJosh Poimboeuf } 2147c7900f8SJosh Poimboeuf 2157c7900f8SJosh Poimboeuf /* set up the starting stack frame */ 216946c1911SJosh Poimboeuf bp = get_frame_pointer(task, regs); 217946c1911SJosh Poimboeuf regs = decode_frame_pointer(bp); 218946c1911SJosh Poimboeuf if (regs) { 219946c1911SJosh Poimboeuf state->regs = regs; 220946c1911SJosh Poimboeuf frame = (unsigned long *)regs; 221946c1911SJosh Poimboeuf len = sizeof(*regs); 222946c1911SJosh Poimboeuf } else { 223946c1911SJosh Poimboeuf state->bp = bp; 224946c1911SJosh Poimboeuf frame = bp; 225946c1911SJosh Poimboeuf len = FRAME_HEADER_SIZE; 226946c1911SJosh Poimboeuf } 2277c7900f8SJosh Poimboeuf 2287c7900f8SJosh Poimboeuf /* initialize stack info and make sure the frame data is accessible */ 229946c1911SJosh Poimboeuf get_stack_info(frame, state->task, &state->stack_info, 2307c7900f8SJosh Poimboeuf &state->stack_mask); 231946c1911SJosh Poimboeuf update_stack_state(state, frame, len); 2327c7900f8SJosh Poimboeuf 2337c7900f8SJosh Poimboeuf /* 2347c7900f8SJosh Poimboeuf * The caller can provide the address of the first frame directly 2357c7900f8SJosh Poimboeuf * (first_frame) or indirectly (regs->sp) to indicate which stack frame 2367c7900f8SJosh Poimboeuf * to start unwinding at. Skip ahead until we reach it. 2377c7900f8SJosh Poimboeuf */ 2387c7900f8SJosh Poimboeuf while (!unwind_done(state) && 2397c7900f8SJosh Poimboeuf (!on_stack(&state->stack_info, first_frame, sizeof(long)) || 2407c7900f8SJosh Poimboeuf state->bp < first_frame)) 2417c7900f8SJosh Poimboeuf unwind_next_frame(state); 2427c7900f8SJosh Poimboeuf } 2437c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(__unwind_start); 244