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 23*b6959a36SJosh Poimboeuf if (!__kernel_text_address(addr)) { 24*b6959a36SJosh Poimboeuf printk_deferred_once(KERN_WARNING 25*b6959a36SJosh Poimboeuf "WARNING: unrecognized kernel stack return address %p at %p in %s:%d\n", 26*b6959a36SJosh Poimboeuf (void *)addr, addr_p, state->task->comm, 27*b6959a36SJosh Poimboeuf state->task->pid); 28*b6959a36SJosh Poimboeuf return 0; 29*b6959a36SJosh Poimboeuf } 30*b6959a36SJosh Poimboeuf 31*b6959a36SJosh Poimboeuf return addr; 327c7900f8SJosh Poimboeuf } 337c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(unwind_get_return_address); 347c7900f8SJosh Poimboeuf 35acb4608aSJosh Poimboeuf static bool is_last_task_frame(struct unwind_state *state) 36acb4608aSJosh Poimboeuf { 37acb4608aSJosh Poimboeuf unsigned long bp = (unsigned long)state->bp; 38acb4608aSJosh Poimboeuf unsigned long regs = (unsigned long)task_pt_regs(state->task); 39acb4608aSJosh Poimboeuf 40acb4608aSJosh Poimboeuf return bp == regs - FRAME_HEADER_SIZE; 41acb4608aSJosh Poimboeuf } 42acb4608aSJosh Poimboeuf 43946c1911SJosh Poimboeuf /* 44946c1911SJosh Poimboeuf * This determines if the frame pointer actually contains an encoded pointer to 45946c1911SJosh Poimboeuf * pt_regs on the stack. See ENCODE_FRAME_POINTER. 46946c1911SJosh Poimboeuf */ 47946c1911SJosh Poimboeuf static struct pt_regs *decode_frame_pointer(unsigned long *bp) 48946c1911SJosh Poimboeuf { 49946c1911SJosh Poimboeuf unsigned long regs = (unsigned long)bp; 50946c1911SJosh Poimboeuf 51946c1911SJosh Poimboeuf if (!(regs & 0x1)) 52946c1911SJosh Poimboeuf return NULL; 53946c1911SJosh Poimboeuf 54946c1911SJosh Poimboeuf return (struct pt_regs *)(regs & ~0x1); 55946c1911SJosh Poimboeuf } 56946c1911SJosh Poimboeuf 577c7900f8SJosh Poimboeuf static bool update_stack_state(struct unwind_state *state, void *addr, 587c7900f8SJosh Poimboeuf size_t len) 597c7900f8SJosh Poimboeuf { 607c7900f8SJosh Poimboeuf struct stack_info *info = &state->stack_info; 617c7900f8SJosh Poimboeuf 627c7900f8SJosh Poimboeuf /* 637c7900f8SJosh Poimboeuf * If addr isn't on the current stack, switch to the next one. 647c7900f8SJosh Poimboeuf * 657c7900f8SJosh Poimboeuf * We may have to traverse multiple stacks to deal with the possibility 667c7900f8SJosh Poimboeuf * that 'info->next_sp' could point to an empty stack and 'addr' could 677c7900f8SJosh Poimboeuf * be on a subsequent stack. 687c7900f8SJosh Poimboeuf */ 697c7900f8SJosh Poimboeuf while (!on_stack(info, addr, len)) 707c7900f8SJosh Poimboeuf if (get_stack_info(info->next_sp, state->task, info, 717c7900f8SJosh Poimboeuf &state->stack_mask)) 727c7900f8SJosh Poimboeuf return false; 737c7900f8SJosh Poimboeuf 747c7900f8SJosh Poimboeuf return true; 757c7900f8SJosh Poimboeuf } 767c7900f8SJosh Poimboeuf 777c7900f8SJosh Poimboeuf bool unwind_next_frame(struct unwind_state *state) 787c7900f8SJosh Poimboeuf { 79946c1911SJosh Poimboeuf struct pt_regs *regs; 80946c1911SJosh Poimboeuf unsigned long *next_bp, *next_frame; 81946c1911SJosh Poimboeuf size_t next_len; 827c7900f8SJosh Poimboeuf 837c7900f8SJosh Poimboeuf if (unwind_done(state)) 847c7900f8SJosh Poimboeuf return false; 857c7900f8SJosh Poimboeuf 86946c1911SJosh Poimboeuf /* have we reached the end? */ 87946c1911SJosh Poimboeuf if (state->regs && user_mode(state->regs)) 88946c1911SJosh Poimboeuf goto the_end; 89946c1911SJosh Poimboeuf 90acb4608aSJosh Poimboeuf if (is_last_task_frame(state)) { 91acb4608aSJosh Poimboeuf regs = task_pt_regs(state->task); 92acb4608aSJosh Poimboeuf 93acb4608aSJosh Poimboeuf /* 94acb4608aSJosh Poimboeuf * kthreads (other than the boot CPU's idle thread) have some 95acb4608aSJosh Poimboeuf * partial regs at the end of their stack which were placed 96acb4608aSJosh Poimboeuf * there by copy_thread_tls(). But the regs don't have any 97acb4608aSJosh Poimboeuf * useful information, so we can skip them. 98acb4608aSJosh Poimboeuf * 99acb4608aSJosh Poimboeuf * This user_mode() check is slightly broader than a PF_KTHREAD 100acb4608aSJosh Poimboeuf * check because it also catches the awkward situation where a 101acb4608aSJosh Poimboeuf * newly forked kthread transitions into a user task by calling 102acb4608aSJosh Poimboeuf * do_execve(), which eventually clears PF_KTHREAD. 103acb4608aSJosh Poimboeuf */ 104acb4608aSJosh Poimboeuf if (!user_mode(regs)) 105acb4608aSJosh Poimboeuf goto the_end; 106acb4608aSJosh Poimboeuf 107acb4608aSJosh Poimboeuf /* 108acb4608aSJosh Poimboeuf * We're almost at the end, but not quite: there's still the 109acb4608aSJosh Poimboeuf * syscall regs frame. Entry code doesn't encode the regs 110acb4608aSJosh Poimboeuf * pointer for syscalls, so we have to set it manually. 111acb4608aSJosh Poimboeuf */ 112acb4608aSJosh Poimboeuf state->regs = regs; 113acb4608aSJosh Poimboeuf state->bp = NULL; 114acb4608aSJosh Poimboeuf return true; 115acb4608aSJosh Poimboeuf } 116acb4608aSJosh Poimboeuf 117946c1911SJosh Poimboeuf /* get the next frame pointer */ 118946c1911SJosh Poimboeuf if (state->regs) 119946c1911SJosh Poimboeuf next_bp = (unsigned long *)state->regs->bp; 120946c1911SJosh Poimboeuf else 1217c7900f8SJosh Poimboeuf next_bp = (unsigned long *)*state->bp; 1227c7900f8SJosh Poimboeuf 123946c1911SJosh Poimboeuf /* is the next frame pointer an encoded pointer to pt_regs? */ 124946c1911SJosh Poimboeuf regs = decode_frame_pointer(next_bp); 125946c1911SJosh Poimboeuf if (regs) { 126946c1911SJosh Poimboeuf next_frame = (unsigned long *)regs; 127946c1911SJosh Poimboeuf next_len = sizeof(*regs); 128946c1911SJosh Poimboeuf } else { 129946c1911SJosh Poimboeuf next_frame = next_bp; 130946c1911SJosh Poimboeuf next_len = FRAME_HEADER_SIZE; 131946c1911SJosh Poimboeuf } 1327c7900f8SJosh Poimboeuf 133946c1911SJosh Poimboeuf /* make sure the next frame's data is accessible */ 134c32c47c6SJosh Poimboeuf if (!update_stack_state(state, next_frame, next_len)) { 135c32c47c6SJosh Poimboeuf /* 136c32c47c6SJosh Poimboeuf * Don't warn on bad regs->bp. An interrupt in entry code 137c32c47c6SJosh Poimboeuf * might cause a false positive warning. 138c32c47c6SJosh Poimboeuf */ 139c32c47c6SJosh Poimboeuf if (state->regs) 140c32c47c6SJosh Poimboeuf goto the_end; 141c32c47c6SJosh Poimboeuf 142c32c47c6SJosh Poimboeuf goto bad_address; 143c32c47c6SJosh Poimboeuf } 144c32c47c6SJosh Poimboeuf 1457c7900f8SJosh Poimboeuf /* move to the next frame */ 146946c1911SJosh Poimboeuf if (regs) { 147946c1911SJosh Poimboeuf state->regs = regs; 148946c1911SJosh Poimboeuf state->bp = NULL; 149946c1911SJosh Poimboeuf } else { 1507c7900f8SJosh Poimboeuf state->bp = next_bp; 151946c1911SJosh Poimboeuf state->regs = NULL; 152946c1911SJosh Poimboeuf } 153946c1911SJosh Poimboeuf 1547c7900f8SJosh Poimboeuf return true; 155946c1911SJosh Poimboeuf 156c32c47c6SJosh Poimboeuf bad_address: 157c32c47c6SJosh Poimboeuf printk_deferred_once(KERN_WARNING 158c32c47c6SJosh Poimboeuf "WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n", 159c32c47c6SJosh Poimboeuf state->bp, state->task->comm, 160c32c47c6SJosh Poimboeuf state->task->pid, next_bp); 161946c1911SJosh Poimboeuf the_end: 162946c1911SJosh Poimboeuf state->stack_info.type = STACK_TYPE_UNKNOWN; 163946c1911SJosh Poimboeuf return false; 1647c7900f8SJosh Poimboeuf } 1657c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(unwind_next_frame); 1667c7900f8SJosh Poimboeuf 1677c7900f8SJosh Poimboeuf void __unwind_start(struct unwind_state *state, struct task_struct *task, 1687c7900f8SJosh Poimboeuf struct pt_regs *regs, unsigned long *first_frame) 1697c7900f8SJosh Poimboeuf { 170946c1911SJosh Poimboeuf unsigned long *bp, *frame; 171946c1911SJosh Poimboeuf size_t len; 172946c1911SJosh Poimboeuf 1737c7900f8SJosh Poimboeuf memset(state, 0, sizeof(*state)); 1747c7900f8SJosh Poimboeuf state->task = task; 1757c7900f8SJosh Poimboeuf 1767c7900f8SJosh Poimboeuf /* don't even attempt to start from user mode regs */ 1777c7900f8SJosh Poimboeuf if (regs && user_mode(regs)) { 1787c7900f8SJosh Poimboeuf state->stack_info.type = STACK_TYPE_UNKNOWN; 1797c7900f8SJosh Poimboeuf return; 1807c7900f8SJosh Poimboeuf } 1817c7900f8SJosh Poimboeuf 1827c7900f8SJosh Poimboeuf /* set up the starting stack frame */ 183946c1911SJosh Poimboeuf bp = get_frame_pointer(task, regs); 184946c1911SJosh Poimboeuf regs = decode_frame_pointer(bp); 185946c1911SJosh Poimboeuf if (regs) { 186946c1911SJosh Poimboeuf state->regs = regs; 187946c1911SJosh Poimboeuf frame = (unsigned long *)regs; 188946c1911SJosh Poimboeuf len = sizeof(*regs); 189946c1911SJosh Poimboeuf } else { 190946c1911SJosh Poimboeuf state->bp = bp; 191946c1911SJosh Poimboeuf frame = bp; 192946c1911SJosh Poimboeuf len = FRAME_HEADER_SIZE; 193946c1911SJosh Poimboeuf } 1947c7900f8SJosh Poimboeuf 1957c7900f8SJosh Poimboeuf /* initialize stack info and make sure the frame data is accessible */ 196946c1911SJosh Poimboeuf get_stack_info(frame, state->task, &state->stack_info, 1977c7900f8SJosh Poimboeuf &state->stack_mask); 198946c1911SJosh Poimboeuf update_stack_state(state, frame, len); 1997c7900f8SJosh Poimboeuf 2007c7900f8SJosh Poimboeuf /* 2017c7900f8SJosh Poimboeuf * The caller can provide the address of the first frame directly 2027c7900f8SJosh Poimboeuf * (first_frame) or indirectly (regs->sp) to indicate which stack frame 2037c7900f8SJosh Poimboeuf * to start unwinding at. Skip ahead until we reach it. 2047c7900f8SJosh Poimboeuf */ 2057c7900f8SJosh Poimboeuf while (!unwind_done(state) && 2067c7900f8SJosh Poimboeuf (!on_stack(&state->stack_info, first_frame, sizeof(long)) || 2077c7900f8SJosh Poimboeuf state->bp < first_frame)) 2087c7900f8SJosh Poimboeuf unwind_next_frame(state); 2097c7900f8SJosh Poimboeuf } 2107c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(__unwind_start); 211