SPM: Use Main Stack for initialization
On reset, the Main Stack is used by default.
TF-M changes the stack to Processor Stack.
This patch remove the change to PSP in startup.s and:
- Rename the ARM_LIB_STACK to ER_INITIAL_PSP
- Rename the ARM_LIB_STACK_MSP to ARM_LIB_STACK
The renaming is to align the __main function of ARMClang where the
ARM_LIB_STACK is set to the current SP (MSP)
For Library Mode, the stacks are set to back to use PSP before entering
c code to avoid massive corresponding changes.
For FF-M implementations, this patch also:
- Seal the Main Stack before entering c code
- Manually free the Main Stack usage after initialization as the init
function does not return
Change-Id: Ie1b6f2fca1f774c4812a89fa45bb039b16fe5af0
Signed-off-by: Kevin Peng <kevin.peng@arm.com>
Co-authored-by: Summer Qin <summer.qin@arm.com>
diff --git a/secure_fw/spm/cmsis_func/main.c b/secure_fw/spm/cmsis_func/main.c
index c9ef011..4cbecd8 100644
--- a/secure_fw/spm/cmsis_func/main.c
+++ b/secure_fw/spm/cmsis_func/main.c
@@ -33,7 +33,7 @@
#error Only TFM_LVL 1 is supported for library model!
#endif
-REGION_DECLARE(Image$$, ARM_LIB_STACK_MSP, $$ZI$$Base);
+REGION_DECLARE(Image$$, ARM_LIB_STACK, $$ZI$$Base);
static fih_int tfm_core_init(void)
{
@@ -134,14 +134,33 @@
FIH_RET(fih_int_encode(TFM_SUCCESS));
}
+__attribute__((naked))
int main(void)
{
+ __ASM volatile(
+#if !defined(__ICCARM__)
+ ".syntax unified \n"
+#endif
+ "ldr r0, =Image$$ARM_LIB_STACK$$ZI$$Limit \n"
+ "msr msp, r0 \n"
+ "ldr r0, =Image$$ER_INITIAL_PSP$$ZI$$Limit \n"
+ "msr psp, r0 \n"
+ "mrs r0, control \n"
+ "movs r1, #2 \n"
+ "orrs r0, r0, r1 \n" /* Switch to PSP */
+ "msr control, r0 \n"
+ "bl c_main \n"
+ );
+}
+
+int c_main(void)
+{
enum spm_err_t spm_err = SPM_ERR_GENERIC_ERR;
fih_int fih_rc = FIH_FAILURE;
/* set Main Stack Pointer limit */
tfm_arch_init_secure_msp((uint32_t)®ION_NAME(Image$$,
- ARM_LIB_STACK_MSP,
+ ARM_LIB_STACK,
$$ZI$$Base));
/* Seal the PSP stacks viz ARM_LIB_STACK and TFM_SECURE_STACK */
@@ -167,9 +186,9 @@
tfm_spm_partition_set_state(TFM_SP_CORE_ID, SPM_PARTITION_STATE_RUNNING);
- REGION_DECLARE(Image$$, ARM_LIB_STACK, $$ZI$$Base)[];
+ REGION_DECLARE(Image$$, ER_INITIAL_PSP, $$ZI$$Base)[];
uint32_t psp_stack_bottom =
- (uint32_t)REGION_NAME(Image$$, ARM_LIB_STACK, $$ZI$$Base);
+ (uint32_t)REGION_NAME(Image$$, ER_INITIAL_PSP, $$ZI$$Base);
tfm_arch_set_psplim(psp_stack_bottom);
@@ -206,4 +225,6 @@
#endif
jump_to_ns_code();
+
+ return 0;
}