casaroli commented on code in PR #22295:
URL: https://github.com/apache/nuttx/pull/22295#discussion_r4696533295


##########
arch/arm/src/arm_m/arm_exception.S:
##########
@@ -237,27 +299,37 @@ exception_common:
 
 #ifdef CONFIG_ARMV8M_STACKCHECK_HARDWARE
        ldmia           r0!, {r1}               /* Get psplim/msplim */
+       add             r0, r0, #4
 #endif
 
        /* The EXC_RETURN value tells us whether we are returning on the MSP or 
PSP
         */
 
-       tst             r14, #EXC_RETURN_PROCESS_STACK          /* Nonzero if 
context on process stack */
+       mov             r2, r14                                 /* Copy 
EXC_RETURN */
+       lsls            r2, #(31 - EXC_RETURN_PROCESS_BITNO)    /* Move to bit 
31 */
+       bmi             4f                                      /* Test bit 31 
*/
+       msr             msp, r0                                 /* R1=The main 
stack pointer */
 #ifdef CONFIG_ARMV8M_STACKCHECK_HARDWARE
-       itete           eq
-       msreq           msplim, r1
-       msrne           psplim, r1
-#else
-       ite             eq                                      /* Next two 
instructions conditional */
+       msr             msplim, r1
 #endif
-       msreq           msp, r0                                 /* R0=The main 
stack pointer */
-       msrne           psp, r0                                 /* R0=The 
process stack pointer */
+       b               5f
+4:
+       msr             psp, r0                                 /* R1=The 
process stack pointer */
+#ifdef CONFIG_ARMV8M_STACKCHECK_HARDWARE
+       msr             psplim, r1
+#endif
+5:
+       isb             sy
 
        /* Restore the interrupt state */
 
+#ifdef CONFIG_ARCH_ARMV6M
+       msr             primask, r3                             /* Restore 
interrupts priority masking*/
+#else
        msr             basepri, r3                             /* Restore 
interrupts priority masking */
+#endif /* CONFIG_ARCH_ARMV6M */
 
-       msr             control, r12
+       msr             control, r12                            /* Restore 
control */

Review Comment:
   On ARMv6-M this writes CONTROL from `r12`, but the ARMv6-M return path never 
reloads `r12`: it restores CONTROL from the saved context with `msr control, 
r6` (line 278) and recovers only R4-R11 and R14. `r12` is a scratch register 
for `arm_doirq()` and the handlers it calls, so CONTROL gets a random value 
(nPRIV on Cortex-M0+), and after a context switch it gets the CONTROL of the 
interrupted task instead of the new one. The old armv6-m/arm_exception.S had 
only `msr control, r6`.
   
   Fix: put `msr control, r12` under `#ifndef CONFIG_ARCH_ARMV6M`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to