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