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, | |
73 | * read the original task stack pointer from the dummy frame. | |
74 | */ | |
75 | if (frame->sp == irq_stack_ptr) | |
76 | frame->sp = IRQ_STACK_TO_TASK_STACK(irq_stack_ptr); | |
77 | ||
60ffc30d CM |
78 | return 0; |
79 | } | |
80 | ||
81 | void notrace walk_stackframe(struct stackframe *frame, | |
82 | int (*fn)(struct stackframe *, void *), void *data) | |
83 | { | |
84 | while (1) { | |
85 | int ret; | |
86 | ||
87 | if (fn(frame, data)) | |
88 | break; | |
89 | ret = unwind_frame(frame); | |
90 | if (ret < 0) | |
91 | break; | |
92 | } | |
93 | } | |
94 | EXPORT_SYMBOL(walk_stackframe); | |
95 | ||
96 | #ifdef CONFIG_STACKTRACE | |
97 | struct stack_trace_data { | |
98 | struct stack_trace *trace; | |
99 | unsigned int no_sched_functions; | |
100 | unsigned int skip; | |
101 | }; | |
102 | ||
103 | static int save_trace(struct stackframe *frame, void *d) | |
104 | { | |
105 | struct stack_trace_data *data = d; | |
106 | struct stack_trace *trace = data->trace; | |
107 | unsigned long addr = frame->pc; | |
108 | ||
109 | if (data->no_sched_functions && in_sched_functions(addr)) | |
110 | return 0; | |
111 | if (data->skip) { | |
112 | data->skip--; | |
113 | return 0; | |
114 | } | |
115 | ||
116 | trace->entries[trace->nr_entries++] = addr; | |
117 | ||
118 | return trace->nr_entries >= trace->max_entries; | |
119 | } | |
120 | ||
121 | void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace) | |
122 | { | |
123 | struct stack_trace_data data; | |
124 | struct stackframe frame; | |
125 | ||
126 | data.trace = trace; | |
127 | data.skip = trace->skip; | |
128 | ||
129 | if (tsk != current) { | |
130 | data.no_sched_functions = 1; | |
131 | frame.fp = thread_saved_fp(tsk); | |
132 | frame.sp = thread_saved_sp(tsk); | |
133 | frame.pc = thread_saved_pc(tsk); | |
134 | } else { | |
60ffc30d CM |
135 | data.no_sched_functions = 0; |
136 | frame.fp = (unsigned long)__builtin_frame_address(0); | |
bb28cec4 | 137 | frame.sp = current_stack_pointer; |
60ffc30d CM |
138 | frame.pc = (unsigned long)save_stack_trace_tsk; |
139 | } | |
140 | ||
141 | walk_stackframe(&frame, save_trace, &data); | |
142 | if (trace->nr_entries < trace->max_entries) | |
143 | trace->entries[trace->nr_entries++] = ULONG_MAX; | |
144 | } | |
145 | ||
146 | void save_stack_trace(struct stack_trace *trace) | |
147 | { | |
148 | save_stack_trace_tsk(current, trace); | |
149 | } | |
150 | EXPORT_SYMBOL_GPL(save_stack_trace); | |
151 | #endif |