Skip to content

trap: mstatus stack, WARL CSRs, misaligned-fetch and illegal-instruction faults, mstatus.FS - #487

Merged
tinebp merged 4 commits into
masterfrom
fix/trap-csr-mstatus
Nov 23, 2026
Merged

tinebp merged 4 commits into
masterfrom
fix/trap-csr-mstatus

Conversation

@tinebp

@tinebp tinebp commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Two commits complete the M-mode trap path. The design doc is updated (docs/designs/trap_and_exception_foundation.md).

1. mstatus stack, WARL CSRs and misa (fixes #447 and #448)

  • Trap entry sets MPIE←MIE and MIE←0, and MPP reads 11.
  • mret restores MIE←MPIE and sets MPIE←1.
  • mtvec and mepc are WARL. misa puts MXL in the top bits at XLEN 64.
  • SimX, the debug module and dtm_test.py match.
  • The rv32mi/rv64mi riscv-tests run, with a documented skip list.

2. Fault producers and mstatus.FS (fixes #445 and #446)

  • Misaligned fetch (cause 0): a taken jump or branch to a target with bit 1 set, when C is off. Before, it fetched the misaligned PC in debug builds and silently dropped bits in release builds.

  • Illegal instruction (cause 2), through a new INST_BR_ILLEGAL uop:

    • unknown opcodes, including extensions built out;
    • reserved F/D encodings;
    • shamt[5] on RV32;
    • writes to read-only CSRs;
    • reserved compressed encodings;
    • reserved branch funct3 and unknown SYSTEM encodings;
    • FP instructions or FP CSR accesses while FS is Off.

    WFI runs as a no-op.

  • FS: resets to Initial. FP writes set Dirty at decode, and SD follows. FS is read-only zero without F.

  • A CSR access to mstatus holds its warp until the CSR unit takes it, so the FS check and the Dirty update stay in program order.

  • Trap CSRs are device state: a launch keeps them on both sides. SimX used to clear them at every launch.

  • mtval is 0 for every cause, which the spec allows. Writing the target there would cost about 330 LUT of per-warp muxes.

Impact and evidence

Impact class: R3 (ci/change_impact.py classify). Maintainer approval needed.

Contracts touched:

  • VX_decode_sched_if: adds fs_off and fs_dirty.
  • VX_sched_csr_if: adds csr_unlock.
  • sched_csr_wr_t
  • INST_BR_ILLEGAL replaces INST_BR_OTHER.
  • RTL and SimX decoders, moved together.
  • VX_types.toml: VX_CSR_MSTATUS_* bit positions.
evidence status detail
functional tests (simx / rtlsim / xrt) run rv32mi/rv64mi (mcsr, scall, sbreak, illegal, shamt, ma_fetch) on SimX and rtlsim at XLEN 32 and 64, with C off and with A+C. New trap_illegal passes on SimX and rtlsim at XLEN 32 and 64; on master it fails every probe. RVC unit test passes at XLEN 32 and 64. multikernel, reopen and module_reload pass on SimX. sgemm, diverge and sgemm_tcu pass.
model_parity not run sgemm instruction and cycle counts are unchanged
perf_gate not run no mstatus access in the perf kernels; sgemm counts unchanged
Beacon logic_gate run core (A+C, CI toolchain): PASS. Depth 15, unchanged. +68 LUT, +5 FF vs master.
synthesis not run Beacon depth unchanged
XLEN 64 build run rtlsim -Wall lint at XLEN 32 and 64 (standard matrix, C+A, 2 ALU lanes) and at XLEN 32 with F off; SimX with -DSIMX_FUNCTIONAL

New CI cases:

  • isa-mi: machine-mode suites with C off, the only build where ma_fetch reaches the trap.

  • trap-illegal

  • No test silenced. The csr riscv-test stays skipped for the documented reason: tests 14-15 need misa.U cleared.

  • No golden data changed.

  • RTL and SimX move together.

  • No toolchain pin change.

🤖 Generated with Claude Code

tinebp and others added 2 commits November 19, 2026 03:42
… misa MXL at XLEN 64

The synchronous trap path (ECALL/EBREAK/MRET) redirected the PC and saved
mepc/mcause but never touched mstatus, and the trap CSRs were plain XLEN
latches with no legalisation:
- trap entry did not do MPIE<-MIE, MIE<-0, and MRET did not restore MIE
  from MPIE, so the interrupt-enable stack that preemption relies on was
  missing (#447);
- mepc kept bit 1 with C disabled (and bit 0 always), and mtvec kept any
  MODE value, though only direct mode exists (#448).

Each warp now stores only the defined bits: mstatus MIE, MPIE and FS (FS is
a plain field for now), with MPP reading 11 since M is the only mode;
mtvec [XLEN-1:2], so MODE reads 0; mepc [XLEN-1:1] with C and [XLEN-1:2]
without. Trap entry and MRET update the stack; hardware still wins a
same-cycle software write. SimX matches bit for bit. The bit positions
are shared through VX_types.toml (VX_CSR_MSTATUS_*). Beacon on the core:
logic depth unchanged, 496 fewer flops (31 per warp).

At XLEN 64 misa placed MXL at bits 31:30 instead of XLEN-1:XLEN-2, in the
RTL, SimX and the SimX debug module; it now uses the top bits. The DTM test
read only DATA0 for misa and never wrote DATA1, which hid a stale high word
in its register writes; it now handles both at XLEN 64.

SimX gaps that blocked the machine-mode suite are closed: misa returned 0,
and mvendorid/marchid/mimpid aborted; they now match the RTL.

The ISA suites gain rv32mi/rv64mi (mcsr, scall, sbreak). Tests that need
fault producers, user mode, triggers, counters or PMP are listed in
ISA_SKIP with the reason. Passing on SimX and rtlsim at XLEN 32 and 64,
with C on and off; rv32ui/rv32uf unchanged.

Fixes #447, fixes #448.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…cking

The trap path handled only ECALL/EBREAK. A taken jump or branch to a target
with bit 1 set (C off) did not trap: DEBUG builds fetched the misaligned PC
and release builds silently dropped the low bits (#445). mstatus.FS was not
tracked, so FP instructions ran with FS=Off and FS never became Dirty
(#446). Illegal encodings had no defined behaviour: undefined decode fields
in the RTL, an abort in SimX.

Misaligned fetch (cause 0): the branch unit takes target bit 1 from the
adder before PC truncation, so DEBUG and release behave the same; the
decision uses the last active lane, the lane the warp follows, and a
trapping jump writes no link register. mepc is the jump's PC (carried in
dest, as for ECALL); mtval is 0 for every cause, which the spec allows and
which avoids ~330 LUT of per-warp target muxes.

Illegal instruction (cause 2), a new INST_BR_ILLEGAL uop that stalls the
warp like ECALL: unknown major opcodes (including built-out extensions),
reserved F/D encodings, shamt[5] on RV32 and RV64 word shifts, writes to
read-only CSRs, reserved compressed encodings (the RTL decompressor now
expands them to the all-zero word; SimX marks the same set, and the rd=x0
HINT forms execute as no-ops on both), and FP instructions or FP-CSR
accesses under FS=Off, including the Vortex extensions that use FP
registers (packed/wide/matrix FP ld/st, TCU MMA, TFU, RTU GETWF/CB_RET),
conditional branches with the reserved funct3 2 or 3, and SYSTEM
encodings other than the CSR accesses, ECALL, EBREAK, URET, SRET, MRET
and WFI. WFI runs as a no-op (there is no interrupt to wait for: the RTL
decodes it as a jump to the next instruction), and no SYSTEM instruction
outside the CSR accesses writes rd.

FS: resets to Initial so kernels that never touch mstatus keep running,
and is read-only zero without F;
SD reads FS==Dirty; FP-state writers set Dirty at decode unless they trap.
An mstatus CSR access stalls its warp at decode until the CSR unit takes
it (csr_unlock on sched_csr_if), keeping the FS check and the Dirty update
in program order; SimX releases at the same point. misa writes are ignored
(WARL).

SimX applies trap, mret and PC-redirect effects only on an instruction's
last lane packet, as the RTL does; before, a split instruction raised a
trap once per packet and clobbered MPIE (also for ECALL/EBREAK/MRET). SimX
aborts with the cause, PC and instruction when an illegal or misaligned
fault is taken with mtvec=0 (a plain kernel has no handler); the RTL jumps
to mtvec as architected. The trap CSRs are device state on both sides: a
device reset clears them and a kernel launch keeps them (SimX used to
clear them at every launch, the RTL never did).

rv32mi/rv64mi now also run illegal, shamt and ma_fetch; csr stays skipped
(tests 14-15 need misa.U cleared or an unimplemented-CSR trap). CI runs
them on the default build with C off (isa-mi), the only build where
ma_fetch reaches the misaligned-fetch trap. A new regression, trap_illegal,
covers what no riscv-test that runs reaches: FP instructions, loads, CSR
accesses and moves under FS=Off, the reserved branch and SYSTEM encodings
and WFI (cause and mepc each), and FS going from Initial to Dirty with SD;
on the old code it fails every probe. The RVC unit test now expects the
rd=x0 HINTs to expand and checks the reserved points. Passing on
SimX and rtlsim at XLEN 32 and 64, default and A+C, rv32ui/uc with C on,
NUM_ALU_LANES=2 (rv32mi, sgemm, diverge), sgemm_tcu; sgemm instruction and
cycle counts unchanged. Beacon (core, A+C): depth unchanged (15, mean-50
11.6, 278 registers at 8+ levels), +68 LUT, +5 FF.

Fixes #445, fixes #446.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tinebp tinebp assigned Copilot and unassigned Copilot Nov 23, 2026
#481 moved mstatus and mtvec to one per core. This branch needs mstatus
per warp: trap entry and MRET stack MIE/MPIE per warp, and FS goes Dirty
at the decode of each warp's FP writes. The merge keeps:
- mtvec per core (#481), in the core scheduler, WARL with MODE reading 0;
- mstatus (MIE, MPIE, FS), mepc (WARL) and mcause (4 bits) per warp, on
  the warp schedulers' trap CSR write path (NUM_TRAP_CSRS = 3);
- mtval read-only zero.
The illegal-instruction trap uop now waits on XREG_2, as ECALL, EBREAK and
MRET do, so a warp's own mtvec write lands before it redirects; SimX
matches. A misaligned-target jump is an ordinary branch and does not wait.

Beacon core vs master: depth 15 -> 15, +111 LUT, +20 FF. isa-mi,
trap-illegal (XLEN 32 and 64) and parity-trap-{fw4,cs4,fw2-sfu4} pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tinebp
tinebp merged commit 279e65b into master Nov 23, 2026
3 of 6 checks passed
@tinebp
tinebp deleted the fix/trap-csr-mstatus branch November 23, 2026 23:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants