Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

1.3 Special registers: masks and CONTROL

System state that A-profile keeps in PSTATE and sysregs is split, on M-profile, between a few special registers and memory-mapped SCS registers. You access the special registers only with MRS/MSR, and the masks also with CPS.

  • PRIMASK (all implementations): bit[0] = 1 raises execution priority to 0, which masks every configurable-priority exception. NMI and HardFault are not masked. CPSID i / CPSIE i set and clear it.

  • FAULTMASK (Mainline): bit[0] = 1 raises execution priority to -1, which masks HardFault as well. NMI is not masked. CPSID f / CPSIE f. Exception return (other than from NMI) clears it automatically.

  • With the Security Extension, PRIMASK and FAULTMASK are banked per Security state, and the priority level the Non-secure copies boost to depends on AIRCR.PRIS and AIRCR.BFHFNMINS. Those rules are not covered here.

  • BASEPRI (Mainline): a non-zero value raises execution priority to that value, so exceptions whose group priority value is greater than or equal to it cannot preempt. 0 disables BASEPRI masking. BASEPRI_MAX is a write alias that updates BASEPRI only if the new value raises the masking level.

  • CONTROL:

    bit0 nPRIV  Thread mode is unprivileged when 1
    bit1 SPSEL  Thread mode uses PSP when 1 (MSP when 0)
    bit2 FPCA   FP context active (FP Ext)
    bit3 SFPA   Secure FP context active (FP + Security Ext)
    (PACBTI adds enable bits above bit3)
    

When unprivileged software writes PRIMASK, FAULTMASK, BASEPRI or CONTROL with MSR, the write is ignored, and CPS behaves as a NOP. Neither faults.

Synchronization:

  • An MSR or CPS that raises execution priority (masking) takes effect for the following instructions without a barrier.
  • An MSR or CPS that lowers execution priority needs an ISB before an already-pending exception is guaranteed to be taken.
  • After MSR CONTROL that changes SPSEL or nPRIV, software must issue an ISB so that the instructions after it are guaranteed to use the new stack pointer or privilege level.

Coming from A-profile: PRIMASK and FAULTMASK are not DAIF bits. They mask by raising the execution priority, so they interact with the NVIC priority scheme instead of acting as per-class enable bits. Unprivileged writes do not trap the way an EL0 MSR DAIF with SCTLR_EL1.UMA = 0 does. They are silently ignored.

Checkpoint

Q1.3.1 An unprivileged Thread-mode task runs CPSID i and then enters a ‘critical section’. What actually happens?

  • A. A UsageFault (UNDEFINSTR) is raised, because CPS is a privileged instruction.
  • B. PRIMASK is set, because CPS is not privilege-checked.
  • C. CPSID is treated as a NOP. PRIMASK is unchanged, interrupts stay enabled and no fault is raised, so the critical section is not protected.
  • D. A HardFault is raised immediately.
Answer

C. For unprivileged execution, the architecture defines CPS as having no effect, and MSR writes to the mask registers are ignored. A-profile experience suggests a trap, but there is none. This is a silent correctness bug, so unprivileged code must request masking through an SVC.

Read more: Armv8-M ARM (DDI0553), CPS and MSR instruction descriptions (privilege checks)

Q1.3.2 Privileged code executes MSR CONTROL, r0 to set SPSEL = 1, then immediately pushes registers. What is the architecturally correct sequence?

  • A. MSR CONTROL, r0 followed by ISB, then the push
  • B. MSR CONTROL, r0 followed by DMB, then the push
  • C. MSR CONTROL, r0 followed by DSB, then the push
  • D. No barrier is needed, because MSR to CONTROL is self-synchronizing.
Answer

A. A change to CONTROL.SPSEL or nPRIV is guaranteed to affect subsequent instructions only after a context synchronization event, which is the ISB. DMB and DSB order or complete memory accesses, but they are not context synchronization events, so they are the wrong tool here. Some cores happen to work without the ISB, but the architecture does not guarantee it.

Read more: Armv8-M ARM (DDI0553), CONTROL register; MSR; context synchronization

Q1.3.3 A Mainline core uses 3 priority bits, so priority values are 0x00, 0x20, … 0xE0, and PRIGROUP is at its reset value (all implemented bits are group priority). With BASEPRI = 0x40, which pending interrupt can still preempt the current Thread-mode code?

  • A. An interrupt at priority 0x40
  • B. An interrupt at priority 0x20
  • C. An interrupt at priority 0x60
  • D. None of them. Any non-zero BASEPRI masks all configurable interrupts.
Answer

B. BASEPRI raises execution priority to 0x40, and only an exception with a lower group priority value (higher urgency) can preempt. So 0x40 and 0x60 are masked and 0x20 is not. Expecting 0x40 to pass is the usual off-by-one mistake: equal priority never preempts.

Read more: Armv8-M ARM (DDI0553), BASEPRI and execution priority