3.1.19-5mseccfg, the Platform Timer, Privileged Instructions, Reset & NMI

Part II Linux boot: required Vol. II (Privileged) pp. 56–63 · ~4 min read

  • mtime/mtimecmp
  • wfi
  • reset state

The rest of the machine-level core: the security-configuration CSR, the timer that drives every OS tick, the four privileged instructions, and what reset actually guarantees.

3.1.19 mseccfg

Exists when any contributing extension does (else reserved). Per Figure 2 (one of the few priv figures that survived — transcribed from the text layout):

mseccfg (0x747)

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

3.2 mtime & mtimecmp: the heartbeat

Both are memory-mapped, both 64-bit on RV32 and RV64. mtime counts wall-clock ticks at a constant, platform-published frequency; MTIP is pending iff mtime ≥ mtimecmp (unsigned), cleared by writing mtimecmp. The time CSR is a read-only shadow. Two software contracts worth memorizing:

# RV32: glitch-free 64-bit comparand update (a1:a0 = new value)
li   t0, -1
sw   t0, 0(t1)     # low ← -1: comparand can't shrink transiently
sw   a1, 4(t1)     # high ← new
sw   a0, 0(t1)     # low ← new

and: MTIP reflection is eventual — a handler that bumps mtimecmp and returns immediately may take one spurious timer interrupt. Tolerate it.

3.3 The privileged instructions

SYSTEM-opcode privileged instructions
Semantics
ECALLRequested trap; cause 8/9/11 by originating mode; epc = the ECALL itself; does NOT retire (no instret count)
EBREAKBreakpoint (cause 3); same epc/retire rules; debuggers may intercept
MRET / SRETPop the x-stack (mstatus page), pc ← xepc. SRET needs S-mode (else illegal) and traps under mstatus.TSR. Executable only in mode ≥ x
WFIStall hint until an interrupt may need service. Wake ≠ take: resumes on a LOCALLY-enabled pending interrupt at any privilege, ignoring global enables AND mideleg; wake without trap → pc+4; a taken interrupt traps on the FOLLOWING instruction (xepc = pc+4)
Custom SYSTEM spaceA designated encoding subspace for vendor instructions; bits 29:28 should encode the minimum privilege
Dotted-underlined cells have explanations — click one.

3.4–3.5 Reset and NMI

Reset guarantees little — deliberately: mode = M; MIE = MPRV = 0; MBE = 0 (if LE supported); misa maximal; no LR reservation; pc = implementation-defined reset vector; mcause = reset cause (0 = most complete reset); PMP entries’ A/L = 0; mnstatus.NMIE = 0; mseccfg’s MML/MMWP/RLB/MLPE = 0; SEED fields defined; no WARL field illegal — and everything else UNSPECIFIED.

NMIs (hardware-error only) jump to their own implementation-defined M-mode vector regardless of every enable bit; mepc/mcause are written (Interrupt=1, code 0 = unknown source), state is not reset — an NMI can land mid-handler and overwrite mepc/mcause, which is exactly the hole the Smrnmi resumable-NMI extension (ch. II·8) plugs.

Hardware Designer Notes

This page completes the boot-critical M-mode core. The reset story for your Linux SoC: PC lands at the ROM vector in M-mode → firmware sizes PMP/delegation → programs mtimecmp for the first tick → MRET into S-mode Linux. The remaining p03 units cover the fabric those accesses traverse: PMAs and PMP.

Minimal Linux-boot hart MUST

  • Put mtime/mtimecmp on the platform bus in their own clock domain; drive MTIP from the ≥ comparison with eventual-reflection semantics
  • Implement WFI’s wake condition separately from the interrupt take condition (locals only, ignore globals/delegation)
  • Reset exactly the listed state; leave the rest genuinely unspecified in DV (random-init testing)
  • Give ECALL/EBREAK the no-retire, epc-points-at-self semantics

MAY simplify / trap-and-emulate

  • Implement WFI as a NOP (correct, powerless) or as clock-gating with any wake heuristic
  • Ship mseccfg entirely read-only-zero until Smepmp/Zkr land
  • Distinguish reset causes in mcause — or always report 0

Check yourself — timer, privileged instructions, reset

1.Why is mtime a memory-mapped register instead of a CSR?

2.The RV32 mtimecmp update sequence writes -1 to the low word first. Why?

3.WFI executes with ALL interrupts globally disabled (MIE=SIE=0) but MTIE set and a timer interrupt pending. What happens?

4.Which of these is guaranteed by reset, and which is NOT?

5.MRET executes. May it clear an outstanding LR reservation?

5 questions