Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cortex M - Atomicity of IRQ disabling

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!

like image 539
Petr Kalandra Avatar asked Oct 22 '19 16:10

Petr Kalandra


People also ask

How do I enable and disable a FIQ interrupt?

Enabling Interrupts to the Core To enable IRQ interrupts, clear the I bit of the CPSR; to enable FIQ interrupts, clear the F bit of the CPSR. Likewise, to disabled these interrupts, set the respective bits.

What are the priority options for Cortex M core interrupt handling?

Interrupt Priority Registers (NVIC_IPR)8 bits are used to configure the priority of each interrupt. The number of supported priority levels is implementation defined and is in the range of 4-256. When less than 256 priority levels are implemented, the lower bits in the field read-as-zero.

What is NVIC explain different interrupts & exceptions of ARM Cortex M3?

The Cortex-M3 processor supports interrupts and system exceptions. The processor and the NVIC prioritize and handle all exceptions. An exception changes the normal flow of software control. The processor uses Handler mode to handle all exceptions except for reset.

What is the use of Primask Faultmask and Basepri respectively?

The PRIMASK, FAULTMASK, and BASEPRI registers are used for exception or interrupt masking.


1 Answers

What happens when the IRQ comes in between the execution of this two instructions and the IRQ handler changes the state of PRIMASK?

The program will randomly lock up, as this would also break most other "wait for interrupt" methods (like volatile variables).

Remember that an interrupt can only occur if it is not masked, so the interrupt handler could only ever disable interrupts. But disabling interrupts globally will also prevent other interrupts from fireing - and the code waiting for some hardware interaction usually does not re-enable interrups randomly.

That is why an interrupt handler is considered br0ken when it modifies PRIMASK or FAULTMASKwithout restoring it on exception return.

like image 54
Turbo J Avatar answered Sep 25 '22 19:09

Turbo J