Commit 162e286
committed
Fix Cortex-R5 ISR stack switching implementation
This implements missing ISR stack switching to prevent thread stack
overflow. Comments described the logic but code was not implemented,
causing ISRs to execute on thread stacks instead of dedicated system
stack.
Context Save (ISR Entry):
- tx_thread_context_save.S: Add missing stack switch for IRQ
- tx_thread_fiq_context_save.S: Add missing stack switch for FIQ
- tx_thread_vectored_context_save.S: Add missing stack switch for vectored
Context Restore (ISR Exit):
- tx_thread_context_restore.S: Restore thread SP before popping registers
(both __tx_thread_no_preempt_restore and __tx_thread_preempt_restore)
- tx_thread_fiq_context_restore.S: Restore thread SP before popping registers
(both FIQ no-preempt and preempt paths)
Implementation:
ISR Entry (Save):
STMDB sp!, {r2, r10, r12, lr} @ Save registers on thread stack
LDR r1, =_tx_thread_system_stack_ptr @ Get system stack address
STR sp, [r0, eclipse-threadx#8] @ Save thread SP to TCB
LDR sp, [r1] @ Switch to system stack
ISR Exit (Restore):
LDR sp, [r0, eclipse-threadx#8] @ Restore thread SP from TCB
LDMIA sp!, {r0, r10, r12, lr} @ Pop from thread stack
Rationale:
- Prevents stack overflow: ISRs execute on dedicated system stack
- Preserves thread stack integrity for debugging and stack checking
- Matches design intent documented in inline comments
- Required for nested interrupt support and stack overflow detection
- Critical for lockstep mode (deterministic stack usage)
Execution Flow:
1. IRQ/FIQ occurs while thread running
2. Save minimal context to thread stack
3. Save thread SP to TCB at offset eclipse-threadx#8 (tx_thread_stack_ptr field)
4. Switch SP to system stack
5. ISR executes on system stack
6. Restore thread SP from TCB before returning
7. Pop saved context from thread stack
8. Return to interrupted thread
All Paths Fixed:
- IRQ no-preempt restore: Thread continues without context switch
- IRQ preempt restore: Higher-priority thread needs to run
- FIQ no-preempt restore: FIQ returns to same thread
- FIQ preempt restore: FIQ triggers thread switch
- Vectored interrupts: Return through context_restore (already fixed)
Impact:
- Fixes stack overflow risk from nested/heavy ISRs
- Enables TX_ENABLE_STACK_CHECKING to work correctly
- Debugging tools can now properly inspect thread stacks
- System stack usage isolated from thread stack usage1 parent 41840f0 commit 162e286
5 files changed
Lines changed: 25 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
158 | 158 | | |
159 | 159 | | |
160 | 160 | | |
| 161 | + | |
| 162 | + | |
161 | 163 | | |
162 | 164 | | |
163 | 165 | | |
| |||
171 | 173 | | |
172 | 174 | | |
173 | 175 | | |
| 176 | + | |
174 | 177 | | |
175 | 178 | | |
176 | 179 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
140 | 140 | | |
141 | 141 | | |
142 | 142 | | |
143 | | - | |
| 143 | + | |
144 | 144 | | |
145 | 145 | | |
146 | 146 | | |
147 | 147 | | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
148 | 151 | | |
149 | 152 | | |
150 | 153 | | |
151 | 154 | | |
152 | | - | |
| 155 | + | |
| 156 | + | |
153 | 157 | | |
154 | 158 | | |
155 | 159 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
160 | 160 | | |
161 | 161 | | |
162 | 162 | | |
| 163 | + | |
| 164 | + | |
163 | 165 | | |
164 | 166 | | |
165 | 167 | | |
| |||
173 | 175 | | |
174 | 176 | | |
175 | 177 | | |
| 178 | + | |
176 | 179 | | |
177 | 180 | | |
178 | 181 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
137 | 137 | | |
138 | 138 | | |
139 | 139 | | |
140 | | - | |
| 140 | + | |
141 | 141 | | |
142 | | - | |
143 | | - | |
| 142 | + | |
| 143 | + | |
144 | 144 | | |
145 | 145 | | |
146 | 146 | | |
147 | 147 | | |
148 | | - | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
149 | 152 | | |
150 | 153 | | |
151 | | - | |
| 154 | + | |
152 | 155 | | |
| 156 | + | |
153 | 157 | | |
154 | 158 | | |
155 | 159 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
135 | 135 | | |
136 | 136 | | |
137 | 137 | | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
138 | 141 | | |
139 | 142 | | |
140 | 143 | | |
141 | 144 | | |
| 145 | + | |
142 | 146 | | |
143 | 147 | | |
144 | 148 | | |
| |||
0 commit comments