# "Nested" context switch

**URL:** <https://forums.freertos.org/t/nested-context-switch/22949>\
**Category:** Kernel\
**Created:** [April 19, 2025, 6:04pm UTC](https://forums.freertos.org/t/nested-context-switch/22949 "2025-04-19T18:04:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matth9814](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/matth9814/32/5174_2.png) [@Matth9814](https://forums.freertos.org/u/Matth9814)\
**Post date:** [April 19, 2025, 6:04pm UTC](https://forums.freertos.org/t/nested-context-switch/22949/1 "2025-04-19T18:04:31Z")

</div>

Hi, I just wanted to ask if it should be possible to execute a context switch at the end of the interrupt handler after interrupting the core while it was executing another context switch. For example this could happen in the [Cortex-A9 port](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/portable/GCC/ARM_CA9/portASM.S) if the _FreeRTOS\_SWI\_Handler_ is interrupted by the SysTick interrupt and _xTaskIncrementTick_ requires a context switch.

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [April 19, 2025, 11:52pm UTC](https://forums.freertos.org/t/nested-context-switch/22949/2 "2025-04-19T23:52:55Z")

</div>

I would expect that the code would have any code needing protection for that to be inside a critical section, thus disabling interrupts.

---

<div class="post-metadata">

**Author:** ![Matth9814](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/matth9814/32/5174_2.png) [@Matth9814](https://forums.freertos.org/u/Matth9814)\
**Post date:** [April 20, 2025, 10:02am UTC](https://forums.freertos.org/t/nested-context-switch/22949/3 "2025-04-20T10:02:39Z")

</div>

The reason why I am asking is that some ports, like the [RP2040](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/portable/ThirdParty/GCC/RP2040/port.c) in both single-core and multi-core, disable the interrupts before entering _vTaskSwitchContext_ whilst the Cortex-A9 port doesn’t.

---

<div class="post-metadata">

**Author:** ![karahulx](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/karahulx/32/4893_2.png) [@karahulx](https://forums.freertos.org/u/karahulx)\
**Post date:** [April 20, 2025, 4:45pm UTC](https://forums.freertos.org/t/nested-context-switch/22949/4 "2025-04-20T16:45:27Z")

</div>

For ports like [RP2040](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/portable/ThirdParty/GCC/RP2040/port.c) , the process for saving the context and restoring the context is done in C, inside the [PendSV handler](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/portable/ThirdParty/GCC/RP2040/port.c#L528) . As a result, `vTaskSwitchContext()` could be interrupted, potentially leading to inconsistent state if, `SysTick` fires mid-way. **So they disable interrupts to guard it.**

For ports like [Cortex-A9 port](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/portable/GCC/ARM_CA9/portASM.S#L145), the SVC handler (triggered by `portYIELD()`) runs in **SVC mode** , which is an exception mode.  
From ARM documentation

> Exceptions are handled in a fixed priority order, and an exception of a given priority cannot preempt another exception of the same or higher priority. Specifically, while an exception handler is executing, the processor masks exceptions of the same or lower priority to prevent reentrancy.

Therefore, when the processor is handling an SVC exception, it will not accept another SVC exception until the current handler has completed. This hardware-enforced behavior ensures that SVC handlers are **not reentrant**. As a result for these ports, the call to `vTaskSwitchContext()` happens **within an atomic section** enforced by the hardware - **SVC mode itself** is the guard.

---

<div class="post-metadata">

**Author:** ![Matth9814](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/matth9814/32/5174_2.png) [@Matth9814](https://forums.freertos.org/u/Matth9814)\
**Post date:** [April 20, 2025, 5:54pm UTC](https://forums.freertos.org/t/nested-context-switch/22949/5 "2025-04-20T17:54:24Z")

</div>

Thanks for the detailed reply. While I agree with you that if the processor enters SVC mode to handle such exception there is no way another exception of the same type can be handled, that was not what my initial problem. My program was servicing the SysTick interrupt within `FreeRTOS_SWI_Handler` and I couldn’t understand why. After looking through some of the ARMv7-A documentation I discovered that when an exception is taken the I bit of the CPSR is automatically set. Apparently, I was mistakenly enabling interrupts by calling `ulPortSetInterruptMask` and `vPortClearInterruptMask` inside `taskCHECK_FOR_STACK_OVERFLOW`.

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [April 21, 2025, 5:10am UTC](https://forums.freertos.org/t/nested-context-switch/22949/6 "2025-04-21T05:10:19Z")

</div>

> [@Matth9814](#):
>
> Apparently, I was mistakenly enabling interrupts by calling `ulPortSetInterruptMask` and `vPortClearInterruptMask` inside `taskCHECK_FOR_STACK_OVERFLOW`.

Thank you for sharing! You are right that the core disables interrupts upon taking an exception. The corresponding handler may decide to enable them explicitly but SWI handler never enables them.
