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 85acb4608aSJosh Poimboeuf static bool is_last_task_frame(struct unwind_state *state) 86acb4608aSJosh Poimboeuf { 87acb4608aSJosh Poimboeuf unsigned long bp = (unsigned long)state->bp; 88acb4608aSJosh Poimboeuf unsigned long regs = (unsigned long)task_pt_regs(state->task); 89acb4608aSJosh Poimboeuf 908023e0e2SJosh Poimboeuf /* 918023e0e2SJosh Poimboeuf * We have to check for the last task frame at two different locations 928023e0e2SJosh Poimboeuf * because gcc can occasionally decide to realign the stack pointer and 938023e0e2SJosh Poimboeuf * change the offset of the stack frame by a word in the prologue of a 948023e0e2SJosh Poimboeuf * function called by head/entry code. 958023e0e2SJosh Poimboeuf */ 968023e0e2SJosh Poimboeuf return bp == regs - FRAME_HEADER_SIZE || 978023e0e2SJosh Poimboeuf bp == regs - FRAME_HEADER_SIZE - sizeof(long); 98acb4608aSJosh Poimboeuf } 99acb4608aSJosh Poimboeuf 100946c1911SJosh Poimboeuf /* 101946c1911SJosh Poimboeuf * This determines if the frame pointer actually contains an encoded pointer to 102946c1911SJosh Poimboeuf * pt_regs on the stack. See ENCODE_FRAME_POINTER. 103946c1911SJosh Poimboeuf */ 104946c1911SJosh Poimboeuf static struct pt_regs *decode_frame_pointer(unsigned long *bp) 105946c1911SJosh Poimboeuf { 106946c1911SJosh Poimboeuf unsigned long regs = (unsigned long)bp; 107946c1911SJosh Poimboeuf 108946c1911SJosh Poimboeuf if (!(regs & 0x1)) 109946c1911SJosh Poimboeuf return NULL; 110946c1911SJosh Poimboeuf 111946c1911SJosh Poimboeuf return (struct pt_regs *)(regs & ~0x1); 112946c1911SJosh Poimboeuf } 113946c1911SJosh Poimboeuf 1147c7900f8SJosh Poimboeuf static bool update_stack_state(struct unwind_state *state, void *addr, 1157c7900f8SJosh Poimboeuf size_t len) 1167c7900f8SJosh Poimboeuf { 1177c7900f8SJosh Poimboeuf struct stack_info *info = &state->stack_info; 1188b5e99f0SJosh Poimboeuf enum stack_type orig_type = info->type; 1197c7900f8SJosh Poimboeuf 1207c7900f8SJosh Poimboeuf /* 1217c7900f8SJosh Poimboeuf * If addr isn't on the current stack, switch to the next one. 1227c7900f8SJosh Poimboeuf * 1237c7900f8SJosh Poimboeuf * We may have to traverse multiple stacks to deal with the possibility 1247c7900f8SJosh Poimboeuf * that 'info->next_sp' could point to an empty stack and 'addr' could 1257c7900f8SJosh Poimboeuf * be on a subsequent stack. 1267c7900f8SJosh Poimboeuf */ 1277c7900f8SJosh Poimboeuf while (!on_stack(info, addr, len)) 1287c7900f8SJosh Poimboeuf if (get_stack_info(info->next_sp, state->task, info, 1297c7900f8SJosh Poimboeuf &state->stack_mask)) 1307c7900f8SJosh Poimboeuf return false; 1317c7900f8SJosh Poimboeuf 1328b5e99f0SJosh Poimboeuf if (!state->orig_sp || info->type != orig_type) 1338b5e99f0SJosh Poimboeuf state->orig_sp = addr; 1348b5e99f0SJosh Poimboeuf 1357c7900f8SJosh Poimboeuf return true; 1367c7900f8SJosh Poimboeuf } 1377c7900f8SJosh Poimboeuf 1387c7900f8SJosh Poimboeuf bool unwind_next_frame(struct unwind_state *state) 1397c7900f8SJosh Poimboeuf { 140946c1911SJosh Poimboeuf struct pt_regs *regs; 141946c1911SJosh Poimboeuf unsigned long *next_bp, *next_frame; 142946c1911SJosh Poimboeuf size_t next_len; 14324d86f59SJosh Poimboeuf enum stack_type prev_type = state->stack_info.type; 1447c7900f8SJosh Poimboeuf 1457c7900f8SJosh Poimboeuf if (unwind_done(state)) 1467c7900f8SJosh Poimboeuf return false; 1477c7900f8SJosh Poimboeuf 148946c1911SJosh Poimboeuf /* have we reached the end? */ 149946c1911SJosh Poimboeuf if (state->regs && user_mode(state->regs)) 150946c1911SJosh Poimboeuf goto the_end; 151946c1911SJosh Poimboeuf 152acb4608aSJosh Poimboeuf if (is_last_task_frame(state)) { 153acb4608aSJosh Poimboeuf regs = task_pt_regs(state->task); 154acb4608aSJosh Poimboeuf 155acb4608aSJosh Poimboeuf /* 156acb4608aSJosh Poimboeuf * kthreads (other than the boot CPU's idle thread) have some 157acb4608aSJosh Poimboeuf * partial regs at the end of their stack which were placed 158acb4608aSJosh Poimboeuf * there by copy_thread_tls(). But the regs don't have any 159acb4608aSJosh Poimboeuf * useful information, so we can skip them. 160acb4608aSJosh Poimboeuf * 161acb4608aSJosh Poimboeuf * This user_mode() check is slightly broader than a PF_KTHREAD 162acb4608aSJosh Poimboeuf * check because it also catches the awkward situation where a 163acb4608aSJosh Poimboeuf * newly forked kthread transitions into a user task by calling 164acb4608aSJosh Poimboeuf * do_execve(), which eventually clears PF_KTHREAD. 165acb4608aSJosh Poimboeuf */ 166acb4608aSJosh Poimboeuf if (!user_mode(regs)) 167acb4608aSJosh Poimboeuf goto the_end; 168acb4608aSJosh Poimboeuf 169acb4608aSJosh Poimboeuf /* 170acb4608aSJosh Poimboeuf * We're almost at the end, but not quite: there's still the 171acb4608aSJosh Poimboeuf * syscall regs frame. Entry code doesn't encode the regs 172acb4608aSJosh Poimboeuf * pointer for syscalls, so we have to set it manually. 173acb4608aSJosh Poimboeuf */ 174acb4608aSJosh Poimboeuf state->regs = regs; 175acb4608aSJosh Poimboeuf state->bp = NULL; 176acb4608aSJosh Poimboeuf return true; 177acb4608aSJosh Poimboeuf } 178acb4608aSJosh Poimboeuf 179946c1911SJosh Poimboeuf /* get the next frame pointer */ 180946c1911SJosh Poimboeuf if (state->regs) 181946c1911SJosh Poimboeuf next_bp = (unsigned long *)state->regs->bp; 182946c1911SJosh Poimboeuf else 18384936118SJosh Poimboeuf next_bp = (unsigned long *)READ_ONCE_TASK_STACK(state->task,*state->bp); 1847c7900f8SJosh Poimboeuf 185946c1911SJosh Poimboeuf /* is the next frame pointer an encoded pointer to pt_regs? */ 186946c1911SJosh Poimboeuf regs = decode_frame_pointer(next_bp); 187946c1911SJosh Poimboeuf if (regs) { 188946c1911SJosh Poimboeuf next_frame = (unsigned long *)regs; 189946c1911SJosh Poimboeuf next_len = sizeof(*regs); 190946c1911SJosh Poimboeuf } else { 191946c1911SJosh Poimboeuf next_frame = next_bp; 192946c1911SJosh Poimboeuf next_len = FRAME_HEADER_SIZE; 193946c1911SJosh Poimboeuf } 1947c7900f8SJosh Poimboeuf 195946c1911SJosh Poimboeuf /* make sure the next frame's data is accessible */ 196c32c47c6SJosh Poimboeuf if (!update_stack_state(state, next_frame, next_len)) { 197c32c47c6SJosh Poimboeuf /* 198c32c47c6SJosh Poimboeuf * Don't warn on bad regs->bp. An interrupt in entry code 199c32c47c6SJosh Poimboeuf * might cause a false positive warning. 200c32c47c6SJosh Poimboeuf */ 201c32c47c6SJosh Poimboeuf if (state->regs) 202c32c47c6SJosh Poimboeuf goto the_end; 203c32c47c6SJosh Poimboeuf 204c32c47c6SJosh Poimboeuf goto bad_address; 205c32c47c6SJosh Poimboeuf } 206c32c47c6SJosh Poimboeuf 20724d86f59SJosh Poimboeuf /* Make sure it only unwinds up and doesn't overlap the last frame: */ 20824d86f59SJosh Poimboeuf if (state->stack_info.type == prev_type) { 20924d86f59SJosh Poimboeuf if (state->regs && (void *)next_frame < (void *)state->regs + regs_size(state->regs)) 21024d86f59SJosh Poimboeuf goto bad_address; 21124d86f59SJosh Poimboeuf 21224d86f59SJosh Poimboeuf if (state->bp && (void *)next_frame < (void *)state->bp + FRAME_HEADER_SIZE) 21324d86f59SJosh Poimboeuf goto bad_address; 21424d86f59SJosh Poimboeuf } 21524d86f59SJosh Poimboeuf 2167c7900f8SJosh Poimboeuf /* move to the next frame */ 217946c1911SJosh Poimboeuf if (regs) { 218946c1911SJosh Poimboeuf state->regs = regs; 219946c1911SJosh Poimboeuf state->bp = NULL; 220946c1911SJosh Poimboeuf } else { 2217c7900f8SJosh Poimboeuf state->bp = next_bp; 222946c1911SJosh Poimboeuf state->regs = NULL; 223946c1911SJosh Poimboeuf } 224946c1911SJosh Poimboeuf 2257c7900f8SJosh Poimboeuf return true; 226946c1911SJosh Poimboeuf 227c32c47c6SJosh Poimboeuf bad_address: 228*af085d90SJosh Poimboeuf state->error = true; 229*af085d90SJosh Poimboeuf 230900742d8SJosh Poimboeuf /* 231900742d8SJosh Poimboeuf * When unwinding a non-current task, the task might actually be 232900742d8SJosh Poimboeuf * running on another CPU, in which case it could be modifying its 233900742d8SJosh Poimboeuf * stack while we're reading it. This is generally not a problem and 234900742d8SJosh Poimboeuf * can be ignored as long as the caller understands that unwinding 235900742d8SJosh Poimboeuf * another task will not always succeed. 236900742d8SJosh Poimboeuf */ 237900742d8SJosh Poimboeuf if (state->task != current) 238900742d8SJosh Poimboeuf goto the_end; 239900742d8SJosh Poimboeuf 24024d86f59SJosh Poimboeuf if (state->regs) { 24124d86f59SJosh Poimboeuf printk_deferred_once(KERN_WARNING 24224d86f59SJosh Poimboeuf "WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n", 24324d86f59SJosh Poimboeuf state->regs, state->task->comm, 24424d86f59SJosh Poimboeuf state->task->pid, next_frame); 2458b5e99f0SJosh Poimboeuf unwind_dump(state, (unsigned long *)state->regs); 24624d86f59SJosh Poimboeuf } else { 247c32c47c6SJosh Poimboeuf printk_deferred_once(KERN_WARNING 248c32c47c6SJosh Poimboeuf "WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n", 249c32c47c6SJosh Poimboeuf state->bp, state->task->comm, 25024d86f59SJosh Poimboeuf state->task->pid, next_frame); 2518b5e99f0SJosh Poimboeuf unwind_dump(state, state->bp); 25224d86f59SJosh Poimboeuf } 253946c1911SJosh Poimboeuf the_end: 254946c1911SJosh Poimboeuf state->stack_info.type = STACK_TYPE_UNKNOWN; 255946c1911SJosh Poimboeuf return false; 2567c7900f8SJosh Poimboeuf } 2577c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(unwind_next_frame); 2587c7900f8SJosh Poimboeuf 2597c7900f8SJosh Poimboeuf void __unwind_start(struct unwind_state *state, struct task_struct *task, 2607c7900f8SJosh Poimboeuf struct pt_regs *regs, unsigned long *first_frame) 2617c7900f8SJosh Poimboeuf { 262946c1911SJosh Poimboeuf unsigned long *bp, *frame; 263946c1911SJosh Poimboeuf size_t len; 264946c1911SJosh Poimboeuf 2657c7900f8SJosh Poimboeuf memset(state, 0, sizeof(*state)); 2667c7900f8SJosh Poimboeuf state->task = task; 2677c7900f8SJosh Poimboeuf 2687c7900f8SJosh Poimboeuf /* don't even attempt to start from user mode regs */ 2697c7900f8SJosh Poimboeuf if (regs && user_mode(regs)) { 2707c7900f8SJosh Poimboeuf state->stack_info.type = STACK_TYPE_UNKNOWN; 2717c7900f8SJosh Poimboeuf return; 2727c7900f8SJosh Poimboeuf } 2737c7900f8SJosh Poimboeuf 2747c7900f8SJosh Poimboeuf /* set up the starting stack frame */ 275946c1911SJosh Poimboeuf bp = get_frame_pointer(task, regs); 276946c1911SJosh Poimboeuf regs = decode_frame_pointer(bp); 277946c1911SJosh Poimboeuf if (regs) { 278946c1911SJosh Poimboeuf state->regs = regs; 279946c1911SJosh Poimboeuf frame = (unsigned long *)regs; 280946c1911SJosh Poimboeuf len = sizeof(*regs); 281946c1911SJosh Poimboeuf } else { 282946c1911SJosh Poimboeuf state->bp = bp; 283946c1911SJosh Poimboeuf frame = bp; 284946c1911SJosh Poimboeuf len = FRAME_HEADER_SIZE; 285946c1911SJosh Poimboeuf } 2867c7900f8SJosh Poimboeuf 2877c7900f8SJosh Poimboeuf /* initialize stack info and make sure the frame data is accessible */ 288946c1911SJosh Poimboeuf get_stack_info(frame, state->task, &state->stack_info, 2897c7900f8SJosh Poimboeuf &state->stack_mask); 290946c1911SJosh Poimboeuf update_stack_state(state, frame, len); 2917c7900f8SJosh Poimboeuf 2927c7900f8SJosh Poimboeuf /* 2937c7900f8SJosh Poimboeuf * The caller can provide the address of the first frame directly 2947c7900f8SJosh Poimboeuf * (first_frame) or indirectly (regs->sp) to indicate which stack frame 2957c7900f8SJosh Poimboeuf * to start unwinding at. Skip ahead until we reach it. 2967c7900f8SJosh Poimboeuf */ 2977c7900f8SJosh Poimboeuf while (!unwind_done(state) && 2987c7900f8SJosh Poimboeuf (!on_stack(&state->stack_info, first_frame, sizeof(long)) || 2997c7900f8SJosh Poimboeuf state->bp < first_frame)) 3007c7900f8SJosh Poimboeuf unwind_next_frame(state); 3017c7900f8SJosh Poimboeuf } 3027c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(__unwind_start); 303