3.1.6mstatus: The Hart Operating-State Register

Part II Linux boot: required Vol. II (Privileged) pp. 31–40 · ~6 min read

  • mstatus privilege stack
  • mprv/mxr/sum
  • fs/vs/xs

One register tracks and controls the hart’s entire operating state: privilege/interrupt stacks, memory-privilege modifiers, endianness, virtualization traps, and extension-context dirtiness. sstatus is a masked view of the same storage. (The priv PDF’s mstatus figures are among its missing diagrams — the layout below is reconstructed from the field descriptions; RV32 splits the high fields into mstatush.)

mstatus (0x300, RV64)

SD63WPRI6243MDT42MPELP41WPRI4038MBE37SBE36SXL3534UXL3332WPRI3124SPELP23TSR22TW21TVM20MXR19SUM18MPRV17XS1615FS1413MPP1211VS109SPP8MPIE7UBE6SPIE5WPRI4MIE3WPRI2SIE1WPRI0
WPRI/RO WLRL WARL RW
Click a field for semantics, reset state, and the minimal-implementation note.

The privilege & interrupt-enable stack

The two-level stack per interrupt-capable mode is the trap mechanism’s heart:

  • Taking a trap from mode y into mode x: xPIE ← xIE; xIE ← 0; xPP ← y (plus mepc/mcause/mtval — next pages).
  • xRET: xIE ← xPIE; mode ← xPP; xPIE ← 1; xPP ← U (least privileged implemented mode); if the destination is below M, also MPRV ← 0.
  • Global-enable geometry: running in mode x, interrupts for modes below x are always off, for modes above x always on — xIE matters only in mode x itself. The low-bit placement of MIE/SIE lets one csrci/csrsi toggle them atomically.

Memory-privilege modifiers & extension context

MPRV/MXR/SUM exist to make M-mode emulation and kernel-to-user copies efficient: MPRV redirects data accesses through MPP’s translation (the firmware misaligned-access emulator’s tool), MXR reads execute-only pages, SUM opens U pages to S-mode (Linux sets it around copy_{to,from}_user).

The FS/VS/XS machinery drives lazy context switching:

Extension-context status protocol (Tables 13/14)
At context saveAt restoreOn use
Off (0)SkipSkipIllegal-instruction
Initial (1)SkipConstant-init (no memory!)Runs; modification → Dirty
Clean (2)SkipLoad from memoryRuns; modification → Dirty
Dirty (3)Save, then mark CleanN/A (never restore Dirty)Runs
Dotted-underlined cells have explanations — click one.

M and S share these fields — M-mode firmware must be conservative with state it touches. Setting FS=Off preserves the FP contents; only access legality changes.

Endianness, virtualization traps, double-trap

Data endianness is per-mode (MBE/SBE/UBE; fetch always LE); implicit page-table accesses follow SBE, so flipping it demands an SFENCE.VMA. The TVM/TW/TSR trio are the classic-virtualization intercepts (shadow paging, idle-scheduling, H-emulation). And Smdbltrp’s MDT bit turns the fragile early-handler window into defined behavior: trap-while-MDT=1 escalates to the RNMI handler (Smrnmi, NMIE=1) or parks the hart in the critical-error state rather than corrupting saved state.

Hardware Designer Notes

mstatus is the register your trap unit, LSU, and CSR file all touch — implement it as distributed flops with a gather/scatter at the CSR port, not a monolithic register. The Linux-relevant subset: the MIE/MPIE/MPP + SIE/SPIE/SPP stacks, FS, MPRV/SUM/MXR, TVM/TW/TSR, and read-only-sane values for everything else. That’s ~20 flops of state doing most of the privileged architecture’s work.

Minimal Linux-boot hart MUST

  • Implement the trap/xRET stack shuffle exactly, including xPP←U and MPRV-clearing on downward xRET
  • Evaluate the global-enable geometry (lower-off/higher-on) in the interrupt-take condition, not just xIE
  • Route MPRV=1 data accesses through MPP’s translation, protection, endianness AND XLEN
  • Keep fetch endianness LE; use SBE for page-table walks
  • Alias sstatus as a masked read/write view of this storage

MAY simplify / trap-and-emulate

  • Hardwire SXL/UXL (=MXL), MBE/SBE/UBE (=0), and all optional-extension fields to zero
  • Track FS imprecisely — Off/Dirty-only is legal and simplifies the pipeline
  • Defer Smdbltrp (MDT read-only 0) on a first core

Check yourself — mstatus

1.A timer interrupt arrives while the hart runs in U-mode with mstatus.MIE=0 (and the trap is not delegated). Is it taken?

2.Walk the mstatus updates when a trap is taken from S-mode into M-mode.

3.M-mode firmware wants to emulate a misaligned S-mode store through the S-mode page tables. Which mstatus tool does the job?

4.Your context-switch code reads mstatus.SD=0. What does it skip?

5.Under Smdbltrp, what happens when a trap targets M-mode while mstatus.MDT is already 1?

5 questions