arm64: pass a task parameter to unwind_frame()
[cascardo/linux.git] / arch / arm64 / kernel / stacktrace.c
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
23 #include <asm/irq.h>
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  */
39 int notrace unwind_frame(struct task_struct *tsk, struct stackframe *frame)
40 {
41         unsigned long high, low;
42         unsigned long fp = frame->fp;
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());
54
55         low  = frame->sp;
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;
61
62         if (fp < low || fp > high || fp & 0xf)
63                 return -EINVAL;
64
65         frame->sp = fp + 0x10;
66         frame->fp = *(unsigned long *)(fp);
67         frame->pc = *(unsigned long *)(fp + 8);
68
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          * unpack the dummy frame to find the original elr.
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.
77          */
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);
81
82                 if (object_is_on_stack((void *)orig_sp) &&
83                    object_is_on_stack((void *)frame->fp)) {
84                         frame->sp = orig_sp;
85
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;
89                 } else {
90                         /*
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.
94                          */
95                         return -EINVAL;
96                 }
97         }
98
99         return 0;
100 }
101
102 void notrace walk_stackframe(struct task_struct *tsk, struct stackframe *frame,
103                      int (*fn)(struct stackframe *, void *), void *data)
104 {
105         while (1) {
106                 int ret;
107
108                 if (fn(frame, data))
109                         break;
110                 ret = unwind_frame(tsk, frame);
111                 if (ret < 0)
112                         break;
113         }
114 }
115 EXPORT_SYMBOL(walk_stackframe);
116
117 #ifdef CONFIG_STACKTRACE
118 struct stack_trace_data {
119         struct stack_trace *trace;
120         unsigned int no_sched_functions;
121         unsigned int skip;
122 };
123
124 static int save_trace(struct stackframe *frame, void *d)
125 {
126         struct stack_trace_data *data = d;
127         struct stack_trace *trace = data->trace;
128         unsigned long addr = frame->pc;
129
130         if (data->no_sched_functions && in_sched_functions(addr))
131                 return 0;
132         if (data->skip) {
133                 data->skip--;
134                 return 0;
135         }
136
137         trace->entries[trace->nr_entries++] = addr;
138
139         return trace->nr_entries >= trace->max_entries;
140 }
141
142 void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
143 {
144         struct stack_trace_data data;
145         struct stackframe frame;
146
147         data.trace = trace;
148         data.skip = trace->skip;
149
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);
155         } else {
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;
160         }
161
162         walk_stackframe(tsk, &frame, save_trace, &data);
163         if (trace->nr_entries < trace->max_entries)
164                 trace->entries[trace->nr_entries++] = ULONG_MAX;
165 }
166
167 void save_stack_trace(struct stack_trace *trace)
168 {
169         save_stack_trace_tsk(current, trace);
170 }
171 EXPORT_SYMBOL_GPL(save_stack_trace);
172 #endif