1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Stack tracing support 4 * 5 * Copyright (C) 2012 ARM Ltd. 6 */ 7 #include <linux/kernel.h> 8 #include <linux/export.h> 9 #include <linux/ftrace.h> 10 #include <linux/sched.h> 11 #include <linux/sched/debug.h> 12 #include <linux/sched/task_stack.h> 13 #include <linux/stacktrace.h> 14 15 #include <asm/irq.h> 16 #include <asm/stack_pointer.h> 17 #include <asm/stacktrace.h> 18 19 /* 20 * Start an unwind from a pt_regs. 21 * 22 * The unwind will begin at the PC within the regs. 23 * 24 * The regs must be on a stack currently owned by the calling task. 25 */ 26 static inline void unwind_init_from_regs(struct unwind_state *state, 27 struct pt_regs *regs) 28 { 29 unwind_init_common(state, current); 30 31 state->fp = regs->regs[29]; 32 state->pc = regs->pc; 33 } 34 35 /* 36 * Start an unwind from a caller. 37 * 38 * The unwind will begin at the caller of whichever function this is inlined 39 * into. 40 * 41 * The function which invokes this must be noinline. 42 */ 43 static __always_inline void unwind_init_from_caller(struct unwind_state *state) 44 { 45 unwind_init_common(state, current); 46 47 state->fp = (unsigned long)__builtin_frame_address(1); 48 state->pc = (unsigned long)__builtin_return_address(0); 49 } 50 51 /* 52 * Start an unwind from a blocked task. 53 * 54 * The unwind will begin at the blocked tasks saved PC (i.e. the caller of 55 * cpu_switch_to()). 56 * 57 * The caller should ensure the task is blocked in cpu_switch_to() for the 58 * duration of the unwind, or the unwind will be bogus. It is never valid to 59 * call this for the current task. 60 */ 61 static inline void unwind_init_from_task(struct unwind_state *state, 62 struct task_struct *task) 63 { 64 unwind_init_common(state, task); 65 66 state->fp = thread_saved_fp(task); 67 state->pc = thread_saved_pc(task); 68 } 69 70 /* 71 * We can only safely access per-cpu stacks from current in a non-preemptible 72 * context. 73 */ 74 static bool on_accessible_stack(const struct task_struct *tsk, 75 unsigned long sp, unsigned long size, 76 struct stack_info *info) 77 { 78 if (info) 79 info->type = STACK_TYPE_UNKNOWN; 80 81 if (on_task_stack(tsk, sp, size, info)) 82 return true; 83 if (tsk != current || preemptible()) 84 return false; 85 if (on_irq_stack(sp, size, info)) 86 return true; 87 if (on_overflow_stack(sp, size, info)) 88 return true; 89 90 if (IS_ENABLED(CONFIG_VMAP_STACK) && 91 IS_ENABLED(CONFIG_ARM_SDE_INTERFACE) && 92 in_nmi()) { 93 if (on_sdei_critical_stack(sp, size, info)) 94 return true; 95 if (on_sdei_normal_stack(sp, size, info)) 96 return true; 97 } 98 99 return false; 100 } 101 102 /* 103 * Unwind from one frame record (A) to the next frame record (B). 104 * 105 * We terminate early if the location of B indicates a malformed chain of frame 106 * records (e.g. a cycle), determined based on the location and fp value of A 107 * and the location (but not the fp value) of B. 108 */ 109 static int notrace unwind_next(struct unwind_state *state) 110 { 111 struct task_struct *tsk = state->task; 112 unsigned long fp = state->fp; 113 int err; 114 115 /* Final frame; nothing to unwind */ 116 if (fp == (unsigned long)task_pt_regs(tsk)->stackframe) 117 return -ENOENT; 118 119 err = unwind_next_frame_record(state, on_accessible_stack, NULL); 120 if (err) 121 return err; 122 123 state->pc = ptrauth_strip_insn_pac(state->pc); 124 125 #ifdef CONFIG_FUNCTION_GRAPH_TRACER 126 if (tsk->ret_stack && 127 (state->pc == (unsigned long)return_to_handler)) { 128 unsigned long orig_pc; 129 /* 130 * This is a case where function graph tracer has 131 * modified a return address (LR) in a stack frame 132 * to hook a function return. 133 * So replace it to an original value. 134 */ 135 orig_pc = ftrace_graph_ret_addr(tsk, NULL, state->pc, 136 (void *)state->fp); 137 if (WARN_ON_ONCE(state->pc == orig_pc)) 138 return -EINVAL; 139 state->pc = orig_pc; 140 } 141 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ 142 #ifdef CONFIG_KRETPROBES 143 if (is_kretprobe_trampoline(state->pc)) 144 state->pc = kretprobe_find_ret_addr(tsk, (void *)state->fp, &state->kr_cur); 145 #endif 146 147 return 0; 148 } 149 NOKPROBE_SYMBOL(unwind_next); 150 151 static void notrace unwind(struct unwind_state *state, 152 stack_trace_consume_fn consume_entry, void *cookie) 153 { 154 while (1) { 155 int ret; 156 157 if (!consume_entry(cookie, state->pc)) 158 break; 159 ret = unwind_next(state); 160 if (ret < 0) 161 break; 162 } 163 } 164 NOKPROBE_SYMBOL(unwind); 165 166 static bool dump_backtrace_entry(void *arg, unsigned long where) 167 { 168 char *loglvl = arg; 169 printk("%s %pSb\n", loglvl, (void *)where); 170 return true; 171 } 172 173 void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk, 174 const char *loglvl) 175 { 176 pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk); 177 178 if (regs && user_mode(regs)) 179 return; 180 181 if (!tsk) 182 tsk = current; 183 184 if (!try_get_task_stack(tsk)) 185 return; 186 187 printk("%sCall trace:\n", loglvl); 188 arch_stack_walk(dump_backtrace_entry, (void *)loglvl, tsk, regs); 189 190 put_task_stack(tsk); 191 } 192 193 void show_stack(struct task_struct *tsk, unsigned long *sp, const char *loglvl) 194 { 195 dump_backtrace(NULL, tsk, loglvl); 196 barrier(); 197 } 198 199 noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry, 200 void *cookie, struct task_struct *task, 201 struct pt_regs *regs) 202 { 203 struct unwind_state state; 204 205 if (regs) { 206 if (task != current) 207 return; 208 unwind_init_from_regs(&state, regs); 209 } else if (task == current) { 210 unwind_init_from_caller(&state); 211 } else { 212 unwind_init_from_task(&state, task); 213 } 214 215 unwind(&state, consume_entry, cookie); 216 } 217