1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2008 ARM Limited 4 * Copyright (C) 2014 Regents of the University of California 5 */ 6 7 #include <linux/export.h> 8 #include <linux/kallsyms.h> 9 #include <linux/sched.h> 10 #include <linux/sched/debug.h> 11 #include <linux/sched/task_stack.h> 12 #include <linux/stacktrace.h> 13 #include <linux/ftrace.h> 14 15 #include <asm/stacktrace.h> 16 17 #ifdef CONFIG_FRAME_POINTER 18 19 extern asmlinkage void ret_from_exception(void); 20 21 static inline int fp_is_valid(unsigned long fp, unsigned long sp) 22 { 23 unsigned long low, high; 24 25 low = sp + sizeof(struct stackframe); 26 high = ALIGN(sp, THREAD_SIZE); 27 28 return !(fp < low || fp > high || fp & 0x07); 29 } 30 31 void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs, 32 bool (*fn)(void *, unsigned long), void *arg) 33 { 34 unsigned long fp, sp, pc; 35 int level = 0; 36 37 if (regs) { 38 fp = frame_pointer(regs); 39 sp = user_stack_pointer(regs); 40 pc = instruction_pointer(regs); 41 } else if (task == NULL || task == current) { 42 fp = (unsigned long)__builtin_frame_address(0); 43 sp = current_stack_pointer; 44 pc = (unsigned long)walk_stackframe; 45 level = -1; 46 } else { 47 /* task blocked in __switch_to */ 48 fp = task->thread.s[0]; 49 sp = task->thread.sp; 50 pc = task->thread.ra; 51 } 52 53 for (;;) { 54 struct stackframe *frame; 55 56 if (unlikely(!__kernel_text_address(pc) || (level++ >= 0 && !fn(arg, pc)))) 57 break; 58 59 if (unlikely(!fp_is_valid(fp, sp))) 60 break; 61 62 /* Unwind stack frame */ 63 frame = (struct stackframe *)fp - 1; 64 sp = fp; 65 if (regs && (regs->epc == pc) && fp_is_valid(frame->ra, sp)) { 66 /* We hit function where ra is not saved on the stack */ 67 fp = frame->ra; 68 pc = regs->ra; 69 } else { 70 fp = frame->fp; 71 pc = ftrace_graph_ret_addr(current, NULL, frame->ra, 72 &frame->ra); 73 if (pc == (unsigned long)ret_from_exception) { 74 if (unlikely(!__kernel_text_address(pc) || !fn(arg, pc))) 75 break; 76 77 pc = ((struct pt_regs *)sp)->epc; 78 fp = ((struct pt_regs *)sp)->s0; 79 } 80 } 81 82 } 83 } 84 85 #else /* !CONFIG_FRAME_POINTER */ 86 87 void notrace walk_stackframe(struct task_struct *task, 88 struct pt_regs *regs, bool (*fn)(void *, unsigned long), void *arg) 89 { 90 unsigned long sp, pc; 91 unsigned long *ksp; 92 93 if (regs) { 94 sp = user_stack_pointer(regs); 95 pc = instruction_pointer(regs); 96 } else if (task == NULL || task == current) { 97 sp = current_stack_pointer; 98 pc = (unsigned long)walk_stackframe; 99 } else { 100 /* task blocked in __switch_to */ 101 sp = task->thread.sp; 102 pc = task->thread.ra; 103 } 104 105 if (unlikely(sp & 0x7)) 106 return; 107 108 ksp = (unsigned long *)sp; 109 while (!kstack_end(ksp)) { 110 if (__kernel_text_address(pc) && unlikely(!fn(arg, pc))) 111 break; 112 pc = READ_ONCE_NOCHECK(*ksp++) - 0x4; 113 } 114 } 115 116 #endif /* CONFIG_FRAME_POINTER */ 117 118 static bool print_trace_address(void *arg, unsigned long pc) 119 { 120 const char *loglvl = arg; 121 122 print_ip_sym(loglvl, pc); 123 return true; 124 } 125 126 noinline void dump_backtrace(struct pt_regs *regs, struct task_struct *task, 127 const char *loglvl) 128 { 129 walk_stackframe(task, regs, print_trace_address, (void *)loglvl); 130 } 131 132 void show_stack(struct task_struct *task, unsigned long *sp, const char *loglvl) 133 { 134 pr_cont("%sCall Trace:\n", loglvl); 135 dump_backtrace(NULL, task, loglvl); 136 } 137 138 static bool save_wchan(void *arg, unsigned long pc) 139 { 140 if (!in_sched_functions(pc)) { 141 unsigned long *p = arg; 142 *p = pc; 143 return false; 144 } 145 return true; 146 } 147 148 unsigned long __get_wchan(struct task_struct *task) 149 { 150 unsigned long pc = 0; 151 152 if (!try_get_task_stack(task)) 153 return 0; 154 walk_stackframe(task, NULL, save_wchan, &pc); 155 put_task_stack(task); 156 return pc; 157 } 158 159 noinline void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie, 160 struct task_struct *task, struct pt_regs *regs) 161 { 162 walk_stackframe(task, regs, consume_entry, cookie); 163 } 164