I've spent hours trying to answer my question, but could not find any satisfying answer.
Since ARM Cortex-M cores does not have any instruction to read the state of global interupt mask (PRIMASK register) and immediately disabling it, all frameworks are using the following sequence of two instructions:
mrs r0, PRIMASK ; Read current state
cpsid i ; Mask IRQs
But there is no explanation, why this piece of code is considered atomic... What happens when the IRQ comes in between the execution of this two instructions and the IRQ handler changes the state of PRIMASK? Like
mrs r0, PRIMASK ; Read current state
; Some weird IRQ handling happens here and changes PRIMASK
cpsid i ; Mask IRQs
Since this code is widely used, I suspect that this situation should never happen by (architecture?) design. Could somebody please explain to me why? :-) Thanks!