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 void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs, 22 bool (*fn)(void *, unsigned long), void *arg) 23 { 24 unsigned long fp, sp, pc; 25 int level = 0; 26 27 if (regs) { 28 fp = frame_pointer(regs); 29 sp = user_stack_pointer(regs); 30 pc = instruction_pointer(regs); 31 } else if (task == NULL || task == current) { 32 fp = (unsigned long)__builtin_frame_address(0); 33 sp = current_stack_pointer; 34 pc = (unsigned long)walk_stackframe; 35 } else { 36 /* task blocked in __switch_to */ 37 fp = task->thread.s[0]; 38 sp = task->thread.sp; 39 pc = task->thread.ra; 40 } 41 42 for (;;) { 43 unsigned long low, high; 44 struct stackframe *frame; 45 46 if (unlikely(!__kernel_text_address(pc) || (level++ >= 1 && !fn(arg, pc)))) 47 break; 48 49 /* Validate frame pointer */ 50 low = sp + sizeof(struct stackframe); 51 high = ALIGN(sp, THREAD_SIZE); 52 if (unlikely(fp < low || fp > high || fp & 0x7)) 53 break; 54 /* Unwind stack frame */ 55 frame = (struct stackframe *)fp - 1; 56 sp = fp; 57 if (regs && (regs->epc == pc) && (frame->fp & 0x7)) { 58 fp = frame->ra; 59 pc = regs->ra; 60 } else { 61 fp = frame->fp; 62 pc = ftrace_graph_ret_addr(current, NULL, frame->ra, 63 &frame->ra); 64 if (pc == (unsigned long)ret_from_exception) { 65 if (unlikely(!__kernel_text_address(pc) || !fn(arg, pc))) 66 break; 67 68 pc = ((struct pt_regs *)sp)->epc; 69 fp = ((struct pt_regs *)sp)->s0; 70 } 71 } 72 73 } 74 } 75 76 #else /* !CONFIG_FRAME_POINTER */ 77 78 void notrace walk_stackframe(struct task_struct *task, 79 struct pt_regs *regs, bool (*fn)(void *, unsigned long), void *arg) 80 { 81 unsigned long sp, pc; 82 unsigned long *ksp; 83 84 if (regs) { 85 sp = user_stack_pointer(regs); 86 pc = instruction_pointer(regs); 87 } else if (task == NULL || task == current) { 88 sp = current_stack_pointer; 89 pc = (unsigned long)walk_stackframe; 90 } else { 91 /* task blocked in __switch_to */ 92 sp = task->thread.sp; 93 pc = task->thread.ra; 94 } 95 96 if (unlikely(sp & 0x7)) 97 return; 98 99 ksp = (unsigned long *)sp; 100 while (!kstack_end(ksp)) { 101 if (__kernel_text_address(pc) && unlikely(!fn(arg, pc))) 102 break; 103 pc = (*ksp++) - 0x4; 104 } 105 } 106 107 #endif /* CONFIG_FRAME_POINTER */ 108 109 static bool print_trace_address(void *arg, unsigned long pc) 110 { 111 const char *loglvl = arg; 112 113 print_ip_sym(loglvl, pc); 114 return true; 115 } 116 117 noinline void dump_backtrace(struct pt_regs *regs, struct task_struct *task, 118 const char *loglvl) 119 { 120 walk_stackframe(task, regs, print_trace_address, (void *)loglvl); 121 } 122 123 void show_stack(struct task_struct *task, unsigned long *sp, const char *loglvl) 124 { 125 pr_cont("%sCall Trace:\n", loglvl); 126 dump_backtrace(NULL, task, loglvl); 127 } 128 129 static bool save_wchan(void *arg, unsigned long pc) 130 { 131 if (!in_sched_functions(pc)) { 132 unsigned long *p = arg; 133 *p = pc; 134 return false; 135 } 136 return true; 137 } 138 139 unsigned long __get_wchan(struct task_struct *task) 140 { 141 unsigned long pc = 0; 142 143 if (!try_get_task_stack(task)) 144 return 0; 145 walk_stackframe(task, NULL, save_wchan, &pc); 146 put_task_stack(task); 147 return pc; 148 } 149 150 noinline void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie, 151 struct task_struct *task, struct pt_regs *regs) 152 { 153 walk_stackframe(task, regs, consume_entry, cookie); 154 } 155