ILD

MSR/MRS指令
作者:Herbert Yuan 邮箱:yuanjp89@163.com
发布时间:2017-10-21 站点:Inside Linux Development

这两条指令读写程序状态寄存器。可用于开关中断、切换模式等。下述内容参考自ARMARM 2005,适用于ARMv6及以下构架。


1 PSR位类型

Reserved bits 保留位

保留给将来扩展。


User-writable bits 用户模式可写位

任何模式都可以写,包括N,Z,C,V,Q,GE[3:0],E


Privileged bits 特权模式位

只在任何特权模式下可写,用户模式下写入被忽略,包括A,I,F,M[4:0]


Execution state bits 执行状态位

任何特权模式下可写,用户模式下写入被忽略,包括J,T。arm模式下均为0。


2 MRS

MRS (Move PSR to general-purpose register) moves the value of the CPSR or the SPSR of the current mode into a general-purpose register.


语法:

MRS{<cond>} Rd, CPSR

MRS{<cond>} Rd, SPSR


MRS指令通常用于3种目的:

- 作为read/modify/write序列的第一步更新PSR。

- 异常发生时,一个同类型的异常可能再次发生,这导致异常嵌套,因此异常模式的SPSR可能被覆盖。为了处理这种情况,必须在嵌套异常发生前保存SPSR,在嵌套异常返回前恢复。此时可使用MRS来读取SPSR然后在保存。

- 在进程切换代码中,被交换出去的程序状态必须被保存,包括对应的PSR内容。


注意:

在用户模式和系统模式访问SPSR具有不可预测的结果。


3 MSR

MSR (Move to Status Register from ARM Register) transfers the value of a general-purpose register or an immediate constant to the CPSR or the SPSR of the current mode.


语法

MSR{<cond>} CPSR_<fields>, #<immediate>

MSR{<cond>} CPSR_<fields>, <Rm>

MSR{<cond>} SPSR_<fields>, #<immediate>

MSR{<cond>} SPSR_<fields>, <Rm>


<fields>

c 设置控制位掩码,对应Privileged bits 

x 设置扩展位掩码,对应Reserved bits

s 设置状态位掩码,对应Execution state bits

f 设置标志位掩码,对应User-writable bits

每个表示1中的一种位类型,可以包含上述一个或多个掩码,出于效率考虑通常只设置要修改的掩码。


<immediate>

立即数是0x0到0xff的0-30范围内的偶数移位。


使用:

下述代码,从另一种特权模式进入Supervisor模式。

1
2
3
4
MRS R0, CPSR
BIC R0,R0,#0x1F
ORR RO,RO,#0x13
MSR CPSR_c,R0

Copyright © linuxdev.cc 2017-2024. Some Rights Reserved.