-
Notifications
You must be signed in to change notification settings - Fork 24
Description
**Description: **
This issue proposes implementing a separate, dedicated stack for Interrupt Service Routines (ISRs) to improve system stability and prevent unpredictable stack overflows.
Current Behavior
Currently, when an interrupt preempts a running task, the subsequent ISR executes using the stack of the interrupted task. This means the ISR's stack frame and any nested function calls consume the stack space allocated for that specific task.
The Problem
This design creates a high risk of stack overflow and makes stack size calculation unreliable for two main reasons:
Non-Deterministic Stack Usage: The required stack size for a task becomes dependent not only on its own call tree but also on the worst-case stack usage of any ISRs that could possibly preempt it, especially nested interrupts or high frequent interrupts occurring.
High Risk of Overflow: A task that is functioning correctly with its allocated stack can crash unexpectedly if it's preempted by a complex, stack-heavy ISR. These overflows are often timing-dependent and extremely difficult to debug.
Proposed Solution
The port should be modified to switch to a dedicated interrupt stack when an ISR is triggered. The standard implementation flow for this is as follows:
-
An interrupt occurs.
-
The minimal context of the interrupted task is saved on the task's stack.
-
The CPU's stack pointer (SP) for each CPU is switched to point to a pre-allocated, dedicated interrupt stack respectively.
-
The main body of the C-level ISR handler is then executed using this safe, dedicated stack.
-
Upon completion of all ISRs, the SP is switched back to the task's stack before its full context is restored and execution resumes.