About portSET_INTERRUPT_MASK_FROM_ISR()

Hello everyone,

I’m currently learning FreeRTOS on ARM Cortex-M3 and came across the macro portSET_INTERRUPT_MASK_FROM_ISR(), which is the ISR version of the API for entering a critical section. The task-level critical-section API uses the uxCriticalNesting variable to keep track of the nesting level, whereas the ISR version uses a “snapshot” (save-and-restore) approach instead.

I’m curious about the rationale behind this design. Why can’t the ISR version also use uxCriticalNesting to track nesting and implement critical-section entry/exit in the same way as the task-level version?

Thanks in advance for any insights.

The ISR version can’t assume that the current state where leaving the critical section is all interrupts enabled, as you might be in a nested interrupt. Thus the ISR version must save the current state and restore it.

At the task level, there can’t be any “other” pending interrupts, as any such interrupt would be higher in execution then the task, so the task level can just assume it can restore all interrupts.

I sort of wish that the task level did the same so they could have been the same function, but I understand the history, the task level not needing code to save makes things simpler to code.

Thank you.The answer the AI gave me is similar to what you said.However,I still don’t quite understand.For instance,if the macro that enters the critical section is called in interrupt A, then all interrupt levels that FreeRtos can manage should be disabled.Only interrupts that exceed the interrupt level managed by FreeRtos can interrupt A, such as interrupt B.But B also prohibits calling API functions with the ISR suffix.So it’s fine for B to interrupt A. Then, after A finishes execution, the interruption should be in the enabled state.
Could you please give an example? For instance, in what scenarios is it necessary to save and restore instead of using the nested counting mode of the normal version.

On Cortex-M, the NVIC hardware ensures that a lower-priority interrupt cannot preempt the current ISR without modifying BASEPRI. As a result, BASEPRI is effectively set to only two values: 0 and configMAX_SYSCALL_INTERRUPT_PRIORITY. This means you can do away with maintaining a nesting count and simply clear BASEPRI to 0 when exiting from the last nested level.

However, on several other platforms, when the hardware takes an interrupt, it writes a mask value to a register to prevent lower-priority interrupts from preempting the current ISR. FreeRTOS also writes to the same register to implement ISR critical sections. On those platforms, it is necessary to save and restore the mask correctly — otherwise, exiting a critical section could inadvertently unmask interrupts that the hardware had masked. Because the C code that uses these macros is shared across all platforms, it performs the save-and-restore on every platform.

Thank you for your reply. I get it now. The purpose of this approach is to support compatibility with other platforms, though it becomes redundant on Cortex‑M platforms.

@suntainyun the issue isn’t going INTO the critical section, but how to exit it. As @aggarg is mentioning, on some processors. that value is what is used to control how interrupts nest. Since the API needs to be consistent over all processors, FreeRTOS needs a way to save that context.

As an example, on a processor that when entering a priority x ISR, saves the mask register as part of the context onto the stack, and then masks off all interrupts of priority x or lower, the ISR based critical section code needs to restore THAT setting, not re-enable all the interrupts as the task level code can do.