hi, experts:
以Cortex-A7 MPCore为例:
如何判断一个Core已经成功切换到Non-Secure state?
MPCore reset后,通过读取SCR register,可知当前Core处于Secure state.
执行相关Non-Secure state切换代码后:访问SCR会hang住,这代表切换Non-Secure state成功了吗?
best wishes,
Hi
ARM ARM中对SCR的定义如下:
所以可以通过SCR.NS来判断是不是 secure world。但是这个寄存器只有secure software (secure PL1)才能访问,普通的OS或者user application是访问不到的。
>执行相关Non-Secure state切换代码后:访问SCR会hang住,
>这代表切换Non-Secure state成功了吗?
SCR在non-secure world里属于未定义的CP15寄存器,non-secure sortware读写SCR会产生 UNDEF exception。
Xingguang
hi, feng:
似乎在Non-Secure state下,无法判断当前的CPU State?!
是的,在non-secure state是无法准确知道当前CPU是否处于non-secure/secure state的,但是我们可以知道是否处于monitor mode。我们的经验是在不产生undefined exception的情况下可以稍微根据一些状态猜测一下,以下代码是我在实现T6过程中进行某些测试时用到的代码,注释写得很详细了,贴出供参考:
// check if we are running in secure mode
// If we are in secure world, we can simply read the Secure Configuration Register(SCR)
// to know whether we are in secure world and SCR is the ideal way to know. However, SCR
// is accessible only secure secre privileged modes only, otherwise an underfined exception
// will be raised.
// WARNING: this function may get a wrong result
int is_secure_world(){
uint cpsr =0, nsacr =0;
asm volatile(
"mrc p15, 0, %[nsacr],cr1,cr1,2\n"
"mrs %[cpsr], cpsr\n" :
[nsacr]"=r" (nsacr),
[cpsr]"=r" (cpsr));
// if NASAR contains the reset value(=0) then most likely
// we are running in Secure Mode. If the cpsr mode is set
// to monitor mode then we cannot load!
//
if(nsacr ==0 || (cpsr & 0x1f)==0b10110)
return 1;
return 0;
}
另一种思路是,如果你能够控制整个kernel的话,可以正常访问SCR寄存器,但是要在undefined exception handler里面添加处理代码。比如在访问SCR之前在某个物理地址写个0x10086,然后在undefined exception handler中加一个corner case:当那个物理地址值为0x10086时就处理这个异常并返回。这样就可以准确知道是否处于non-secure state啦!
希望以上回答可以帮到你哈,
Wenhao