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 static bool on_accessible_stack(const struct task_struct *tsk, 71 unsigned long sp, unsigned long size, 72 struct stack_info *info) 73 { 74 struct stack_info tmp; 75 76 tmp = stackinfo_get_task(tsk); 77 if (stackinfo_on_stack(&tmp, sp, size)) 78 goto found; 79 80 /* 81 * We can only safely access per-cpu stacks when unwinding the current 82 * task in a non-preemptible context. 83 */ 84 if (tsk != current || preemptible()) 85 goto not_found; 86 87 tmp = stackinfo_get_irq(); 88 if (stackinfo_on_stack(&tmp, sp, size)) 89 goto found; 90 91 tmp = stackinfo_get_overflow(); 92 if (stackinfo_on_stack(&tmp, sp, size)) 93 goto found; 94 95 /* 96 * We can only safely access SDEI stacks which unwinding the current 97 * task in an NMI context. 98 */ 99 if (!IS_ENABLED(CONFIG_VMAP_STACK) || 100 !IS_ENABLED(CONFIG_ARM_SDE_INTERFACE) || 101 !in_nmi()) 102 goto not_found; 103 104 tmp = stackinfo_get_sdei_normal(); 105 if (stackinfo_on_stack(&tmp, sp, size)) 106 goto found; 107 108 tmp = stackinfo_get_sdei_critical(); 109 if (stackinfo_on_stack(&tmp, sp, size)) 110 goto found; 111 112 not_found: 113 *info = stackinfo_get_unknown(); 114 return false; 115 116 found: 117 *info = tmp; 118 return true; 119 } 120 121 /* 122 * Unwind from one frame record (A) to the next frame record (B). 123 * 124 * We terminate early if the location of B indicates a malformed chain of frame 125 * records (e.g. a cycle), determined based on the location and fp value of A 126 * and the location (but not the fp value) of B. 127 */ 128 static int notrace unwind_next(struct unwind_state *state) 129 { 130 struct task_struct *tsk = state->task; 131 unsigned long fp = state->fp; 132 int err; 133 134 /* Final frame; nothing to unwind */ 135 if (fp == (unsigned long)task_pt_regs(tsk)->stackframe) 136 return -ENOENT; 137 138 err = unwind_next_frame_record(state, on_accessible_stack, NULL); 139 if (err) 140 return err; 141 142 state->pc = ptrauth_strip_insn_pac(state->pc); 143 144 #ifdef CONFIG_FUNCTION_GRAPH_TRACER 145 if (tsk->ret_stack && 146 (state->pc == (unsigned long)return_to_handler)) { 147 unsigned long orig_pc; 148 /* 149 * This is a case where function graph tracer has 150 * modified a return address (LR) in a stack frame 151 * to hook a function return. 152 * So replace it to an original value. 153 */ 154 orig_pc = ftrace_graph_ret_addr(tsk, NULL, state->pc, 155 (void *)state->fp); 156 if (WARN_ON_ONCE(state->pc == orig_pc)) 157 return -EINVAL; 158 state->pc = orig_pc; 159 } 160 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ 161 #ifdef CONFIG_KRETPROBES 162 if (is_kretprobe_trampoline(state->pc)) 163 state->pc = kretprobe_find_ret_addr(tsk, (void *)state->fp, &state->kr_cur); 164 #endif 165 166 return 0; 167 } 168 NOKPROBE_SYMBOL(unwind_next); 169 170 static void notrace unwind(struct unwind_state *state, 171 stack_trace_consume_fn consume_entry, void *cookie) 172 { 173 while (1) { 174 int ret; 175 176 if (!consume_entry(cookie, state->pc)) 177 break; 178 ret = unwind_next(state); 179 if (ret < 0) 180 break; 181 } 182 } 183 NOKPROBE_SYMBOL(unwind); 184 185 static bool dump_backtrace_entry(void *arg, unsigned long where) 186 { 187 char *loglvl = arg; 188 printk("%s %pSb\n", loglvl, (void *)where); 189 return true; 190 } 191 192 void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk, 193 const char *loglvl) 194 { 195 pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk); 196 197 if (regs && user_mode(regs)) 198 return; 199 200 if (!tsk) 201 tsk = current; 202 203 if (!try_get_task_stack(tsk)) 204 return; 205 206 printk("%sCall trace:\n", loglvl); 207 arch_stack_walk(dump_backtrace_entry, (void *)loglvl, tsk, regs); 208 209 put_task_stack(tsk); 210 } 211 212 void show_stack(struct task_struct *tsk, unsigned long *sp, const char *loglvl) 213 { 214 dump_backtrace(NULL, tsk, loglvl); 215 barrier(); 216 } 217 218 noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry, 219 void *cookie, struct task_struct *task, 220 struct pt_regs *regs) 221 { 222 struct unwind_state state; 223 224 if (regs) { 225 if (task != current) 226 return; 227 unwind_init_from_regs(&state, regs); 228 } else if (task == current) { 229 unwind_init_from_caller(&state); 230 } else { 231 unwind_init_from_task(&state, task); 232 } 233 234 unwind(&state, consume_entry, cookie); 235 } 236