Commit | Line | Data |
---|---|---|
7af76c5f ME |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | ||
fd3e0bbc | 3 | /* |
7af76c5f | 4 | * Stack trace utility functions etc. |
fd3e0bbc CH |
5 | * |
6 | * Copyright 2008 Christoph Hellwig, IBM Corp. | |
df78d3f6 | 7 | * Copyright 2018 SUSE Linux GmbH |
7af76c5f | 8 | * Copyright 2018 Nick Piggin, Michael Ellerman, IBM Corp. |
fd3e0bbc CH |
9 | */ |
10 | ||
a6cae77f | 11 | #include <linux/delay.h> |
4b16f8e2 | 12 | #include <linux/export.h> |
df78d3f6 TD |
13 | #include <linux/kallsyms.h> |
14 | #include <linux/module.h> | |
5cc05910 | 15 | #include <linux/nmi.h> |
fd3e0bbc | 16 | #include <linux/sched.h> |
b17b0153 | 17 | #include <linux/sched/debug.h> |
df78d3f6 | 18 | #include <linux/sched/task_stack.h> |
fd3e0bbc CH |
19 | #include <linux/stacktrace.h> |
20 | #include <asm/ptrace.h> | |
01f4b8b8 | 21 | #include <asm/processor.h> |
df78d3f6 TD |
22 | #include <linux/ftrace.h> |
23 | #include <asm/kprobes.h> | |
fd3e0bbc | 24 | |
5cc05910 ME |
25 | #include <asm/paca.h> |
26 | ||
b112fb91 DA |
27 | void __no_sanitize_address arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie, |
28 | struct task_struct *task, struct pt_regs *regs) | |
fd3e0bbc | 29 | { |
a1cdef04 CL |
30 | unsigned long sp; |
31 | ||
a2308836 CL |
32 | if (regs && !consume_entry(cookie, regs->nip)) |
33 | return; | |
34 | ||
a1cdef04 CL |
35 | if (regs) |
36 | sp = regs->gpr[1]; | |
37 | else if (task == current) | |
38 | sp = current_stack_frame(); | |
39 | else | |
40 | sp = task->thread.ksp; | |
41 | ||
fd3e0bbc CH |
42 | for (;;) { |
43 | unsigned long *stack = (unsigned long *) sp; | |
44 | unsigned long newsp, ip; | |
45 | ||
4cefb0f6 | 46 | if (!validate_sp(sp, task)) |
fd3e0bbc CH |
47 | return; |
48 | ||
49 | newsp = stack[0]; | |
50 | ip = stack[STACK_FRAME_LR_SAVE]; | |
51 | ||
a1cdef04 | 52 | if (!consume_entry(cookie, ip)) |
fd3e0bbc CH |
53 | return; |
54 | ||
55 | sp = newsp; | |
56 | } | |
57 | } | |
01f4b8b8 | 58 | |
18be3760 JL |
59 | /* |
60 | * This function returns an error if it detects any unreliable features of the | |
61 | * stack. Otherwise it guarantees that the stack trace is reliable. | |
62 | * | |
63 | * If the task is not 'current', the caller *must* ensure the task is inactive. | |
64 | */ | |
b112fb91 DA |
65 | int __no_sanitize_address arch_stack_walk_reliable(stack_trace_consume_fn consume_entry, |
66 | void *cookie, struct task_struct *task) | |
df78d3f6 TD |
67 | { |
68 | unsigned long sp; | |
29a77bbb | 69 | unsigned long newsp; |
826a307b | 70 | unsigned long stack_page = (unsigned long)task_stack_page(task); |
df78d3f6 TD |
71 | unsigned long stack_end; |
72 | int graph_idx = 0; | |
29a77bbb | 73 | bool firstframe; |
df78d3f6 TD |
74 | |
75 | stack_end = stack_page + THREAD_SIZE; | |
c5cc3ca7 ME |
76 | |
77 | // See copy_thread() for details. | |
78 | if (task->flags & PF_KTHREAD) | |
90f1b431 | 79 | stack_end -= STACK_FRAME_MIN_SIZE; |
c5cc3ca7 ME |
80 | else |
81 | stack_end -= STACK_USER_INT_FRAME_SIZE; | |
df78d3f6 | 82 | |
826a307b | 83 | if (task == current) |
3d13e839 | 84 | sp = current_stack_frame(); |
29a77bbb | 85 | else |
826a307b | 86 | sp = task->thread.ksp; |
29a77bbb | 87 | |
df78d3f6 TD |
88 | if (sp < stack_page + sizeof(struct thread_struct) || |
89 | sp > stack_end - STACK_FRAME_MIN_SIZE) { | |
3de27dcf | 90 | return -EINVAL; |
df78d3f6 TD |
91 | } |
92 | ||
29a77bbb JL |
93 | for (firstframe = true; sp != stack_end; |
94 | firstframe = false, sp = newsp) { | |
df78d3f6 | 95 | unsigned long *stack = (unsigned long *) sp; |
29a77bbb | 96 | unsigned long ip; |
df78d3f6 TD |
97 | |
98 | /* sanity check: ABI requires SP to be aligned 16 bytes. */ | |
99 | if (sp & 0xF) | |
3de27dcf | 100 | return -EINVAL; |
df78d3f6 | 101 | |
df78d3f6 TD |
102 | newsp = stack[0]; |
103 | /* Stack grows downwards; unwinder may only go up. */ | |
104 | if (newsp <= sp) | |
3de27dcf | 105 | return -EINVAL; |
df78d3f6 TD |
106 | |
107 | if (newsp != stack_end && | |
108 | newsp > stack_end - STACK_FRAME_MIN_SIZE) { | |
3de27dcf | 109 | return -EINVAL; /* invalid backlink, too far up. */ |
df78d3f6 TD |
110 | } |
111 | ||
18be3760 JL |
112 | /* |
113 | * We can only trust the bottom frame's backlink, the | |
114 | * rest of the frame may be uninitialized, continue to | |
115 | * the next. | |
116 | */ | |
29a77bbb JL |
117 | if (firstframe) |
118 | continue; | |
18be3760 JL |
119 | |
120 | /* Mark stacktraces with exception frames as unreliable. */ | |
121 | if (sp <= stack_end - STACK_INT_FRAME_SIZE && | |
e856e336 | 122 | stack[STACK_INT_FRAME_MARKER_LONGS] == STACK_FRAME_REGS_MARKER) { |
3de27dcf | 123 | return -EINVAL; |
18be3760 JL |
124 | } |
125 | ||
df78d3f6 TD |
126 | /* Examine the saved LR: it must point into kernel code. */ |
127 | ip = stack[STACK_FRAME_LR_SAVE]; | |
18be3760 | 128 | if (!__kernel_text_address(ip)) |
3de27dcf | 129 | return -EINVAL; |
df78d3f6 TD |
130 | |
131 | /* | |
132 | * FIXME: IMHO these tests do not belong in | |
133 | * arch-dependent code, they are generic. | |
134 | */ | |
826a307b | 135 | ip = ftrace_graph_ret_addr(task, &graph_idx, ip, stack); |
5e3f0d15 | 136 | #ifdef CONFIG_KPROBES |
df78d3f6 TD |
137 | /* |
138 | * Mark stacktraces with kretprobed functions on them | |
139 | * as unreliable. | |
140 | */ | |
adf8a61a | 141 | if (ip == (unsigned long)__kretprobe_trampoline) |
3de27dcf | 142 | return -EINVAL; |
5e3f0d15 | 143 | #endif |
df78d3f6 | 144 | |
a1cdef04 CL |
145 | if (!consume_entry(cookie, ip)) |
146 | return -EINVAL; | |
df78d3f6 TD |
147 | } |
148 | return 0; | |
149 | } | |
018cce33 | 150 | |
e08ecba1 | 151 | #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_NMI_IPI) |
5cc05910 ME |
152 | static void handle_backtrace_ipi(struct pt_regs *regs) |
153 | { | |
154 | nmi_cpu_backtrace(regs); | |
155 | } | |
156 | ||
157 | static void raise_backtrace_ipi(cpumask_t *mask) | |
158 | { | |
7c6986ad | 159 | struct paca_struct *p; |
5cc05910 | 160 | unsigned int cpu; |
7c6986ad | 161 | u64 delay_us; |
5cc05910 ME |
162 | |
163 | for_each_cpu(cpu, mask) { | |
7c6986ad | 164 | if (cpu == smp_processor_id()) { |
5cc05910 | 165 | handle_backtrace_ipi(NULL); |
7c6986ad ME |
166 | continue; |
167 | } | |
5cc05910 | 168 | |
7c6986ad ME |
169 | delay_us = 5 * USEC_PER_SEC; |
170 | ||
171 | if (smp_send_safe_nmi_ipi(cpu, handle_backtrace_ipi, delay_us)) { | |
172 | // Now wait up to 5s for the other CPU to do its backtrace | |
173 | while (cpumask_test_cpu(cpu, mask) && delay_us) { | |
174 | udelay(1); | |
175 | delay_us--; | |
176 | } | |
177 | ||
178 | // Other CPU cleared itself from the mask | |
179 | if (delay_us) | |
180 | continue; | |
181 | } | |
182 | ||
183 | p = paca_ptrs[cpu]; | |
5cc05910 ME |
184 | |
185 | cpumask_clear_cpu(cpu, mask); | |
186 | ||
187 | pr_warn("CPU %d didn't respond to backtrace IPI, inspecting paca.\n", cpu); | |
188 | if (!virt_addr_valid(p)) { | |
189 | pr_warn("paca pointer appears corrupt? (%px)\n", p); | |
190 | continue; | |
191 | } | |
192 | ||
193 | pr_warn("irq_soft_mask: 0x%02x in_mce: %d in_nmi: %d", | |
194 | p->irq_soft_mask, p->in_mce, p->in_nmi); | |
195 | ||
196 | if (virt_addr_valid(p->__current)) | |
197 | pr_cont(" current: %d (%s)\n", p->__current->pid, | |
198 | p->__current->comm); | |
199 | else | |
200 | pr_cont(" current pointer corrupt? (%px)\n", p->__current); | |
201 | ||
202 | pr_warn("Back trace of paca->saved_r1 (0x%016llx) (possibly stale):\n", p->saved_r1); | |
9cb8f069 | 203 | show_stack(p->__current, (unsigned long *)p->saved_r1, KERN_WARNING); |
5cc05910 ME |
204 | } |
205 | } | |
206 | ||
8d539b84 | 207 | void arch_trigger_cpumask_backtrace(const cpumask_t *mask, int exclude_cpu) |
5cc05910 | 208 | { |
8d539b84 | 209 | nmi_trigger_cpumask_backtrace(mask, exclude_cpu, raise_backtrace_ipi); |
5cc05910 | 210 | } |
e08ecba1 | 211 | #endif /* defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_NMI_IPI) */ |