Skip to content

Commit 3e4196a

Browse files
jcmvbkbcczankel
authored andcommitted
xtensa: add stacktrace support
Signed-off-by: Max Filippov <jcmvbkbc@gmail.com> Signed-off-by: Chris Zankel <chris@zankel.net>
1 parent 4e96274 commit 3e4196a

5 files changed

Lines changed: 140 additions & 41 deletions

File tree

‎arch/xtensa/Kconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ config HZ
5252
source "init/Kconfig"
5353
source "kernel/Kconfig.freezer"
5454

55+
config STACKTRACE_SUPPORT
56+
def_bool y
57+
5558
config MMU
5659
def_bool n
5760

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* arch/xtensa/include/asm/stacktrace.h
3+
*
4+
* This file is subject to the terms and conditions of the GNU General Public
5+
* License. See the file "COPYING" in the main directory of this archive
6+
* for more details.
7+
*
8+
* Copyright (C) 2001 - 2013 Tensilica Inc.
9+
*/
10+
#ifndef _XTENSA_STACKTRACE_H
11+
#define _XTENSA_STACKTRACE_H
12+
13+
#include <linux/sched.h>
14+
15+
struct stackframe {
16+
unsigned long pc;
17+
unsigned long sp;
18+
};
19+
20+
static __always_inline unsigned long *stack_pointer(struct task_struct *task)
21+
{
22+
unsigned long *sp;
23+
24+
if (!task || task == current)
25+
__asm__ __volatile__ ("mov %0, a1\n" : "=a"(sp));
26+
else
27+
sp = (unsigned long *)task->thread.sp;
28+
29+
return sp;
30+
}
31+
32+
void walk_stackframe(unsigned long *sp,
33+
int (*fn)(struct stackframe *frame, void *data),
34+
void *data);
35+
36+
#endif /* _XTENSA_STACKTRACE_H */

‎arch/xtensa/kernel/Makefile‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
extra-y := head.o vmlinux.lds
66

7-
obj-y := align.o entry.o irq.o coprocessor.o process.o ptrace.o \
8-
setup.o signal.o syscall.o time.o traps.o vectors.o platform.o \
9-
pci-dma.o
7+
obj-y := align.o coprocessor.o entry.o irq.o pci-dma.o platform.o process.o \
8+
ptrace.o setup.o signal.o stacktrace.o syscall.o time.o traps.o \
9+
vectors.o
1010

1111
obj-$(CONFIG_KGDB) += xtensa-stub.o
1212
obj-$(CONFIG_PCI) += pci.o

‎arch/xtensa/kernel/stacktrace.c‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* arch/xtensa/kernel/stacktrace.c
3+
*
4+
* This file is subject to the terms and conditions of the GNU General Public
5+
* License. See the file "COPYING" in the main directory of this archive
6+
* for more details.
7+
*
8+
* Copyright (C) 2001 - 2013 Tensilica Inc.
9+
*/
10+
#include <linux/export.h>
11+
#include <linux/sched.h>
12+
#include <linux/stacktrace.h>
13+
14+
#include <asm/stacktrace.h>
15+
#include <asm/traps.h>
16+
17+
void walk_stackframe(unsigned long *sp,
18+
int (*fn)(struct stackframe *frame, void *data),
19+
void *data)
20+
{
21+
unsigned long a0, a1;
22+
unsigned long sp_end;
23+
24+
a1 = (unsigned long)sp;
25+
sp_end = ALIGN(a1, THREAD_SIZE);
26+
27+
spill_registers();
28+
29+
while (a1 < sp_end) {
30+
struct stackframe frame;
31+
32+
sp = (unsigned long *)a1;
33+
34+
a0 = *(sp - 4);
35+
a1 = *(sp - 3);
36+
37+
if (a1 <= (unsigned long)sp)
38+
break;
39+
40+
frame.pc = MAKE_PC_FROM_RA(a0, a1);
41+
frame.sp = a1;
42+
43+
if (fn(&frame, data))
44+
return;
45+
}
46+
}
47+
48+
#ifdef CONFIG_STACKTRACE
49+
50+
struct stack_trace_data {
51+
struct stack_trace *trace;
52+
unsigned skip;
53+
};
54+
55+
static int stack_trace_cb(struct stackframe *frame, void *data)
56+
{
57+
struct stack_trace_data *trace_data = data;
58+
struct stack_trace *trace = trace_data->trace;
59+
60+
if (trace_data->skip) {
61+
--trace_data->skip;
62+
return 0;
63+
}
64+
if (!kernel_text_address(frame->pc))
65+
return 0;
66+
67+
trace->entries[trace->nr_entries++] = frame->pc;
68+
return trace->nr_entries >= trace->max_entries;
69+
}
70+
71+
void save_stack_trace_tsk(struct task_struct *task, struct stack_trace *trace)
72+
{
73+
struct stack_trace_data trace_data = {
74+
.trace = trace,
75+
.skip = trace->skip,
76+
};
77+
walk_stackframe(stack_pointer(task), stack_trace_cb, &trace_data);
78+
}
79+
EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
80+
81+
void save_stack_trace(struct stack_trace *trace)
82+
{
83+
save_stack_trace_tsk(current, trace);
84+
}
85+
EXPORT_SYMBOL_GPL(save_stack_trace);
86+
87+
#endif

