2 * Stack tracing support
4 * Copyright (C) 2012 ARM Ltd.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include <linux/kernel.h>
19 #include <linux/export.h>
20 #include <linux/sched.h>
21 #include <linux/stacktrace.h>
24 #include <asm/stacktrace.h>
27 * AArch64 PCS assigns the frame pointer to x29.
29 * A simple function prologue looks like this:
34 * A simple function epilogue looks like this:
39 int notrace unwind_frame(struct task_struct *tsk, struct stackframe *frame)
41 unsigned long high, low;
42 unsigned long fp = frame->fp;
43 unsigned long irq_stack_ptr;
46 * Use raw_smp_processor_id() to avoid false-positives from
47 * CONFIG_DEBUG_PREEMPT. get_wchan() calls unwind_frame() on sleeping
48 * task stacks, we can be pre-empted in this case, so
49 * {raw_,}smp_processor_id() may give us the wrong value. Sleeping
50 * tasks can't ever be on an interrupt stack, so regardless of cpu,
51 * the checks will always fail.
53 irq_stack_ptr = IRQ_STACK_PTR(raw_smp_processor_id());
56 /* irq stacks are not THREAD_SIZE aligned */
57 if (on_irq_stack(frame->sp, raw_smp_processor_id()))
60 high = ALIGN(low, THREAD_SIZE) - 0x20;
62 if (fp < low || fp > high || fp & 0xf)
65 frame->sp = fp + 0x10;
66 frame->fp = *(unsigned long *)(fp);
67 frame->pc = *(unsigned long *)(fp + 8);
70 * Check whether we are going to walk through from interrupt stack
72 * If we reach the end of the stack - and its an interrupt stack,
73 * unpack the dummy frame to find the original elr.
75 * Check the frame->fp we read from the bottom of the irq_stack,
76 * and the original task stack pointer are both in current->stack.
78 if (frame->sp == irq_stack_ptr) {
79 struct pt_regs *irq_args;
80 unsigned long orig_sp = IRQ_STACK_TO_TASK_STACK(irq_stack_ptr);
82 if (object_is_on_stack((void *)orig_sp) &&
83 object_is_on_stack((void *)frame->fp)) {
86 /* orig_sp is the saved pt_regs, find the elr */
87 irq_args = (struct pt_regs *)orig_sp;
88 frame->pc = irq_args->pc;
91 * This frame has a non-standard format, and we
92 * didn't fix it, because the data looked wrong.
93 * Refuse to output this frame.
102 void notrace walk_stackframe(struct task_struct *tsk, struct stackframe *frame,
103 int (*fn)(struct stackframe *, void *), void *data)
110 ret = unwind_frame(tsk, frame);
115 EXPORT_SYMBOL(walk_stackframe);
117 #ifdef CONFIG_STACKTRACE
118 struct stack_trace_data {
119 struct stack_trace *trace;
120 unsigned int no_sched_functions;
124 static int save_trace(struct stackframe *frame, void *d)
126 struct stack_trace_data *data = d;
127 struct stack_trace *trace = data->trace;
128 unsigned long addr = frame->pc;
130 if (data->no_sched_functions && in_sched_functions(addr))
137 trace->entries[trace->nr_entries++] = addr;
139 return trace->nr_entries >= trace->max_entries;
142 void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
144 struct stack_trace_data data;
145 struct stackframe frame;
148 data.skip = trace->skip;
150 if (tsk != current) {
151 data.no_sched_functions = 1;
152 frame.fp = thread_saved_fp(tsk);
153 frame.sp = thread_saved_sp(tsk);
154 frame.pc = thread_saved_pc(tsk);
156 data.no_sched_functions = 0;
157 frame.fp = (unsigned long)__builtin_frame_address(0);
158 frame.sp = current_stack_pointer;
159 frame.pc = (unsigned long)save_stack_trace_tsk;
162 walk_stackframe(tsk, &frame, save_trace, &data);
163 if (trace->nr_entries < trace->max_entries)
164 trace->entries[trace->nr_entries++] = ULONG_MAX;
167 void save_stack_trace(struct stack_trace *trace)
169 save_stack_trace_tsk(current, trace);
171 EXPORT_SYMBOL_GPL(save_stack_trace);