7种processor mode又分为3类:
User mode
Privileged mode
system mode
对于System mode的作用一直有些模糊,今天在arm的网站上找到了一份说明文档,对这个问题的解析很到位,特记录如下:
The ARM Architecture defines a User mode that has 15 general purpose registers, a pc, and a CPSR.
除User Mode外,还有5种privileged modes
每一种Priviledged mode都有一个SPSR and a number of registers that replace some of the 15 User mode general purpose registers.
当exception发生时:
the current PC is copied into the link register for the exception mode,
the CPSR is copied into the SPSR for the exception mode.
The CPSR is then altered in an exception-dependent way, and the program counter is set to an exception-defined address to start the
exception handler.
BL指令 copies the return address into r14 before changing the PC, so the subroutine return instruction moves r14 to pc (MOV pc,lr).
Together these actions imply that ARM modes that handle exceptions must ensure that another exception of the same type cannot
occur if they call subroutines, because the subroutine return address will be overwritten with the exception return address.
(要防止lr被覆盖)
(In earlier versions of the ARM architecture, this problem has been solved by either carefully avoiding subroutine calls in exception
code, or changing from the privileged mode to User mode.The first solution is often too restrictive, and the second means the task
may not have the privileged access it needs to run correctly.)
从ARMv4架构开始,提供了system mode来解决lr覆盖问题.
System mode is a privileged processor mode that shares the User mode registers. (与User Mode共用registers)
Privileged mode tasks can run in this mode, and exceptions no longer overwrite the link register.
注意:
System mode cannot be entered by an exception.
The exception handlers modify the CPSR to enter System mode. See Reentrant interrupt handlers for an example.
就不逐字翻译了,大体意思如下:
当处理器异常出现时,当前程序计数器(也就是 PC+offset,offset与异常种类相关)会被拷贝的相应异常模式的LR,CPSR也会被拷贝到
相应异常模式的SPSR。然后CPSR会被设置为相应的异常模式, PC被设置到对应异常的入口处执行其处理函数。
(上面这些都是ARM核自动操作的)
ARM的子程序转移指令BL会在改变PC前将返回地址放到LR中,所以从子程序返回时可以把r14放到PC来操作。如MOV pc, lr。
所有上面的动作都暗示了如果异常处理函数会调用子程序(使用 BL),那么各异常模式都必须保证异常处理函数执行的过程中不能出现
同样的异常,因为子函数的返回地址会被异常的返回地址覆盖掉。(在早期的ARM版本中,可以通过禁止异常处理函数调用子函数或者切
换处理模式到User模式来解决这个问题。第一种方式过于严格,而第二种方式可以会由于User模式缺少相应的权限而不能执行某些动
作)。
为此,ARM v4及之后的版本提供了system mode这样一种处理器模式来解决这个问题。System mode是一种privileged的模式,而且共用
User模式的所有寄存器。Privileged模式的程序可以运行在这个模式,而不用担心处理器异常会擦除LR。
上一篇:ARMv7的非对齐地址访问
下一篇:ARM TCM(紧耦合内存)内存--简介
推荐阅读最新更新时间:2024-03-16 15:01