3.1.15-18M-Mode CSRs III: mcause, Exception Priority, mtval & menvcfg

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

  • mcause
  • mtval

When a trap fires, three registers tell the handler everything: mcause (what), mepc (where — previous page), mtval (the details). This page also covers menvcfg, M-mode’s feature-gate panel.

3.1.15 mcause and Table 16

mcause = Interrupt bit (MSB) + WLRL exception code. One sign-branch separates interrupts; a shift scales the code into a dispatch table.

mcause codes (Table 16). Loads/LR → load exceptions; store/SC/AMO → store/AMO exceptions.
Interrupts (MSB=1)Exceptions (MSB=0)
0ReservedInstruction address misaligned
1Supervisor software (SSI)Instruction access fault
2ReservedIllegal instruction
3Machine software (MSI)Breakpoint (EBREAK, triggers)
4 / 5Reserved / Supervisor timer (STI)Load address misaligned / Load access fault
6 / 7Reserved / Machine timer (MTI)Store-AMO misaligned / Store-AMO access fault
8 / 9 / 11Reserved / Supervisor external (SEI) / Machine external (MEI)ECALL from U / S / M
12 / 13 / 1512 reserved / 13 counter-overflow (LCOFI)Instruction / load / store-AMO page fault
16 / 18 / 19≥16 platform useDouble trap / software check / hardware error
24-31, 48-63Designated for custom use
Dotted-underlined cells have explanations — click one.

Table 17: when several exceptions collide

Synchronous exception priority (decreasing)
Exception
1 (highest)Instruction address breakpoint (trigger on PC)
2Fetch page fault or access fault during address translation (first encountered)
3Instruction access fault (physical)
4Illegal instruction
5Instruction address misaligned
6Environment call (8/9/11), then environment break (3)
7Load/store address breakpoint (watchpoint)
8[Optionally] load/store misaligned — OR after translation faults
9Load/store page fault or access fault during translation
10 (lowest)Load/store access fault (physical), then misaligned if not taken earlier
Dotted-underlined cells have explanations — click one.

3.1.16 mtval

mtval carries the trap’s payload (or zero — the platform defines which exceptions must be informative):

  • Faulting virtual address for breakpoint/misaligned/access/page faults on fetch, load, or store — including the portion address for a misaligned or page-straddling access (mepc still points at the instruction start).
  • The faulting instruction bits (right-justified) on illegal-instruction — the emulation fast path.
  • Software-check code (2 = landing pad, 3 = shadow stack).
  • With paging on, even physical access faults report the virtual address — cheaper datapaths.

3.1.17–3.1.18 mconfigptr & menvcfg

mconfigptr (read-only, MXLEN-aligned) points at a platform configuration structure (format not yet standardized; zero legal). menvcfg (64-bit, exists iff U-mode) is where M-mode switches on features for the modes below:

menvcfg (0x30A)

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

Hardware Designer Notes

The mcause/mtval writeback muxes and the Table 17 priority encoder are one RTL block — generate them from a single exception-source table in your design database so RTL, DV, and documentation can’t drift. The custom-use code ranges (24-31, 48-63) are where your accelerator’s faults belong.

Minimal Linux-boot hart MUST

  • Report causes per Table 16 and resolve collisions per Table 17 — including the translate-before-misalign order if you support any misaligned accesses
  • Fill mtval per the platform contract you document (address faults: yes; illegal-instruction bits: strongly recommended)
  • Gate S-mode stimecmp on menvcfg.STCE and cache-block ops on CBZE/CBCFE/CBIE

MAY simplify / trap-and-emulate

  • Hardwire mtval to zero platform-wide (legal, hostile to software — avoid)
  • Leave every optional menvcfg field read-only zero until its extension lands

Check yourself — causes, priorities & mtval

1.How does a trap handler distinguish interrupts from exceptions in one instruction?

2.One instruction is simultaneously: at a misaligned target, an illegal encoding, and its fetch page-faults. What does mcause report?

3.On an illegal-instruction trap, what may mtval usefully contain?

4.Which menvcfg bit must be set for Linux's modern timer path (Sstc) to work in S-mode?

4 questions