‎arch/xtensa/kernel/traps.c‎

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
* Essentially rewritten for the Xtensa architecture port.
1313
*
14-
* Copyright (C) 2001 - 2005 Tensilica Inc.
14+
* Copyright (C) 2001 - 2013 Tensilica Inc.
1515
*
1616
* Joe Taylor <joe@tensilica.com, joetylr@yahoo.com>
1717
* Chris Zankel <chris@zankel.net>
@@ -32,6 +32,7 @@
3232
#include <linux/delay.h>
3333
#include <linux/hardirq.h>
3434

35+
#include <asm/stacktrace.h>
3536
#include <asm/ptrace.h>
3637
#include <asm/timex.h>
3738
#include <asm/uaccess.h>
@@ -402,53 +403,25 @@ void show_regs(struct pt_regs * regs)
402403
regs->syscall);
403404
}
404405

405-
static __always_inline unsigned long *stack_pointer(struct task_struct *task)
406+
static int show_trace_cb(struct stackframe *frame, void *data)
406407
{
407-
unsigned long *sp;
408-
409-
if (!task || task == current)
410-
__asm__ __volatile__ ("mov %0, a1\n" : "=a"(sp));
411-
else
412-
sp = (unsigned long *)task->thread.sp;
413-
414-
return sp;
408+
if (kernel_text_address(frame->pc)) {
409+
printk(" [<%08lx>] ", frame->pc);
410+
print_symbol("%s\n", frame->pc);
411+
}
412+
return 0;
415413
}
416414

417415
void show_trace(struct task_struct *task, unsigned long *sp)
418416
{
419-
unsigned long a0, a1, pc;
420-
unsigned long sp_start, sp_end;
421-
422-
if (sp)
423-
a1 = (unsigned long)sp;
424-
else
425-
a1 = (unsigned long)stack_pointer(task);
426-
427-
sp_start = a1 & ~(THREAD_SIZE-1);
428-
sp_end = sp_start + THREAD_SIZE;
417+
if (!sp)
418+
sp = stack_pointer(task);
429419

430420
printk("Call Trace:");
431421
#ifdef CONFIG_KALLSYMS
432422
printk("\n");
433423
#endif
434-
spill_registers();
435-
436-
while (a1 > sp_start && a1 < sp_end) {
437-
sp = (unsigned long*)a1;
438-
439-
a0 = *(sp - 4);
440-
a1 = *(sp - 3);
441-
442-
if (a1 <= (unsigned long) sp)
443-
break;
444-
445-
pc = MAKE_PC_FROM_RA(a0, a1);
446-
447-
if (kernel_text_address(pc)) {
448-
printk(" [<%08lx>] ", pc);
449-
print_symbol("%s\n", pc);
450-
}
451-
}
424+
walk_stackframe(sp, show_trace_cb, NULL);
452425
printk("\n");
453426
}
454427

0 commit comments

Comments
 (0)