Skip to content

Commit ce3266c

Browse files
Steven J. Magnanimichalsimek
authored andcommitted
microblaze: Add stack unwinder
Implement intelligent backtracing by searching for stack frame creation, and emitting only return addresses. Use print_hex_dump() to display the entire binary kernel stack. Limitation: MMU kernels are not currently able to trace beyond a system trap (interrupt, syscall, etc.). It is the intent of this patch to provide infrastructure that can be extended to add this capability later. Changes from V1: * Removed checks in find_frame_creation() that prevented location of the frame creation instruction in heavily optimized code * Various formatting/commenting/file location tweaks per review comments * Dropped Kconfig option to enable STACKTRACE as something logically separate Signed-off-by: Steven J. Magnani <steve@digidescorp.com>
1 parent ba9c4f8 commit ce3266c

10 files changed

Lines changed: 454 additions & 95 deletions

File tree

‎arch/microblaze/include/asm/exceptions.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
#define _ASM_MICROBLAZE_EXCEPTIONS_H
1515

1616
#ifdef __KERNEL__
17+
18+
#ifndef CONFIG_MMU
19+
#define EX_HANDLER_STACK_SIZ (4*19)
20+
#endif
21+
1722
#ifndef __ASSEMBLY__
1823

1924
/* Macros to enable and disable HW exceptions in the MSR */

