While investigating Mbed OS startup code, I stumbled across the following anomalous (within Mbed OS) code, derived from TF-M:
MRS R0, control ; Get control value MOVS R1, #2 ORRS R0, R0, R1 ; Select switch to PSP MSR control, R0
This caused (me) some confusion, and I deleted it assuming it to be dead code (https://github.com/ARMmbed/mbed-os/pull/11698). But it looks like TF-M is relying on it, as it never seems to manipulate CONTROL_S.SPSEL itself.
It seems strange that TF-M is getting each target to do that itself. Why isn't TF-M setting this up itself in common code, just as it sets up PSP and PSPLIM?
I see some TF-M startups like mps2_an519 go on to do
LDR R0, =|Image$$ARM_LIB_STACK$$ZI$$Limit| MOVS R1, #7 BICS R0, R1 ; Make sure that the SP address is aligned to 8 MOV SP, R0 ; Initialise PSP
And that seems wrong to me. That is running in Handler mode, so SPSEL will not have had any effect, so that's actually setting MSP, not PSP. Isn't it? And PSP doesn't need initialising anyway?