xref: /openbmc/linux/arch/arm/kernel/stacktrace.c (revision 7b4c9505f2fd82b117dd015b561f723b9a5dab79)
17b104bcbSAl Viro #include <linux/module.h>
2f16fb1ecSRussell King #include <linux/sched.h>
3f16fb1ecSRussell King #include <linux/stacktrace.h>
4f16fb1ecSRussell King 
5f16fb1ecSRussell King #include "stacktrace.h"
6f16fb1ecSRussell King 
7f16fb1ecSRussell King int walk_stackframe(unsigned long fp, unsigned long low, unsigned long high,
8f16fb1ecSRussell King 		    int (*fn)(struct stackframe *, void *), void *data)
9f16fb1ecSRussell King {
10f16fb1ecSRussell King 	struct stackframe *frame;
11f16fb1ecSRussell King 
12f16fb1ecSRussell King 	do {
13f16fb1ecSRussell King 		/*
14f16fb1ecSRussell King 		 * Check current frame pointer is within bounds
15f16fb1ecSRussell King 		 */
165b10c8e4SRussell King 		if (fp < (low + 12) || fp + 4 >= high)
17f16fb1ecSRussell King 			break;
18f16fb1ecSRussell King 
19f16fb1ecSRussell King 		frame = (struct stackframe *)(fp - 12);
20f16fb1ecSRussell King 
21f16fb1ecSRussell King 		if (fn(frame, data))
22f16fb1ecSRussell King 			break;
23f16fb1ecSRussell King 
24f16fb1ecSRussell King 		/*
25f16fb1ecSRussell King 		 * Update the low bound - the next frame must always
26f16fb1ecSRussell King 		 * be at a higher address than the current frame.
27f16fb1ecSRussell King 		 */
28f16fb1ecSRussell King 		low = fp + 4;
29f16fb1ecSRussell King 		fp = frame->fp;
30f16fb1ecSRussell King 	} while (fp);
31f16fb1ecSRussell King 
32f16fb1ecSRussell King 	return 0;
33f16fb1ecSRussell King }
347b104bcbSAl Viro EXPORT_SYMBOL(walk_stackframe);
35f16fb1ecSRussell King 
36f16fb1ecSRussell King #ifdef CONFIG_STACKTRACE
37f16fb1ecSRussell King struct stack_trace_data {
38f16fb1ecSRussell King 	struct stack_trace *trace;
39f16fb1ecSRussell King 	unsigned int skip;
40f16fb1ecSRussell King };
41f16fb1ecSRussell King 
42f16fb1ecSRussell King static int save_trace(struct stackframe *frame, void *d)
43f16fb1ecSRussell King {
44f16fb1ecSRussell King 	struct stack_trace_data *data = d;
45f16fb1ecSRussell King 	struct stack_trace *trace = data->trace;
46f16fb1ecSRussell King 
47f16fb1ecSRussell King 	if (data->skip) {
48f16fb1ecSRussell King 		data->skip--;
49f16fb1ecSRussell King 		return 0;
50f16fb1ecSRussell King 	}
51f16fb1ecSRussell King 
52f16fb1ecSRussell King 	trace->entries[trace->nr_entries++] = frame->lr;
53f16fb1ecSRussell King 
54f16fb1ecSRussell King 	return trace->nr_entries >= trace->max_entries;
55f16fb1ecSRussell King }
56f16fb1ecSRussell King 
57fac07790SAndrew Morton void save_stack_trace(struct stack_trace *trace)
58f16fb1ecSRussell King {
59f16fb1ecSRussell King 	struct stack_trace_data data;
60f16fb1ecSRussell King 	unsigned long fp, base;
61f16fb1ecSRussell King 
62f16fb1ecSRussell King 	data.trace = trace;
63f16fb1ecSRussell King 	data.skip = trace->skip;
64f16fb1ecSRussell King 	base = (unsigned long)task_stack_page(current);
65f16fb1ecSRussell King 	asm("mov %0, fp" : "=r" (fp));
66f16fb1ecSRussell King 
67f16fb1ecSRussell King 	walk_stackframe(fp, base, base + THREAD_SIZE, save_trace, &data);
68f16fb1ecSRussell King }
69*7b4c9505SIngo Molnar EXPORT_SYMBOL_GPL(save_stack_trace);
70f16fb1ecSRussell King #endif
71