Commit | Line | Data |
---|---|---|
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 | 39 | int 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, | |
971c67ce | 73 | * unpack the dummy frame to find the original elr. |
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 | 78 | if (frame->sp == irq_stack_ptr) { |
971c67ce | 79 | struct pt_regs *irq_args; |
1ffe199b JM |
80 | unsigned long orig_sp = IRQ_STACK_TO_TASK_STACK(irq_stack_ptr); |
81 | ||
971c67ce JM |
82 | if (object_is_on_stack((void *)orig_sp) && |
83 | object_is_on_stack((void *)frame->fp)) { | |
1ffe199b | 84 | frame->sp = orig_sp; |
971c67ce JM |
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 | } | |
1ffe199b | 97 | } |
132cd887 | 98 | |
60ffc30d CM |
99 | return 0; |
100 | } | |
101 | ||
102 | void notrace walk_stackframe(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(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 { | |
60ffc30d CM |
156 | data.no_sched_functions = 0; |
157 | frame.fp = (unsigned long)__builtin_frame_address(0); | |
bb28cec4 | 158 | frame.sp = current_stack_pointer; |
60ffc30d CM |
159 | frame.pc = (unsigned long)save_stack_trace_tsk; |
160 | } | |
161 | ||
162 | walk_stackframe(&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 |