Merge branch 'aarch64/efi' into aarch64/for-next/core
[linux-2.6-block.git] / arch / arm64 / kernel / stacktrace.c
CommitLineData
60ffc30d
CM
1/*
2 * Stack tracing support
3 *
4 * Copyright (C) 2012 ARM Ltd.
5 *
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.
9 *
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.
14 *
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/>.
17 */
18#include <linux/kernel.h>
19#include <linux/export.h>
20#include <linux/sched.h>
21#include <linux/stacktrace.h>
22
132cd887 23#include <asm/irq.h>
60ffc30d
CM
24#include <asm/stacktrace.h>
25
26/*
27 * AArch64 PCS assigns the frame pointer to x29.
28 *
29 * A simple function prologue looks like this:
30 * sub sp, sp, #0x10
31 * stp x29, x30, [sp]
32 * mov x29, sp
33 *
34 * A simple function epilogue looks like this:
35 * mov sp, x29
36 * ldp x29, x30, [sp]
37 * add sp, sp, #0x10
38 */
26e2ae39 39int notrace unwind_frame(struct stackframe *frame)
60ffc30d
CM
40{
41 unsigned long high, low;
42 unsigned long fp = frame->fp;
132cd887
AT
43 unsigned long irq_stack_ptr;
44
45 /*
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.
52 */
53 irq_stack_ptr = IRQ_STACK_PTR(raw_smp_processor_id());
60ffc30d
CM
54
55 low = frame->sp;
132cd887
AT
56 /* irq stacks are not THREAD_SIZE aligned */
57 if (on_irq_stack(frame->sp, raw_smp_processor_id()))
58 high = irq_stack_ptr;
59 else
60 high = ALIGN(low, THREAD_SIZE) - 0x20;
60ffc30d 61
132cd887 62 if (fp < low || fp > high || fp & 0xf)
60ffc30d
CM
63 return -EINVAL;
64
65 frame->sp = fp + 0x10;
66 frame->fp = *(unsigned long *)(fp);
9702970c 67 frame->pc = *(unsigned long *)(fp + 8);
60ffc30d 68
132cd887
AT
69 /*
70 * Check whether we are going to walk through from interrupt stack
71 * to task stack.
72 * If we reach the end of the stack - and its an interrupt stack,
73 * read the original task stack pointer from the dummy frame.
1ffe199b
JM
74 *
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.
132cd887 77 */
1ffe199b
JM
78 if (frame->sp == irq_stack_ptr) {
79 unsigned long orig_sp = IRQ_STACK_TO_TASK_STACK(irq_stack_ptr);
80
81 if(object_is_on_stack((void *)orig_sp) &&
82 object_is_on_stack((void *)frame->fp))
83 frame->sp = orig_sp;
84 }
132cd887 85
60ffc30d
CM
86 return 0;
87}
88
89void notrace walk_stackframe(struct stackframe *frame,
90 int (*fn)(struct stackframe *, void *), void *data)
91{
92 while (1) {
93 int ret;
94
95 if (fn(frame, data))
96 break;
97 ret = unwind_frame(frame);
98 if (ret < 0)
99 break;
100 }
101}
102EXPORT_SYMBOL(walk_stackframe);
103
104#ifdef CONFIG_STACKTRACE
105struct stack_trace_data {
106 struct stack_trace *trace;
107 unsigned int no_sched_functions;
108 unsigned int skip;
109};
110
111static int save_trace(struct stackframe *frame, void *d)
112{
113 struct stack_trace_data *data = d;
114 struct stack_trace *trace = data->trace;
115 unsigned long addr = frame->pc;
116
117 if (data->no_sched_functions && in_sched_functions(addr))
118 return 0;
119 if (data->skip) {
120 data->skip--;
121 return 0;
122 }
123
124 trace->entries[trace->nr_entries++] = addr;
125
126 return trace->nr_entries >= trace->max_entries;
127}
128
129void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
130{
131 struct stack_trace_data data;
132 struct stackframe frame;
133
134 data.trace = trace;
135 data.skip = trace->skip;
136
137 if (tsk != current) {
138 data.no_sched_functions = 1;
139 frame.fp = thread_saved_fp(tsk);
140 frame.sp = thread_saved_sp(tsk);
141 frame.pc = thread_saved_pc(tsk);
142 } else {
60ffc30d
CM
143 data.no_sched_functions = 0;
144 frame.fp = (unsigned long)__builtin_frame_address(0);
bb28cec4 145 frame.sp = current_stack_pointer;
60ffc30d
CM
146 frame.pc = (unsigned long)save_stack_trace_tsk;
147 }
148
149 walk_stackframe(&frame, save_trace, &data);
150 if (trace->nr_entries < trace->max_entries)
151 trace->entries[trace->nr_entries++] = ULONG_MAX;
152}
153
154void save_stack_trace(struct stack_trace *trace)
155{
156 save_stack_trace_tsk(current, trace);
157}
158EXPORT_SYMBOL_GPL(save_stack_trace);
159#endif