Commit 2d7c11bf authored by Catalin Marinas's avatar Catalin Marinas Committed by Russell King

[ARM] 5382/1: unwind: Reorganise the stacktrace support

This patch changes the walk_stacktrace and its callers for easier
integration of stack unwinding. The arch/arm/kernel/stacktrace.h file is
also moved to arch/arm/include/asm/stacktrace.h.
Signed-off-by: default avatarCatalin Marinas <catalin.marinas@arm.com>
Signed-off-by: default avatarRussell King <rmk+kernel@arm.linux.org.uk>
parent 67a94c23
#ifndef __ASM_STACKTRACE_H
#define __ASM_STACKTRACE_H
struct stackframe {
unsigned long fp;
unsigned long sp;
unsigned long lr;
unsigned long pc;
};
extern int unwind_frame(struct stackframe *frame);
extern void walk_stackframe(struct stackframe *frame,
int (*fn)(struct stackframe *, void *), void *data);
#endif /* __ASM_STACKTRACE_H */
...@@ -99,6 +99,8 @@ static inline struct thread_info *current_thread_info(void) ...@@ -99,6 +99,8 @@ static inline struct thread_info *current_thread_info(void)
#define thread_saved_pc(tsk) \ #define thread_saved_pc(tsk) \
((unsigned long)(task_thread_info(tsk)->cpu_context.pc)) ((unsigned long)(task_thread_info(tsk)->cpu_context.pc))
#define thread_saved_sp(tsk) \
((unsigned long)(task_thread_info(tsk)->cpu_context.sp))
#define thread_saved_fp(tsk) \ #define thread_saved_fp(tsk) \
((unsigned long)(task_thread_info(tsk)->cpu_context.fp)) ((unsigned long)(task_thread_info(tsk)->cpu_context.fp))
......
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#include <asm/processor.h> #include <asm/processor.h>
#include <asm/system.h> #include <asm/system.h>
#include <asm/thread_notify.h> #include <asm/thread_notify.h>
#include <asm/stacktrace.h>
#include <asm/mach/time.h> #include <asm/mach/time.h>
static const char *processor_modes[] = { static const char *processor_modes[] = {
...@@ -372,23 +373,21 @@ EXPORT_SYMBOL(kernel_thread); ...@@ -372,23 +373,21 @@ EXPORT_SYMBOL(kernel_thread);
unsigned long get_wchan(struct task_struct *p) unsigned long get_wchan(struct task_struct *p)
{ {
unsigned long fp, lr; struct stackframe frame;
unsigned long stack_start, stack_end;
int count = 0; int count = 0;
if (!p || p == current || p->state == TASK_RUNNING) if (!p || p == current || p->state == TASK_RUNNING)
return 0; return 0;
stack_start = (unsigned long)end_of_stack(p); frame.fp = thread_saved_fp(p);
stack_end = (unsigned long)task_stack_page(p) + THREAD_SIZE; frame.sp = thread_saved_sp(p);
frame.lr = 0; /* recovered from the stack */
fp = thread_saved_fp(p); frame.pc = thread_saved_pc(p);
do { do {
if (fp < stack_start || fp > stack_end) int ret = unwind_frame(&frame);
if (ret < 0)
return 0; return 0;
lr = ((unsigned long *)fp)[-1]; if (!in_sched_functions(frame.pc))
if (!in_sched_functions(lr)) return frame.pc;
return lr;
fp = *(unsigned long *) (fp - 12);
} while (count ++ < 16); } while (count ++ < 16);
return 0; return 0;
} }
...@@ -2,35 +2,60 @@ ...@@ -2,35 +2,60 @@
#include <linux/sched.h> #include <linux/sched.h>
#include <linux/stacktrace.h> #include <linux/stacktrace.h>
#include "stacktrace.h" #include <asm/stacktrace.h>
int walk_stackframe(unsigned long fp, unsigned long low, unsigned long high, #if defined(CONFIG_FRAME_POINTER) && !defined(CONFIG_ARM_UNWIND)
int (*fn)(struct stackframe *, void *), void *data) /*
* Unwind the current stack frame and store the new register values in the
* structure passed as argument. Unwinding is equivalent to a function return,
* hence the new PC value rather than LR should be used for backtrace.
*
* With framepointer enabled, a simple function prologue looks like this:
* mov ip, sp
* stmdb sp!, {fp, ip, lr, pc}
* sub fp, ip, #4
*
* A simple function epilogue looks like this:
* ldm sp, {fp, sp, pc}
*
* Note that with framepointer enabled, even the leaf functions have the same
* prologue and epilogue, therefore we can ignore the LR value in this case.
*/
int unwind_frame(struct stackframe *frame)
{ {
struct stackframe *frame; unsigned long high, low;
unsigned long fp = frame->fp;
do {
/*
* Check current frame pointer is within bounds
*/
if (fp < (low + 12) || fp + 4 >= high)
break;
frame = (struct stackframe *)(fp - 12); /* only go to a higher address on the stack */
low = frame->sp;
high = ALIGN(low, THREAD_SIZE) + THREAD_SIZE;
if (fn(frame, data)) /* check current frame pointer is within bounds */
break; if (fp < (low + 12) || fp + 4 >= high)
return -EINVAL;
/* /* restore the registers from the stack frame */
* Update the low bound - the next frame must always frame->fp = *(unsigned long *)(fp - 12);
* be at a higher address than the current frame. frame->sp = *(unsigned long *)(fp - 8);
*/ frame->pc = *(unsigned long *)(fp - 4);
low = fp + 4;
fp = frame->fp;
} while (fp);
return 0; return 0;
} }
#endif
void walk_stackframe(struct stackframe *frame,
int (*fn)(struct stackframe *, void *), void *data)
{
while (1) {
int ret;
if (fn(frame, data))
break;
ret = unwind_frame(frame);
if (ret < 0)
break;
}
}
EXPORT_SYMBOL(walk_stackframe); EXPORT_SYMBOL(walk_stackframe);
#ifdef CONFIG_STACKTRACE #ifdef CONFIG_STACKTRACE
...@@ -44,7 +69,7 @@ static int save_trace(struct stackframe *frame, void *d) ...@@ -44,7 +69,7 @@ static int save_trace(struct stackframe *frame, void *d)
{ {
struct stack_trace_data *data = d; struct stack_trace_data *data = d;
struct stack_trace *trace = data->trace; struct stack_trace *trace = data->trace;
unsigned long addr = frame->lr; unsigned long addr = frame->pc;
if (data->no_sched_functions && in_sched_functions(addr)) if (data->no_sched_functions && in_sched_functions(addr))
return 0; return 0;
...@@ -61,11 +86,10 @@ static int save_trace(struct stackframe *frame, void *d) ...@@ -61,11 +86,10 @@ static int save_trace(struct stackframe *frame, void *d)
void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace) void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
{ {
struct stack_trace_data data; struct stack_trace_data data;
unsigned long fp, base; struct stackframe frame;
data.trace = trace; data.trace = trace;
data.skip = trace->skip; data.skip = trace->skip;
base = (unsigned long)task_stack_page(tsk);
if (tsk != current) { if (tsk != current) {
#ifdef CONFIG_SMP #ifdef CONFIG_SMP
...@@ -76,14 +100,22 @@ void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace) ...@@ -76,14 +100,22 @@ void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
BUG(); BUG();
#else #else
data.no_sched_functions = 1; data.no_sched_functions = 1;
fp = thread_saved_fp(tsk); frame.fp = thread_saved_fp(tsk);
frame.sp = thread_saved_sp(tsk);
frame.lr = 0; /* recovered from the stack */
frame.pc = thread_saved_pc(tsk);
#endif #endif
} else { } else {
register unsigned long current_sp asm ("sp");
data.no_sched_functions = 0; data.no_sched_functions = 0;
asm("mov %0, fp" : "=r" (fp)); frame.fp = (unsigned long)__builtin_frame_address(0);
frame.sp = current_sp;
frame.lr = (unsigned long)__builtin_return_address(0);
frame.pc = (unsigned long)save_stack_trace_tsk;
} }
walk_stackframe(fp, base, base + THREAD_SIZE, save_trace, &data); walk_stackframe(&frame, save_trace, &data);
if (trace->nr_entries < trace->max_entries) if (trace->nr_entries < trace->max_entries)
trace->entries[trace->nr_entries++] = ULONG_MAX; trace->entries[trace->nr_entries++] = ULONG_MAX;
} }
......
struct stackframe {
unsigned long fp;
unsigned long sp;
unsigned long lr;
unsigned long pc;
};
int walk_stackframe(unsigned long fp, unsigned long low, unsigned long high,
int (*fn)(struct stackframe *, void *), void *data);
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
#include <asm/leds.h> #include <asm/leds.h>
#include <asm/thread_info.h> #include <asm/thread_info.h>
#include <asm/stacktrace.h>
#include <asm/mach/time.h> #include <asm/mach/time.h>
/* /*
...@@ -55,14 +56,22 @@ EXPORT_SYMBOL(rtc_lock); ...@@ -55,14 +56,22 @@ EXPORT_SYMBOL(rtc_lock);
#ifdef CONFIG_SMP #ifdef CONFIG_SMP
unsigned long profile_pc(struct pt_regs *regs) unsigned long profile_pc(struct pt_regs *regs)
{ {
unsigned long fp, pc = instruction_pointer(regs); struct stackframe frame;
if (in_lock_functions(pc)) { if (!in_lock_functions(regs->ARM_pc))
fp = regs->ARM_fp; return regs->ARM_pc;
pc = ((unsigned long *)fp)[-1];
} frame.fp = regs->ARM_fp;
frame.sp = regs->ARM_sp;
frame.lr = regs->ARM_lr;
frame.pc = regs->ARM_pc;
do {
int ret = unwind_frame(&frame);
if (ret < 0)
return 0;
} while (in_lock_functions(frame.pc));
return pc; return frame.pc;
} }
EXPORT_SYMBOL(profile_pc); EXPORT_SYMBOL(profile_pc);
#endif #endif
......
...@@ -18,15 +18,14 @@ ...@@ -18,15 +18,14 @@
#include <linux/mm.h> #include <linux/mm.h>
#include <linux/uaccess.h> #include <linux/uaccess.h>
#include <asm/ptrace.h> #include <asm/ptrace.h>
#include <asm/stacktrace.h>
#include "../kernel/stacktrace.h"
static int report_trace(struct stackframe *frame, void *d) static int report_trace(struct stackframe *frame, void *d)
{ {
unsigned int *depth = d; unsigned int *depth = d;
if (*depth) { if (*depth) {
oprofile_add_trace(frame->lr); oprofile_add_trace(frame->pc);
(*depth)--; (*depth)--;
} }
...@@ -70,9 +69,12 @@ void arm_backtrace(struct pt_regs * const regs, unsigned int depth) ...@@ -70,9 +69,12 @@ void arm_backtrace(struct pt_regs * const regs, unsigned int depth)
struct frame_tail *tail = ((struct frame_tail *) regs->ARM_fp) - 1; struct frame_tail *tail = ((struct frame_tail *) regs->ARM_fp) - 1;
if (!user_mode(regs)) { if (!user_mode(regs)) {
unsigned long base = ((unsigned long)regs) & ~(THREAD_SIZE - 1); struct stackframe frame;
walk_stackframe(regs->ARM_fp, base, base + THREAD_SIZE, frame.fp = regs->ARM_fp;
report_trace, &depth); frame.sp = regs->ARM_sp;
frame.lr = regs->ARM_lr;
frame.pc = regs->ARM_pc;
walk_stackframe(&frame, report_trace, &depth);
return; return;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment