Microprocessor context switch

75 views Asked by At

I've started investigating FreeRTOS and looked into the task context save routine. This routines stores the registers and the stack pointer. My question is about the stack in different threads. What if there is a thread which performs pushes and pops generated by the compiler. Wouldn't it be possible to overwrite the stack of a different thread?

1

There are 1 answers

0
Clifford On BEST ANSWER

Each thread must be allocated sufficient stack for its own call-stack plus that required for context storage. The amount of additional stack space required for context storage will depend on the target, but in teh case of FreeRTOS specifically, the constant configMINIMAL_STACK_SIZE will be at least that size plus some margin.

On some targets where the thread stack is used in interrupt contexts, you will also need to account for stack usage by interrupts. If interrupts are nestable; the worst case condition will be when all interrupts become active in priority order before any have completed - perhaps an unlikely scenario, but one you should consider.

Advice on stack allocation for FreeRTOS is provided in the FAQ at http://www.freertos.org/FAQMem.html#StackSize