IvanZuy / freertos_c28x

FreeRTOS port for TI C2000's C28x based microcontrollers
MIT License
71 stars 32 forks source link

TMS320F2812 stack overflow #4

Closed RicPPBR closed 7 years ago

RicPPBR commented 7 years ago

FreeRTOS/portable/CCS/c28x/porASM.asm:

; Save stack pointer in the task control block. MOVL XAR0, #_pxCurrentTCB MOVL XAR0, XAR0 MOV AR6, @SP MOVL XAR0, XAR6

The value of the SP is copied to the low word of XAR6, AR6, and the high word of the XAR6 isn't modified. It makes the pxTopOfStack point to an adress ot of stack area of the stack's current task, resulting in data corruption and stack overflow. XAR6 must be initialized before:

; Save stack pointer in the task control block. MOVL XAR0, #_pxCurrentTCB MOVL XAR0, XAR0 MOVL XAR6, #0 ;set to 0 XAR6 before move the new value of pxTopOfStack MOV AR6, @SP MOVL XAR0, XAR6

IvanZuy commented 7 years ago

I agree that without setting high word of XAR6 to zero pxTopOfStack can possibly point to the wrong position in the memory. But SP is 16 bits register. When it's being restored at the end of context switch, only low word of pxTopOfStack will be used. Do you experience this issue with real HW?

RicPPBR commented 7 years ago

Yes. I tested the code in a real hw.

I took a look again in the code and I agree with you. It doesn't corrupt the stack data. The code block deals only with pointer addresses, but when #define configCHECK_FOR_STACK_OVERFLOW 1, makes taskCHECK_FOR_STACK_OVERFLOW() verify if( pxCurrentTCB->pxTopOfStack <= pxCurrentTCB->pxStack ). If it's true, call the function vApplicationStackOverflowHook.

If the high word of XAR6 is different of zero, taskCHECK_FOR_STACK_OVERFLOW() willnot detect true stack overflows

IvanZuy commented 7 years ago

Now it makes sense. Good catch! I'll make fix today.