bschleusner wrote on Thursday, January 01, 2015:
Hello,
I’ve been digging through the source of the ARM_CM3 port of FreeRTOS (V8.1.2 and older), and noticed that in the prvPortStartFirstTask function resets the MSR register to the top of the stack. Is there a reason why the MSR needs to be reset?
I bring this up because I have been running into issues with variables located on the stack in main() getting corrupted. Looking into the corruption issue I noticed that the prvPortStartFirstTask was reseting MSR, which leads to privlidged code corrupting the main stack.
Excerpt from FreeRTOS/Source/portable/GCC/ARM_CM3/port.c
static void prvPortStartFirstTask( void )
{
__asm volatile(
" ldr r0, =0xE000ED08 \n" /* Use the NVIC offset register to locate the stack. */
" ldr r0, [r0] \n"
" ldr r0, [r0] \n"
" msr msp, r0 \n" /* Set the msp back to the start of the stack. */
" cpsie i \n" /* Globally enable interrupts. */
" cpsie f \n"
" dsb \n"
" isb \n"
" svc 0 \n" /* System call to start first task. */
" nop \n"
);
}
I’ve disabled the part that resets the MSR and it appears to function just fine. It seams like a bug to be overwriting the stack.
Thanks