17c7900f8SJosh Poimboeuf #include <linux/sched.h> 229930025SIngo Molnar #include <linux/sched/task.h> 368db0cf1SIngo Molnar #include <linux/sched/task_stack.h> 47c7900f8SJosh Poimboeuf #include <asm/ptrace.h> 57c7900f8SJosh Poimboeuf #include <asm/bitops.h> 67c7900f8SJosh Poimboeuf #include <asm/stacktrace.h> 77c7900f8SJosh Poimboeuf #include <asm/unwind.h> 87c7900f8SJosh Poimboeuf 97c7900f8SJosh Poimboeuf #define FRAME_HEADER_SIZE (sizeof(long) * 2) 107c7900f8SJosh Poimboeuf 1184936118SJosh Poimboeuf /* 1284936118SJosh Poimboeuf * This disables KASAN checking when reading a value from another task's stack, 1384936118SJosh Poimboeuf * since the other task could be running on another CPU and could have poisoned 1484936118SJosh Poimboeuf * the stack in the meantime. 1584936118SJosh Poimboeuf */ 1684936118SJosh Poimboeuf #define READ_ONCE_TASK_STACK(task, x) \ 1784936118SJosh Poimboeuf ({ \ 1884936118SJosh Poimboeuf unsigned long val; \ 1984936118SJosh Poimboeuf if (task == current) \ 2084936118SJosh Poimboeuf val = READ_ONCE(x); \ 2184936118SJosh Poimboeuf else \ 2284936118SJosh Poimboeuf val = READ_ONCE_NOCHECK(x); \ 2384936118SJosh Poimboeuf val; \ 2484936118SJosh Poimboeuf }) 2584936118SJosh Poimboeuf 268b5e99f0SJosh Poimboeuf static void unwind_dump(struct unwind_state *state, unsigned long *sp) 278b5e99f0SJosh Poimboeuf { 288b5e99f0SJosh Poimboeuf static bool dumped_before = false; 298b5e99f0SJosh Poimboeuf bool prev_zero, zero = false; 308b5e99f0SJosh Poimboeuf unsigned long word; 318b5e99f0SJosh Poimboeuf 328b5e99f0SJosh Poimboeuf if (dumped_before) 338b5e99f0SJosh Poimboeuf return; 348b5e99f0SJosh Poimboeuf 358b5e99f0SJosh Poimboeuf dumped_before = true; 368b5e99f0SJosh Poimboeuf 378b5e99f0SJosh Poimboeuf printk_deferred("unwind stack type:%d next_sp:%p mask:%lx graph_idx:%d\n", 388b5e99f0SJosh Poimboeuf state->stack_info.type, state->stack_info.next_sp, 398b5e99f0SJosh Poimboeuf state->stack_mask, state->graph_idx); 408b5e99f0SJosh Poimboeuf 418b5e99f0SJosh Poimboeuf for (sp = state->orig_sp; sp < state->stack_info.end; sp++) { 428b5e99f0SJosh Poimboeuf word = READ_ONCE_NOCHECK(*sp); 438b5e99f0SJosh Poimboeuf 448b5e99f0SJosh Poimboeuf prev_zero = zero; 458b5e99f0SJosh Poimboeuf zero = word == 0; 468b5e99f0SJosh Poimboeuf 478b5e99f0SJosh Poimboeuf if (zero) { 488b5e99f0SJosh Poimboeuf if (!prev_zero) 498b5e99f0SJosh Poimboeuf printk_deferred("%p: %016x ...\n", sp, 0); 508b5e99f0SJosh Poimboeuf continue; 518b5e99f0SJosh Poimboeuf } 528b5e99f0SJosh Poimboeuf 538b5e99f0SJosh Poimboeuf printk_deferred("%p: %016lx (%pB)\n", sp, word, (void *)word); 548b5e99f0SJosh Poimboeuf } 558b5e99f0SJosh Poimboeuf } 568b5e99f0SJosh Poimboeuf 577c7900f8SJosh Poimboeuf unsigned long unwind_get_return_address(struct unwind_state *state) 587c7900f8SJosh Poimboeuf { 597c7900f8SJosh Poimboeuf unsigned long addr; 607c7900f8SJosh Poimboeuf unsigned long *addr_p = unwind_get_return_address_ptr(state); 617c7900f8SJosh Poimboeuf 627c7900f8SJosh Poimboeuf if (unwind_done(state)) 637c7900f8SJosh Poimboeuf return 0; 647c7900f8SJosh Poimboeuf 65946c1911SJosh Poimboeuf if (state->regs && user_mode(state->regs)) 66946c1911SJosh Poimboeuf return 0; 67946c1911SJosh Poimboeuf 6884936118SJosh Poimboeuf addr = READ_ONCE_TASK_STACK(state->task, *addr_p); 6984936118SJosh Poimboeuf addr = ftrace_graph_ret_addr(state->task, &state->graph_idx, addr, 707c7900f8SJosh Poimboeuf addr_p); 717c7900f8SJosh Poimboeuf 72c280f773SJosh Poimboeuf return __kernel_text_address(addr) ? addr : 0; 737c7900f8SJosh Poimboeuf } 747c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(unwind_get_return_address); 757c7900f8SJosh Poimboeuf 7624d86f59SJosh Poimboeuf static size_t regs_size(struct pt_regs *regs) 7724d86f59SJosh Poimboeuf { 7824d86f59SJosh Poimboeuf /* x86_32 regs from kernel mode are two words shorter: */ 7924d86f59SJosh Poimboeuf if (IS_ENABLED(CONFIG_X86_32) && !user_mode(regs)) 8024d86f59SJosh Poimboeuf return sizeof(*regs) - 2*sizeof(long); 8124d86f59SJosh Poimboeuf 8224d86f59SJosh Poimboeuf return sizeof(*regs); 8324d86f59SJosh Poimboeuf } 8424d86f59SJosh Poimboeuf 85*87a6b297SJosh Poimboeuf #ifdef CONFIG_X86_32 86*87a6b297SJosh Poimboeuf #define GCC_REALIGN_WORDS 3 87*87a6b297SJosh Poimboeuf #else 88*87a6b297SJosh Poimboeuf #define GCC_REALIGN_WORDS 1 89*87a6b297SJosh Poimboeuf #endif 90*87a6b297SJosh Poimboeuf 91acb4608aSJosh Poimboeuf static bool is_last_task_frame(struct unwind_state *state) 92acb4608aSJosh Poimboeuf { 93*87a6b297SJosh Poimboeuf unsigned long *last_bp = (unsigned long *)task_pt_regs(state->task) - 2; 94*87a6b297SJosh Poimboeuf unsigned long *aligned_bp = last_bp - GCC_REALIGN_WORDS; 95acb4608aSJosh Poimboeuf 968023e0e2SJosh Poimboeuf /* 978023e0e2SJosh Poimboeuf * We have to check for the last task frame at two different locations 988023e0e2SJosh Poimboeuf * because gcc can occasionally decide to realign the stack pointer and 99*87a6b297SJosh Poimboeuf * change the offset of the stack frame in the prologue of a function 100*87a6b297SJosh Poimboeuf * called by head/entry code. Examples: 101*87a6b297SJosh Poimboeuf * 102*87a6b297SJosh Poimboeuf * <start_secondary>: 103*87a6b297SJosh Poimboeuf * push %edi 104*87a6b297SJosh Poimboeuf * lea 0x8(%esp),%edi 105*87a6b297SJosh Poimboeuf * and $0xfffffff8,%esp 106*87a6b297SJosh Poimboeuf * pushl -0x4(%edi) 107*87a6b297SJosh Poimboeuf * push %ebp 108*87a6b297SJosh Poimboeuf * mov %esp,%ebp 109*87a6b297SJosh Poimboeuf * 110*87a6b297SJosh Poimboeuf * <x86_64_start_kernel>: 111*87a6b297SJosh Poimboeuf * lea 0x8(%rsp),%r10 112*87a6b297SJosh Poimboeuf * and $0xfffffffffffffff0,%rsp 113*87a6b297SJosh Poimboeuf * pushq -0x8(%r10) 114*87a6b297SJosh Poimboeuf * push %rbp 115*87a6b297SJosh Poimboeuf * mov %rsp,%rbp 116*87a6b297SJosh Poimboeuf * 117*87a6b297SJosh Poimboeuf * Note that after aligning the stack, it pushes a duplicate copy of 118*87a6b297SJosh Poimboeuf * the return address before pushing the frame pointer. 1198023e0e2SJosh Poimboeuf */ 120*87a6b297SJosh Poimboeuf return (state->bp == last_bp || 121*87a6b297SJosh Poimboeuf (state->bp == aligned_bp && *(aligned_bp+1) == *(last_bp+1))); 122acb4608aSJosh Poimboeuf } 123acb4608aSJosh Poimboeuf 124946c1911SJosh Poimboeuf /* 125946c1911SJosh Poimboeuf * This determines if the frame pointer actually contains an encoded pointer to 126946c1911SJosh Poimboeuf * pt_regs on the stack. See ENCODE_FRAME_POINTER. 127946c1911SJosh Poimboeuf */ 128946c1911SJosh Poimboeuf static struct pt_regs *decode_frame_pointer(unsigned long *bp) 129946c1911SJosh Poimboeuf { 130946c1911SJosh Poimboeuf unsigned long regs = (unsigned long)bp; 131946c1911SJosh Poimboeuf 132946c1911SJosh Poimboeuf if (!(regs & 0x1)) 133946c1911SJosh Poimboeuf return NULL; 134946c1911SJosh Poimboeuf 135946c1911SJosh Poimboeuf return (struct pt_regs *)(regs & ~0x1); 136946c1911SJosh Poimboeuf } 137946c1911SJosh Poimboeuf 1387c7900f8SJosh Poimboeuf static bool update_stack_state(struct unwind_state *state, void *addr, 1397c7900f8SJosh Poimboeuf size_t len) 1407c7900f8SJosh Poimboeuf { 1417c7900f8SJosh Poimboeuf struct stack_info *info = &state->stack_info; 1428b5e99f0SJosh Poimboeuf enum stack_type orig_type = info->type; 1437c7900f8SJosh Poimboeuf 1447c7900f8SJosh Poimboeuf /* 1457c7900f8SJosh Poimboeuf * If addr isn't on the current stack, switch to the next one. 1467c7900f8SJosh Poimboeuf * 1477c7900f8SJosh Poimboeuf * We may have to traverse multiple stacks to deal with the possibility 1487c7900f8SJosh Poimboeuf * that 'info->next_sp' could point to an empty stack and 'addr' could 1497c7900f8SJosh Poimboeuf * be on a subsequent stack. 1507c7900f8SJosh Poimboeuf */ 1517c7900f8SJosh Poimboeuf while (!on_stack(info, addr, len)) 1527c7900f8SJosh Poimboeuf if (get_stack_info(info->next_sp, state->task, info, 1537c7900f8SJosh Poimboeuf &state->stack_mask)) 1547c7900f8SJosh Poimboeuf return false; 1557c7900f8SJosh Poimboeuf 1568b5e99f0SJosh Poimboeuf if (!state->orig_sp || info->type != orig_type) 1578b5e99f0SJosh Poimboeuf state->orig_sp = addr; 1588b5e99f0SJosh Poimboeuf 1597c7900f8SJosh Poimboeuf return true; 1607c7900f8SJosh Poimboeuf } 1617c7900f8SJosh Poimboeuf 1627c7900f8SJosh Poimboeuf bool unwind_next_frame(struct unwind_state *state) 1637c7900f8SJosh Poimboeuf { 164946c1911SJosh Poimboeuf struct pt_regs *regs; 165946c1911SJosh Poimboeuf unsigned long *next_bp, *next_frame; 166946c1911SJosh Poimboeuf size_t next_len; 16724d86f59SJosh Poimboeuf enum stack_type prev_type = state->stack_info.type; 1687c7900f8SJosh Poimboeuf 1697c7900f8SJosh Poimboeuf if (unwind_done(state)) 1707c7900f8SJosh Poimboeuf return false; 1717c7900f8SJosh Poimboeuf 172946c1911SJosh Poimboeuf /* have we reached the end? */ 173946c1911SJosh Poimboeuf if (state->regs && user_mode(state->regs)) 174946c1911SJosh Poimboeuf goto the_end; 175946c1911SJosh Poimboeuf 176acb4608aSJosh Poimboeuf if (is_last_task_frame(state)) { 177acb4608aSJosh Poimboeuf regs = task_pt_regs(state->task); 178acb4608aSJosh Poimboeuf 179acb4608aSJosh Poimboeuf /* 180acb4608aSJosh Poimboeuf * kthreads (other than the boot CPU's idle thread) have some 181acb4608aSJosh Poimboeuf * partial regs at the end of their stack which were placed 182acb4608aSJosh Poimboeuf * there by copy_thread_tls(). But the regs don't have any 183acb4608aSJosh Poimboeuf * useful information, so we can skip them. 184acb4608aSJosh Poimboeuf * 185acb4608aSJosh Poimboeuf * This user_mode() check is slightly broader than a PF_KTHREAD 186acb4608aSJosh Poimboeuf * check because it also catches the awkward situation where a 187acb4608aSJosh Poimboeuf * newly forked kthread transitions into a user task by calling 188acb4608aSJosh Poimboeuf * do_execve(), which eventually clears PF_KTHREAD. 189acb4608aSJosh Poimboeuf */ 190acb4608aSJosh Poimboeuf if (!user_mode(regs)) 191acb4608aSJosh Poimboeuf goto the_end; 192acb4608aSJosh Poimboeuf 193acb4608aSJosh Poimboeuf /* 194acb4608aSJosh Poimboeuf * We're almost at the end, but not quite: there's still the 195acb4608aSJosh Poimboeuf * syscall regs frame. Entry code doesn't encode the regs 196acb4608aSJosh Poimboeuf * pointer for syscalls, so we have to set it manually. 197acb4608aSJosh Poimboeuf */ 198acb4608aSJosh Poimboeuf state->regs = regs; 199acb4608aSJosh Poimboeuf state->bp = NULL; 200acb4608aSJosh Poimboeuf return true; 201acb4608aSJosh Poimboeuf } 202acb4608aSJosh Poimboeuf 203946c1911SJosh Poimboeuf /* get the next frame pointer */ 204946c1911SJosh Poimboeuf if (state->regs) 205946c1911SJosh Poimboeuf next_bp = (unsigned long *)state->regs->bp; 206946c1911SJosh Poimboeuf else 20784936118SJosh Poimboeuf next_bp = (unsigned long *)READ_ONCE_TASK_STACK(state->task,*state->bp); 2087c7900f8SJosh Poimboeuf 209946c1911SJosh Poimboeuf /* is the next frame pointer an encoded pointer to pt_regs? */ 210946c1911SJosh Poimboeuf regs = decode_frame_pointer(next_bp); 211946c1911SJosh Poimboeuf if (regs) { 212946c1911SJosh Poimboeuf next_frame = (unsigned long *)regs; 213946c1911SJosh Poimboeuf next_len = sizeof(*regs); 214946c1911SJosh Poimboeuf } else { 215946c1911SJosh Poimboeuf next_frame = next_bp; 216946c1911SJosh Poimboeuf next_len = FRAME_HEADER_SIZE; 217946c1911SJosh Poimboeuf } 2187c7900f8SJosh Poimboeuf 219946c1911SJosh Poimboeuf /* make sure the next frame's data is accessible */ 220c32c47c6SJosh Poimboeuf if (!update_stack_state(state, next_frame, next_len)) { 221c32c47c6SJosh Poimboeuf /* 222c32c47c6SJosh Poimboeuf * Don't warn on bad regs->bp. An interrupt in entry code 223c32c47c6SJosh Poimboeuf * might cause a false positive warning. 224c32c47c6SJosh Poimboeuf */ 225c32c47c6SJosh Poimboeuf if (state->regs) 226c32c47c6SJosh Poimboeuf goto the_end; 227c32c47c6SJosh Poimboeuf 228c32c47c6SJosh Poimboeuf goto bad_address; 229c32c47c6SJosh Poimboeuf } 230c32c47c6SJosh Poimboeuf 23124d86f59SJosh Poimboeuf /* Make sure it only unwinds up and doesn't overlap the last frame: */ 23224d86f59SJosh Poimboeuf if (state->stack_info.type == prev_type) { 23324d86f59SJosh Poimboeuf if (state->regs && (void *)next_frame < (void *)state->regs + regs_size(state->regs)) 23424d86f59SJosh Poimboeuf goto bad_address; 23524d86f59SJosh Poimboeuf 23624d86f59SJosh Poimboeuf if (state->bp && (void *)next_frame < (void *)state->bp + FRAME_HEADER_SIZE) 23724d86f59SJosh Poimboeuf goto bad_address; 23824d86f59SJosh Poimboeuf } 23924d86f59SJosh Poimboeuf 2407c7900f8SJosh Poimboeuf /* move to the next frame */ 241946c1911SJosh Poimboeuf if (regs) { 242946c1911SJosh Poimboeuf state->regs = regs; 243946c1911SJosh Poimboeuf state->bp = NULL; 244946c1911SJosh Poimboeuf } else { 2457c7900f8SJosh Poimboeuf state->bp = next_bp; 246946c1911SJosh Poimboeuf state->regs = NULL; 247946c1911SJosh Poimboeuf } 248946c1911SJosh Poimboeuf 2497c7900f8SJosh Poimboeuf return true; 250946c1911SJosh Poimboeuf 251c32c47c6SJosh Poimboeuf bad_address: 252900742d8SJosh Poimboeuf /* 253900742d8SJosh Poimboeuf * When unwinding a non-current task, the task might actually be 254900742d8SJosh Poimboeuf * running on another CPU, in which case it could be modifying its 255900742d8SJosh Poimboeuf * stack while we're reading it. This is generally not a problem and 256900742d8SJosh Poimboeuf * can be ignored as long as the caller understands that unwinding 257900742d8SJosh Poimboeuf * another task will not always succeed. 258900742d8SJosh Poimboeuf */ 259900742d8SJosh Poimboeuf if (state->task != current) 260900742d8SJosh Poimboeuf goto the_end; 261900742d8SJosh Poimboeuf 26224d86f59SJosh Poimboeuf if (state->regs) { 26324d86f59SJosh Poimboeuf printk_deferred_once(KERN_WARNING 26424d86f59SJosh Poimboeuf "WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n", 26524d86f59SJosh Poimboeuf state->regs, state->task->comm, 26624d86f59SJosh Poimboeuf state->task->pid, next_frame); 2678b5e99f0SJosh Poimboeuf unwind_dump(state, (unsigned long *)state->regs); 26824d86f59SJosh Poimboeuf } else { 269c32c47c6SJosh Poimboeuf printk_deferred_once(KERN_WARNING 270c32c47c6SJosh Poimboeuf "WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n", 271c32c47c6SJosh Poimboeuf state->bp, state->task->comm, 27224d86f59SJosh Poimboeuf state->task->pid, next_frame); 2738b5e99f0SJosh Poimboeuf unwind_dump(state, state->bp); 27424d86f59SJosh Poimboeuf } 275946c1911SJosh Poimboeuf the_end: 276946c1911SJosh Poimboeuf state->stack_info.type = STACK_TYPE_UNKNOWN; 277946c1911SJosh Poimboeuf return false; 2787c7900f8SJosh Poimboeuf } 2797c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(unwind_next_frame); 2807c7900f8SJosh Poimboeuf 2817c7900f8SJosh Poimboeuf void __unwind_start(struct unwind_state *state, struct task_struct *task, 2827c7900f8SJosh Poimboeuf struct pt_regs *regs, unsigned long *first_frame) 2837c7900f8SJosh Poimboeuf { 284946c1911SJosh Poimboeuf unsigned long *bp, *frame; 285946c1911SJosh Poimboeuf size_t len; 286946c1911SJosh Poimboeuf 2877c7900f8SJosh Poimboeuf memset(state, 0, sizeof(*state)); 2887c7900f8SJosh Poimboeuf state->task = task; 2897c7900f8SJosh Poimboeuf 2907c7900f8SJosh Poimboeuf /* don't even attempt to start from user mode regs */ 2917c7900f8SJosh Poimboeuf if (regs && user_mode(regs)) { 2927c7900f8SJosh Poimboeuf state->stack_info.type = STACK_TYPE_UNKNOWN; 2937c7900f8SJosh Poimboeuf return; 2947c7900f8SJosh Poimboeuf } 2957c7900f8SJosh Poimboeuf 2967c7900f8SJosh Poimboeuf /* set up the starting stack frame */ 297946c1911SJosh Poimboeuf bp = get_frame_pointer(task, regs); 298946c1911SJosh Poimboeuf regs = decode_frame_pointer(bp); 299946c1911SJosh Poimboeuf if (regs) { 300946c1911SJosh Poimboeuf state->regs = regs; 301946c1911SJosh Poimboeuf frame = (unsigned long *)regs; 302946c1911SJosh Poimboeuf len = sizeof(*regs); 303946c1911SJosh Poimboeuf } else { 304946c1911SJosh Poimboeuf state->bp = bp; 305946c1911SJosh Poimboeuf frame = bp; 306946c1911SJosh Poimboeuf len = FRAME_HEADER_SIZE; 307946c1911SJosh Poimboeuf } 3087c7900f8SJosh Poimboeuf 3097c7900f8SJosh Poimboeuf /* initialize stack info and make sure the frame data is accessible */ 310946c1911SJosh Poimboeuf get_stack_info(frame, state->task, &state->stack_info, 3117c7900f8SJosh Poimboeuf &state->stack_mask); 312946c1911SJosh Poimboeuf update_stack_state(state, frame, len); 3137c7900f8SJosh Poimboeuf 3147c7900f8SJosh Poimboeuf /* 3157c7900f8SJosh Poimboeuf * The caller can provide the address of the first frame directly 3167c7900f8SJosh Poimboeuf * (first_frame) or indirectly (regs->sp) to indicate which stack frame 3177c7900f8SJosh Poimboeuf * to start unwinding at. Skip ahead until we reach it. 3187c7900f8SJosh Poimboeuf */ 3197c7900f8SJosh Poimboeuf while (!unwind_done(state) && 3207c7900f8SJosh Poimboeuf (!on_stack(&state->stack_info, first_frame, sizeof(long)) || 3217c7900f8SJosh Poimboeuf state->bp < first_frame)) 3227c7900f8SJosh Poimboeuf unwind_next_frame(state); 3237c7900f8SJosh Poimboeuf } 3247c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(__unwind_start); 325