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 register unsigned long sp_in_global __asm__("sp");
18
19 #ifdef CONFIG_FRAME_POINTER
20
walk_stackframe(struct task_struct * task,struct pt_regs * regs,bool (* fn)(void *,unsigned long),void * arg)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
26 if (regs) {
27 fp = frame_pointer(regs);
28 sp = user_stack_pointer(regs);
29 pc = instruction_pointer(regs);
30 } else if (task == NULL || task == current) {
31 const register unsigned long current_sp = sp_in_global;
32 fp = (unsigned long)__builtin_frame_address(0);
33 sp = current_sp;
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) || !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 }
65
66 }
67 }
68
69 #else /* !CONFIG_FRAME_POINTER */
70
walk_stackframe(struct task_struct * task,struct pt_regs * regs,bool (* fn)(void *,unsigned long),void * arg)71 void notrace walk_stackframe(struct task_struct *task,
72 struct pt_regs *regs, bool (*fn)(void *, unsigned long), void *arg)
73 {
74 unsigned long sp, pc;
75 unsigned long *ksp;
76
77 if (regs) {
78 sp = user_stack_pointer(regs);
79 pc = instruction_pointer(regs);
80 } else if (task == NULL || task == current) {
81 sp = sp_in_global;
82 pc = (unsigned long)walk_stackframe;
83 } else {
84 /* task blocked in __switch_to */
85 sp = task->thread.sp;
86 pc = task->thread.ra;
87 }
88
89 if (unlikely(sp & 0x7))
90 return;
91
92 ksp = (unsigned long *)sp;
93 while (!kstack_end(ksp)) {
94 if (__kernel_text_address(pc) && unlikely(!fn(arg, pc)))
95 break;
96 pc = READ_ONCE_NOCHECK(*ksp++) - 0x4;
97 }
98 }
99
100 #endif /* CONFIG_FRAME_POINTER */
101
print_trace_address(void * arg,unsigned long pc)102 static bool print_trace_address(void *arg, unsigned long pc)
103 {
104 const char *loglvl = arg;
105
106 print_ip_sym(loglvl, pc);
107 return true;
108 }
109
show_stack(struct task_struct * task,unsigned long * sp,const char * loglvl)110 void show_stack(struct task_struct *task, unsigned long *sp, const char *loglvl)
111 {
112 pr_cont("Call Trace:\n");
113 walk_stackframe(task, NULL, print_trace_address, (void *)loglvl);
114 }
115
save_wchan(void * arg,unsigned long pc)116 static bool save_wchan(void *arg, unsigned long pc)
117 {
118 if (!in_sched_functions(pc)) {
119 unsigned long *p = arg;
120 *p = pc;
121 return false;
122 }
123 return true;
124 }
125
get_wchan(struct task_struct * task)126 unsigned long get_wchan(struct task_struct *task)
127 {
128 unsigned long pc = 0;
129
130 if (likely(task && task != current && task->state != TASK_RUNNING))
131 walk_stackframe(task, NULL, save_wchan, &pc);
132 return pc;
133 }
134
135 #ifdef CONFIG_STACKTRACE
136
arch_stack_walk(stack_trace_consume_fn consume_entry,void * cookie,struct task_struct * task,struct pt_regs * regs)137 void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie,
138 struct task_struct *task, struct pt_regs *regs)
139 {
140 walk_stackframe(task, regs, consume_entry, cookie);
141 }
142
143 #endif /* CONFIG_STACKTRACE */
144