关于异常处理,分为三部分:
1. ARM异常和模式:core处理异常时的操作,几种模式介绍。
2. Vector table:
3. 异常优先级
4. lr偏移:几种异常如何返回
异常和中断处理简介
在嵌入式系统中异常处理是核心之一。高效的处理能够极大的提升系统的性能。
ARM处理器一共有7种可以暂停指令的执行序列的异常。
本章节主要分为三个部分:
1. Exception handling
2. Interrupts
3. Interrupt handling schemes
本文介绍第一部分
Exception Handling
1.ARM Processor Exceptions And Modes
任何一种中断模式都可以通过手动的修改cpsr的值来进入。但是User和System模式是仅有的2个不能由相应中断进入的模式,换句话说,我们必须要通过手动修改cpsr才能进入。
Exception | Mode | Main purpose |
---|---|---|
Fast Interrupt Request | FIQ | fast interrupt handling |
Interrupt Request | IRQ | interrupt request handling |
SWI and Reset | SVC | protected mode for operating systems |
Prefetch Abort and Data Abort | abort | virtual memory or memory protection handling |
Undefined Intsruction | undefined | software emulation of hardware coprocessor |
当一个异常产生的时候,core会自动进行如下4步:
1. saves the cpsr to the spsr of the exception mode
2. saves the pc to the lr of the exception mode
3. sets the cpsr to the exception mode
4. sets the pc to the address of the exception handler
需要注意的是,当异常产生的时候,ARM处理器总是会切换到ARM状态。
2.Vector Table
Exception | Mode | Vector table offset |
---|---|---|
Reset | SVC | +0x00 |
Undefined Instrution | UND | +0x04 |
Software Interrupt | SVC | +0x08 |
Prefetch Abort | ABT | +0x0c |
Data Abort | ABT | +0x10 |
Not assigned | - | +0x14 |
IRQ | IRQ | +0x18 |
FIQ | FIQ | +0x1c |
handler定位在相应内存位置中,如下图的IRQ,FIQ
0x00000018: 0xe59ffa38 IRQ ; ldr pc, [pc, #irq]
0x0000001c: 0xe59ffa38 FIQ ; ldr pc, [pc, #fiq]
3.Exception Priorities
Exception | Priority | I bit | F bit | Description |
---|---|---|---|---|
Reset | 1 | 1 | 1 | 1.最高优先级,哪里发生哪里就立即执行 2.初始化系统,包括memory和cache 3.在使能FIQ和IRQ前,初始化外部中断资源 4.为所有处理器模式建立stack pointers |
Data Abort | 2 | 1 | - | 1.当memory contrller或MMU访问非法的内存地址或者当前代码在没有权限的情况下读写内存的时候会产生该中断 2.此外,当FIQ enable状态时,在Data Abort handler中会产生FIQ,当FIQ服务结束后,再回到Data Abort handler中继续处理 |
FIQ | 3 | 1 | 1 | 1.会在当外设set FIQ pin to nFIQ的时候产生 2.拥有最高中断优先级 |
IRQ | 4 | 1 | - | 1.会在当外设set IRQ pin to nIRQ的时候产生 2.拥有第二高中断优先级 3.当IRQ disable的时候,disable状态会持续到当前中断源被清除 |
Prefetch Abort | 5 | 1 | - | 当试图fetch an instruction的时候,产生了内存错误,此时会产生该异常 |
Software Interrupt | 6 | 1 | - | 产生在:当一个SWI指令产生,并且其他高优先级异常均没有产生的时候。此外进入该异常的时候,cpsr会进入supervisor模式 |
Undefined Instruction | 6 | 1 | - | 指令不在ARM和Thumb指令集中 |
4.Link Register Offset
Exception | Address | Use |
---|---|---|
Reset | - | lr没有在Reset定义 |
Data Abort | lr-8 | 指向造成Data abort异常的指令 |
FIQ | lr-4 | return address from FIQ handler |
IRQ | lr-4 | return address from IRQ handler |
Prefetch Abort | lr-4 | 只想造成prefetch abort异常的指令 |
SWI | lr | 指向SWI指令的下一条指令 |
Undefined Instruction | lr | 指向undefined instruction的下一条指令 |
如下三种从IRQ和FIQ异常处理返回的例子
例1:
handler
...
SUBS pc, r14, #4 ;pc = r14 -4
因为SUB后的S和pc作为目标寄存器,cpsr的值会自动从spsr中恢复出来
例2:
handler
SUB r14, r14, #4 ;r14 -= 4
...
<code>
...
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中恢复出来。
具体STMFD等指令请看另一篇博客的stack指令部分:http://blog.youkuaiyun.com/feather_wch/article/details/50418107,也可以参考书籍p70.