‎arch/microblaze/include/asm/system.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ extern struct task_struct *_switch_to(struct thread_info *prev,
4545
#define smp_rmb() rmb()
4646
#define smp_wmb() wmb()
4747

48-
void show_trace(struct task_struct *task, unsigned long *stack);
4948
void __bad_xchg(volatile void *ptr, int size);
5049

5150
static inline unsigned long __xchg(unsigned long x, volatile void *ptr,
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Backtrace support for Microblaze
3+
*
4+
* Copyright (C) 2010 Digital Design Corporation
5+
*
6+
* This file is subject to the terms and conditions of the GNU General Public
7+
* License. See the file "COPYING" in the main directory of this archive
8+
* for more details.
9+
*/
10+
11+
#ifndef __MICROBLAZE_UNWIND_H
12+
#define __MICROBLAZE_UNWIND_H
13+
14+
struct stack_trace;
15+
16+
struct trap_handler_info {
17+
unsigned long start_addr;
18+
unsigned long end_addr;
19+
const char *trap_name;
20+
};
21+
extern struct trap_handler_info microblaze_trap_handlers;
22+
23+
extern const char _hw_exception_handler;
24+
extern const char ex_handler_unhandled;
25+
26+
void microblaze_unwind(struct task_struct *task, struct stack_trace *trace);
27+
28+
#endif /* __MICROBLAZE_UNWIND_H */
29+

‎arch/microblaze/kernel/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ extra-y := head.o vmlinux.lds
1717
obj-y += dma.o exceptions.o \
1818
hw_exception_handler.o init_task.o intc.o irq.o of_device.o \
1919
of_platform.o process.o prom.o prom_parse.o ptrace.o \
20-
setup.o signal.o sys_microblaze.o timer.o traps.o reset.o
20+
reset.o setup.o signal.o sys_microblaze.o timer.o traps.o unwind.o
2121

2222
obj-y += cpu/
2323

‎arch/microblaze/kernel/entry-nommu.S‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,3 +588,31 @@ sys_rt_sigsuspend_wrapper:
588588
#include "syscall_table.S"
589589

590590
syscall_table_size=(.-sys_call_table)
591+
592+
type_SYSCALL:
593+
.ascii "SYSCALL\0"
594+
type_IRQ:
595+
.ascii "IRQ\0"
596+
type_IRQ_PREEMPT:
597+
.ascii "IRQ (PREEMPTED)\0"
598+
type_SYSCALL_PREEMPT:
599+
.ascii " SYSCALL (PREEMPTED)\0"
600+
601+
/*
602+
* Trap decoding for stack unwinder
603+
* Tuples are (start addr, end addr, string)
604+
* If return address lies on [start addr, end addr],
605+
* unwinder displays 'string'
606+
*/
607+
608+
.align 4
609+
.global microblaze_trap_handlers
610+
microblaze_trap_handlers:
611+
/* Exact matches come first */
612+
.word ret_to_user ; .word ret_to_user ; .word type_SYSCALL
613+
.word ret_from_intr; .word ret_from_intr ; .word type_IRQ
614+
/* Fuzzy matches go here */
615+
.word ret_from_intr; .word no_intr_resched; .word type_IRQ_PREEMPT
616+
.word work_pending ; .word no_work_pending; .word type_SYSCALL_PREEMPT
617+
/* End of table */
618+
.word 0 ; .word 0 ; .word 0

‎arch/microblaze/kernel/entry.S‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,3 +1127,30 @@ ENTRY(_break)
11271127

11281128
syscall_table_size=(.-sys_call_table)
11291129

1130+
type_SYSCALL:
1131+
.ascii "SYSCALL\0"
1132+
type_IRQ:
1133+
.ascii "IRQ\0"
1134+
type_IRQ_PREEMPT:
1135+
.ascii "IRQ (PREEMPTED)\0"
1136+
type_SYSCALL_PREEMPT:
1137+
.ascii " SYSCALL (PREEMPTED)\0"
1138+
1139+
/*
1140+
* Trap decoding for stack unwinder
1141+
* Tuples are (start addr, end addr, string)
1142+
* If return address lies on [start addr, end addr],
1143+
* unwinder displays 'string'
1144+
*/
1145+
1146+
.align 4
1147+
.global microblaze_trap_handlers
1148+
microblaze_trap_handlers:
1149+
/* Exact matches come first */
1150+
.word ret_from_trap; .word ret_from_trap ; .word type_SYSCALL
1151+
.word ret_from_irq ; .word ret_from_irq ; .word type_IRQ
1152+
/* Fuzzy matches go here */
1153+
.word ret_from_irq ; .word no_intr_resched ; .word type_IRQ_PREEMPT
1154+
.word ret_from_trap; .word TRAP_return ; .word type_SYSCALL_PREEMPT
1155+
/* End of table */
1156+
.word 0 ; .word 0 ; .word 0

‎arch/microblaze/kernel/hw_exception_handler.S‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,6 @@
7878
#include <asm/asm-offsets.h>
7979

8080
/* Helpful Macros */
81-
#ifndef CONFIG_MMU
82-
#define EX_HANDLER_STACK_SIZ (4*19)
83-
#endif
8481
#define NUM_TO_REG(num) r ## num
8582

8683
#ifdef CONFIG_MMU
@@ -988,6 +985,7 @@ ex_unaligned_fixup:
988985
.end _unaligned_data_exception
989986
#endif /* CONFIG_MMU */
990987

988+
.global ex_handler_unhandled
991989
ex_handler_unhandled:
992990
/* FIXME add handle function for unhandled exception - dump register */
993991
bri 0

‎arch/microblaze/kernel/stacktrace.c‎

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,52 +14,18 @@
1414
#include <linux/thread_info.h>
1515
#include <linux/ptrace.h>
1616
#include <linux/module.h>
17+
#include <asm/unwind.h>
1718

18-
/* FIXME initial support */
1919
void save_stack_trace(struct stack_trace *trace)
2020
{
21-
unsigned long *sp;
22-
unsigned long addr;
23-
asm("addik %0, r1, 0" : "=r" (sp));
24-
25-
while (!kstack_end(sp)) {
26-
addr = *sp++;
27-
if (__kernel_text_address(addr)) {
28-
if (trace->skip > 0)
29-
trace->skip--;
30-
else
31-
trace->entries[trace->nr_entries++] = addr;
32-
33-
if (trace->nr_entries >= trace->max_entries)
34-
break;
35-
}
36-
}
21+
/* Exclude our helper functions from the trace*/
22+
trace->skip += 2;
23+
microblaze_unwind(NULL, trace);
3724
}
3825
EXPORT_SYMBOL_GPL(save_stack_trace);
3926

4027
void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
4128
{
42-
unsigned int *sp;
43-
unsigned long addr;
44-
45-
struct thread_info *ti = task_thread_info(tsk);
46-
47-
if (tsk == current)
48-
asm("addik %0, r1, 0" : "=r" (sp));
49-
else
50-
sp = (unsigned int *)ti->cpu_context.r1;
51-
52-
while (!kstack_end(sp)) {
53-
addr = *sp++;
54-
if (__kernel_text_address(addr)) {
55-
if (trace->skip > 0)
56-
trace->skip--;
57-
else
58-
trace->entries[trace->nr_entries++] = addr;
59-
60-
if (trace->nr_entries >= trace->max_entries)
61-
break;
62-
}
63-
}
29+
microblaze_unwind(tsk, trace);
6430
}
6531
EXPORT_SYMBOL_GPL(save_stack_trace_tsk);

‎arch/microblaze/kernel/traps.c‎

Lines changed: 40 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -16,80 +16,69 @@
1616

1717
#include <asm/exceptions.h>
1818
#include <asm/system.h>
19+
#include <asm/unwind.h>
1920

2021
void trap_init(void)
2122
{
2223
__enable_hw_exceptions();
2324
}
2425

25-
static unsigned long kstack_depth_to_print = 24;
26+
static unsigned long kstack_depth_to_print; /* 0 == entire stack */
2627

2728
static int __init kstack_setup(char *s)
2829
{
2930
return !strict_strtoul(s, 0, &kstack_depth_to_print);
3031
}
3132
__setup("kstack=", kstack_setup);
3233

33-
void show_trace(struct task_struct *task, unsigned long *stack)
34+
void show_stack(struct task_struct *task, unsigned long *sp)
3435
{
35-
unsigned long addr;
36-
37-
if (!stack)
38-
stack = (unsigned long *)&stack;
36+
unsigned long words_to_show;
37+
u32 fp = (u32) sp;
38+
39+
if (fp == 0) {
40+
if (task) {
41+
fp = ((struct thread_info *)
42+
(task->stack))->cpu_context.r1;
43+
} else {
44+
/* Pick up caller of dump_stack() */
45+
fp = (u32)&sp - 8;
46+
}
47+
}
3948

40-
printk(KERN_NOTICE "Call Trace: ");
41-
#ifdef CONFIG_KALLSYMS
42-
printk(KERN_NOTICE "\n");
43-
#endif
44-
while (!kstack_end(stack)) {
45-
addr = *stack++;
46-
/*
47-
* If the address is either in the text segment of the
48-
* kernel, or in the region which contains vmalloc'ed
49-
* memory, it *may* be the address of a calling
50-
* routine; if so, print it so that someone tracing
51-
* down the cause of the crash will be able to figure
52-
* out the call path that was taken.
53-
*/
54-
if (kernel_text_address(addr))
55-
print_ip_sym(addr);
49+
words_to_show = (THREAD_SIZE - (fp & (THREAD_SIZE - 1))) >> 2;
50+
if (kstack_depth_to_print && (words_to_show > kstack_depth_to_print))
51+
words_to_show = kstack_depth_to_print;
52+
53+
pr_info("Kernel Stack:\n");
54+
55+
/*
56+
* Make the first line an 'odd' size if necessary to get
57+
* remaining lines to start at an address multiple of 0x10
58+
*/
59+
if (fp & 0xF) {
60+
unsigned long line1_words = (0x10 - (fp & 0xF)) >> 2;
61+
if (line1_words < words_to_show) {
62+
print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 32,
63+
4, (void *)fp, line1_words << 2, 0);
64+
fp += line1_words << 2;
65+
words_to_show -= line1_words;
66+
}
5667
}
57-
printk(KERN_NOTICE "\n");
68+
print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 32, 4, (void *)fp,
69+
words_to_show << 2, 0);
70+
printk(KERN_INFO "\n\n");
71+
72+
pr_info("Call Trace:\n");
73+
microblaze_unwind(task, NULL);
74+
pr_info("\n");
5875

5976
if (!task)
6077
task = current;
6178

6279
debug_show_held_locks(task);
6380
}
6481

65-
void show_stack(struct task_struct *task, unsigned long *sp)
66-
{
67-
unsigned long *stack;
68-
int i;
69-
70-
if (sp == NULL) {
71-
if (task)
72-
sp = (unsigned long *) ((struct thread_info *)
73-
(task->stack))->cpu_context.r1;
74-
else
75-
sp = (unsigned long *)&sp;
76-
}
77-
78-
stack = sp;
79-
80-
printk(KERN_INFO "\nStack:\n ");
81-
82-
for (i = 0; i < kstack_depth_to_print; i++) {
83-
if (kstack_end(sp))
84-
break;
85-
if (i && ((i % 8) == 0))
86-
printk("\n ");
87-
printk("%08lx ", *sp++);
88-
}
89-
printk("\n");
90-
show_trace(task, stack);
91-
}
92-
9382
void dump_stack(void)
9483
{
9584
show_stack(NULL, NULL);

0 commit comments

Comments
 (0)