Skip to content

Commit b3fe647

Browse files
wearyzennashif
authored andcommitted
arch: arm: cortex_a_r: Fix restore of registers while exiting exception
This commit fixes potential unpredictable behavior, caused by using the ^ form of ldmia instruction, while exiting an exception in SMP mode on Cortex-A/R. Change: Use "pop" instead of "ldmia" to restore user mode registers while exiting from an exception via `z_arm_cortex_ar_exit_exc`. Reason for change: Processor mode is always set to system (MODE_SYS) before calling `z_arm_cortex_ar_exit_exc` and hence, the user mode register can be accessed directly without the ^ form of the instruction. Also, LDMIA instruction is UNPREDICTABLE in SYStem mode. Signed-off-by: Sudan Landge <[email protected]>
1 parent 6d8deac commit b3fe647

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

arch/arm/core/cortex_a_r/isr_wrapper.S

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,15 @@ z_arm_cortex_ar_irq_done:
339339
str r0, [r2, #___cpu_t_nested_OFFSET]
340340
/* Do not context switch if exiting a nested interrupt */
341341
cmp r0, #0
342+
/* Note that this function is only called from `z_arm_svc`,
343+
* while handling irq_offload, with below modes set:
344+
* ```
345+
* if (cpu interrupts are nested)
346+
* mode=MODE_SYS
347+
* else
348+
* mode=MODE_IRQ
349+
* ```
350+
*/
342351
bhi __EXIT_INT
343352

344353
/* retrieve pointer to the current thread */

arch/arm/core/cortex_a_r/vector_table.S

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ SECTION_SUBSEC_FUNC(exc_vector_table,_vector_table_section,_vector_table)
4141
GTEXT(z_arm_cortex_ar_exit_exc)
4242
SECTION_SUBSEC_FUNC(TEXT, _HandlerModeExit, z_arm_cortex_ar_exit_exc)
4343

44+
/* Note:
45+
* This function is expected to be *always* called with
46+
* processor mode set to MODE_SYS.
47+
*/
48+
4449
/* decrement exception depth */
4550
get_cpu r2
4651
ldrb r1, [r2, #_cpu_offset_to_exc_depth]
@@ -51,7 +56,6 @@ SECTION_SUBSEC_FUNC(TEXT, _HandlerModeExit, z_arm_cortex_ar_exit_exc)
5156
* Restore r0-r3, r12, lr, lr_und and spsr_und from the exception stack
5257
* and return to the current thread.
5358
*/
54-
ldmia sp, {r0-r3, r12, lr}^
55-
add sp, #24
59+
pop {r0-r3, r12, lr}
5660
rfeia sp!
5761
#endif

0 commit comments

Comments
 (0)