17c7900f8SJosh Poimboeuf #include <linux/sched.h> 229930025SIngo Molnar #include <linux/sched/task.h> 368db0cf1SIngo Molnar #include <linux/sched/task_stack.h> 4*a8b7a923SJosh Poimboeuf #include <linux/interrupt.h> 5*a8b7a923SJosh Poimboeuf #include <asm/sections.h> 67c7900f8SJosh Poimboeuf #include <asm/ptrace.h> 77c7900f8SJosh Poimboeuf #include <asm/bitops.h> 87c7900f8SJosh Poimboeuf #include <asm/stacktrace.h> 97c7900f8SJosh Poimboeuf #include <asm/unwind.h> 107c7900f8SJosh Poimboeuf 117c7900f8SJosh Poimboeuf #define FRAME_HEADER_SIZE (sizeof(long) * 2) 127c7900f8SJosh Poimboeuf 1384936118SJosh Poimboeuf /* 1484936118SJosh Poimboeuf * This disables KASAN checking when reading a value from another task's stack, 1584936118SJosh Poimboeuf * since the other task could be running on another CPU and could have poisoned 1684936118SJosh Poimboeuf * the stack in the meantime. 1784936118SJosh Poimboeuf */ 1884936118SJosh Poimboeuf #define READ_ONCE_TASK_STACK(task, x) \ 1984936118SJosh Poimboeuf ({ \ 2084936118SJosh Poimboeuf unsigned long val; \ 2184936118SJosh Poimboeuf if (task == current) \ 2284936118SJosh Poimboeuf val = READ_ONCE(x); \ 2384936118SJosh Poimboeuf else \ 2484936118SJosh Poimboeuf val = READ_ONCE_NOCHECK(x); \ 2584936118SJosh Poimboeuf val; \ 2684936118SJosh Poimboeuf }) 2784936118SJosh Poimboeuf 288b5e99f0SJosh Poimboeuf static void unwind_dump(struct unwind_state *state, unsigned long *sp) 298b5e99f0SJosh Poimboeuf { 308b5e99f0SJosh Poimboeuf static bool dumped_before = false; 318b5e99f0SJosh Poimboeuf bool prev_zero, zero = false; 328b5e99f0SJosh Poimboeuf unsigned long word; 338b5e99f0SJosh Poimboeuf 348b5e99f0SJosh Poimboeuf if (dumped_before) 358b5e99f0SJosh Poimboeuf return; 368b5e99f0SJosh Poimboeuf 378b5e99f0SJosh Poimboeuf dumped_before = true; 388b5e99f0SJosh Poimboeuf 398b5e99f0SJosh Poimboeuf printk_deferred("unwind stack type:%d next_sp:%p mask:%lx graph_idx:%d\n", 408b5e99f0SJosh Poimboeuf state->stack_info.type, state->stack_info.next_sp, 418b5e99f0SJosh Poimboeuf state->stack_mask, state->graph_idx); 428b5e99f0SJosh Poimboeuf 438b5e99f0SJosh Poimboeuf for (sp = state->orig_sp; sp < state->stack_info.end; sp++) { 448b5e99f0SJosh Poimboeuf word = READ_ONCE_NOCHECK(*sp); 458b5e99f0SJosh Poimboeuf 468b5e99f0SJosh Poimboeuf prev_zero = zero; 478b5e99f0SJosh Poimboeuf zero = word == 0; 488b5e99f0SJosh Poimboeuf 498b5e99f0SJosh Poimboeuf if (zero) { 508b5e99f0SJosh Poimboeuf if (!prev_zero) 518b5e99f0SJosh Poimboeuf printk_deferred("%p: %016x ...\n", sp, 0); 528b5e99f0SJosh Poimboeuf continue; 538b5e99f0SJosh Poimboeuf } 548b5e99f0SJosh Poimboeuf 558b5e99f0SJosh Poimboeuf printk_deferred("%p: %016lx (%pB)\n", sp, word, (void *)word); 568b5e99f0SJosh Poimboeuf } 578b5e99f0SJosh Poimboeuf } 588b5e99f0SJosh Poimboeuf 597c7900f8SJosh Poimboeuf unsigned long unwind_get_return_address(struct unwind_state *state) 607c7900f8SJosh Poimboeuf { 617c7900f8SJosh Poimboeuf if (unwind_done(state)) 627c7900f8SJosh Poimboeuf return 0; 637c7900f8SJosh Poimboeuf 646bcdf9d5SJosh Poimboeuf return __kernel_text_address(state->ip) ? state->ip : 0; 657c7900f8SJosh Poimboeuf } 667c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(unwind_get_return_address); 677c7900f8SJosh Poimboeuf 6824d86f59SJosh Poimboeuf static size_t regs_size(struct pt_regs *regs) 6924d86f59SJosh Poimboeuf { 7024d86f59SJosh Poimboeuf /* x86_32 regs from kernel mode are two words shorter: */ 7124d86f59SJosh Poimboeuf if (IS_ENABLED(CONFIG_X86_32) && !user_mode(regs)) 7224d86f59SJosh Poimboeuf return sizeof(*regs) - 2*sizeof(long); 7324d86f59SJosh Poimboeuf 7424d86f59SJosh Poimboeuf return sizeof(*regs); 7524d86f59SJosh Poimboeuf } 7624d86f59SJosh Poimboeuf 77*a8b7a923SJosh Poimboeuf static bool in_entry_code(unsigned long ip) 78*a8b7a923SJosh Poimboeuf { 79*a8b7a923SJosh Poimboeuf char *addr = (char *)ip; 80*a8b7a923SJosh Poimboeuf 81*a8b7a923SJosh Poimboeuf if (addr >= __entry_text_start && addr < __entry_text_end) 82*a8b7a923SJosh Poimboeuf return true; 83*a8b7a923SJosh Poimboeuf 84*a8b7a923SJosh Poimboeuf #if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN) 85*a8b7a923SJosh Poimboeuf if (addr >= __irqentry_text_start && addr < __irqentry_text_end) 86*a8b7a923SJosh Poimboeuf return true; 87*a8b7a923SJosh Poimboeuf #endif 88*a8b7a923SJosh Poimboeuf 89*a8b7a923SJosh Poimboeuf return false; 90*a8b7a923SJosh Poimboeuf } 91*a8b7a923SJosh Poimboeuf 9287a6b297SJosh Poimboeuf #ifdef CONFIG_X86_32 9387a6b297SJosh Poimboeuf #define GCC_REALIGN_WORDS 3 9487a6b297SJosh Poimboeuf #else 9587a6b297SJosh Poimboeuf #define GCC_REALIGN_WORDS 1 9687a6b297SJosh Poimboeuf #endif 9787a6b297SJosh Poimboeuf 98acb4608aSJosh Poimboeuf static bool is_last_task_frame(struct unwind_state *state) 99acb4608aSJosh Poimboeuf { 10087a6b297SJosh Poimboeuf unsigned long *last_bp = (unsigned long *)task_pt_regs(state->task) - 2; 10187a6b297SJosh Poimboeuf unsigned long *aligned_bp = last_bp - GCC_REALIGN_WORDS; 102acb4608aSJosh Poimboeuf 1038023e0e2SJosh Poimboeuf /* 1048023e0e2SJosh Poimboeuf * We have to check for the last task frame at two different locations 1058023e0e2SJosh Poimboeuf * because gcc can occasionally decide to realign the stack pointer and 10687a6b297SJosh Poimboeuf * change the offset of the stack frame in the prologue of a function 10787a6b297SJosh Poimboeuf * called by head/entry code. Examples: 10887a6b297SJosh Poimboeuf * 10987a6b297SJosh Poimboeuf * <start_secondary>: 11087a6b297SJosh Poimboeuf * push %edi 11187a6b297SJosh Poimboeuf * lea 0x8(%esp),%edi 11287a6b297SJosh Poimboeuf * and $0xfffffff8,%esp 11387a6b297SJosh Poimboeuf * pushl -0x4(%edi) 11487a6b297SJosh Poimboeuf * push %ebp 11587a6b297SJosh Poimboeuf * mov %esp,%ebp 11687a6b297SJosh Poimboeuf * 11787a6b297SJosh Poimboeuf * <x86_64_start_kernel>: 11887a6b297SJosh Poimboeuf * lea 0x8(%rsp),%r10 11987a6b297SJosh Poimboeuf * and $0xfffffffffffffff0,%rsp 12087a6b297SJosh Poimboeuf * pushq -0x8(%r10) 12187a6b297SJosh Poimboeuf * push %rbp 12287a6b297SJosh Poimboeuf * mov %rsp,%rbp 12387a6b297SJosh Poimboeuf * 12487a6b297SJosh Poimboeuf * Note that after aligning the stack, it pushes a duplicate copy of 12587a6b297SJosh Poimboeuf * the return address before pushing the frame pointer. 1268023e0e2SJosh Poimboeuf */ 12787a6b297SJosh Poimboeuf return (state->bp == last_bp || 12887a6b297SJosh Poimboeuf (state->bp == aligned_bp && *(aligned_bp+1) == *(last_bp+1))); 129acb4608aSJosh Poimboeuf } 130acb4608aSJosh Poimboeuf 131946c1911SJosh Poimboeuf /* 132946c1911SJosh Poimboeuf * This determines if the frame pointer actually contains an encoded pointer to 133946c1911SJosh Poimboeuf * pt_regs on the stack. See ENCODE_FRAME_POINTER. 134946c1911SJosh Poimboeuf */ 135946c1911SJosh Poimboeuf static struct pt_regs *decode_frame_pointer(unsigned long *bp) 136946c1911SJosh Poimboeuf { 137946c1911SJosh Poimboeuf unsigned long regs = (unsigned long)bp; 138946c1911SJosh Poimboeuf 139946c1911SJosh Poimboeuf if (!(regs & 0x1)) 140946c1911SJosh Poimboeuf return NULL; 141946c1911SJosh Poimboeuf 142946c1911SJosh Poimboeuf return (struct pt_regs *)(regs & ~0x1); 143946c1911SJosh Poimboeuf } 144946c1911SJosh Poimboeuf 1455ed8d8bbSJosh Poimboeuf static bool update_stack_state(struct unwind_state *state, 1465ed8d8bbSJosh Poimboeuf unsigned long *next_bp) 1477c7900f8SJosh Poimboeuf { 1487c7900f8SJosh Poimboeuf struct stack_info *info = &state->stack_info; 1495ed8d8bbSJosh Poimboeuf enum stack_type prev_type = info->type; 1505ed8d8bbSJosh Poimboeuf struct pt_regs *regs; 1516bcdf9d5SJosh Poimboeuf unsigned long *frame, *prev_frame_end, *addr_p, addr; 1525ed8d8bbSJosh Poimboeuf size_t len; 1535ed8d8bbSJosh Poimboeuf 1545ed8d8bbSJosh Poimboeuf if (state->regs) 1555ed8d8bbSJosh Poimboeuf prev_frame_end = (void *)state->regs + regs_size(state->regs); 1565ed8d8bbSJosh Poimboeuf else 1575ed8d8bbSJosh Poimboeuf prev_frame_end = (void *)state->bp + FRAME_HEADER_SIZE; 1585ed8d8bbSJosh Poimboeuf 1595ed8d8bbSJosh Poimboeuf /* Is the next frame pointer an encoded pointer to pt_regs? */ 1605ed8d8bbSJosh Poimboeuf regs = decode_frame_pointer(next_bp); 1615ed8d8bbSJosh Poimboeuf if (regs) { 1625ed8d8bbSJosh Poimboeuf frame = (unsigned long *)regs; 1635ed8d8bbSJosh Poimboeuf len = regs_size(regs); 164*a8b7a923SJosh Poimboeuf state->got_irq = true; 1655ed8d8bbSJosh Poimboeuf } else { 1665ed8d8bbSJosh Poimboeuf frame = next_bp; 1675ed8d8bbSJosh Poimboeuf len = FRAME_HEADER_SIZE; 1685ed8d8bbSJosh Poimboeuf } 1697c7900f8SJosh Poimboeuf 1707c7900f8SJosh Poimboeuf /* 1715ed8d8bbSJosh Poimboeuf * If the next bp isn't on the current stack, switch to the next one. 1727c7900f8SJosh Poimboeuf * 1737c7900f8SJosh Poimboeuf * We may have to traverse multiple stacks to deal with the possibility 1745ed8d8bbSJosh Poimboeuf * that info->next_sp could point to an empty stack and the next bp 1755ed8d8bbSJosh Poimboeuf * could be on a subsequent stack. 1767c7900f8SJosh Poimboeuf */ 1775ed8d8bbSJosh Poimboeuf while (!on_stack(info, frame, len)) 1787c7900f8SJosh Poimboeuf if (get_stack_info(info->next_sp, state->task, info, 1797c7900f8SJosh Poimboeuf &state->stack_mask)) 1807c7900f8SJosh Poimboeuf return false; 1817c7900f8SJosh Poimboeuf 1825ed8d8bbSJosh Poimboeuf /* Make sure it only unwinds up and doesn't overlap the prev frame: */ 1835ed8d8bbSJosh Poimboeuf if (state->orig_sp && state->stack_info.type == prev_type && 1845ed8d8bbSJosh Poimboeuf frame < prev_frame_end) 1855ed8d8bbSJosh Poimboeuf return false; 1865ed8d8bbSJosh Poimboeuf 1875ed8d8bbSJosh Poimboeuf /* Move state to the next frame: */ 1885ed8d8bbSJosh Poimboeuf if (regs) { 1895ed8d8bbSJosh Poimboeuf state->regs = regs; 1905ed8d8bbSJosh Poimboeuf state->bp = NULL; 1915ed8d8bbSJosh Poimboeuf } else { 1925ed8d8bbSJosh Poimboeuf state->bp = next_bp; 1935ed8d8bbSJosh Poimboeuf state->regs = NULL; 1945ed8d8bbSJosh Poimboeuf } 1955ed8d8bbSJosh Poimboeuf 1966bcdf9d5SJosh Poimboeuf /* Save the return address: */ 1976bcdf9d5SJosh Poimboeuf if (state->regs && user_mode(state->regs)) 1986bcdf9d5SJosh Poimboeuf state->ip = 0; 1996bcdf9d5SJosh Poimboeuf else { 2006bcdf9d5SJosh Poimboeuf addr_p = unwind_get_return_address_ptr(state); 2016bcdf9d5SJosh Poimboeuf addr = READ_ONCE_TASK_STACK(state->task, *addr_p); 2026bcdf9d5SJosh Poimboeuf state->ip = ftrace_graph_ret_addr(state->task, &state->graph_idx, 2036bcdf9d5SJosh Poimboeuf addr, addr_p); 2046bcdf9d5SJosh Poimboeuf } 2056bcdf9d5SJosh Poimboeuf 2065ed8d8bbSJosh Poimboeuf /* Save the original stack pointer for unwind_dump(): */ 2075ed8d8bbSJosh Poimboeuf if (!state->orig_sp || info->type != prev_type) 2085ed8d8bbSJosh Poimboeuf state->orig_sp = frame; 2098b5e99f0SJosh Poimboeuf 2107c7900f8SJosh Poimboeuf return true; 2117c7900f8SJosh Poimboeuf } 2127c7900f8SJosh Poimboeuf 2137c7900f8SJosh Poimboeuf bool unwind_next_frame(struct unwind_state *state) 2147c7900f8SJosh Poimboeuf { 215946c1911SJosh Poimboeuf struct pt_regs *regs; 2165ed8d8bbSJosh Poimboeuf unsigned long *next_bp; 2177c7900f8SJosh Poimboeuf 2187c7900f8SJosh Poimboeuf if (unwind_done(state)) 2197c7900f8SJosh Poimboeuf return false; 2207c7900f8SJosh Poimboeuf 2215ed8d8bbSJosh Poimboeuf /* Have we reached the end? */ 222946c1911SJosh Poimboeuf if (state->regs && user_mode(state->regs)) 223946c1911SJosh Poimboeuf goto the_end; 224946c1911SJosh Poimboeuf 225acb4608aSJosh Poimboeuf if (is_last_task_frame(state)) { 226acb4608aSJosh Poimboeuf regs = task_pt_regs(state->task); 227acb4608aSJosh Poimboeuf 228acb4608aSJosh Poimboeuf /* 229acb4608aSJosh Poimboeuf * kthreads (other than the boot CPU's idle thread) have some 230acb4608aSJosh Poimboeuf * partial regs at the end of their stack which were placed 231acb4608aSJosh Poimboeuf * there by copy_thread_tls(). But the regs don't have any 232acb4608aSJosh Poimboeuf * useful information, so we can skip them. 233acb4608aSJosh Poimboeuf * 234acb4608aSJosh Poimboeuf * This user_mode() check is slightly broader than a PF_KTHREAD 235acb4608aSJosh Poimboeuf * check because it also catches the awkward situation where a 236acb4608aSJosh Poimboeuf * newly forked kthread transitions into a user task by calling 237acb4608aSJosh Poimboeuf * do_execve(), which eventually clears PF_KTHREAD. 238acb4608aSJosh Poimboeuf */ 239acb4608aSJosh Poimboeuf if (!user_mode(regs)) 240acb4608aSJosh Poimboeuf goto the_end; 241acb4608aSJosh Poimboeuf 242acb4608aSJosh Poimboeuf /* 243acb4608aSJosh Poimboeuf * We're almost at the end, but not quite: there's still the 244acb4608aSJosh Poimboeuf * syscall regs frame. Entry code doesn't encode the regs 245acb4608aSJosh Poimboeuf * pointer for syscalls, so we have to set it manually. 246acb4608aSJosh Poimboeuf */ 247acb4608aSJosh Poimboeuf state->regs = regs; 248acb4608aSJosh Poimboeuf state->bp = NULL; 2496bcdf9d5SJosh Poimboeuf state->ip = 0; 250acb4608aSJosh Poimboeuf return true; 251acb4608aSJosh Poimboeuf } 252acb4608aSJosh Poimboeuf 2535ed8d8bbSJosh Poimboeuf /* Get the next frame pointer: */ 254946c1911SJosh Poimboeuf if (state->regs) 255946c1911SJosh Poimboeuf next_bp = (unsigned long *)state->regs->bp; 256946c1911SJosh Poimboeuf else 25784936118SJosh Poimboeuf next_bp = (unsigned long *)READ_ONCE_TASK_STACK(state->task, *state->bp); 2587c7900f8SJosh Poimboeuf 2595ed8d8bbSJosh Poimboeuf /* Move to the next frame if it's safe: */ 260*a8b7a923SJosh Poimboeuf if (!update_stack_state(state, next_bp)) 261c32c47c6SJosh Poimboeuf goto bad_address; 262c32c47c6SJosh Poimboeuf 2637c7900f8SJosh Poimboeuf return true; 264946c1911SJosh Poimboeuf 265c32c47c6SJosh Poimboeuf bad_address: 266900742d8SJosh Poimboeuf /* 267900742d8SJosh Poimboeuf * When unwinding a non-current task, the task might actually be 268900742d8SJosh Poimboeuf * running on another CPU, in which case it could be modifying its 269900742d8SJosh Poimboeuf * stack while we're reading it. This is generally not a problem and 270900742d8SJosh Poimboeuf * can be ignored as long as the caller understands that unwinding 271900742d8SJosh Poimboeuf * another task will not always succeed. 272900742d8SJosh Poimboeuf */ 273900742d8SJosh Poimboeuf if (state->task != current) 274900742d8SJosh Poimboeuf goto the_end; 275900742d8SJosh Poimboeuf 276*a8b7a923SJosh Poimboeuf /* 277*a8b7a923SJosh Poimboeuf * Don't warn if the unwinder got lost due to an interrupt in entry 278*a8b7a923SJosh Poimboeuf * code before the stack was set up: 279*a8b7a923SJosh Poimboeuf */ 280*a8b7a923SJosh Poimboeuf if (state->got_irq && in_entry_code(state->ip)) 281*a8b7a923SJosh Poimboeuf goto the_end; 282*a8b7a923SJosh Poimboeuf 28324d86f59SJosh Poimboeuf if (state->regs) { 28424d86f59SJosh Poimboeuf printk_deferred_once(KERN_WARNING 28524d86f59SJosh Poimboeuf "WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n", 28624d86f59SJosh Poimboeuf state->regs, state->task->comm, 2875ed8d8bbSJosh Poimboeuf state->task->pid, next_bp); 2888b5e99f0SJosh Poimboeuf unwind_dump(state, (unsigned long *)state->regs); 28924d86f59SJosh Poimboeuf } else { 290c32c47c6SJosh Poimboeuf printk_deferred_once(KERN_WARNING 291c32c47c6SJosh Poimboeuf "WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n", 292c32c47c6SJosh Poimboeuf state->bp, state->task->comm, 2935ed8d8bbSJosh Poimboeuf state->task->pid, next_bp); 2948b5e99f0SJosh Poimboeuf unwind_dump(state, state->bp); 29524d86f59SJosh Poimboeuf } 296946c1911SJosh Poimboeuf the_end: 297946c1911SJosh Poimboeuf state->stack_info.type = STACK_TYPE_UNKNOWN; 298946c1911SJosh Poimboeuf return false; 2997c7900f8SJosh Poimboeuf } 3007c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(unwind_next_frame); 3017c7900f8SJosh Poimboeuf 3027c7900f8SJosh Poimboeuf void __unwind_start(struct unwind_state *state, struct task_struct *task, 3037c7900f8SJosh Poimboeuf struct pt_regs *regs, unsigned long *first_frame) 3047c7900f8SJosh Poimboeuf { 3055ed8d8bbSJosh Poimboeuf unsigned long *bp; 306946c1911SJosh Poimboeuf 3077c7900f8SJosh Poimboeuf memset(state, 0, sizeof(*state)); 3087c7900f8SJosh Poimboeuf state->task = task; 309*a8b7a923SJosh Poimboeuf state->got_irq = (regs); 3107c7900f8SJosh Poimboeuf 3115ed8d8bbSJosh Poimboeuf /* Don't even attempt to start from user mode regs: */ 3127c7900f8SJosh Poimboeuf if (regs && user_mode(regs)) { 3137c7900f8SJosh Poimboeuf state->stack_info.type = STACK_TYPE_UNKNOWN; 3147c7900f8SJosh Poimboeuf return; 3157c7900f8SJosh Poimboeuf } 3167c7900f8SJosh Poimboeuf 317946c1911SJosh Poimboeuf bp = get_frame_pointer(task, regs); 3187c7900f8SJosh Poimboeuf 3195ed8d8bbSJosh Poimboeuf /* Initialize stack info and make sure the frame data is accessible: */ 3205ed8d8bbSJosh Poimboeuf get_stack_info(bp, state->task, &state->stack_info, 3217c7900f8SJosh Poimboeuf &state->stack_mask); 3225ed8d8bbSJosh Poimboeuf update_stack_state(state, bp); 3237c7900f8SJosh Poimboeuf 3247c7900f8SJosh Poimboeuf /* 3257c7900f8SJosh Poimboeuf * The caller can provide the address of the first frame directly 3267c7900f8SJosh Poimboeuf * (first_frame) or indirectly (regs->sp) to indicate which stack frame 3277c7900f8SJosh Poimboeuf * to start unwinding at. Skip ahead until we reach it. 3287c7900f8SJosh Poimboeuf */ 3297c7900f8SJosh Poimboeuf while (!unwind_done(state) && 3307c7900f8SJosh Poimboeuf (!on_stack(&state->stack_info, first_frame, sizeof(long)) || 3317c7900f8SJosh Poimboeuf state->bp < first_frame)) 3327c7900f8SJosh Poimboeuf unwind_next_frame(state); 3337c7900f8SJosh Poimboeuf } 3347c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(__unwind_start); 335