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