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 17*946c1911SJosh Poimboeuf if (state->regs && user_mode(state->regs)) 18*946c1911SJosh Poimboeuf return 0; 19*946c1911SJosh Poimboeuf 207c7900f8SJosh Poimboeuf addr = ftrace_graph_ret_addr(state->task, &state->graph_idx, *addr_p, 217c7900f8SJosh Poimboeuf addr_p); 227c7900f8SJosh Poimboeuf 237c7900f8SJosh Poimboeuf return __kernel_text_address(addr) ? addr : 0; 247c7900f8SJosh Poimboeuf } 257c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(unwind_get_return_address); 267c7900f8SJosh Poimboeuf 27*946c1911SJosh Poimboeuf /* 28*946c1911SJosh Poimboeuf * This determines if the frame pointer actually contains an encoded pointer to 29*946c1911SJosh Poimboeuf * pt_regs on the stack. See ENCODE_FRAME_POINTER. 30*946c1911SJosh Poimboeuf */ 31*946c1911SJosh Poimboeuf static struct pt_regs *decode_frame_pointer(unsigned long *bp) 32*946c1911SJosh Poimboeuf { 33*946c1911SJosh Poimboeuf unsigned long regs = (unsigned long)bp; 34*946c1911SJosh Poimboeuf 35*946c1911SJosh Poimboeuf if (!(regs & 0x1)) 36*946c1911SJosh Poimboeuf return NULL; 37*946c1911SJosh Poimboeuf 38*946c1911SJosh Poimboeuf return (struct pt_regs *)(regs & ~0x1); 39*946c1911SJosh Poimboeuf } 40*946c1911SJosh Poimboeuf 417c7900f8SJosh Poimboeuf static bool update_stack_state(struct unwind_state *state, void *addr, 427c7900f8SJosh Poimboeuf size_t len) 437c7900f8SJosh Poimboeuf { 447c7900f8SJosh Poimboeuf struct stack_info *info = &state->stack_info; 457c7900f8SJosh Poimboeuf 467c7900f8SJosh Poimboeuf /* 477c7900f8SJosh Poimboeuf * If addr isn't on the current stack, switch to the next one. 487c7900f8SJosh Poimboeuf * 497c7900f8SJosh Poimboeuf * We may have to traverse multiple stacks to deal with the possibility 507c7900f8SJosh Poimboeuf * that 'info->next_sp' could point to an empty stack and 'addr' could 517c7900f8SJosh Poimboeuf * be on a subsequent stack. 527c7900f8SJosh Poimboeuf */ 537c7900f8SJosh Poimboeuf while (!on_stack(info, addr, len)) 547c7900f8SJosh Poimboeuf if (get_stack_info(info->next_sp, state->task, info, 557c7900f8SJosh Poimboeuf &state->stack_mask)) 567c7900f8SJosh Poimboeuf return false; 577c7900f8SJosh Poimboeuf 587c7900f8SJosh Poimboeuf return true; 597c7900f8SJosh Poimboeuf } 607c7900f8SJosh Poimboeuf 617c7900f8SJosh Poimboeuf bool unwind_next_frame(struct unwind_state *state) 627c7900f8SJosh Poimboeuf { 63*946c1911SJosh Poimboeuf struct pt_regs *regs; 64*946c1911SJosh Poimboeuf unsigned long *next_bp, *next_frame; 65*946c1911SJosh Poimboeuf size_t next_len; 667c7900f8SJosh Poimboeuf 677c7900f8SJosh Poimboeuf if (unwind_done(state)) 687c7900f8SJosh Poimboeuf return false; 697c7900f8SJosh Poimboeuf 70*946c1911SJosh Poimboeuf /* have we reached the end? */ 71*946c1911SJosh Poimboeuf if (state->regs && user_mode(state->regs)) 72*946c1911SJosh Poimboeuf goto the_end; 73*946c1911SJosh Poimboeuf 74*946c1911SJosh Poimboeuf /* get the next frame pointer */ 75*946c1911SJosh Poimboeuf if (state->regs) 76*946c1911SJosh Poimboeuf next_bp = (unsigned long *)state->regs->bp; 77*946c1911SJosh Poimboeuf else 787c7900f8SJosh Poimboeuf next_bp = (unsigned long *)*state->bp; 797c7900f8SJosh Poimboeuf 80*946c1911SJosh Poimboeuf /* is the next frame pointer an encoded pointer to pt_regs? */ 81*946c1911SJosh Poimboeuf regs = decode_frame_pointer(next_bp); 82*946c1911SJosh Poimboeuf if (regs) { 83*946c1911SJosh Poimboeuf next_frame = (unsigned long *)regs; 84*946c1911SJosh Poimboeuf next_len = sizeof(*regs); 85*946c1911SJosh Poimboeuf } else { 86*946c1911SJosh Poimboeuf next_frame = next_bp; 87*946c1911SJosh Poimboeuf next_len = FRAME_HEADER_SIZE; 88*946c1911SJosh Poimboeuf } 897c7900f8SJosh Poimboeuf 90*946c1911SJosh Poimboeuf /* make sure the next frame's data is accessible */ 91*946c1911SJosh Poimboeuf if (!update_stack_state(state, next_frame, next_len)) 92*946c1911SJosh Poimboeuf return false; 937c7900f8SJosh Poimboeuf /* move to the next frame */ 94*946c1911SJosh Poimboeuf if (regs) { 95*946c1911SJosh Poimboeuf state->regs = regs; 96*946c1911SJosh Poimboeuf state->bp = NULL; 97*946c1911SJosh Poimboeuf } else { 987c7900f8SJosh Poimboeuf state->bp = next_bp; 99*946c1911SJosh Poimboeuf state->regs = NULL; 100*946c1911SJosh Poimboeuf } 101*946c1911SJosh Poimboeuf 1027c7900f8SJosh Poimboeuf return true; 103*946c1911SJosh Poimboeuf 104*946c1911SJosh Poimboeuf the_end: 105*946c1911SJosh Poimboeuf state->stack_info.type = STACK_TYPE_UNKNOWN; 106*946c1911SJosh Poimboeuf return false; 1077c7900f8SJosh Poimboeuf } 1087c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(unwind_next_frame); 1097c7900f8SJosh Poimboeuf 1107c7900f8SJosh Poimboeuf void __unwind_start(struct unwind_state *state, struct task_struct *task, 1117c7900f8SJosh Poimboeuf struct pt_regs *regs, unsigned long *first_frame) 1127c7900f8SJosh Poimboeuf { 113*946c1911SJosh Poimboeuf unsigned long *bp, *frame; 114*946c1911SJosh Poimboeuf size_t len; 115*946c1911SJosh Poimboeuf 1167c7900f8SJosh Poimboeuf memset(state, 0, sizeof(*state)); 1177c7900f8SJosh Poimboeuf state->task = task; 1187c7900f8SJosh Poimboeuf 1197c7900f8SJosh Poimboeuf /* don't even attempt to start from user mode regs */ 1207c7900f8SJosh Poimboeuf if (regs && user_mode(regs)) { 1217c7900f8SJosh Poimboeuf state->stack_info.type = STACK_TYPE_UNKNOWN; 1227c7900f8SJosh Poimboeuf return; 1237c7900f8SJosh Poimboeuf } 1247c7900f8SJosh Poimboeuf 1257c7900f8SJosh Poimboeuf /* set up the starting stack frame */ 126*946c1911SJosh Poimboeuf bp = get_frame_pointer(task, regs); 127*946c1911SJosh Poimboeuf regs = decode_frame_pointer(bp); 128*946c1911SJosh Poimboeuf if (regs) { 129*946c1911SJosh Poimboeuf state->regs = regs; 130*946c1911SJosh Poimboeuf frame = (unsigned long *)regs; 131*946c1911SJosh Poimboeuf len = sizeof(*regs); 132*946c1911SJosh Poimboeuf } else { 133*946c1911SJosh Poimboeuf state->bp = bp; 134*946c1911SJosh Poimboeuf frame = bp; 135*946c1911SJosh Poimboeuf len = FRAME_HEADER_SIZE; 136*946c1911SJosh Poimboeuf } 1377c7900f8SJosh Poimboeuf 1387c7900f8SJosh Poimboeuf /* initialize stack info and make sure the frame data is accessible */ 139*946c1911SJosh Poimboeuf get_stack_info(frame, state->task, &state->stack_info, 1407c7900f8SJosh Poimboeuf &state->stack_mask); 141*946c1911SJosh Poimboeuf update_stack_state(state, frame, len); 1427c7900f8SJosh Poimboeuf 1437c7900f8SJosh Poimboeuf /* 1447c7900f8SJosh Poimboeuf * The caller can provide the address of the first frame directly 1457c7900f8SJosh Poimboeuf * (first_frame) or indirectly (regs->sp) to indicate which stack frame 1467c7900f8SJosh Poimboeuf * to start unwinding at. Skip ahead until we reach it. 1477c7900f8SJosh Poimboeuf */ 1487c7900f8SJosh Poimboeuf while (!unwind_done(state) && 1497c7900f8SJosh Poimboeuf (!on_stack(&state->stack_info, first_frame, sizeof(long)) || 1507c7900f8SJosh Poimboeuf state->bp < first_frame)) 1517c7900f8SJosh Poimboeuf unwind_next_frame(state); 1527c7900f8SJosh Poimboeuf } 1537c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(__unwind_start); 154