1 #include <linux/export.h>
2 #include <linux/sched.h>
3 #include <linux/stacktrace.h>
5 #include <asm/stacktrace.h>
7 #if defined(CONFIG_FRAME_POINTER)
10 #include <linux/kallsyms.h>
11 #include <linux/module.h>
13 static unsigned long tbi_boing_addr
;
14 static unsigned long tbi_boing_size
;
16 static void tbi_boing_init(void)
18 /* We need to know where TBIBoingVec is and it's size */
21 char modname
[MODULE_NAME_LEN
];
22 char name
[KSYM_NAME_LEN
];
23 tbi_boing_addr
= kallsyms_lookup_name("___TBIBoingVec");
26 else if (!lookup_symbol_attrs(tbi_boing_addr
, &size
,
27 &offset
, modname
, name
))
28 tbi_boing_size
= size
;
32 #define ALIGN_DOWN(addr, size) ((addr)&(~((size)-1)))
35 * Unwind the current stack frame and store the new register values in the
36 * structure passed as argument. Unwinding is equivalent to a function return,
37 * hence the new PC value rather than LR should be used for backtrace.
39 int notrace
unwind_frame(struct stackframe
*frame
)
41 struct metag_frame
*fp
= (struct metag_frame
*)frame
->fp
;
51 #ifdef CONFIG_KALLSYMS
52 /* If we've reached TBIBoingVec then we're at an interrupt
53 * entry point or a syscall entry point. The frame pointer
54 * points to a pt_regs which can be used to continue tracing on
55 * the other side of the boing.
59 if (tbi_boing_size
&& lr
>= tbi_boing_addr
&&
60 lr
< tbi_boing_addr
+ tbi_boing_size
) {
61 struct pt_regs
*regs
= (struct pt_regs
*)fpnew
;
64 fpnew
= regs
->ctx
.AX
[1].U0
;
65 lr
= regs
->ctx
.DX
[4].U1
;
69 /* stack grows up, so frame pointers must decrease */
70 if (fpnew
< (ALIGN_DOWN((unsigned long)fp
, THREAD_SIZE
) +
71 sizeof(struct thread_info
)) || fpnew
>= (unsigned long)fp
)
74 /* restore the registers from the stack frame */
81 int notrace
unwind_frame(struct stackframe
*frame
)
83 struct metag_frame
*sp
= (struct metag_frame
*)frame
->sp
;
88 while (!kstack_end(sp
)) {
89 unsigned long addr
= sp
->lr
- 4;
92 if (__kernel_text_address(addr
)) {
93 frame
->sp
= (unsigned long)sp
;
102 void notrace
walk_stackframe(struct stackframe
*frame
,
103 int (*fn
)(struct stackframe
*, void *), void *data
)
110 ret
= unwind_frame(frame
);
115 EXPORT_SYMBOL(walk_stackframe
);
117 #ifdef CONFIG_STACKTRACE
118 struct stack_trace_data
{
119 struct stack_trace
*trace
;
120 unsigned int no_sched_functions
;
124 static int save_trace(struct stackframe
*frame
, void *d
)
126 struct stack_trace_data
*data
= d
;
127 struct stack_trace
*trace
= data
->trace
;
128 unsigned long addr
= frame
->pc
;
130 if (data
->no_sched_functions
&& in_sched_functions(addr
))
137 trace
->entries
[trace
->nr_entries
++] = addr
;
139 return trace
->nr_entries
>= trace
->max_entries
;
142 void save_stack_trace_tsk(struct task_struct
*tsk
, struct stack_trace
*trace
)
144 struct stack_trace_data data
;
145 struct stackframe frame
;
148 data
.skip
= trace
->skip
;
150 if (tsk
!= current
) {
153 * What guarantees do we have here that 'tsk' is not
154 * running on another CPU? For now, ignore it as we
155 * can't guarantee we won't explode.
157 if (trace
->nr_entries
< trace
->max_entries
)
158 trace
->entries
[trace
->nr_entries
++] = ULONG_MAX
;
161 data
.no_sched_functions
= 1;
162 frame
.fp
= thread_saved_fp(tsk
);
163 frame
.sp
= thread_saved_sp(tsk
);
164 frame
.lr
= 0; /* recovered from the stack */
165 frame
.pc
= thread_saved_pc(tsk
);
168 register unsigned long current_sp
asm ("A0StP");
170 data
.no_sched_functions
= 0;
171 frame
.fp
= (unsigned long)__builtin_frame_address(0);
172 frame
.sp
= current_sp
;
173 frame
.lr
= (unsigned long)__builtin_return_address(0);
174 frame
.pc
= (unsigned long)save_stack_trace_tsk
;
177 walk_stackframe(&frame
, save_trace
, &data
);
178 if (trace
->nr_entries
< trace
->max_entries
)
179 trace
->entries
[trace
->nr_entries
++] = ULONG_MAX
;
182 void save_stack_trace(struct stack_trace
*trace
)
184 save_stack_trace_tsk(current
, trace
);
186 EXPORT_SYMBOL_GPL(save_stack_trace
);