Skip to content

Commit e933c1f

Browse files
committed
调整异常处理代码结构,以支持backtrace功能
1 parent 782960f commit e933c1f

File tree

3 files changed

+84
-21
lines changed

3 files changed

+84
-21
lines changed

include/rtdef.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,10 @@ typedef siginfo_t rt_siginfo_t;
567567
#define RT_SCHEDULE_IPI 0
568568
#endif
569569

570+
#ifndef RT_STOP_IPI
571+
#define RT_STOP_IPI 1
572+
#endif
573+
570574
/**
571575
* CPUs definitions
572576
*

libcpu/arm/cortex-a/start_gcc.S

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,18 @@ rt_hw_context_switch_interrupt_do:
373373
mrs r6, spsr @/* Save CPSR */
374374
str lr, [r0, #15*4] @/* Push PC */
375375
str r6, [r0, #16*4] @/* Push CPSR */
376-
cps #Mode_SVC
376+
mrs r5, cpsr @/* Save CPSR */
377+
378+
and r4, r6, #0x1F
379+
cmp r4, #Mode_USR
380+
moveq r6, #Mode_SYS
381+
382+
orr r6, r6, #0x80 @/* Switch to previous mode, then save SP & PC */
383+
msr cpsr_c, r6
377384
str sp, [r0, #13*4] @/* Save calling SP */
378385
str lr, [r0, #14*4] @/* Save calling PC */
386+
387+
msr cpsr_c, r5 @/* Switch back to current mode */
379388
.endm
380389

381390
.align 5
@@ -482,6 +491,8 @@ secondary_cpu_start:
482491
.bss
483492
.align 2 //align to 2~2=4
484493

494+
.global sub_stack_top /* used for backtrace to calculate stack top of irq mode */
495+
485496
sub_stack_start:
486497
.space (SUB_ISR_Stack_Size * (RT_CPUS_NR-1))
487498
sub_stack_top:

libcpu/arm/cortex-a/trap.c

Lines changed: 68 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ void rt_hw_show_register(struct rt_hw_exp_stack *regs)
3535
rt_kprintf("cpsr:0x%08x\n", regs->cpsr);
3636
}
3737

38+
void (*rt_trap_hook)(struct rt_hw_exp_stack *regs, const char *ex, unsigned int exception_type);
39+
40+
/**
41+
* This function will set a hook function to trap handler.
42+
*
43+
* @param hook the hook function
44+
*/
45+
void rt_hw_trap_set_hook(void (*hook)(struct rt_hw_exp_stack *regs, const char *ex, unsigned int exception_type))
46+
{
47+
rt_trap_hook = hook;
48+
}
49+
3850
/**
3951
* When comes across an instruction which it cannot handle,
4052
* it takes the undefined instruction trap.
@@ -72,12 +84,20 @@ void rt_hw_trap_undef(struct rt_hw_exp_stack *regs)
7284
}
7385
}
7486
#endif
75-
rt_kprintf("undefined instruction:\n");
76-
rt_hw_show_register(regs);
87+
88+
if (rt_trap_hook == RT_NULL)
89+
{
90+
rt_kprintf("undefined instruction:\n");
91+
rt_hw_show_register(regs);
7792
#ifdef RT_USING_FINSH
78-
list_thread();
93+
list_thread();
7994
#endif
80-
rt_hw_cpu_shutdown();
95+
rt_hw_cpu_shutdown();
96+
}
97+
else
98+
{
99+
rt_trap_hook(regs, "undefined instruction", UND_EXCEPTION);
100+
}
81101
}
82102

83103
/**
@@ -91,12 +111,19 @@ void rt_hw_trap_undef(struct rt_hw_exp_stack *regs)
91111
*/
92112
void rt_hw_trap_swi(struct rt_hw_exp_stack *regs)
93113
{
94-
rt_kprintf("software interrupt:\n");
95-
rt_hw_show_register(regs);
114+
if (rt_trap_hook == RT_NULL)
115+
{
116+
rt_kprintf("software interrupt:\n");
117+
rt_hw_show_register(regs);
96118
#ifdef RT_USING_FINSH
97-
list_thread();
119+
list_thread();
98120
#endif
99-
rt_hw_cpu_shutdown();
121+
rt_hw_cpu_shutdown();
122+
}
123+
else
124+
{
125+
rt_trap_hook(regs, "software instruction", SWI_EXCEPTION);
126+
}
100127
}
101128

102129
/**
@@ -109,12 +136,19 @@ void rt_hw_trap_swi(struct rt_hw_exp_stack *regs)
109136
*/
110137
void rt_hw_trap_pabt(struct rt_hw_exp_stack *regs)
111138
{
112-
rt_kprintf("prefetch abort:\n");
113-
rt_hw_show_register(regs);
139+
if (rt_trap_hook == RT_NULL)
140+
{
141+
rt_kprintf("prefetch abort:\n");
142+
rt_hw_show_register(regs);
114143
#ifdef RT_USING_FINSH
115-
list_thread();
144+
list_thread();
116145
#endif
117-
rt_hw_cpu_shutdown();
146+
rt_hw_cpu_shutdown();
147+
}
148+
else
149+
{
150+
rt_trap_hook(regs, "prefetch abort", PABT_EXCEPTION);
151+
}
118152
}
119153

120154
/**
@@ -127,12 +161,19 @@ void rt_hw_trap_pabt(struct rt_hw_exp_stack *regs)
127161
*/
128162
void rt_hw_trap_dabt(struct rt_hw_exp_stack *regs)
129163
{
130-
rt_kprintf("data abort:");
131-
rt_hw_show_register(regs);
164+
if (rt_trap_hook == RT_NULL)
165+
{
166+
rt_kprintf("data abort:");
167+
rt_hw_show_register(regs);
132168
#ifdef RT_USING_FINSH
133-
list_thread();
169+
list_thread();
134170
#endif
135-
rt_hw_cpu_shutdown();
171+
rt_hw_cpu_shutdown();
172+
}
173+
else
174+
{
175+
rt_trap_hook(regs, "data abort", DABT_EXCEPTION);
176+
}
136177
}
137178

138179
/**
@@ -144,12 +185,19 @@ void rt_hw_trap_dabt(struct rt_hw_exp_stack *regs)
144185
*/
145186
void rt_hw_trap_resv(struct rt_hw_exp_stack *regs)
146187
{
147-
rt_kprintf("reserved trap:\n");
148-
rt_hw_show_register(regs);
188+
if (rt_trap_hook == RT_NULL)
189+
{
190+
rt_kprintf("reserved trap:\n");
191+
rt_hw_show_register(regs);
149192
#ifdef RT_USING_FINSH
150-
list_thread();
193+
list_thread();
151194
#endif
152-
rt_hw_cpu_shutdown();
195+
rt_hw_cpu_shutdown();
196+
}
197+
else
198+
{
199+
rt_trap_hook(regs, "reserved trap", RESV_EXCEPTION);
200+
}
153201
}
154202

155203
void rt_hw_trap_irq(void)

0 commit comments

Comments
 (0)