关于异常处理,分为三部分:
- ARM异常和模式:core处理异常时的操作,几种模式介绍。
- Vector table:
- 异常优先级
- lr偏移:几种异常如何返回
异常和中断处理简介
在嵌入式系统中异常处理是核心之一。高效的处理能够极大的提升系统的性能。
ARM处理器一共有7种可以暂停指令的执行序列的异常。
主要分为三个部分:
点击这里 http://www.makeru.com.cn/live/3523_1666.html?s=70408
创客学院带你搞定异常和中断处理
- Exception handling
- Interrupts
- Interrupt handling schemes
今天我们主要介绍第一部分
Exception Handling
1.ARM Processor Exceptions And Modes
任何一种中断模式都可以通过手动的修改cpsr的值来进入。但是User和System模式是仅有的2个不能由相应中断进入的模式,换句话说,我们必须要通过手动修改cpsr才能进入。
当一个异常产生的时候,core会自动进行如下4步:
- saves the cpsr to the spsr of the exception mode
- saves the pc to the lr of the exception mode
- sets the cpsr to the exception mode
- sets the pc to the address of the exception handler
需要注意的是,当异常产生的时候,ARM处理器总是会切换到ARM状态。
2.Vector Table
handler定位在相应内存位置中,如下图的IRQ,FIQ
0x00000018: 0xe59ffa38 IRQ ; ldr pc, [pc, #irq]
0x0000001c: 0xe59ffa38 FIQ ; ldr pc, [pc, #fiq]
3.Exception Priorities
4.Link Register Offset
如下三种从IRQ和FIQ异常处理返回的例子
例1:
handler
…
SUBS pc, r14, #4 ;pc = r14 -4
因为SUB后的S和pc作为目标寄存器,cpsr的值会自动从spsr中恢复出来
例2:
handler
SUB r14, r14, #4 ;r14 -= 4
…
…
MOVS pc, r14
r14 = r14 -4, pc = r14 , cpsr =spsr(因为S)
例3:
handler
SUB r14, r14, #4 ; r14 = r14 - 4
STMFD r13!, {r0-r3, r14} ;store context
…
LDMFD r13!, {r0-r3, r14}^ ;return
STMFD,LDMFD 分别提供了pop/push的功能。STMFD sp!, {r0-r3, r14}就是依次将r14, r3, r2, r1放入栈中。LDMFD r13!, {r0-r3, r14}则依次反向取出。 这个符号,则强制cpsr从spsr中恢复出来。