I am porting FreeRTOS with TrustZone on LPC5500, I put FreeRTOS in secure memory, and created several user tasks in non-secure memory, as shown below:
But so far, I have not successfully switched from a secure task to a non-secure task.
How to switch from a secure task to a non-secure task?
In Non-secure task stack:
The stack frame as following:
Thanks
Regarding stack frame, sorry for not clear. I meant the stack frame you created in Non-secure stack for the exception return to take place. What is in that stack frame?
Hi Joseph
1) The psp_ns set to Non-secure memory.
2) The stack frame is standard stack frame , the value of the xPSR seems to be incorrect(T bit set , IPSR=3)
3) In PendSV interrupt, the PC is pointing Secure memory, new PC value is stored in the non-secure task stack and the new PC will point Non-secure memory.
4) The value of the fault status registers are shown as following:
CFSR:0X00000000
HFSR:0X40000000
DFSR:0X00000001
AFSR:0X00000000
SFSR:0X00000000
Can you help me analyze the cause of the HardFault?
Thanks.
Best Regards
Yang Zhang
There could be different causes for the HardFault:
- is the PSP_NS set correctly to Non-secure memory?
- what is in the stack frame? Was the stacked xPSR correct? (T bit set and IPSR=0)
- Is the PC value really pointing to Non-secure memory?
- what is showing in the fault status registers?
Thank you for your answer, but after I change the value of r14 (EXC_RETURN) from 0XFFFFFFFD to 0XFFFFFFBD in the PendSV interrupt, when the "bx r14" is executed, the program enters the hardfault state, it seems 0XFFFFFFBD is an illegal EXC_RETURN value?
Assumed that
- Non-secure task use PSP_NS (pointing to return stack for the Non-secure task)
- At the beginning of the Non-secure task, FPCA was 0 (no floating point context at start)
You can return from Secure handler to Non-secure thread with a EXC_RETURN code of 0xFFFFFFBD.
However, with the guidance from PSA & Trusted Firmware team, the plan for FreeRTOS port is to have the OS running in Non-secure world, and OS helper APIs in Secure world to help context switching.
regards,
Joseph