17c7900f8SJosh Poimboeuf #include <linux/sched.h> 229930025SIngo Molnar #include <linux/sched/task.h> 368db0cf1SIngo Molnar #include <linux/sched/task_stack.h> 4a8b7a923SJosh Poimboeuf #include <linux/interrupt.h> 5a8b7a923SJosh 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 39*4ea3d741SJosh Poimboeuf printk_deferred("unwind stack type:%d next_sp:%p mask:0x%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) 519b135b23SJosh Poimboeuf printk_deferred("%p: %0*x ...\n", 529b135b23SJosh Poimboeuf sp, BITS_PER_LONG/4, 0); 538b5e99f0SJosh Poimboeuf continue; 548b5e99f0SJosh Poimboeuf } 558b5e99f0SJosh Poimboeuf 569b135b23SJosh Poimboeuf printk_deferred("%p: %0*lx (%pB)\n", 579b135b23SJosh Poimboeuf sp, BITS_PER_LONG/4, word, (void *)word); 588b5e99f0SJosh Poimboeuf } 598b5e99f0SJosh Poimboeuf } 608b5e99f0SJosh Poimboeuf 617c7900f8SJosh Poimboeuf unsigned long unwind_get_return_address(struct unwind_state *state) 627c7900f8SJosh Poimboeuf { 637c7900f8SJosh Poimboeuf if (unwind_done(state)) 647c7900f8SJosh Poimboeuf return 0; 657c7900f8SJosh Poimboeuf 666bcdf9d5SJosh Poimboeuf return __kernel_text_address(state->ip) ? state->ip : 0; 677c7900f8SJosh Poimboeuf } 687c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(unwind_get_return_address); 697c7900f8SJosh Poimboeuf 7024d86f59SJosh Poimboeuf static size_t regs_size(struct pt_regs *regs) 7124d86f59SJosh Poimboeuf { 7224d86f59SJosh Poimboeuf /* x86_32 regs from kernel mode are two words shorter: */ 7324d86f59SJosh Poimboeuf if (IS_ENABLED(CONFIG_X86_32) && !user_mode(regs)) 7424d86f59SJosh Poimboeuf return sizeof(*regs) - 2*sizeof(long); 7524d86f59SJosh Poimboeuf 7624d86f59SJosh Poimboeuf return sizeof(*regs); 7724d86f59SJosh Poimboeuf } 7824d86f59SJosh Poimboeuf 79a8b7a923SJosh Poimboeuf static bool in_entry_code(unsigned long ip) 80a8b7a923SJosh Poimboeuf { 81a8b7a923SJosh Poimboeuf char *addr = (char *)ip; 82a8b7a923SJosh Poimboeuf 83a8b7a923SJosh Poimboeuf if (addr >= __entry_text_start && addr < __entry_text_end) 84a8b7a923SJosh Poimboeuf return true; 85a8b7a923SJosh Poimboeuf 86a8b7a923SJosh Poimboeuf #if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN) 87a8b7a923SJosh Poimboeuf if (addr >= __irqentry_text_start && addr < __irqentry_text_end) 88a8b7a923SJosh Poimboeuf return true; 89a8b7a923SJosh Poimboeuf #endif 90a8b7a923SJosh Poimboeuf 91a8b7a923SJosh Poimboeuf return false; 92a8b7a923SJosh Poimboeuf } 93a8b7a923SJosh Poimboeuf 9487a6b297SJosh Poimboeuf #ifdef CONFIG_X86_32 9587a6b297SJosh Poimboeuf #define GCC_REALIGN_WORDS 3 9687a6b297SJosh Poimboeuf #else 9787a6b297SJosh Poimboeuf #define GCC_REALIGN_WORDS 1 9887a6b297SJosh Poimboeuf #endif 9987a6b297SJosh Poimboeuf 100acb4608aSJosh Poimboeuf static bool is_last_task_frame(struct unwind_state *state) 101acb4608aSJosh Poimboeuf { 10287a6b297SJosh Poimboeuf unsigned long *last_bp = (unsigned long *)task_pt_regs(state->task) - 2; 10387a6b297SJosh Poimboeuf unsigned long *aligned_bp = last_bp - GCC_REALIGN_WORDS; 104acb4608aSJosh Poimboeuf 1058023e0e2SJosh Poimboeuf /* 1068023e0e2SJosh Poimboeuf * We have to check for the last task frame at two different locations 1078023e0e2SJosh Poimboeuf * because gcc can occasionally decide to realign the stack pointer and 10887a6b297SJosh Poimboeuf * change the offset of the stack frame in the prologue of a function 10987a6b297SJosh Poimboeuf * called by head/entry code. Examples: 11087a6b297SJosh Poimboeuf * 11187a6b297SJosh Poimboeuf * <start_secondary>: 11287a6b297SJosh Poimboeuf * push %edi 11387a6b297SJosh Poimboeuf * lea 0x8(%esp),%edi 11487a6b297SJosh Poimboeuf * and $0xfffffff8,%esp 11587a6b297SJosh Poimboeuf * pushl -0x4(%edi) 11687a6b297SJosh Poimboeuf * push %ebp 11787a6b297SJosh Poimboeuf * mov %esp,%ebp 11887a6b297SJosh Poimboeuf * 11987a6b297SJosh Poimboeuf * <x86_64_start_kernel>: 12087a6b297SJosh Poimboeuf * lea 0x8(%rsp),%r10 12187a6b297SJosh Poimboeuf * and $0xfffffffffffffff0,%rsp 12287a6b297SJosh Poimboeuf * pushq -0x8(%r10) 12387a6b297SJosh Poimboeuf * push %rbp 12487a6b297SJosh Poimboeuf * mov %rsp,%rbp 12587a6b297SJosh Poimboeuf * 12687a6b297SJosh Poimboeuf * Note that after aligning the stack, it pushes a duplicate copy of 12787a6b297SJosh Poimboeuf * the return address before pushing the frame pointer. 1288023e0e2SJosh Poimboeuf */ 12987a6b297SJosh Poimboeuf return (state->bp == last_bp || 13087a6b297SJosh Poimboeuf (state->bp == aligned_bp && *(aligned_bp+1) == *(last_bp+1))); 131acb4608aSJosh Poimboeuf } 132acb4608aSJosh Poimboeuf 133946c1911SJosh Poimboeuf /* 134946c1911SJosh Poimboeuf * This determines if the frame pointer actually contains an encoded pointer to 135946c1911SJosh Poimboeuf * pt_regs on the stack. See ENCODE_FRAME_POINTER. 136946c1911SJosh Poimboeuf */ 137946c1911SJosh Poimboeuf static struct pt_regs *decode_frame_pointer(unsigned long *bp) 138946c1911SJosh Poimboeuf { 139946c1911SJosh Poimboeuf unsigned long regs = (unsigned long)bp; 140946c1911SJosh Poimboeuf 141946c1911SJosh Poimboeuf if (!(regs & 0x1)) 142946c1911SJosh Poimboeuf return NULL; 143946c1911SJosh Poimboeuf 144946c1911SJosh Poimboeuf return (struct pt_regs *)(regs & ~0x1); 145946c1911SJosh Poimboeuf } 146946c1911SJosh Poimboeuf 1475ed8d8bbSJosh Poimboeuf static bool update_stack_state(struct unwind_state *state, 1485ed8d8bbSJosh Poimboeuf unsigned long *next_bp) 1497c7900f8SJosh Poimboeuf { 1507c7900f8SJosh Poimboeuf struct stack_info *info = &state->stack_info; 1515ed8d8bbSJosh Poimboeuf enum stack_type prev_type = info->type; 1525ed8d8bbSJosh Poimboeuf struct pt_regs *regs; 1536bcdf9d5SJosh Poimboeuf unsigned long *frame, *prev_frame_end, *addr_p, addr; 1545ed8d8bbSJosh Poimboeuf size_t len; 1555ed8d8bbSJosh Poimboeuf 1565ed8d8bbSJosh Poimboeuf if (state->regs) 1575ed8d8bbSJosh Poimboeuf prev_frame_end = (void *)state->regs + regs_size(state->regs); 1585ed8d8bbSJosh Poimboeuf else 1595ed8d8bbSJosh Poimboeuf prev_frame_end = (void *)state->bp + FRAME_HEADER_SIZE; 1605ed8d8bbSJosh Poimboeuf 1615ed8d8bbSJosh Poimboeuf /* Is the next frame pointer an encoded pointer to pt_regs? */ 1625ed8d8bbSJosh Poimboeuf regs = decode_frame_pointer(next_bp); 1635ed8d8bbSJosh Poimboeuf if (regs) { 1645ed8d8bbSJosh Poimboeuf frame = (unsigned long *)regs; 1655ed8d8bbSJosh Poimboeuf len = regs_size(regs); 166a8b7a923SJosh Poimboeuf state->got_irq = true; 1675ed8d8bbSJosh Poimboeuf } else { 1685ed8d8bbSJosh Poimboeuf frame = next_bp; 1695ed8d8bbSJosh Poimboeuf len = FRAME_HEADER_SIZE; 1705ed8d8bbSJosh Poimboeuf } 1717c7900f8SJosh Poimboeuf 1727c7900f8SJosh Poimboeuf /* 1735ed8d8bbSJosh Poimboeuf * If the next bp isn't on the current stack, switch to the next one. 1747c7900f8SJosh Poimboeuf * 1757c7900f8SJosh Poimboeuf * We may have to traverse multiple stacks to deal with the possibility 1765ed8d8bbSJosh Poimboeuf * that info->next_sp could point to an empty stack and the next bp 1775ed8d8bbSJosh Poimboeuf * could be on a subsequent stack. 1787c7900f8SJosh Poimboeuf */ 1795ed8d8bbSJosh Poimboeuf while (!on_stack(info, frame, len)) 1807c7900f8SJosh Poimboeuf if (get_stack_info(info->next_sp, state->task, info, 1817c7900f8SJosh Poimboeuf &state->stack_mask)) 1827c7900f8SJosh Poimboeuf return false; 1837c7900f8SJosh Poimboeuf 1845ed8d8bbSJosh Poimboeuf /* Make sure it only unwinds up and doesn't overlap the prev frame: */ 1855ed8d8bbSJosh Poimboeuf if (state->orig_sp && state->stack_info.type == prev_type && 1865ed8d8bbSJosh Poimboeuf frame < prev_frame_end) 1875ed8d8bbSJosh Poimboeuf return false; 1885ed8d8bbSJosh Poimboeuf 1895ed8d8bbSJosh Poimboeuf /* Move state to the next frame: */ 1905ed8d8bbSJosh Poimboeuf if (regs) { 1915ed8d8bbSJosh Poimboeuf state->regs = regs; 1925ed8d8bbSJosh Poimboeuf state->bp = NULL; 1935ed8d8bbSJosh Poimboeuf } else { 1945ed8d8bbSJosh Poimboeuf state->bp = next_bp; 1955ed8d8bbSJosh Poimboeuf state->regs = NULL; 1965ed8d8bbSJosh Poimboeuf } 1975ed8d8bbSJosh Poimboeuf 1986bcdf9d5SJosh Poimboeuf /* Save the return address: */ 1996bcdf9d5SJosh Poimboeuf if (state->regs && user_mode(state->regs)) 2006bcdf9d5SJosh Poimboeuf state->ip = 0; 2016bcdf9d5SJosh Poimboeuf else { 2026bcdf9d5SJosh Poimboeuf addr_p = unwind_get_return_address_ptr(state); 2036bcdf9d5SJosh Poimboeuf addr = READ_ONCE_TASK_STACK(state->task, *addr_p); 2046bcdf9d5SJosh Poimboeuf state->ip = ftrace_graph_ret_addr(state->task, &state->graph_idx, 2056bcdf9d5SJosh Poimboeuf addr, addr_p); 2066bcdf9d5SJosh Poimboeuf } 2076bcdf9d5SJosh Poimboeuf 2085ed8d8bbSJosh Poimboeuf /* Save the original stack pointer for unwind_dump(): */ 2095ed8d8bbSJosh Poimboeuf if (!state->orig_sp || info->type != prev_type) 2105ed8d8bbSJosh Poimboeuf state->orig_sp = frame; 2118b5e99f0SJosh Poimboeuf 2127c7900f8SJosh Poimboeuf return true; 2137c7900f8SJosh Poimboeuf } 2147c7900f8SJosh Poimboeuf 2157c7900f8SJosh Poimboeuf bool unwind_next_frame(struct unwind_state *state) 2167c7900f8SJosh Poimboeuf { 217946c1911SJosh Poimboeuf struct pt_regs *regs; 2185ed8d8bbSJosh Poimboeuf unsigned long *next_bp; 2197c7900f8SJosh Poimboeuf 2207c7900f8SJosh Poimboeuf if (unwind_done(state)) 2217c7900f8SJosh Poimboeuf return false; 2227c7900f8SJosh Poimboeuf 2235ed8d8bbSJosh Poimboeuf /* Have we reached the end? */ 224946c1911SJosh Poimboeuf if (state->regs && user_mode(state->regs)) 225946c1911SJosh Poimboeuf goto the_end; 226946c1911SJosh Poimboeuf 227acb4608aSJosh Poimboeuf if (is_last_task_frame(state)) { 228acb4608aSJosh Poimboeuf regs = task_pt_regs(state->task); 229acb4608aSJosh Poimboeuf 230acb4608aSJosh Poimboeuf /* 231acb4608aSJosh Poimboeuf * kthreads (other than the boot CPU's idle thread) have some 232acb4608aSJosh Poimboeuf * partial regs at the end of their stack which were placed 233acb4608aSJosh Poimboeuf * there by copy_thread_tls(). But the regs don't have any 234acb4608aSJosh Poimboeuf * useful information, so we can skip them. 235acb4608aSJosh Poimboeuf * 236acb4608aSJosh Poimboeuf * This user_mode() check is slightly broader than a PF_KTHREAD 237acb4608aSJosh Poimboeuf * check because it also catches the awkward situation where a 238acb4608aSJosh Poimboeuf * newly forked kthread transitions into a user task by calling 239acb4608aSJosh Poimboeuf * do_execve(), which eventually clears PF_KTHREAD. 240acb4608aSJosh Poimboeuf */ 241acb4608aSJosh Poimboeuf if (!user_mode(regs)) 242acb4608aSJosh Poimboeuf goto the_end; 243acb4608aSJosh Poimboeuf 244acb4608aSJosh Poimboeuf /* 245acb4608aSJosh Poimboeuf * We're almost at the end, but not quite: there's still the 246acb4608aSJosh Poimboeuf * syscall regs frame. Entry code doesn't encode the regs 247acb4608aSJosh Poimboeuf * pointer for syscalls, so we have to set it manually. 248acb4608aSJosh Poimboeuf */ 249acb4608aSJosh Poimboeuf state->regs = regs; 250acb4608aSJosh Poimboeuf state->bp = NULL; 2516bcdf9d5SJosh Poimboeuf state->ip = 0; 252acb4608aSJosh Poimboeuf return true; 253acb4608aSJosh Poimboeuf } 254acb4608aSJosh Poimboeuf 2555ed8d8bbSJosh Poimboeuf /* Get the next frame pointer: */ 256946c1911SJosh Poimboeuf if (state->regs) 257946c1911SJosh Poimboeuf next_bp = (unsigned long *)state->regs->bp; 258946c1911SJosh Poimboeuf else 25984936118SJosh Poimboeuf next_bp = (unsigned long *)READ_ONCE_TASK_STACK(state->task, *state->bp); 2607c7900f8SJosh Poimboeuf 2615ed8d8bbSJosh Poimboeuf /* Move to the next frame if it's safe: */ 262a8b7a923SJosh Poimboeuf if (!update_stack_state(state, next_bp)) 263c32c47c6SJosh Poimboeuf goto bad_address; 264c32c47c6SJosh Poimboeuf 2657c7900f8SJosh Poimboeuf return true; 266946c1911SJosh Poimboeuf 267c32c47c6SJosh Poimboeuf bad_address: 268900742d8SJosh Poimboeuf /* 269900742d8SJosh Poimboeuf * When unwinding a non-current task, the task might actually be 270900742d8SJosh Poimboeuf * running on another CPU, in which case it could be modifying its 271900742d8SJosh Poimboeuf * stack while we're reading it. This is generally not a problem and 272900742d8SJosh Poimboeuf * can be ignored as long as the caller understands that unwinding 273900742d8SJosh Poimboeuf * another task will not always succeed. 274900742d8SJosh Poimboeuf */ 275900742d8SJosh Poimboeuf if (state->task != current) 276900742d8SJosh Poimboeuf goto the_end; 277900742d8SJosh Poimboeuf 278a8b7a923SJosh Poimboeuf /* 279a8b7a923SJosh Poimboeuf * Don't warn if the unwinder got lost due to an interrupt in entry 280a8b7a923SJosh Poimboeuf * code before the stack was set up: 281a8b7a923SJosh Poimboeuf */ 282a8b7a923SJosh Poimboeuf if (state->got_irq && in_entry_code(state->ip)) 283a8b7a923SJosh Poimboeuf goto the_end; 284a8b7a923SJosh Poimboeuf 28524d86f59SJosh Poimboeuf if (state->regs) { 28624d86f59SJosh Poimboeuf printk_deferred_once(KERN_WARNING 28724d86f59SJosh Poimboeuf "WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n", 28824d86f59SJosh Poimboeuf state->regs, state->task->comm, 2895ed8d8bbSJosh Poimboeuf state->task->pid, next_bp); 2908b5e99f0SJosh Poimboeuf unwind_dump(state, (unsigned long *)state->regs); 29124d86f59SJosh Poimboeuf } else { 292c32c47c6SJosh Poimboeuf printk_deferred_once(KERN_WARNING 293c32c47c6SJosh Poimboeuf "WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n", 294c32c47c6SJosh Poimboeuf state->bp, state->task->comm, 2955ed8d8bbSJosh Poimboeuf state->task->pid, next_bp); 2968b5e99f0SJosh Poimboeuf unwind_dump(state, state->bp); 29724d86f59SJosh Poimboeuf } 298946c1911SJosh Poimboeuf the_end: 299946c1911SJosh Poimboeuf state->stack_info.type = STACK_TYPE_UNKNOWN; 300946c1911SJosh Poimboeuf return false; 3017c7900f8SJosh Poimboeuf } 3027c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(unwind_next_frame); 3037c7900f8SJosh Poimboeuf 3047c7900f8SJosh Poimboeuf void __unwind_start(struct unwind_state *state, struct task_struct *task, 3057c7900f8SJosh Poimboeuf struct pt_regs *regs, unsigned long *first_frame) 3067c7900f8SJosh Poimboeuf { 3075ed8d8bbSJosh Poimboeuf unsigned long *bp; 308946c1911SJosh Poimboeuf 3097c7900f8SJosh Poimboeuf memset(state, 0, sizeof(*state)); 3107c7900f8SJosh Poimboeuf state->task = task; 311a8b7a923SJosh Poimboeuf state->got_irq = (regs); 3127c7900f8SJosh Poimboeuf 3135ed8d8bbSJosh Poimboeuf /* Don't even attempt to start from user mode regs: */ 3147c7900f8SJosh Poimboeuf if (regs && user_mode(regs)) { 3157c7900f8SJosh Poimboeuf state->stack_info.type = STACK_TYPE_UNKNOWN; 3167c7900f8SJosh Poimboeuf return; 3177c7900f8SJosh Poimboeuf } 3187c7900f8SJosh Poimboeuf 319946c1911SJosh Poimboeuf bp = get_frame_pointer(task, regs); 3207c7900f8SJosh Poimboeuf 3215ed8d8bbSJosh Poimboeuf /* Initialize stack info and make sure the frame data is accessible: */ 3225ed8d8bbSJosh Poimboeuf get_stack_info(bp, state->task, &state->stack_info, 3237c7900f8SJosh Poimboeuf &state->stack_mask); 3245ed8d8bbSJosh Poimboeuf update_stack_state(state, bp); 3257c7900f8SJosh Poimboeuf 3267c7900f8SJosh Poimboeuf /* 3277c7900f8SJosh Poimboeuf * The caller can provide the address of the first frame directly 3287c7900f8SJosh Poimboeuf * (first_frame) or indirectly (regs->sp) to indicate which stack frame 3297c7900f8SJosh Poimboeuf * to start unwinding at. Skip ahead until we reach it. 3307c7900f8SJosh Poimboeuf */ 3317c7900f8SJosh Poimboeuf while (!unwind_done(state) && 3327c7900f8SJosh Poimboeuf (!on_stack(&state->stack_info, first_frame, sizeof(long)) || 3337c7900f8SJosh Poimboeuf state->bp < first_frame)) 3347c7900f8SJosh Poimboeuf unwind_next_frame(state); 3357c7900f8SJosh Poimboeuf } 3367c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(__unwind_start); 337