From 33284ca7e0e4a686c2a71532d861a1161cea36b5 Mon Sep 17 00:00:00 2001 From: tinebp Date: Wed, 18 Nov 2026 23:10:36 -0800 Subject: [PATCH 1/2] trap: mstatus MIE/MPIE stack on trap entry and mret; WARL mtvec/mepc; 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 --- VX_types.toml | 5 ++ ci/dtm_test.py | 15 +++- docs/designs/trap_and_exception_foundation.md | 88 ++++++++++++++----- hw/rtl/core/VX_csr_data.sv | 6 +- hw/rtl/core/VX_warp_sched.sv | 65 +++++++++++--- sim/simx/csr_unit.cpp | 14 +-- sim/simx/dtm/debug_module.cpp | 11 ++- sim/simx/scheduler.cpp | 10 ++- sim/simx/scheduler.h | 23 ++++- tests/riscv/common.mk | 9 +- tests/riscv/isa/Makefile | 47 ++++++++-- 11 files changed, 221 insertions(+), 72 deletions(-) diff --git a/VX_types.toml b/VX_types.toml index 4babbab5f2..dbc268d125 100644 --- a/VX_types.toml +++ b/VX_types.toml @@ -538,6 +538,11 @@ VX_CSR_SATP = 0x180 VX_CSR_PMPCFG0 = 0x3A0 VX_CSR_PMPADDR0 = 0x3B0 VX_CSR_MSTATUS = 0x300 +# mstatus field LSBs; MPP and FS are 2 bits wide +VX_CSR_MSTATUS_MIE = 3 +VX_CSR_MSTATUS_MPIE = 7 +VX_CSR_MSTATUS_MPP = 11 +VX_CSR_MSTATUS_FS = 13 VX_CSR_MISA = 0x301 VX_CSR_MEDELEG = 0x302 VX_CSR_MIDELEG = 0x303 diff --git a/ci/dtm_test.py b/ci/dtm_test.py index 269f463d5b..6ddec82105 100755 --- a/ci/dtm_test.py +++ b/ci/dtm_test.py @@ -40,6 +40,7 @@ # Debug Module register addresses (DMI space, 7-bit) DM_DATA0 = 0x04 +DM_DATA1 = 0x05 DM_DMCONTROL = 0x10 DM_DMSTATUS = 0x11 DM_COMMAND = 0x17 @@ -129,17 +130,23 @@ def dmi_write(sock, addr, value): def abstract_reg_read(sock, regaddr): - """Issue Access Register (cmdtype=0, transfer=1, read). Return DATA0.""" + """Issue Access Register (cmdtype=0, transfer=1, read). Return the + XLEN-bit value: DATA0, with DATA1 as the high word for XLEN=64.""" # aarsize=2 (32-bit) for XLEN=32, aarsize=3 (64-bit) for XLEN=64 aarsize = 2 if XLEN == 32 else 3 cmd = (0 << 24) | (aarsize << 20) | (1 << 17) | (regaddr & 0xFFFF) dmi_write(sock, DM_COMMAND, cmd) - return dmi_read(sock, DM_DATA0) + value = dmi_read(sock, DM_DATA0) + if XLEN == 64: + value |= dmi_read(sock, DM_DATA1) << 32 + return value def abstract_reg_write(sock, regaddr, value): aarsize = 2 if XLEN == 32 else 3 dmi_write(sock, DM_DATA0, value & 0xFFFFFFFF) + if XLEN == 64: + dmi_write(sock, DM_DATA1, (value >> 32) & 0xFFFFFFFF) cmd = (0 << 24) | (aarsize << 20) | (1 << 17) | (1 << 16) | (regaddr & 0xFFFF) dmi_write(sock, DM_COMMAND, cmd) @@ -195,11 +202,11 @@ def t_abstract_read_pc(sock): def t_abstract_read_misa(sock): misa = abstract_reg_read(sock, REG_MISA) - mxl = (misa >> 30) & 0x3 + mxl = (misa >> (XLEN - 2)) & 0x3 i_ext = (misa >> 8) & 1 expected_mxl = 1 if XLEN == 32 else 2 ok = mxl == expected_mxl and i_ext == 1 - return ok, f"MISA=0x{misa:08x} (MXL={mxl}, I={i_ext}, expected MXL={expected_mxl})" + return ok, f"MISA=0x{misa:0{XLEN // 4}x} (MXL={mxl}, I={i_ext}, expected MXL={expected_mxl})" def t_gpr_roundtrip(sock): diff --git a/docs/designs/trap_and_exception_foundation.md b/docs/designs/trap_and_exception_foundation.md index e53820d83a..5b6d92db9c 100644 --- a/docs/designs/trap_and_exception_foundation.md +++ b/docs/designs/trap_and_exception_foundation.md @@ -4,7 +4,7 @@ `MRET` with per-warp M-mode CSRs — that is the foundation for future preemption, plus the host-side ELF/HTIF plumbing that the riscv-tests suite rides on. Covers the RTL -([`hw/rtl/core/VX_scheduler.sv`](../../hw/rtl/core/VX_scheduler.sv), +([`hw/rtl/core/VX_warp_sched.sv`](../../hw/rtl/core/VX_warp_sched.sv), [`VX_alu_int.sv`](../../hw/rtl/core/VX_alu_int.sv), [`VX_csr_data.sv`](../../hw/rtl/core/VX_csr_data.sv)) and the SimX model ([`sim/simx/scheduler.cpp`](../../sim/simx/scheduler.cpp), @@ -22,34 +22,73 @@ suite rides on. Covers the RTL ## 1. Architecture -Each warp owns five M-mode trap CSRs held in the scheduler -([`VX_scheduler.sv:59`](../../hw/rtl/core/VX_scheduler.sv#L59)): -`mstatus`, `mtvec`, `mepc`, `mcause`, `mtval` -(`[NUM_WARPS][XLEN]`). Addresses: `MSTATUS=0x300`, `MTVEC=0x305`, -`MSCRATCH=0x340`, `MEPC=0x341`, `MCAUSE=0x342`, `MTVAL=0x343` -([`VX_types.toml:325-334`](../../VX_types.toml#L325)). +Each warp owns five M-mode trap CSRs held in its slice's warp scheduler +([`VX_warp_sched.sv`](../../hw/rtl/core/VX_warp_sched.sv)): +`mstatus`, `mtvec`, `mepc`, `mcause`, `mtval`. Each is stored in its legal +(WARL) form, keeping only the bits that can be nonzero (§1.1). Addresses: +`MSTATUS=0x300`, `MTVEC=0x305`, `MSCRATCH=0x340`, `MEPC=0x341`, +`MCAUSE=0x342`, `MTVAL=0x343`, with the `mstatus` field positions +(`VX_CSR_MSTATUS_{MIE,MPIE,MPP,FS}`) beside them in +[`VX_types.toml`](../../VX_types.toml). The branch unit is the trap producer -([`VX_alu_int.sv:310-339`](../../hw/rtl/core/VX_alu_int.sv#L310)): it +([`VX_alu_int.sv`](../../hw/rtl/core/VX_alu_int.sv)): it raises `is_trap_entry` / `is_mret_op` with a `br_trap_cause` (`EBREAK=3`, `ECALL=11`). These ride the existing `VX_branch_ctl_if` (the proposed standalone `VX_trap_if.sv` was folded in — functionally equivalent). The -scheduler redirects `warp_pcs_n`: a trap goes to `mtvec & ~3`, an `mret` -returns to `mepc` -([`VX_scheduler.sv:306-311`](../../hw/rtl/core/VX_scheduler.sv#L306)). -On trap entry the hardware snapshots `mepc`/`mcause`; a same-cycle software -`csrw` and a hardware trap entry are ordered hardware-wins -([`:394-412`](../../hw/rtl/core/VX_scheduler.sv#L394)). The CSR file does -only the read mux and routes writes back via `sched_csr_if` -([`VX_csr_data.sv:253-257`](../../hw/rtl/core/VX_csr_data.sv#L253)). +scheduler redirects the warp PC: a trap goes to `mtvec`, an `mret` +returns to `mepc`. On trap entry the hardware snapshots `mepc`/`mcause`, +clears `mtval` and updates `mstatus`; `mret` updates `mstatus` too (§1.1). +A same-cycle software `csrw` and a hardware trap entry or return are ordered +hardware-wins, per field: the fields the hardware writes take the hardware +value, and the software write still lands in the others. The CSR file +([`VX_csr_data.sv`](../../hw/rtl/core/VX_csr_data.sv)) does only the read +mux and routes writes back via `sched_csr_if`; the scheduler legalises +them. SimX mirrors this exactly: -[`scheduler.cpp:271-298`](../../sim/simx/scheduler.cpp#L271) defines -`raise_trap`, `mret`, `trigger_ecall(wid,pc)`, `trigger_ebreak(wid,pc)`; -[`alu_unit.cpp:363-366`](../../sim/simx/alu_unit.cpp#L363) dispatches -ecall/ebreak/mret per warp. The old "kill all warps" +[`scheduler.cpp`](../../sim/simx/scheduler.cpp) defines `raise_trap`, +`mret`, `trigger_ecall(wid,pc)`, `trigger_ebreak(wid,pc)`; +[`alu_unit.cpp`](../../sim/simx/alu_unit.cpp) dispatches ecall/ebreak/mret +per warp; [`csr_unit.cpp`](../../sim/simx/csr_unit.cpp) applies the same +write masks and read values as the RTL. The old "kill all warps" (`active_warps_.reset()`) hack is gone. +### 1.1 Legal values and the `mstatus` protocol + +Machine mode is the only privilege mode, there are no interrupts yet, and +`mtvec` supports Direct mode only. The trap CSRs therefore hold: + +| CSR | Stored | Reads | +|---|---|---| +| `mstatus` | `MIE` (3), `MPIE` (7), `FS` (14:13) | those fields, `MPP` (12:11) hardwired `11`; every other bit 0 (`SD` is not implemented) | +| `mtvec` | `[XLEN-1:2]` | `MODE` (1:0) always 0 (Direct) | +| `mepc` | `[XLEN-1:1]` with C, `[XLEN-1:2]` without | bit 0 always 0; bit 1 also 0 without C (IALIGN=32) | +| `mcause`, `mtval` | `[XLEN-1:0]` | as written | + +A write keeps only the stored bits; a CSRRS/CSRRC reads the legal value, +so it cannot set a hardwired bit. `FS` is a plain read/write field and +gates nothing yet. + +| Event | `MIE` | `MPIE` | `MPP` | +|---|---|---|---| +| trap entry (ECALL/EBREAK) | 0 | old `MIE` | `11` (M) | +| `mret` | old `MPIE` | 1 | `11` (M) | + +`MPP` stays `11` across `mret` because M is the only mode to return to. + +### 1.2 Conformance tests + +The upstream riscv-tests run unmodified through `tests/riscv/isa` +(`make run-{simx,rtlsim}-{32,64}mi` for the machine-mode suites; the +`run-*` aggregates of the matching XLEN include them). Of `rv32mi-p` and +`rv64mi-p`, `mcsr`, `scall` and `sbreak` run. The rest are skipped, with the reason beside the skip list in +[`tests/riscv/isa/Makefile`](../../tests/riscv/isa/Makefile): they need a +trap source other than ECALL/EBREAK (illegal instruction, misaligned +fetch/load/store, or `mstatus.FS`=0 trapping an FP store), or a CSR Vortex +does not model (Sdtrig triggers, the Zicntr user counters, writable +`minstret`/`mcountinhibit`, PMP). + --- ## 2. Host ELF / HTIF plumbing @@ -87,7 +126,9 @@ The named **preemption** capability is future work: cause, and a safe-point check so the CP can preempt a running grid. This is the actual preemption deliverable; nothing is built. 2. **Fault producers** — illegal-instruction / misaligned-access / page- - fault causes (2/4/6). + fault causes (2/4/6). Each one also unlocks the matching skipped + `rv32mi-p` tests (§1.2), and the illegal-instruction trap lets `FS` + gate the FP unit. 3. **Supervisor mode** — `stvec`/`sepc`/`scause` + `medeleg`-driven redirect. 4. **Interrupts** — `mie`/`mip`, async injection, `mcause` MSB. @@ -95,7 +136,10 @@ The named **preemption** capability is future work: context switch (only the trap CSRs are saved today). 6. **`tests/regression/csr_smoke/`** unit test — absent. 7. **Guardrails** (open risks): same-cycle `csrw`/trap conflict assertion, - `mtvec` MODE≠0 assertion, multi-warp `tohost` race. + multi-warp `tohost` race. (`mtvec` MODE≠0 can no longer happen: MODE is + hardwired to Direct.) +8. **`misa.U`** is still set (`VX_CFG_MISA_STD` bit 20) although U-mode does + not exist and `mstatus.MPP` is hardwired to M. **Superseded directions** (recorded to avoid revival): the standalone `VX_trap_if.sv` interface (folded into `VX_branch_ctl_if`); adding CSR diff --git a/hw/rtl/core/VX_csr_data.sv b/hw/rtl/core/VX_csr_data.sv index 0214f50e99..ad56ebb7d0 100644 --- a/hw/rtl/core/VX_csr_data.sv +++ b/hw/rtl/core/VX_csr_data.sv @@ -120,7 +120,7 @@ import VX_fpu_pkg::*; `VX_CSR_MVENDORID : stable_w = `VX_CFG_XLEN'(`VX_ISA_VENDOR_ID); `VX_CSR_MARCHID : stable_w = `VX_CFG_XLEN'(`VX_ISA_ARCH_ID); `VX_CSR_MIMPID : stable_w = `VX_CFG_XLEN'(`VX_ISA_IMPL_ID); - `VX_CSR_MISA : stable_w = `VX_CFG_XLEN'({2'(`CLOG2(`VX_CFG_XLEN/16)), 30'(`VX_CFG_MISA_STD)}); + `VX_CSR_MISA : stable_w = {2'(`CLOG2(`VX_CFG_XLEN/16)), (`VX_CFG_XLEN-2)'(`VX_CFG_MISA_STD)}; `ifdef VX_CFG_EXT_F_ENABLE `VX_CSR_FFLAGS : begin live_sel_w[LIVE_FFLAGS] = 1; wr_sel_w[WR_FFLAGS] = 1; end `VX_CSR_FRM : begin live_sel_w[LIVE_FRM] = 1; wr_sel_w[WR_FRM] = 1; end @@ -184,8 +184,8 @@ import VX_fpu_pkg::*; // Registered in the cycle before the request fires. The stable values // cannot change for this warp in that cycle: CSR writes land at an earlier // request's fire, and the scheduler's own writes (mscratch at launch or - // spawn, mepc/mcause at trap entry) target a warp with no CSR request in - // flight. The SIMULATION check below compares the two at fire. + // spawn, the trap CSRs at trap entry and return) target a warp with no CSR + // request in flight. The SIMULATION check below compares the two at fire. reg [`VX_CFG_XLEN-1:0] stable_r, wr_data_r; reg [NUM_LIVE-1:0] live_sel_r; reg [NUM_WR-1:0] wr_sel_r; diff --git a/hw/rtl/core/VX_warp_sched.sv b/hw/rtl/core/VX_warp_sched.sv index fca7e336b1..0f47ae507d 100644 --- a/hw/rtl/core/VX_warp_sched.sv +++ b/hw/rtl/core/VX_warp_sched.sv @@ -140,8 +140,34 @@ module VX_warp_sched import VX_gpu_pkg::*; #( wire cand_valid; wire sched_fire; + // M is the only privilege mode and mtvec supports direct mode only, so + // mstatus keeps MIE/MPIE/FS, and mtvec/mepc keep the bits that can be set. +`ifdef VX_CFG_EXT_C_ENABLE + localparam MEPC_LSB = 1; +`else + localparam MEPC_LSB = 2; +`endif + localparam MTVEC_LSB = 2; + + function automatic logic [`VX_CFG_XLEN-1:0] mstatus_value( + input logic mie, + input logic mpie, + input logic [1:0] fs + ); + mstatus_value = '0; + mstatus_value[`VX_CSR_MSTATUS_MIE] = mie; + mstatus_value[`VX_CSR_MSTATUS_MPIE] = mpie; + mstatus_value[`VX_CSR_MSTATUS_MPP +: 2] = 2'b11; + mstatus_value[`VX_CSR_MSTATUS_FS +: 2] = fs; + endfunction + reg [SW-1:0][`VX_CFG_MEM_ADDR_WIDTH-1:0] mscratch_r; - reg [SW-1:0][`VX_CFG_XLEN-1:0] mstatus_r, mtvec_r, mepc_r, mcause_r, mtval_r; + reg [SW-1:0] mstatus_mie_r, mstatus_mpie_r; + reg [SW-1:0][1:0] mstatus_fs_r; + reg [SW-1:0][`VX_CFG_XLEN-1:MTVEC_LSB] mtvec_r; + reg [SW-1:0][`VX_CFG_XLEN-1:MEPC_LSB] mepc_r; + reg [SW-1:0][`VX_CFG_XLEN-1:0] mcause_r, mtval_r; + wire [SW-1:0][`VX_CFG_XLEN-1:0] mstatus, mtvec, mepc; for (genvar l = 0; l < SW; ++l) begin : g_warps localparam [LW-1:0] LID = LW'(l); @@ -158,6 +184,10 @@ module VX_warp_sched import VX_gpu_pkg::*; #( wire br_hit = branch[QA].valid && (br_lid[QA] == LID); wire join_hit = join_valid[QS] && (join_k[QS] == K); + assign mstatus[l] = mstatus_value(mstatus_mie_r[l], mstatus_mpie_r[l], mstatus_fs_r[l]); + assign mtvec[l] = {mtvec_r[l], MTVEC_LSB'(0)}; + assign mepc[l] = {mepc_r[l], MEPC_LSB'(0)}; + always @(*) begin active_n = active_r; stalled_n = stalled_r; @@ -232,11 +262,10 @@ module VX_warp_sched import VX_gpu_pkg::*; #( if (br_hit) begin if (branch[QA].is_trap) begin - // ECALL/EBREAK: redirect to trap vector (mtvec[1:0] = MODE field; mask off to get base address). - pc_n = from_fullPC(mtvec_r[l] & ~`VX_CFG_XLEN'(3)); + pc_n = from_fullPC(mtvec[l]); end else if (branch[QA].is_mret) begin // MRET/SRET/URET: restore the saved PC from mepc. - pc_n = from_fullPC(mepc_r[l]); + pc_n = from_fullPC(mepc[l]); end else if (branch[QA].taken) begin pc_n = branch[QA].dest; end @@ -274,7 +303,9 @@ module VX_warp_sched import VX_gpu_pkg::*; #( tmask_r <= '0; pc_r <= '0; mscratch_r[l] <= '0; - mstatus_r[l] <= '0; + mstatus_mie_r[l] <= 1'b0; + mstatus_mpie_r[l] <= 1'b0; + mstatus_fs_r[l] <= '0; mtvec_r[l] <= '0; mepc_r[l] <= '0; mcause_r[l] <= '0; @@ -300,13 +331,15 @@ module VX_warp_sched import VX_gpu_pkg::*; #( if (csr_wr_lid[QS] == LID) begin if (csr_wr[QS].trap_valid[0]) begin - mstatus_r[l] <= csr_wr[QS].trap_data; + mstatus_mie_r[l] <= csr_wr[QS].trap_data[`VX_CSR_MSTATUS_MIE]; + mstatus_mpie_r[l] <= csr_wr[QS].trap_data[`VX_CSR_MSTATUS_MPIE]; + mstatus_fs_r[l] <= csr_wr[QS].trap_data[`VX_CSR_MSTATUS_FS +: 2]; end if (csr_wr[QS].trap_valid[1]) begin - mtvec_r[l] <= csr_wr[QS].trap_data; + mtvec_r[l] <= csr_wr[QS].trap_data[`VX_CFG_XLEN-1:MTVEC_LSB]; end if (csr_wr[QS].trap_valid[2]) begin - mepc_r[l] <= csr_wr[QS].trap_data; + mepc_r[l] <= csr_wr[QS].trap_data[`VX_CFG_XLEN-1:MEPC_LSB]; end if (csr_wr[QS].trap_valid[3]) begin mcause_r[l] <= csr_wr[QS].trap_data; @@ -316,12 +349,18 @@ module VX_warp_sched import VX_gpu_pkg::*; #( end end - // Hardware trap entry (ECALL/EBREAK) wins a same-cycle software write. + // Hardware trap entry (ECALL/EBREAK) and return win a same-cycle software write. if (br_hit && branch[QA].is_trap) begin - mepc_r[l] <= to_fullPC(branch[QA].dest); + mstatus_mie_r[l] <= 1'b0; + mstatus_mpie_r[l] <= mstatus_mie_r[l]; + mepc_r[l] <= (`VX_CFG_XLEN-MEPC_LSB)'(to_fullPC(branch[QA].dest) >> MEPC_LSB); mcause_r[l] <= `VX_CFG_XLEN'(branch[QA].trap_cause); mtval_r[l] <= '0; end + if (br_hit && branch[QA].is_mret) begin + mstatus_mie_r[l] <= mstatus_mpie_r[l]; + mstatus_mpie_r[l] <= 1'b1; + end end end @@ -425,9 +464,9 @@ module VX_warp_sched import VX_gpu_pkg::*; #( assign csr_rd[q].thread_mask = thread_masks[csr_rd_lid[q]]; assign csr_rd[q].mscratch = mscratch_r[csr_rd_lid[q]]; - assign csr_rd[q].mstatus = mstatus_r[csr_rd_lid[q]]; - assign csr_rd[q].mtvec = mtvec_r[csr_rd_lid[q]]; - assign csr_rd[q].mepc = mepc_r[csr_rd_lid[q]]; + assign csr_rd[q].mstatus = mstatus[csr_rd_lid[q]]; + assign csr_rd[q].mtvec = mtvec[csr_rd_lid[q]]; + assign csr_rd[q].mepc = mepc[csr_rd_lid[q]]; assign csr_rd[q].mcause = mcause_r[csr_rd_lid[q]]; assign csr_rd[q].mtval = mtval_r[csr_rd_lid[q]]; diff --git a/sim/simx/csr_unit.cpp b/sim/simx/csr_unit.cpp index 9291bf8a86..dbb878db60 100644 --- a/sim/simx/csr_unit.cpp +++ b/sim/simx/csr_unit.cpp @@ -50,9 +50,13 @@ Word CsrUnit::get_csr(uint32_t addr, uint32_t wid, uint32_t tid) { auto& warp = sched.warp(wid); auto core_perf = core_->perf_stats(); switch (addr) { + case VX_CSR_MVENDORID: return VX_ISA_VENDOR_ID; + case VX_CSR_MARCHID: return VX_ISA_ARCH_ID; + case VX_CSR_MIMPID: return VX_ISA_IMPL_ID; + case VX_CSR_MISA: + return (Word(log2floor(VX_CFG_XLEN) - 4) << (VX_CFG_XLEN - 2)) | VX_CFG_MISA_STD; case VX_CSR_PMPCFG0: case VX_CSR_PMPADDR0: - case VX_CSR_MISA: case VX_CSR_MEDELEG: case VX_CSR_MIDELEG: case VX_CSR_MIE: @@ -60,7 +64,7 @@ Word CsrUnit::get_csr(uint32_t addr, uint32_t wid, uint32_t tid) { return 0; // Per-warp machine-mode trap CSRs (see warp_t). - case VX_CSR_MSTATUS: return warp.mstatus; + case VX_CSR_MSTATUS: return warp.mstatus | MSTATUS_MPP_M; case VX_CSR_MTVEC: return warp.mtvec; case VX_CSR_MEPC: return warp.mepc; case VX_CSR_MCAUSE: return warp.mcause; @@ -361,13 +365,13 @@ void CsrUnit::set_csr(uint32_t addr, Word value, uint32_t wid, uint32_t tid) { break; // Per-warp machine-mode trap CSRs (see warp_t). case VX_CSR_MSTATUS: - warp.mstatus = value; + warp.mstatus = value & MSTATUS_WMASK; break; case VX_CSR_MTVEC: - warp.mtvec = value; + warp.mtvec = value & MTVEC_WMASK; break; case VX_CSR_MEPC: - warp.mepc = value; + warp.mepc = value & MEPC_WMASK; break; case VX_CSR_MCAUSE: warp.mcause = value; diff --git a/sim/simx/dtm/debug_module.cpp b/sim/simx/dtm/debug_module.cpp index d26722a0db..84edf9360c 100644 --- a/sim/simx/dtm/debug_module.cpp +++ b/sim/simx/dtm/debug_module.cpp @@ -816,17 +816,16 @@ vortex::Word DebugModule::read_register(uint16_t regaddr) dm_log("[DM] READ REG dpc (0x7B1) -> 0x%0*llx\n", (VX_CFG_XLEN == 64) ? 16 : 8, (unsigned long long)value); return value; } - // Helper function to read CSR by number - // Note: CSRs are always 32-bit per RISC-V spec, but we return Word for consistency + // Helper function to read CSR by number (CSRs are XLEN bits wide) auto read_csr = [this](uint16_t csr_num, uint16_t regaddr) -> vortex::Word { if (csr_num == 0x0301) { // Calculate MISA based on configured extensions - // MXL field (bits 31:30): 1=RV32, 2=RV64, 3=RV128 + // MXL field (bits XLEN-1:XLEN-2): 1=RV32, 2=RV64, 3=RV128 uint32_t mxl = (vortex::log2floor(VX_CFG_XLEN) - 4); - uint32_t value = (mxl << 30) | VX_CFG_MISA_STD; - dm_log("[DM] READ REG misa (0x%03x via 0x%04x) -> 0x%08x (RV%d, MXL=%d, VX_CFG_MISA_STD=0x%08x)\n", - csr_num, regaddr, value, VX_CFG_XLEN, mxl, VX_CFG_MISA_STD); + vortex::Word value = (vortex::Word(mxl) << (VX_CFG_XLEN - 2)) | VX_CFG_MISA_STD; + dm_log("[DM] READ REG misa (0x%03x via 0x%04x) -> 0x%0*llx (RV%d, MXL=%d, VX_CFG_MISA_STD=0x%08x)\n", + csr_num, regaddr, (VX_CFG_XLEN == 64) ? 16 : 8, (unsigned long long)value, VX_CFG_XLEN, mxl, VX_CFG_MISA_STD); return value; } diff --git a/sim/simx/scheduler.cpp b/sim/simx/scheduler.cpp index 5781fbd816..bfd3169841 100644 --- a/sim/simx/scheduler.cpp +++ b/sim/simx/scheduler.cpp @@ -363,18 +363,20 @@ namespace { void Scheduler::raise_trap(uint32_t wid, Word cause, Word trap_pc) { auto& warp = warps_.at(wid); - warp.mepc = trap_pc; + warp.mstatus = (warp.mstatus & ~(MSTATUS_MIE | MSTATUS_MPIE)) + | ((warp.mstatus & MSTATUS_MIE) ? MSTATUS_MPIE : 0); + warp.mepc = trap_pc & MEPC_WMASK; warp.mcause = cause; warp.mtval = 0; - // Redirect to the handler. Low 2 bits of mtvec are the MODE field; - // v1 supports direct mode only, so mask them off. - warp.PC = warp.mtvec & ~Word(3); + warp.PC = warp.mtvec; DT(3, core_->name() << " trap: wid=" << wid << ", cause=" << cause << ", mepc=0x" << std::hex << trap_pc << ", mtvec=0x" << warp.mtvec << std::dec); } void Scheduler::mret(uint32_t wid) { auto& warp = warps_.at(wid); + warp.mstatus = (warp.mstatus & ~MSTATUS_MIE) | MSTATUS_MPIE + | ((warp.mstatus & MSTATUS_MPIE) ? MSTATUS_MIE : 0); warp.PC = warp.mepc; DT(3, core_->name() << " mret: wid=" << wid << ", mepc=0x" << std::hex << warp.mepc << std::dec); diff --git a/sim/simx/scheduler.h b/sim/simx/scheduler.h index a594a660f7..f2c21b8a01 100644 --- a/sim/simx/scheduler.h +++ b/sim/simx/scheduler.h @@ -21,6 +21,7 @@ #include "instr.h" #include "cta_dispatcher.h" #include "barrier_unit.h" +#include "VX_types.h" #ifdef VX_CFG_EXT_RASTER_ENABLE #include #endif @@ -31,6 +32,21 @@ class Core; class Instr; class instr_trace_t; +// Legal values of the per-warp trap CSRs. M is the only privilege mode, so +// mstatus keeps MIE/MPIE/FS and MPP reads as M; mtvec supports direct mode +// only; mepc is aligned to the instruction alignment. +constexpr Word MSTATUS_MIE = Word(1) << VX_CSR_MSTATUS_MIE; +constexpr Word MSTATUS_MPIE = Word(1) << VX_CSR_MSTATUS_MPIE; +constexpr Word MSTATUS_MPP_M = Word(3) << VX_CSR_MSTATUS_MPP; +constexpr Word MSTATUS_FS = Word(3) << VX_CSR_MSTATUS_FS; +constexpr Word MSTATUS_WMASK = MSTATUS_MIE | MSTATUS_MPIE | MSTATUS_FS; +constexpr Word MTVEC_WMASK = ~Word(3); +#ifdef VX_CFG_EXT_C_ENABLE +constexpr Word MEPC_WMASK = ~Word(1); +#else +constexpr Word MEPC_WMASK = ~Word(3); +#endif + // IPDOM stack entry — one per nested SPLIT/JOIN. struct ipdom_entry_t { ThreadMask orig_tmask; @@ -85,9 +101,10 @@ struct warp_t { // Per-warp MSCRATCH (holds kernel arg pointer, set at CTA dispatch) Word mscratch; - // Per-warp machine-mode trap CSRs. Synchronous ECALL/EBREAK traps - // redirect the warp PC to mtvec and snapshot the faulting PC/cause - // here; MRET restores the PC from mepc. See Scheduler::raise_trap. + // Per-warp machine-mode trap CSRs, held masked to their writable bits. + // Synchronous ECALL/EBREAK traps redirect the warp PC to mtvec and + // snapshot the faulting PC/cause here; MRET restores the PC from mepc. + // See Scheduler::raise_trap. Word mstatus = 0; Word mtvec = 0; Word mepc = 0; diff --git a/tests/riscv/common.mk b/tests/riscv/common.mk index b3c3a9413d..c2b1a64448 100644 --- a/tests/riscv/common.mk +++ b/tests/riscv/common.mk @@ -41,9 +41,10 @@ BENCH_LIST := median qsort rsort towers memcpy multiply dhrystone spmv # (VX_MEM_IO_EXIT_CODE, VX_MEM_IO_COUT_*) via the patched crt.S + # syscalls.c, so the stamp must invalidate when those values change. # Listing $(ROOT_DIR)/sw/VX_types.h as a prerequisite forces a rebuild -# after a memory-map shift (e.g. a COUT region size change), and listing -# the patch re-applies an edited patch to a fresh clone. -$(RISCV_TESTS_STAMP): $(ROOT_DIR)/sw/VX_types.h $(VORTEX_HOME)/miscs/patches/riscv-benchmarks.patch +# after a memory-map shift (e.g. a COUT region size change), listing +# the patch re-applies an edited patch to a fresh clone, and listing this +# file rebuilds the cache when the set of built suites changes. +$(RISCV_TESTS_STAMP): $(ROOT_DIR)/sw/VX_types.h $(VORTEX_HOME)/miscs/patches/riscv-benchmarks.patch $(COMMON_MK_DIR)common.mk rm -rf $(RISCV_TESTS_DIR) git clone $(RISCV_TESTS_REPO) $(RISCV_TESTS_DIR) cd $(RISCV_TESTS_DIR) && git checkout --quiet $(RISCV_TESTS_COMMIT) && git submodule update --init --recursive @@ -57,7 +58,7 @@ $(RISCV_TESTS_STAMP): $(ROOT_DIR)/sw/VX_types.h $(VORTEX_HOME)/miscs/patches/ris $(RISCV_TESTS_DIR)/env/p/link.ld PATH=$(RISCV_TOOLCHAIN_PATH)/bin:$$PATH $(MAKE) -C $(ISA_DIR) \ XLEN=$(XLEN) RISCV_PREFIX=$(RISCV_PREFIX)- \ - rv$(XLEN)ui rv$(XLEN)um rv$(XLEN)uf rv$(XLEN)ud rv$(XLEN)ua rv$(XLEN)uc + rv$(XLEN)ui rv$(XLEN)um rv$(XLEN)uf rv$(XLEN)ud rv$(XLEN)ua rv$(XLEN)uc rv$(XLEN)mi ifeq ($(XLEN),64) # Even on XLEN=64, the debug suite's test_csv_trace32 (called # unconditionally) drives the simulators with the rv32* ISA ELFs. diff --git a/tests/riscv/isa/Makefile b/tests/riscv/isa/Makefile index 178ca2c8dd..dd0f6f968d 100644 --- a/tests/riscv/isa/Makefile +++ b/tests/riscv/isa/Makefile @@ -33,6 +33,8 @@ P_64F := rv64uf-p P_64D := P_64A := P_64C := +P_32MI := rv32mi-p +P_64MI := rv64mi-p ifneq ($(HAS_EXT_A),0) P_32A := rv32ua-p P_64A := rv64ua-p @@ -46,17 +48,34 @@ P_32D := rv32ud-p P_64D := rv64ud-p endif +# Known-unsupported tests, skipped by run_isa: fence_i (no instruction-cache +# coherence), ma_data (no misaligned-access trap), rv64uc-p-rvc (misaligned +# C.FLD the upstream test expects to trap on). +ISA_SKIP := *-fence_i|*-ma_data|rv64uc-p-rvc +# The machine-mode suites run mcsr, scall and sbreak. Only ECALL/EBREAK trap +# today, so the tests that need another trap source or an unmodelled CSR are +# skipped: +# illegal, shamt - illegal-instruction trap +# csr - mstatus.FS=0 must make FP stores trap +# ma_fetch, ma_addr, +# *-misaligned - misaligned fetch/load/store traps +# breakpoint - Sdtrig trigger CSRs (tselect, tdata1/2, tcontrol) +# zicntr - user counters cycle/instret (Zicntr) +# instret_overflow - writable minstret and mcountinhibit +# pmpaddr - PMP +ISA_SKIP := $(ISA_SKIP)|rv*mi-p-illegal|rv*mi-p-shamt|rv*mi-p-csr +ISA_SKIP := $(ISA_SKIP)|rv*mi-p-ma_fetch|rv*mi-p-ma_addr|rv*mi-p-*-misaligned +ISA_SKIP := $(ISA_SKIP)|rv*mi-p-breakpoint|rv*mi-p-zicntr|rv*mi-p-instret_overflow +ISA_SKIP := $(ISA_SKIP)|rv*mi-p-pmpaddr + # Run one or more test-suite prefixes against a simulator. # $(1) = simulator (simx | rtlsim) # $(2) = space-separated test prefixes (e.g. "rv32ui-p rv32um-p") -# Known-unsupported tests are skipped: fence_i (no instruction-cache -# coherence), ma_data (no misaligned-access trap), rv64uc-p-rvc -# (misaligned C.FLD the upstream test expects to trap on). define run_isa @set -e; for pfx in $(2); do for t in $(ISA_DIR)/$$pfx-*; do \ case "$$t" in *.dump) continue;; esac; [ -f "$$t" ] || continue; \ b=`basename $$t`; \ - case "$$b" in *-fence_i|*-ma_data|rv64uc-p-rvc) echo "[skip] $$b"; continue;; esac; \ + case "$$b" in $(ISA_SKIP)) echo "[skip] $$b"; continue;; esac; \ echo "[run] $$b"; $(SIM_DIR)/$(1)/$(1) "$$t" || { echo "[FAIL] $$b"; exit 1; }; \ done; done; echo "[$(1)] passed: $(2)" endef @@ -93,6 +112,9 @@ run-simx-32a: $(RISCV_TESTS_STAMP) run-simx-32c: $(RISCV_TESTS_STAMP) $(call run_isa,simx,$(P_32C)) +run-simx-32mi: $(RISCV_TESTS_STAMP) + $(call run_isa,simx,$(P_32MI)) + run-simx-64im: $(RISCV_TESTS_STAMP) $(call run_isa,simx,$(P_64I) $(P_64M)) @@ -108,9 +130,12 @@ run-simx-64a: $(RISCV_TESTS_STAMP) run-simx-64c: $(RISCV_TESTS_STAMP) $(call run_isa,simx,$(P_64C)) -run-simx-32: run-simx-32imafd +run-simx-64mi: $(RISCV_TESTS_STAMP) + $(call run_isa,simx,$(P_64MI)) + +run-simx-32: run-simx-32imafd run-simx-32mi -run-simx-64: run-simx-32imafd run-simx-64imafd +run-simx-64: run-simx-32imafd run-simx-64imafd run-simx-64mi run-simx: run-simx-$(XLEN) @@ -135,6 +160,9 @@ run-rtlsim-32a: $(RISCV_TESTS_STAMP) run-rtlsim-32c: $(RISCV_TESTS_STAMP) $(call run_isa,rtlsim,$(P_32C)) +run-rtlsim-32mi: $(RISCV_TESTS_STAMP) + $(call run_isa,rtlsim,$(P_32MI)) + run-rtlsim-64im: $(RISCV_TESTS_STAMP) $(call run_isa,rtlsim,$(P_64I) $(P_64M)) @@ -150,9 +178,12 @@ run-rtlsim-64a: $(RISCV_TESTS_STAMP) run-rtlsim-64c: $(RISCV_TESTS_STAMP) $(call run_isa,rtlsim,$(P_64C)) -run-rtlsim-32: run-rtlsim-32imf +run-rtlsim-64mi: $(RISCV_TESTS_STAMP) + $(call run_isa,rtlsim,$(P_64MI)) + +run-rtlsim-32: run-rtlsim-32imf run-rtlsim-32mi -run-rtlsim-64: run-rtlsim-32imf run-rtlsim-64imf +run-rtlsim-64: run-rtlsim-32imf run-rtlsim-64imf run-rtlsim-64mi run-rtlsim: run-rtlsim-$(XLEN) From c26906ee3ec5ff52b8e43201ad11fbe35209de7e Mon Sep 17 00:00:00 2001 From: tinebp Date: Thu, 19 Nov 2026 03:42:12 -0800 Subject: [PATCH 2/2] trap: misaligned-fetch and illegal-instruction faults; mstatus.FS tracking 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 --- ci/testcases/isa/riscv.yaml | 20 +++ docs/designs/trap_and_exception_foundation.md | 142 ++++++++++++++--- hw/rtl/VX_gpu_pkg.sv | 3 +- hw/rtl/VX_trace_pkg.sv | 1 + hw/rtl/core/VX_alu_int.sv | 59 +++++-- hw/rtl/core/VX_compute_slice.sv | 1 + hw/rtl/core/VX_csr_data.sv | 1 + hw/rtl/core/VX_csr_unit.sv | 3 + hw/rtl/core/VX_decode.sv | 137 ++++++++++++++-- hw/rtl/core/VX_decompressor.sv | 86 +++++++--- hw/rtl/core/VX_frontend_xbar.sv | 1 + hw/rtl/core/VX_warp_sched.sv | 25 ++- hw/rtl/interfaces/VX_branch_ctl_if.sv | 11 +- hw/rtl/interfaces/VX_decode_sched_if.sv | 12 +- hw/rtl/interfaces/VX_sched_csr_if.sv | 8 +- sim/simx/alu_unit.cpp | 93 +++++++---- sim/simx/core.cpp | 6 +- sim/simx/csr_unit.cpp | 5 +- sim/simx/decode.cpp | 147 ++++++++++++++++-- sim/simx/decode.h | 6 +- sim/simx/decompressor.cpp | 25 +-- sim/simx/dtm/debug_module.cpp | 1 - sim/simx/instr.h | 6 + sim/simx/scheduler.cpp | 46 ++++-- sim/simx/scheduler.h | 24 ++- sim/simx/sfu_unit.cpp | 6 + sim/simx/types.h | 2 + tests/regression/Makefile | 2 +- tests/regression/trap_illegal/Makefile | 14 ++ tests/regression/trap_illegal/common.h | 36 +++++ tests/regression/trap_illegal/kernel.cpp | 73 +++++++++ tests/regression/trap_illegal/main.cpp | 123 +++++++++++++++ tests/riscv/isa/Makefile | 16 +- tests/unittest/rvc_decompress/main.cpp | 22 ++- 34 files changed, 986 insertions(+), 177 deletions(-) create mode 100644 tests/regression/trap_illegal/Makefile create mode 100644 tests/regression/trap_illegal/common.h create mode 100644 tests/regression/trap_illegal/kernel.cpp create mode 100644 tests/regression/trap_illegal/main.cpp diff --git a/ci/testcases/isa/riscv.yaml b/ci/testcases/isa/riscv.yaml index b2f75e6b7b..d66b13329c 100644 --- a/ci/testcases/isa/riscv.yaml +++ b/ci/testcases/isa/riscv.yaml @@ -131,3 +131,23 @@ tests: - rtlsim dir: tests/riscv/isa target: run-{driver}-64f +# The machine-mode suites on the default config, with C off: only then can a +# branch target be misaligned, so this is the case that reaches the +# misaligned-fetch trap (ma_fetch); riscv-1/riscv-2 build with C on. +- id: isa-mi + via: make-run + drivers: + - simx + - rtlsim + dir: tests/riscv/isa + target: run-{driver}-{xlen}mi + configs: "" +# Illegal-instruction traps raised at decode: FP instructions with +# mstatus.FS=Off and reserved branch/SYSTEM encodings; an FP write sets FS +# to Dirty and SD. +- id: trap-illegal + via: blackbox + drivers: + - simx + - rtlsim + app: trap_illegal diff --git a/docs/designs/trap_and_exception_foundation.md b/docs/designs/trap_and_exception_foundation.md index 5b6d92db9c..ce95e30b65 100644 --- a/docs/designs/trap_and_exception_foundation.md +++ b/docs/designs/trap_and_exception_foundation.md @@ -33,12 +33,14 @@ Each warp owns five M-mode trap CSRs held in its slice's warp scheduler The branch unit is the trap producer ([`VX_alu_int.sv`](../../hw/rtl/core/VX_alu_int.sv)): it -raises `is_trap_entry` / `is_mret_op` with a `br_trap_cause` (`EBREAK=3`, -`ECALL=11`). These ride the existing `VX_branch_ctl_if` (the proposed -standalone `VX_trap_if.sv` was folded in — functionally equivalent). The +raises `is_trap_entry` / `is_mret_op` with a `br_trap_cause` (§1.3). +These ride the existing `VX_branch_ctl_if` (the proposed +standalone `VX_trap_if.sv` was folded in — functionally equivalent), which +carries the trapping instruction's PC in `dest`. The scheduler redirects the warp PC: a trap goes to `mtvec`, an `mret` -returns to `mepc`. On trap entry the hardware snapshots `mepc`/`mcause`, -clears `mtval` and updates `mstatus`; `mret` updates `mstatus` too (§1.1). +returns to `mepc`. On trap entry the hardware snapshots `mepc` (the +trapping instruction's PC) and `mcause`, clears `mtval` and updates `mstatus`; +`mret` updates `mstatus` too (§1.1). A same-cycle software `csrw` and a hardware trap entry or return are ordered hardware-wins, per field: the fields the hardware writes take the hardware value, and the software write still lands in the others. The CSR file @@ -48,10 +50,13 @@ them. SimX mirrors this exactly: [`scheduler.cpp`](../../sim/simx/scheduler.cpp) defines `raise_trap`, -`mret`, `trigger_ecall(wid,pc)`, `trigger_ebreak(wid,pc)`; -[`alu_unit.cpp`](../../sim/simx/alu_unit.cpp) dispatches ecall/ebreak/mret -per warp; [`csr_unit.cpp`](../../sim/simx/csr_unit.cpp) applies the same -write masks and read values as the RTL. The old "kill all warps" +`mret`, `trigger_ecall`, `trigger_ebreak`, `trigger_illegal` and +`trigger_misaligned_fetch`; +[`alu_unit.cpp`](../../sim/simx/alu_unit.cpp) dispatches the traps and +`mret` per warp; [`decode.cpp`](../../sim/simx/decode.cpp) produces the +same illegal-instruction uop as the RTL decoder; +[`csr_unit.cpp`](../../sim/simx/csr_unit.cpp) applies the same write masks +and read values as the RTL. The old "kill all warps" (`active_warps_.reset()`) hack is gone. ### 1.1 Legal values and the `mstatus` protocol @@ -61,33 +66,121 @@ Machine mode is the only privilege mode, there are no interrupts yet, and | CSR | Stored | Reads | |---|---|---| -| `mstatus` | `MIE` (3), `MPIE` (7), `FS` (14:13) | those fields, `MPP` (12:11) hardwired `11`; every other bit 0 (`SD` is not implemented) | +| `mstatus` | `MIE` (3), `MPIE` (7), `FS` (14:13) | those fields, `MPP` (12:11) hardwired `11`, `SD` (XLEN-1) = (`FS` == Dirty); every other bit 0 | | `mtvec` | `[XLEN-1:2]` | `MODE` (1:0) always 0 (Direct) | | `mepc` | `[XLEN-1:1]` with C, `[XLEN-1:2]` without | bit 0 always 0; bit 1 also 0 without C (IALIGN=32) | | `mcause`, `mtval` | `[XLEN-1:0]` | as written | A write keeps only the stored bits; a CSRRS/CSRRC reads the legal value, -so it cannot set a hardwired bit. `FS` is a plain read/write field and -gates nothing yet. +so it cannot set a hardwired bit. `misa` is read-only: a write is ignored. | Event | `MIE` | `MPIE` | `MPP` | |---|---|---|---| -| trap entry (ECALL/EBREAK) | 0 | old `MIE` | `11` (M) | +| trap entry | 0 | old `MIE` | `11` (M) | | `mret` | old `MPIE` | 1 | `11` (M) | `MPP` stays `11` across `mret` because M is the only mode to return to. +**`FS`** gates the F/D state (the FP registers and `fflags`/`frm`/`fcsr`): + +- It resets to Initial (`01`) with F enabled; without F it is read-only + `00`. Kernels never write `mstatus`, so a reset value of Off would trap + every FP kernel; Initial keeps FP usable and still shows the first write + as the move to Dirty, so `SD` reads 0 until the warp writes FP state. +- Like `mtvec`, it resets only with the core, not at a kernel or CTA + launch: a kernel that turns FP off must turn it back on before it ends, + or the next kernel on those warps traps on its first FP instruction. +- With `FS`=Off, an instruction that names an FP register or FP CSR raises + an illegal-instruction trap (§1.3): the F/D instructions (FP + loads/stores, `OP-FP`, the fused multiply-adds), any CSR access to + `fflags`/`frm`/`fcsr`, and the Vortex extensions on FP registers (the + packed, wide and matrix FP loads and stores, the TCU MMA, TFU, and the + RTU `GETWF`/`CB_RET`). The RTU trace/wait ops, which use the FP window + by convention rather than by encoding, are not gated. +- An instruction that writes an FP register (Vortex extensions included, + e.g. a TCU MMA or a packed FP load), or an F/D instruction or FP CSR + write that writes `fflags`/`frm`, sets `FS` to Dirty. It is set at + decode, conservatively (an FP op that raises no flag still dirties). +- The check and the Dirty update are both made at decode, so they must + see `mstatus` in program order: any CSR access to `mstatus` stalls its + warp at decode and releases it as the CSR request fires + (`sched_csr_if.csr_unlock` in the RTL, a warp-control release in SimX). + ### 1.2 Conformance tests The upstream riscv-tests run unmodified through `tests/riscv/isa` (`make run-{simx,rtlsim}-{32,64}mi` for the machine-mode suites; the `run-*` aggregates of the matching XLEN include them). Of `rv32mi-p` and -`rv64mi-p`, `mcsr`, `scall` and `sbreak` run. The rest are skipped, with the reason beside the skip list in -[`tests/riscv/isa/Makefile`](../../tests/riscv/isa/Makefile): they need a -trap source other than ECALL/EBREAK (illegal instruction, misaligned -fetch/load/store, or `mstatus.FS`=0 trapping an FP store), or a CSR Vortex -does not model (Sdtrig triggers, the Zicntr user counters, writable -`minstret`/`mcountinhibit`, PMP). +`rv64mi-p`, `mcsr`, `scall`, `sbreak`, `illegal`, `shamt` (RV32 only) and +`ma_fetch` run. The rest are skipped, with the reason beside the skip list +in [`tests/riscv/isa/Makefile`](../../tests/riscv/isa/Makefile): they need a +misaligned load/store trap, `misa.U` cleared or an unimplemented-CSR trap +(`csr`, tests 14-15), or a CSR Vortex does not model (Sdtrig triggers, the +Zicntr user counters, writable `minstret`/`mcountinhibit`, PMP). + +CI runs the machine-mode suites on the default build, with C off (`isa-mi` +in `ci/testcases/isa/riscv.yaml`): only there can a branch target be +misaligned, so only there does `ma_fetch` reach the trap. +`tests/regression/trap_illegal` covers the `FS` gating and Dirty tracking +(§1.1), which no riscv-test that runs reaches, and the reserved branch and +`SYSTEM` encodings of §1.3. + +### 1.3 Trap sources + +| Cause | Producer | `mepc` | `mtval` | +|---|---|---|---| +| 0 instruction address misaligned | branch unit | the jump/branch | 0 | +| 2 illegal instruction | decoder (trap uop) | the instruction | 0 | +| 3 breakpoint (`EBREAK`) | branch unit | the instruction | 0 | +| 11 `ECALL` from M | branch unit | the instruction | 0 | + +**Misaligned fetch.** With C disabled (IALIGN=32), a JAL, a JALR or a +*taken* conditional branch whose target has bit 1 set traps instead of +jumping; JALR clears bit 0 first, as the ISA says, so only bit 1 counts, +and a not-taken branch never traps. With C enabled nothing is misaligned. +The ALU computes the target bit before the PC is truncated to `PC_BITS`, +so the DEBUG (`PC_BITS`=XLEN) and release builds behave the same. A JAL or +JALR that traps writes no lane of `rd`. A warp takes one PC, its last +active lane's target (lane-divergent JALR targets are not otherwise +supported); the trap follows the same lane, so a warp traps exactly when +the target it would jump to is misaligned, and the trap is per-warp like +every other. + +`mtval` is 0 for every cause, which the privileged spec allows; writing the +target or the instruction bits would cost a second data source into every +warp's `mtval`. Only `dest`, the trapping PC, travels with the trap. + +The RTL jumps to `mtvec` as architected even when no handler is installed +(`mtvec`=0, as in every plain Vortex kernel); SimX instead aborts on an +illegal-instruction or misaligned-fetch trap with `mtvec`=0, reporting the +cause, PC and instruction bits. + +**Illegal instruction.** The decoder turns the instruction into a branch +uop (`INST_BR_ILLEGAL`) that reads and writes no register and stalls its +warp like `ECALL`; the branch unit raises cause 2. It covers: + +- an unknown major opcode, including the opcodes of an extension the core + is built without (F, A, the wide/matrix loads and stores, TFU); +- a reserved F/D encoding (an unsupported format, a reserved `funct5`, a + reserved `funct3` of `FCLASS`/`FMV`); +- a shift-immediate with `shamt[5]` set on RV32, or on an RV64 word shift; +- a conditional branch with the reserved `funct3` 2 or 3; +- a `SYSTEM` instruction other than a CSR access, `ECALL`, `EBREAK`, + `URET`, `SRET`, `MRET` and `WFI` (`WFI` runs as a no-op: there is no + interrupt to wait for); +- a CSR write to a read-only CSR (address `0xC00`-`0xFFF`); +- with C, a reserved compressed encoding (including `0x0000`), which the + decompressor expands to the all-zero word; the `rd`=x0 forms the RVC + spec calls HINTs execute as no-ops; +- an FP instruction while `FS`=Off (§1.1). + +It does not cover an unknown sub-opcode inside the Vortex custom opcodes +(still decode-time assertions where checked), or an access to an +unimplemented CSR, which would need the CSR address map at decode. + +With more threads than ALU lanes, an instruction runs as several lane +packets; each packet writes only its lanes, and the last one alone takes +the redirect, trap or `mret`. --- @@ -125,10 +218,10 @@ The named **preemption** capability is future work: 1. **Async preempt-from-CP** — a `preempt_pending` port, a custom trap cause, and a safe-point check so the CP can preempt a running grid. This is the actual preemption deliverable; nothing is built. -2. **Fault producers** — illegal-instruction / misaligned-access / page- - fault causes (2/4/6). Each one also unlocks the matching skipped - `rv32mi-p` tests (§1.2), and the illegal-instruction trap lets `FS` - gate the FP unit. +2. **Fault producers** — misaligned load/store and page-fault causes + (4/6, 13/15), each unlocking the matching skipped `rv32mi-p` tests + (§1.2). The misaligned-fetch and illegal-instruction producers exist + (§1.3). 3. **Supervisor mode** — `stvec`/`sepc`/`scause` + `medeleg`-driven redirect. 4. **Interrupts** — `mie`/`mip`, async injection, `mcause` MSB. @@ -139,7 +232,8 @@ The named **preemption** capability is future work: multi-warp `tohost` race. (`mtvec` MODE≠0 can no longer happen: MODE is hardwired to Direct.) 8. **`misa.U`** is still set (`VX_CFG_MISA_STD` bit 20) although U-mode does - not exist and `mstatus.MPP` is hardwired to M. + not exist and `mstatus.MPP` is hardwired to M. Clearing it also lets the + `csr` riscv-test run. **Superseded directions** (recorded to avoid revival): the standalone `VX_trap_if.sv` interface (folded into `VX_branch_ctl_if`); adding CSR diff --git a/hw/rtl/VX_gpu_pkg.sv b/hw/rtl/VX_gpu_pkg.sv index 86b3976a16..e30c9740e9 100644 --- a/hw/rtl/VX_gpu_pkg.sv +++ b/hw/rtl/VX_gpu_pkg.sv @@ -464,7 +464,7 @@ package VX_gpu_pkg; localparam INST_BR_URET = 4'b1100; localparam INST_BR_SRET = 4'b1101; localparam INST_BR_MRET = 4'b1110; - localparam INST_BR_OTHER = 4'b1111; + localparam INST_BR_ILLEGAL = 4'b1111; localparam INST_BR_BITS = 4; function automatic logic [1:0] inst_br_class(input logic [INST_BR_BITS-1:0] op); @@ -1389,6 +1389,7 @@ package VX_gpu_pkg; logic [`VX_CFG_MEM_ADDR_WIDTH-1:0] data; logic [NUM_TRAP_CSRS-1:0] trap_valid; logic [`VX_CFG_XLEN-1:0] trap_data; + logic unlock; } sched_csr_wr_t; // SFU CSR reads of per-warp state, selected by the read wid and CTA id diff --git a/hw/rtl/VX_trace_pkg.sv b/hw/rtl/VX_trace_pkg.sv index 48d83eda99..48626a3bf5 100644 --- a/hw/rtl/VX_trace_pkg.sv +++ b/hw/rtl/VX_trace_pkg.sv @@ -141,6 +141,7 @@ package VX_trace_pkg; INST_BR_URET: `TRACE(level, ("URET")) INST_BR_SRET: `TRACE(level, ("SRET")) INST_BR_MRET: `TRACE(level, ("MRET")) + INST_BR_ILLEGAL:`TRACE(level, ("ILLEGAL")) default: `TRACE(level, ("?")) endcase end diff --git a/hw/rtl/core/VX_alu_int.sv b/hw/rtl/core/VX_alu_int.sv index e6c05158a6..326db7a895 100644 --- a/hw/rtl/core/VX_alu_int.sv +++ b/hw/rtl/core/VX_alu_int.sv @@ -292,6 +292,16 @@ module VX_alu_int import VX_gpu_pkg::*; #( assign last_tid = 0; end + // Without C, a jump or branch target with bit 1 set is misaligned (JALR + // clears bit 0 first); the warp follows its last active lane's target. A + // jump to it traps without writing any lane. +`ifdef VX_CFG_EXT_C_ENABLE + wire br_misaligned = 1'b0; +`else + wire is_jmp_cbr_op = ~br_op[3] || (br_op[2:1] == 2'b00); + wire br_misaligned = is_br_op && is_jmp_cbr_op && add_result[last_tid][1]; +`endif + // For WGATHER, suppress write-back for every source lane (one per group of 4). // Lane k is a source if its lower 2 bits equal wg_src_offset. wire [NUM_LANES-1:0] wg_src_mask; @@ -310,6 +320,9 @@ module VX_alu_int import VX_gpu_pkg::*; #( // Full warps are unchanged (header.tmask is all-ones there). alu_hdr_in.tmask = ~wg_src_mask; end + if (br_misaligned && inst_br_is_static(br_op)) begin + alu_hdr_in.tmask = '0; + end end `ifdef VX_CFG_EXT_C_ENABLE @@ -317,17 +330,18 @@ module VX_alu_int import VX_gpu_pkg::*; #( wire is_rvc_in = execute_if.data.op_args.br.is_rvc; wire is_rvc_r; `endif + wire br_misaligned_r; VX_elastic_buffer #( - .DATAW ($bits(alu_header_t) + (NUM_LANES * `VX_CFG_XLEN) + PC_BITS + 1 + INST_BR_BITS + LANE_WIDTH + `VX_CFG_EXT_C_ENABLED) + .DATAW ($bits(alu_header_t) + (NUM_LANES * `VX_CFG_XLEN) + PC_BITS + 1 + INST_BR_BITS + LANE_WIDTH + 1 + `VX_CFG_EXT_C_ENABLED) ) rsp_buf ( .clk (clk), .reset (reset), .valid_in (execute_if.valid), .ready_in (execute_if.ready), - .data_in ({alu_hdr_in, alu_result, cbr_dest, is_br_op, br_op, last_tid + .data_in ({alu_hdr_in, alu_result, cbr_dest, is_br_op, br_op, last_tid, br_misaligned `ifdef VX_CFG_EXT_C_ENABLE , is_rvc_in `endif }), - .data_out ({result_if.data.header, alu_result_r, cbr_dest_r, is_br_op_r, br_op_r, last_tid_r + .data_out ({result_if.data.header, alu_result_r, cbr_dest_r, is_br_op_r, br_op_r, last_tid_r, br_misaligned_r `ifdef VX_CFG_EXT_C_ENABLE , is_rvc_r `endif }), .valid_out (result_if.valid), .ready_out (result_if.ready) @@ -338,13 +352,6 @@ module VX_alu_int import VX_gpu_pkg::*; #( wire is_br_less = inst_br_is_less(br_op_r); wire is_br_static = inst_br_is_static(br_op_r); - // Synchronous trap ops: ECALL/EBREAK enter a trap (redirect to mtvec), - // MRET/SRET/URET return (restore PC from mepc). The scheduler acts on - // these via branch_ctl_if.{is_trap,is_mret,trap_cause}. - wire is_trap_entry = is_br_op_r && (br_op_r == INST_BR_ECALL || br_op_r == INST_BR_EBREAK); - wire is_mret_op = is_br_op_r && (br_op_r == INST_BR_MRET || br_op_r == INST_BR_SRET || br_op_r == INST_BR_URET); - wire [3:0] br_trap_cause = (br_op_r == INST_BR_EBREAK) ? 4'd3 : 4'd11; // EBREAK / ECALL_M - wire [`VX_CFG_XLEN-1:0] br_result = alu_result_r[last_tid_r]; wire is_less = br_result[0]; wire is_equal = br_result[1]; @@ -352,10 +359,32 @@ module VX_alu_int import VX_gpu_pkg::*; #( wire result_fire = result_if.valid && result_if.ready; wire br_enable = result_fire && is_br_op_r && result_if.data.header.eop; wire br_taken = ((is_br_less ? is_less : is_equal) ^ is_br_neg) | is_br_static; - // For a trap entry, `dest` carries the faulting PC (saved into mepc). - wire [PC_BITS-1:0] br_dest = is_trap_entry ? result_if.data.header.PC - : is_br_static ? from_fullPC(br_result) + + // Synchronous traps: ECALL/EBREAK, an illegal instruction and a taken + // transfer to a misaligned target enter a trap (redirect to mtvec); + // MRET/SRET/URET return (restore PC from mepc). The scheduler acts on + // these via branch_ctl_if.{is_trap,is_mret,trap_cause}. + wire is_trap_op = is_br_op_r && (br_op_r == INST_BR_ECALL || br_op_r == INST_BR_EBREAK || br_op_r == INST_BR_ILLEGAL); + wire is_misaligned = br_misaligned_r && br_taken; + wire is_trap_entry = is_trap_op || is_misaligned; + // A trap's `dest` is its own PC (saved into mepc). The select does not wait + // for the compare: a not-taken branch with a misaligned target ignores dest. + wire [PC_BITS-1:0] br_dest = (is_trap_op || br_misaligned_r) ? result_if.data.header.PC + : is_br_static ? from_fullPC({br_result[`VX_CFG_XLEN-1:1], 1'b0}) : cbr_dest_r; + wire is_mret_op = is_br_op_r && (br_op_r == INST_BR_MRET || br_op_r == INST_BR_SRET || br_op_r == INST_BR_URET); + reg [3:0] br_trap_cause; + always @(*) begin + if (is_misaligned) begin + br_trap_cause = 4'd0; // instruction address misaligned + end else begin + case (br_op_r) + INST_BR_EBREAK: br_trap_cause = 4'd3; // breakpoint + INST_BR_ILLEGAL: br_trap_cause = 4'd2; // illegal instruction + default: br_trap_cause = 4'd11; // ECALL from M-mode + endcase + end + end wire [NW_WIDTH-1:0] br_wid; `ASSIGN_BLOCKED_WID (br_wid, result_if.data.header.wid, block_idx, `VX_CFG_NUM_ALU_BLOCKS) if (`VX_CFG_NUM_ALU_BLOCKS == 1) begin : g_block_idx_unused @@ -389,8 +418,8 @@ module VX_alu_int import VX_gpu_pkg::*; #( `ifdef DBG_TRACE_PIPELINE always @(posedge clk) begin if (br_enable) begin - `TRACE(2, ("%t: %s branch: wid=%0d, cta_id=%0d, PC=0x%0h, taken=%b, dest=0x%0h (#%0d)\n", - $time, INSTANCE_ID, br_wid, result_if.data.header.cta_id, to_fullPC(result_if.data.header.PC), br_taken, to_fullPC(br_dest), result_if.data.header.uuid)) + `TRACE(2, ("%t: %s branch: wid=%0d, cta_id=%0d, PC=0x%0h, taken=%b, dest=0x%0h, trap=%b, cause=%0d (#%0d)\n", + $time, INSTANCE_ID, br_wid, result_if.data.header.cta_id, to_fullPC(result_if.data.header.PC), br_taken, to_fullPC(br_dest), is_trap_entry, br_trap_cause, result_if.data.header.uuid)) end end `endif diff --git a/hw/rtl/core/VX_compute_slice.sv b/hw/rtl/core/VX_compute_slice.sv index 4d8c0b6619..e1959cfb63 100644 --- a/hw/rtl/core/VX_compute_slice.sv +++ b/hw/rtl/core/VX_compute_slice.sv @@ -249,6 +249,7 @@ import VX_tcu_pkg::*; assign csr_wr[0].data = sched_csr_if.csr_wr_data; assign csr_wr[0].trap_valid = sched_csr_if.trap_csr_wr_valid; assign csr_wr[0].trap_data = sched_csr_if.trap_csr_wr_data; + assign csr_wr[0].unlock = sched_csr_if.csr_unlock; assign sched_csr_if.cycles = cycles; assign sched_csr_if.instret = instret; diff --git a/hw/rtl/core/VX_csr_data.sv b/hw/rtl/core/VX_csr_data.sv index ad56ebb7d0..076123a2a2 100644 --- a/hw/rtl/core/VX_csr_data.sv +++ b/hw/rtl/core/VX_csr_data.sv @@ -311,6 +311,7 @@ import VX_fpu_pkg::*; `VX_CSR_FCSR, `endif `VX_CSR_SATP, + `VX_CSR_MISA, `VX_CSR_MSTATUS, `VX_CSR_MNSTATUS, `VX_CSR_MEDELEG, diff --git a/hw/rtl/core/VX_csr_unit.sv b/hw/rtl/core/VX_csr_unit.sv index 139967ec63..2893737aea 100644 --- a/hw/rtl/core/VX_csr_unit.sv +++ b/hw/rtl/core/VX_csr_unit.sv @@ -126,6 +126,9 @@ module VX_csr_unit import VX_gpu_pkg::*; #( assign fcsr_wid = execute_if.data.header.wid; `endif + // An mstatus access holds its warp from decode until it has fired. + assign sched_csr_if.csr_unlock = csr_req_fire && execute_if.data.header.eop && (csr_addr == `VX_CSR_MSTATUS); + // Per-lane CSRs // Thread ids are the lane index on top of a per-request base. diff --git a/hw/rtl/core/VX_decode.sv b/hw/rtl/core/VX_decode.sv index 35698a128a..0938be1afb 100644 --- a/hw/rtl/core/VX_decode.sv +++ b/hw/rtl/core/VX_decode.sv @@ -46,6 +46,9 @@ module VX_decode import reg [NUM_XREGS-1:0] wr_xregs; reg [BYTESEL_BITS-1:0] bytesel; reg is_wstall; + reg illegal; + reg use_rd, fs_write_fcsr; + reg trap_uop; wire [31:0] instr = fetch_if.data.instr; wire [6:0] opcode = instr[6:0]; @@ -139,6 +142,58 @@ module VX_decode import endcase end + // mstatus.FS of the instruction's warp + wire fs_off_w; + if (FETCH_WFS_BITS != 0) begin : g_fs_off + assign fs_off_w = decode_sched_if.fs_off[fetch_if.data.wid[FETCH_WFS_W-1:0]]; + end else begin : g_fs_off_1 + assign fs_off_w = decode_sched_if.fs_off[0]; + end +`ifdef VX_CFG_EXT_F_ENABLE + // The instructions that name an FP register or FP CSR, which FS=Off makes + // illegal: the F/D instructions, the FP CSR accesses, and the Vortex + // extensions on FP registers. + reg is_fp_inst; + always @(*) begin + case (opcode) + INST_FL, INST_FS, INST_FMADD, INST_FMSUB, INST_FNMSUB, INST_FNMADD, INST_FCI: + is_fp_inst = 1'b1; + INST_SYS: + is_fp_inst = (funct3[1:0] != 0) && (is_csr_fflags || is_csr_frm || is_csr_fcsr); + INST_EXT1: begin + // packed FP loads + is_fp_inst = (funct7 == 7'h04) && ((funct3 == 3'h1) || (funct3 == 3'h2)); + `ifdef VX_CFG_EXT_TCU_ENABLE + // MMA; its metadata load names no FP register + `ifdef TCU_META_ENABLE + is_fp_inst |= (funct7 == 7'h02) && (funct3 != 3'h2); + `else + is_fp_inst |= (funct7 == 7'h02); + `endif + `endif + end + `ifdef VX_CFG_EXT_RTU_ENABLE + INST_EXT2: + // GETWF and CB_RET + is_fp_inst = (funct3 == 3'h6) && ((funct2 == 2'd0) || (funct2 == 2'd2)); + `endif + `ifdef VX_CFG_EXT_TFU_ENABLE + INST_EXT3: + is_fp_inst = 1'b1; + `endif + `ifdef EXT_LDST_ANY_ENABLE + INST_EXT4: // wide and matrix FP loads and stores + is_fp_inst = 1'b1; + `endif + default: + is_fp_inst = 1'b0; + endcase + end +`else + wire is_fp_inst = 1'b0; + `UNUSED_VAR (fs_off_w) +`endif + wire [19:0] ui_imm = instr[31:12]; `ifdef VX_CFG_XLEN_64 wire [11:0] i_imm = is_itype_sh ? {6'b0, instr[25:20]} : u_12; @@ -178,14 +233,21 @@ module VX_decode import end reg [INST_BR_BITS-1:0] s_type; + reg s_legal; always @(*) begin + s_legal = 1; case (u_12) 12'h000: s_type = INST_OP_BITS'(INST_BR_ECALL); 12'h001: s_type = INST_OP_BITS'(INST_BR_EBREAK); 12'h002: s_type = INST_OP_BITS'(INST_BR_URET); 12'h102: s_type = INST_OP_BITS'(INST_BR_SRET); 12'h302: s_type = INST_OP_BITS'(INST_BR_MRET); - default: s_type = 'x; + // WFI: no interrupt to wait for, so a jump to the next instruction + 12'h105: s_type = INST_OP_BITS'(INST_BR_JAL); + default: begin + s_type = 'x; + s_legal = 0; + end endcase end @@ -252,6 +314,7 @@ module VX_decode import bytesel = BYTESEL_DEFAULT; is_wstall = 0; fence_op = 'x; + illegal = 0; `ifdef VX_CFG_EXT_A_ENABLE // Default the LSU amo sideband to "not an AMO" so plain @@ -279,6 +342,10 @@ module VX_decode import op_args.alu.imm20 = `SEXT(20, i_imm); `USED_IREG (rd); `USED_IREG (rs1); + `ifndef VX_CFG_XLEN_64 + // shamt[5] is reserved on RV32 + illegal = is_itype_sh && instr[25]; + `endif end INST_R: begin ex_type = EX_ALU; @@ -325,6 +392,8 @@ module VX_decode import op_args.alu.imm20 = `SEXT(20, iw_imm); `USED_IREG (rd); `USED_IREG (rs1); + // shamt[5] is reserved for the word shifts + illegal = is_itype_sh && instr[25]; end INST_R_W: begin ex_type = EX_ALU; @@ -406,6 +475,7 @@ module VX_decode import INST_B: begin ex_type = EX_ALU; op_type = INST_OP_BITS'(b_type); + illegal = (funct3[2:1] == 2'b01); op_args.br.xtype = ALU_TYPE_BRANCH; op_args.br.use_PC = 1; op_args.br.use_imm= 1; @@ -486,11 +556,16 @@ module VX_decode import rd_xregs[XREG_1] = is_csr_fcsr || is_csr_frm; wr_xregs[XREG_0] = csr_write && (is_csr_fcsr || is_csr_fflags); wr_xregs[XREG_1] = csr_write && (is_csr_fcsr || is_csr_frm); + // mstatus gates the decode of the warp's next instructions + is_wstall = (u_12 == `VX_CSR_MSTATUS); + // CSR addresses 0xC00-0xFFF are read-only + illegal = csr_write && (u_12[11:10] == 2'b11); `USED_IREG (rd); `USED_REG (REG_TYPE_I, rs1, ~funct3[2]); end else begin ex_type = EX_ALU; op_type = INST_OP_BITS'(s_type); + illegal = funct3[2] || ~s_legal; op_args.br.xtype = ALU_TYPE_BRANCH; op_args.br.use_imm= 1; op_args.br.imm_hi = 0; @@ -499,7 +574,6 @@ module VX_decode import op_args.br.imm20 = 20'd4; op_args.br.is_rvc = decode_is_rvc; is_wstall = 1; - `USED_IREG (rd); end end `ifdef VX_CFG_EXT_F_ENABLE @@ -613,13 +687,9 @@ module VX_decode import op_args.fpu.dst_fmt = funct2; op_args.fpu.src_fmt = funct2; op_args.fpu.op_mod = 1'b0; - if (~fp_legal) begin - // A sign injection with no destination writes neither rd nor fflags. - op_type = INST_OP_BITS'(INST_FPU_MISC); - op_args.fpu.frm = INST_FRM_BITS'(0); - op_args.fpu.dst_fmt = INST_FMT_S; - op_args.fpu.src_fmt = INST_FMT_S; - end else if (opcode != INST_FCI) begin + // a reserved encoding traps; the fields below are then unused + illegal = ~fp_legal; + if (opcode != INST_FCI) begin op_type = INST_OP_BITS'({2'b00, 1'b1, opcode[3]}); op_args.fpu.op_mod = opcode[3] ^ opcode[2]; // MSUB, NMSUB @@ -1048,8 +1118,31 @@ module VX_decode import end end `endif - default:; + default: illegal = 1; endcase + + use_rd = use_regs[RV_RD]; + fs_write_fcsr = is_fp_inst && (| wr_xregs); + + // An illegal instruction, or an F/D instruction while mstatus.FS is + // Off, becomes a trap uop: no operands, no writeback. + trap_uop = illegal || (is_fp_inst && fs_off_w); + if (trap_uop) begin + ex_type = EX_ALU; + op_type = INST_OP_BITS'(INST_BR_ILLEGAL); + op_args.br.xtype = ALU_TYPE_BRANCH; + op_args.br.use_PC = 1; + op_args.br.use_imm = 1; + op_args.br.imm_hi = 0; + op_args.br.imm_x2 = 0; + op_args.br.imm20 = 20'd4; + op_args.br.is_rvc = decode_is_rvc; + use_regs = '0; + rd_xregs = '0; + wr_xregs = '0; + bytesel = BYTESEL_DEFAULT; + is_wstall = 1; + end end // Without its extension a TCU, DXA, wide or matrix load/store instruction @@ -1065,15 +1158,20 @@ module VX_decode import ("%t: %s DXA instruction on a core built without VX_CFG_EXT_DXA_ENABLE, wid=%0d, PC=0x%0h (#%0d)", $time, INSTANCE_ID, fetch_if.data.wid, to_fullPC(fetch_if.data.PC), fetch_if.data.uuid)) `endif + // Without either load/store extension the opcode is illegal and traps. +`ifdef EXT_LDST_ANY_ENABLE `ifndef VX_CFG_EXT_LDST_WIDE_ENABLE `RUNTIME_ASSERT(~(fetch_if.valid && (opcode == INST_EXT4) && (funct3 >= 3'h3) && (funct3 <= 3'h6)), ("%t: %s wide load/store on a core built without VX_CFG_EXT_LDST_WIDE_ENABLE, wid=%0d, PC=0x%0h (#%0d)", $time, INSTANCE_ID, fetch_if.data.wid, to_fullPC(fetch_if.data.PC), fetch_if.data.uuid)) `endif +`endif `ifndef VX_CFG_EXT_LDST_MATRIX_ENABLE +`ifdef EXT_LDST_ANY_ENABLE `RUNTIME_ASSERT(~(fetch_if.valid && (opcode == INST_EXT4) && ((funct3 == 3'h0) || (funct3 == 3'h2))), ("%t: %s matrix load/store on a core built without VX_CFG_EXT_LDST_MATRIX_ENABLE, wid=%0d, PC=0x%0h (#%0d)", $time, INSTANCE_ID, fetch_if.data.wid, to_fullPC(fetch_if.data.PC), fetch_if.data.uuid)) +`endif `else `RUNTIME_ASSERT(~(fetch_if.valid && (opcode == INST_EXT4) && (((funct3 == 3'h0) && ~mtx_ldm_valid(u_12[2:0])) || ((funct3 == 3'h2) && ~mtx_ldm_valid(rd[2:0])))), @@ -1081,13 +1179,6 @@ module VX_decode import $time, INSTANCE_ID, fetch_if.data.wid, to_fullPC(fetch_if.data.PC), fetch_if.data.uuid)) `endif -`ifdef VX_CFG_EXT_F_ENABLE - `RUNTIME_ASSERT(~(fetch_if.valid && ~fp_legal && ((opcode == INST_FCI) || (opcode == INST_FMADD) || (opcode == INST_FMSUB) - || (opcode == INST_FNMSUB) || (opcode == INST_FNMADD))), - ("%t: %s FP instruction with an unsupported format or a reserved encoding, wid=%0d, PC=0x%0h (#%0d)", - $time, INSTANCE_ID, fetch_if.data.wid, to_fullPC(fetch_if.data.PC), fetch_if.data.uuid)) -`endif - // disable writes to x0 wire wb = use_regs[RV_RD] && (reg_ids[RV_RD] != 0); @@ -1112,6 +1203,12 @@ module VX_decode import // Register decode_sched_if to break the long combinational path. reg decode_sched_valid_r; reg decode_sched_unlock_r; + // Writing an FP register or FP CSR state makes mstatus.FS Dirty, unless + // the instruction traps. The terms are registered apart to keep them shallow. + reg decode_sched_use_rd_r; + reg decode_sched_rd_freg_r; + reg decode_sched_fs_write_r; + reg decode_sched_trap_r; reg [NW_WIDTH-1:0] decode_sched_wid_r; `ifdef VX_CFG_EXT_C_ENABLE reg decode_sched_is_rvc_r; @@ -1123,6 +1220,10 @@ module VX_decode import end else begin decode_sched_valid_r <= fetch_fire; decode_sched_unlock_r <= ~is_wstall; + decode_sched_use_rd_r <= use_rd; + decode_sched_rd_freg_r <= (get_reg_type(reg_ids[RV_RD]) == REG_TYPE_F); + decode_sched_fs_write_r <= fs_write_fcsr; + decode_sched_trap_r <= trap_uop; decode_sched_wid_r <= fetch_if.data.wid; `ifdef VX_CFG_EXT_C_ENABLE decode_sched_is_rvc_r <= fetch_if.data.is_rvc; @@ -1133,6 +1234,8 @@ module VX_decode import assign decode_sched_if.valid = decode_sched_valid_r; assign decode_sched_if.wid = decode_sched_wid_r; assign decode_sched_if.unlock = decode_sched_unlock_r; + assign decode_sched_if.fs_dirty = ((decode_sched_use_rd_r && decode_sched_rd_freg_r) || decode_sched_fs_write_r) + && ~decode_sched_trap_r; `ifdef VX_CFG_EXT_C_ENABLE assign decode_sched_if.is_rvc = decode_sched_is_rvc_r; `endif diff --git a/hw/rtl/core/VX_decompressor.sv b/hw/rtl/core/VX_decompressor.sv index f79f849360..245aefbc34 100644 --- a/hw/rtl/core/VX_decompressor.sv +++ b/hw/rtl/core/VX_decompressor.sv @@ -194,6 +194,7 @@ module VX_decompressor import VX_gpu_pkg::*; #( logic [11:0] i_imm, b_imm, x_imm, w_imm; logic [19:0] j_imm; logic [31:0] instr_o; + logic reserved; func3 = instr_i[15:13]; rd = instr_i[11:7]; @@ -216,12 +217,19 @@ module VX_decompressor import VX_gpu_pkg::*; #( instr_i[2], instr_i[11], instr_i[5:3]}; x_imm = {{3{instr_i[12]}}, instr_i[4:3], instr_i[5], instr_i[2], instr_i[6], 4'b0}; w_imm = {2'd0, instr_i[10:7], instr_i[12:11], instr_i[5], instr_i[6], 2'b0}; - instr_o = '0; + instr_o = '0; + reserved = 1'b0; + // A reserved encoding expands to the all-zero word, which decodes as an + // illegal instruction. The rd=x0 forms the spec calls HINTs execute as + // no-ops. case (instr_i[1:0]) 2'b00: begin // Quadrant 0 case (func3) - 3'b000: instr_o = {w_imm, 5'd2, 3'b000, rdp, OPC_I}; + 3'b000: begin // C.ADDI4SPN; nzuimm=0 is reserved + instr_o = {w_imm, 5'd2, 3'b000, rdp, OPC_I}; + reserved = (instr_i[12:5] == 8'd0); + end `ifdef VX_CFG_FLEN_64 3'b001: instr_o = {lsd_imm, rs1p, 3'b011, rdp, OPC_FL}; `endif @@ -240,7 +248,7 @@ module VX_decompressor import VX_gpu_pkg::*; #( `else 3'b111: instr_o = {lsw_imm[11:5], rs2p, rs1p, 3'b010, lsw_imm[4:0], OPC_FS}; `endif - default: instr_o = 32'h00000013; + default: reserved = 1'b1; endcase end 2'b01: begin // Quadrant 1 @@ -252,22 +260,37 @@ module VX_decompressor import VX_gpu_pkg::*; #( instr_o = {i_imm, rd, 3'b000, rd, OPC_I}; end `ifdef VX_CFG_XLEN_64 - 3'b001: instr_o = {i_imm, rd, 3'b000, rd, OPC_I_W}; + 3'b001: begin // C.ADDIW; rd=x0 is reserved + instr_o = {i_imm, rd, 3'b000, rd, OPC_I_W}; + reserved = (rd == 5'd0); + end `else // RV32C: C.JAL -> JAL x1, j_imm (CJ-format). 3'b001: instr_o = {j_imm[19], j_imm[9:0], j_imm[10], j_imm[18:11], 5'd1, OPC_JAL}; `endif 3'b010: instr_o = {i_imm, 5'd0, 3'b000, rd, OPC_I}; - 3'b011: begin - if (rd == 5'd2) + 3'b011: begin // C.ADDI16SP, C.LUI; a zero immediate is reserved + if (rd == 5'd2) begin instr_o = {x_imm, 5'd2, 3'b000, 5'd2, OPC_I}; - else + end else begin instr_o = {{{8{i_imm[11]}}, i_imm}, rd, OPC_LUI}; + end + reserved = ~instr_i[12] && (instr_i[6:2] == 5'd0); end 3'b100: begin case (instr_i[11:10]) - 2'b00: instr_o = {{6'b000000, i_imm[5:0]}, rs1p, 3'b101, rs1p, OPC_I}; - 2'b01: instr_o = {{6'b010000, i_imm[5:0]}, rs1p, 3'b101, rs1p, OPC_I}; + 2'b00: begin // C.SRLI + instr_o = {{6'b000000, i_imm[5:0]}, rs1p, 3'b101, rs1p, OPC_I}; + `ifndef VX_CFG_XLEN_64 + reserved = instr_i[12]; + `endif + end + 2'b01: begin // C.SRAI + instr_o = {{6'b010000, i_imm[5:0]}, rs1p, 3'b101, rs1p, OPC_I}; + `ifndef VX_CFG_XLEN_64 + reserved = instr_i[12]; + `endif + end 2'b10: instr_o = {i_imm, rs1p, 3'b111, rs1p, OPC_I}; 2'b11: begin case ({instr_i[12], instr_i[6:5]}) @@ -279,44 +302,59 @@ module VX_decompressor import VX_gpu_pkg::*; #( 3'b100: instr_o = {7'b0100000, rs2p, rs1p, 3'b000, rs1p, OPC_R_W}; 3'b101: instr_o = {7'b0000000, rs2p, rs1p, 3'b000, rs1p, OPC_R_W}; `endif - default: instr_o = 32'h00000013; + default: reserved = 1'b1; endcase end - default: instr_o = 32'h00000013; + default: reserved = 1'b1; endcase end 3'b101: instr_o = {j_imm[19], j_imm[9:0], j_imm[10], j_imm[18:11], 5'd0, OPC_JAL}; 3'b110: instr_o = {b_imm[11], b_imm[9:4], 5'd0, rs1p, 3'b000, b_imm[3:0], b_imm[10], OPC_B}; 3'b111: instr_o = {b_imm[11], b_imm[9:4], 5'd0, rs1p, 3'b001, b_imm[3:0], b_imm[10], OPC_B}; - default: instr_o = 32'h00000013; + default: reserved = 1'b1; endcase end 2'b10: begin // Quadrant 2 case (func3) - 3'b000: instr_o = {{6'b000000, i_imm[5:0]}, rd, 3'b001, rd, OPC_I}; + 3'b000: begin // C.SLLI + instr_o = {{6'b000000, i_imm[5:0]}, rd, 3'b001, rd, OPC_I}; + `ifndef VX_CFG_XLEN_64 + reserved = instr_i[12]; + `endif + end `ifdef VX_CFG_FLEN_64 3'b001: instr_o = {ldsp_imm, 5'd2, 3'b011, rd, OPC_FL}; `endif - 3'b010: instr_o = {lwsp_imm, 5'd2, 3'b010, rd, OPC_L}; + 3'b010: begin // C.LWSP; rd=x0 is reserved + instr_o = {lwsp_imm, 5'd2, 3'b010, rd, OPC_L}; + reserved = (rd == 5'd0); + end `ifdef VX_CFG_XLEN_64 - 3'b011: instr_o = {ldsp_imm, 5'd2, 3'b011, rd, OPC_L}; + 3'b011: begin // C.LDSP; rd=x0 is reserved + instr_o = {ldsp_imm, 5'd2, 3'b011, rd, OPC_L}; + reserved = (rd == 5'd0); + end `else 3'b011: instr_o = {lwsp_imm, 5'd2, 3'b010, rd, OPC_FL}; `endif 3'b100: begin if (instr_i[12] == 1'b0) begin - if (rs2 == 5'd0) - instr_o = {12'd0, rs1, 3'b000, 5'd0, OPC_JALR}; - else + if (rs2 == 5'd0) begin // C.JR; rs1=x0 is reserved + instr_o = {12'd0, rs1, 3'b000, 5'd0, OPC_JALR}; + reserved = (rs1 == 5'd0); + end else begin instr_o = {7'b0000000, rs2, 5'd0, 3'b000, rd, OPC_R}; + end end else begin if (rs2 == 5'd0) begin - if (rs1 == 5'd0) + if (rs1 == 5'd0) begin instr_o = 32'b000000000001_00000_000_00000_1110011; - else + end else begin instr_o = {12'd0, rs1, 3'b000, 5'd1, OPC_JALR}; - end else + end + end else begin instr_o = {7'b0000000, rs2, rd, 3'b000, rd, OPC_R}; + end end end `ifdef VX_CFG_FLEN_64 @@ -328,12 +366,12 @@ module VX_decompressor import VX_gpu_pkg::*; #( `else 3'b111: instr_o = {swsp_imm[11:5], rs2, 5'd2, 3'b010, swsp_imm[4:0], OPC_FS}; `endif - default: instr_o = 32'h00000013; + default: reserved = 1'b1; endcase end - default: instr_o = 32'h00000013; + default: reserved = 1'b1; endcase - return instr_o; + return reserved ? 32'h0 : instr_o; endfunction // ------------------------------------------------------------------ diff --git a/hw/rtl/core/VX_frontend_xbar.sv b/hw/rtl/core/VX_frontend_xbar.sv index b3e7abb273..ba859ab04c 100644 --- a/hw/rtl/core/VX_frontend_xbar.sv +++ b/hw/rtl/core/VX_frontend_xbar.sv @@ -172,6 +172,7 @@ module VX_frontend_xbar import VX_gpu_pkg::*; ( assign fs_csr_wr[f][q].data = sched_csr_if[B].csr_wr_data; assign fs_csr_wr[f][q].trap_valid = sched_csr_if[B].trap_csr_wr_valid & {NUM_TRAP_CSRS{own_wr}}; assign fs_csr_wr[f][q].trap_data = sched_csr_if[B].trap_csr_wr_data; + assign fs_csr_wr[f][q].unlock = sched_csr_if[B].csr_unlock && own_wr; `ifdef VX_CFG_EXT_RTU_ENABLE assign fs_rtu_unlock_valid[f][q] = sched_unlock_if[B].valid && own_unlock; diff --git a/hw/rtl/core/VX_warp_sched.sv b/hw/rtl/core/VX_warp_sched.sv index 0f47ae507d..d35605eee5 100644 --- a/hw/rtl/core/VX_warp_sched.sv +++ b/hw/rtl/core/VX_warp_sched.sv @@ -148,6 +148,15 @@ module VX_warp_sched import VX_gpu_pkg::*; #( localparam MEPC_LSB = 2; `endif localparam MTVEC_LSB = 2; + // FS resets to Initial so that FP code runs without first writing mstatus. + // Without F, FS is read-only zero. +`ifdef VX_CFG_EXT_F_ENABLE + localparam [1:0] FS_RESET = 2'b01; + localparam [1:0] FS_WMASK = 2'b11; +`else + localparam [1:0] FS_RESET = 2'b00; + localparam [1:0] FS_WMASK = 2'b00; +`endif function automatic logic [`VX_CFG_XLEN-1:0] mstatus_value( input logic mie, @@ -159,6 +168,7 @@ module VX_warp_sched import VX_gpu_pkg::*; #( mstatus_value[`VX_CSR_MSTATUS_MPIE] = mpie; mstatus_value[`VX_CSR_MSTATUS_MPP +: 2] = 2'b11; mstatus_value[`VX_CSR_MSTATUS_FS +: 2] = fs; + mstatus_value[`VX_CFG_XLEN-1] = (fs == 2'b11); // SD endfunction reg [SW-1:0][`VX_CFG_MEM_ADDR_WIDTH-1:0] mscratch_r; @@ -187,6 +197,7 @@ module VX_warp_sched import VX_gpu_pkg::*; #( assign mstatus[l] = mstatus_value(mstatus_mie_r[l], mstatus_mpie_r[l], mstatus_fs_r[l]); assign mtvec[l] = {mtvec_r[l], MTVEC_LSB'(0)}; assign mepc[l] = {mepc_r[l], MEPC_LSB'(0)}; + assign decode_sched_if.fs_off[l] = (mstatus_fs_r[l] == 2'b00); always @(*) begin active_n = active_r; @@ -207,6 +218,10 @@ module VX_warp_sched import VX_gpu_pkg::*; #( stalled_n = 0; end + if (csr_wr[QS].unlock && (csr_wr_lid[QS] == LID)) begin + stalled_n = 0; + end + if (sched_if.wspawn_apply) begin if (sched_if.wspawn_wmask[l]) begin active_n = 1; @@ -305,7 +320,7 @@ module VX_warp_sched import VX_gpu_pkg::*; #( mscratch_r[l] <= '0; mstatus_mie_r[l] <= 1'b0; mstatus_mpie_r[l] <= 1'b0; - mstatus_fs_r[l] <= '0; + mstatus_fs_r[l] <= FS_RESET; mtvec_r[l] <= '0; mepc_r[l] <= '0; mcause_r[l] <= '0; @@ -329,11 +344,15 @@ module VX_warp_sched import VX_gpu_pkg::*; #( mscratch_r[l] <= csr_wr[QS].data; end + if (decode_sched_if.valid && decode_sched_if.fs_dirty && (dec_lid == LID)) begin + mstatus_fs_r[l] <= 2'b11; + end + if (csr_wr_lid[QS] == LID) begin if (csr_wr[QS].trap_valid[0]) begin mstatus_mie_r[l] <= csr_wr[QS].trap_data[`VX_CSR_MSTATUS_MIE]; mstatus_mpie_r[l] <= csr_wr[QS].trap_data[`VX_CSR_MSTATUS_MPIE]; - mstatus_fs_r[l] <= csr_wr[QS].trap_data[`VX_CSR_MSTATUS_FS +: 2]; + mstatus_fs_r[l] <= csr_wr[QS].trap_data[`VX_CSR_MSTATUS_FS +: 2] & FS_WMASK; end if (csr_wr[QS].trap_valid[1]) begin mtvec_r[l] <= csr_wr[QS].trap_data[`VX_CFG_XLEN-1:MTVEC_LSB]; @@ -349,7 +368,7 @@ module VX_warp_sched import VX_gpu_pkg::*; #( end end - // Hardware trap entry (ECALL/EBREAK) and return win a same-cycle software write. + // Hardware trap entry and return win a same-cycle software write. if (br_hit && branch[QA].is_trap) begin mstatus_mie_r[l] <= 1'b0; mstatus_mpie_r[l] <= mstatus_mie_r[l]; diff --git a/hw/rtl/interfaces/VX_branch_ctl_if.sv b/hw/rtl/interfaces/VX_branch_ctl_if.sv index 664dd56502..7251452bd3 100644 --- a/hw/rtl/interfaces/VX_branch_ctl_if.sv +++ b/hw/rtl/interfaces/VX_branch_ctl_if.sv @@ -19,11 +19,12 @@ interface VX_branch_ctl_if import VX_gpu_pkg::*; (); wire [NW_WIDTH-1:0] wid; wire taken; wire [PC_BITS-1:0] dest; - // Synchronous trap signalling (ECALL/EBREAK/MRET). When is_trap is set - // `dest` carries the faulting PC (saved into mepc) and trap_cause the - // mcause code; the scheduler redirects the warp to mtvec. is_mret - // restores the warp PC from mepc. Both are mutually exclusive and - // override the normal taken/dest path in the scheduler. + // Synchronous trap signalling (ECALL/EBREAK, an illegal instruction, a + // misaligned target, MRET). When is_trap is set `dest` carries the + // trapping instruction's PC (saved into mepc) and trap_cause the mcause + // code; the scheduler redirects the warp to mtvec. is_mret restores the + // warp PC from mepc. Both are mutually exclusive and override the normal + // taken/dest path in the scheduler. wire is_trap; wire is_mret; wire [3:0] trap_cause; diff --git a/hw/rtl/interfaces/VX_decode_sched_if.sv b/hw/rtl/interfaces/VX_decode_sched_if.sv index 1e92b6d8ae..6dfaeb9248 100644 --- a/hw/rtl/interfaces/VX_decode_sched_if.sv +++ b/hw/rtl/interfaces/VX_decode_sched_if.sv @@ -21,6 +21,10 @@ interface VX_decode_sched_if import VX_gpu_pkg::*; (); wire is_rvc; `endif wire [NW_WIDTH-1:0] wid; + // the decoded instruction may write FP state: mstatus.FS becomes Dirty + wire fs_dirty; + // per slice warp: mstatus.FS is Off + wire [PER_FETCH_WARPS-1:0] fs_off; modport master ( output valid, @@ -28,7 +32,9 @@ interface VX_decode_sched_if import VX_gpu_pkg::*; (); `ifdef VX_CFG_EXT_C_ENABLE output is_rvc, `endif - output wid + output wid, + output fs_dirty, + input fs_off ); modport slave ( @@ -37,7 +43,9 @@ interface VX_decode_sched_if import VX_gpu_pkg::*; (); `ifdef VX_CFG_EXT_C_ENABLE input is_rvc, `endif - input wid + input wid, + input fs_dirty, + output fs_off ); endinterface diff --git a/hw/rtl/interfaces/VX_sched_csr_if.sv b/hw/rtl/interfaces/VX_sched_csr_if.sv index cc88a4d0e2..0c318d8a3c 100644 --- a/hw/rtl/interfaces/VX_sched_csr_if.sv +++ b/hw/rtl/interfaces/VX_sched_csr_if.sv @@ -52,6 +52,8 @@ interface VX_sched_csr_if import VX_gpu_pkg::*; (); logic [`VX_CFG_XLEN-1:0] csr_mtval; logic [NUM_TRAP_CSRS-1:0] trap_csr_wr_valid; logic [`VX_CFG_XLEN-1:0] trap_csr_wr_data; + // An mstatus access stalls its warp from decode; this releases it (csr_wr_wid). + logic csr_unlock; modport master ( output cycles, @@ -76,7 +78,8 @@ interface VX_sched_csr_if import VX_gpu_pkg::*; (); input csr_wr_wid, input csr_wr_data, input trap_csr_wr_valid, - input trap_csr_wr_data + input trap_csr_wr_data, + input csr_unlock ); modport slave ( @@ -102,7 +105,8 @@ interface VX_sched_csr_if import VX_gpu_pkg::*; (); output csr_wr_wid, output csr_wr_data, output trap_csr_wr_valid, - output trap_csr_wr_data + output trap_csr_wr_data, + output csr_unlock ); endinterface diff --git a/sim/simx/alu_unit.cpp b/sim/simx/alu_unit.cpp index 0c0e09dd0c..72791cfdcf 100644 --- a/sim/simx/alu_unit.cpp +++ b/sim/simx/alu_unit.cpp @@ -79,6 +79,7 @@ uint32_t AluUnit::latency_of(const instr_trace_t* trace) const { case BrType::JAL: case BrType::JALR: case BrType::SYS: + case BrType::ILLEGAL: return 0; default: std::abort(); @@ -381,6 +382,19 @@ void AluUnit::execute(instr_trace_t* trace) { auto br_type = std::get(trace->op_type); auto brArgs = std::get(instrArgs); Word offset = sext(brArgs.offset, 32); + // Without C, a target with bit 1 set is misaligned: the jump or taken + // branch traps instead, and writes no lane. + auto misaligned = [](Word target) { + #ifdef VX_CFG_EXT_C_ENABLE + __unused(target); + return false; + #else + return (target & 0x2) != 0; + #endif + }; + // A warp follows its last active thread; with more threads than lanes + // the last packet decides the redirect or trap, as each packet only + // writes its own lanes. switch (br_type) { case BrType::BR: { bool curr_taken = false; @@ -394,43 +408,62 @@ void AluUnit::execute(instr_trace_t* trace) { case 7: curr_taken = (rs1_data[t].u >= rs2_data[t].u); break; default: std::abort(); } - if (curr_taken) { - warp.PC = trace->PC + offset; - } - core_->perf_stats().branches += 1; - } break; - case BrType::JAL: { - // RVC source returns PC+2; full RV32I returns PC+4. - Word link_pc = trace->PC + (brArgs.is_rvc ? 2 : 4); - for (uint32_t t = thread_start; t < num_threads; ++t) { - if (!tmask.test(t)) continue; - rd_data[t].i = link_pc; + if (trace->eop) { + if (curr_taken) { + Word target = trace->PC + offset; + if (misaligned(target)) { + sched.trigger_misaligned_fetch(trace->wid, trace->PC, trace->code, target); + } else { + warp.PC = target; + } + } + core_->perf_stats().branches += 1; } - warp.PC = trace->PC + offset; - core_->perf_stats().branches += 1; } break; + case BrType::JAL: case BrType::JALR: { - // RVC source returns PC+2; full RV32I returns PC+4. - Word link_pc = trace->PC + (brArgs.is_rvc ? 2 : 4); - for (uint32_t t = thread_start; t < num_threads; ++t) { - if (!tmask.test(t)) continue; - rd_data[t].i = link_pc; + // JALR clears bit 0 of the computed target. + Word target = (br_type == BrType::JAL) ? (trace->PC + offset) + : ((rs1_data[thread_last].u + offset) & ~Word(1)); + if (misaligned(target)) { + tmask.reset(); + } else { + // RVC source returns PC+2; full RV32I returns PC+4. + Word link_pc = trace->PC + (brArgs.is_rvc ? 2 : 4); + for (uint32_t t = thread_start; t < num_threads; ++t) { + if (!tmask.test(t)) continue; + rd_data[t].i = link_pc; + } + } + if (trace->eop) { + if (misaligned(target)) { + sched.trigger_misaligned_fetch(trace->wid, trace->PC, trace->code, target); + } else { + warp.PC = target; + } + core_->perf_stats().branches += 1; } - // JALR clears bit 0 of the computed target (RISC-V ISA); the RTL - // release build drops it implicitly via from_fullPC() truncation. - warp.PC = (rs1_data[thread_last].u + offset) & ~Word(1); - core_->perf_stats().branches += 1; } break; case BrType::SYS: - switch (brArgs.offset) { - // ECALL / EBREAK: synchronous trap — redirect warp PC to mtvec. - case 0x000: sched.trigger_ecall(trace->wid, trace->PC); break; - case 0x001: sched.trigger_ebreak(trace->wid, trace->PC); break; - // URET / SRET / MRET: trap return — restore warp PC from mepc. - case 0x002: case 0x102: case 0x302: sched.mret(trace->wid); break; - default: std::abort(); + if (trace->eop) { + switch (brArgs.offset) { + // ECALL / EBREAK: synchronous trap — redirect warp PC to mtvec. + case 0x000: sched.trigger_ecall(trace->wid, trace->PC); break; + case 0x001: sched.trigger_ebreak(trace->wid, trace->PC); break; + // URET / SRET / MRET: trap return — restore warp PC from mepc. + case 0x002: case 0x102: case 0x302: sched.mret(trace->wid); break; + // WFI: no interrupt to wait for, so it falls through. + case 0x105: break; + default: std::abort(); + } + core_->perf_stats().branches += 1; + } + break; + case BrType::ILLEGAL: + if (trace->eop) { + sched.trigger_illegal(trace->wid, trace->PC, trace->code); + core_->perf_stats().branches += 1; } - core_->perf_stats().branches += 1; break; default: std::abort(); diff --git a/sim/simx/core.cpp b/sim/simx/core.cpp index f7e9726c8b..0e79071fe3 100644 --- a/sim/simx/core.cpp +++ b/sim/simx/core.cpp @@ -737,7 +737,11 @@ class Core::Impl { // Decode: fill trace metadata from instruction bits. The decoder // recognises RVC encoding (low 2 bits != 11), expands internally, and // stamps IntrBrArgs.is_rvc. - auto instr = decoder_->decode(trace->code, trace->uuid); + auto& warp = scheduler_->warp(trace->wid); + auto instr = decoder_->decode(trace->code, trace->uuid, (warp.mstatus & MSTATUS_FS) == 0); + if (instr->fs_dirty()) { + warp.mstatus |= MSTATUS_FS; + } DP(1, "Instr: " << *instr << " (#" << trace->uuid << ")"); trace->instr_ptr = instr; trace->fu_type = instr->get_fu_type(); diff --git a/sim/simx/csr_unit.cpp b/sim/simx/csr_unit.cpp index dbb878db60..6d30aeb7b8 100644 --- a/sim/simx/csr_unit.cpp +++ b/sim/simx/csr_unit.cpp @@ -64,7 +64,9 @@ Word CsrUnit::get_csr(uint32_t addr, uint32_t wid, uint32_t tid) { return 0; // Per-warp machine-mode trap CSRs (see warp_t). - case VX_CSR_MSTATUS: return warp.mstatus | MSTATUS_MPP_M; + case VX_CSR_MSTATUS: + return warp.mstatus | MSTATUS_MPP_M + | (((warp.mstatus & MSTATUS_FS) == MSTATUS_FS) ? MSTATUS_SD : 0); case VX_CSR_MTVEC: return warp.mtvec; case VX_CSR_MEPC: return warp.mepc; case VX_CSR_MCAUSE: return warp.mcause; @@ -379,6 +381,7 @@ void CsrUnit::set_csr(uint32_t addr, Word value, uint32_t wid, uint32_t tid) { case VX_CSR_MTVAL: warp.mtval = value; break; + case VX_CSR_MISA: case VX_CSR_MEDELEG: case VX_CSR_MIDELEG: case VX_CSR_MIE: diff --git a/sim/simx/decode.cpp b/sim/simx/decode.cpp index 0905cc5864..cc2e8a7008 100644 --- a/sim/simx/decode.cpp +++ b/sim/simx/decode.cpp @@ -225,6 +225,7 @@ static op_string_t op_string(const Instr &instr) { default: std::abort(); } + case BrType::ILLEGAL: return {"ILLEGAL", ""}; default: std::abort(); } @@ -558,13 +559,27 @@ Decoder::Decoder(const SimContext& ctx, const char* name, PoolAllocator(instr_pool_, uuid, FUType::ALU); + instr->set_op_type(BrType::ILLEGAL); + instr->set_args(IntrBrArgs{0, is_rvc, 0}); + instr->set_wstall(true); + return instr; +} + +Instr::Ptr Decoder::decode(uint32_t code, uint64_t uuid, bool fs_off) { // Detect RVC from the raw bits and expand to a 32-bit instruction. const bool is_rvc = (code & 0x3u) != 0x3u; if (is_rvc) { + #ifdef VX_CFG_EXT_C_ENABLE auto r = rvc_decompress(code & 0xFFFFu); - assert(!r.illegal && "illegal RVC encoding"); + if (r.illegal) { + return this->illegal(uuid, true); + } code = r.instr32; + #else + return this->illegal(uuid, false); + #endif } auto op = Opcode((code >> shift_opcode) & mask_opcode); auto funct2 = (code >> shift_funct2) & mask_funct2; @@ -579,6 +594,72 @@ Instr::Ptr Decoder::decode(uint32_t code, uint64_t uuid) { auto rs2 = (code >> shift_rs2) & mask_reg; auto rs3 = (code >> shift_rs3) & mask_reg; + // The instructions that name an FP register or FP CSR, which mstatus.FS=Off + // makes illegal: the F/D instructions, the FP CSR accesses, and the Vortex + // extensions on FP registers. + bool is_fp = false; + switch (op) { + case Opcode::FL: + case Opcode::FS: + case Opcode::FMADD: + case Opcode::FMSUB: + case Opcode::FNMSUB: + case Opcode::FNMADD: + case Opcode::FCI: + #ifndef VX_CFG_EXT_F_ENABLE + return this->illegal(uuid, is_rvc); + #endif + is_fp = true; + break; +#ifdef VX_CFG_EXT_F_ENABLE + case Opcode::SYS: { + uint32_t csr = code >> shift_rs2; + is_fp = ((funct3 & 0x3) != 0) + && (csr == VX_CSR_FFLAGS || csr == VX_CSR_FRM || csr == VX_CSR_FCSR); + } break; + case Opcode::EXT1: + // packed FP loads + is_fp = (funct7 == 0x4) && (funct3 == 1 || funct3 == 2); + #ifdef VX_CFG_EXT_TCU_ENABLE + // MMA; its metadata load names no FP register + #ifdef TCU_META_ENABLE + is_fp |= (funct7 == 0x2) && (funct3 != 2); + #else + is_fp |= (funct7 == 0x2); + #endif + #endif + break; +#ifdef VX_CFG_EXT_RTU_ENABLE + case Opcode::EXT2: // GETWF and CB_RET + is_fp = (funct3 == 6) && (funct2 == 0 || funct2 == 2); + break; +#endif +#ifdef VX_CFG_EXT_TFU_ENABLE + case Opcode::EXT3: + is_fp = true; + break; +#endif +#if defined(VX_CFG_EXT_LDST_MATRIX_ENABLE) || defined(VX_CFG_EXT_LDST_WIDE_ENABLE) + case Opcode::EXT4: // wide and matrix FP loads and stores + is_fp = true; + break; +#endif +#endif +#ifndef VX_CFG_EXT_A_ENABLE + case Opcode::AMO: + return this->illegal(uuid, is_rvc); +#endif +#if !defined(VX_CFG_EXT_LDST_MATRIX_ENABLE) && !defined(VX_CFG_EXT_LDST_WIDE_ENABLE) + case Opcode::EXT4: + return this->illegal(uuid, is_rvc); +#endif + default: + break; + } + if (is_fp && fs_off) { + return this->illegal(uuid, is_rvc); + } + auto instr = std::allocate_shared(instr_pool_, uuid, FUType::ALU); switch (op) { @@ -597,6 +678,16 @@ Instr::Ptr Decoder::decode(uint32_t code, uint64_t uuid) { case Opcode::I: { bool is_w = (op == Opcode::R_W) || (op == Opcode::I_W); bool is_imm = (op == Opcode::I) || (op == Opcode::I_W); + // shamt[5] of an immediate shift is reserved on RV32 and for the word shifts + if (is_imm && (funct3 & 0x3) == 0x1 && (funct7 & 0x1)) { + #ifdef VX_CFG_XLEN_64 + if (is_w) { + return this->illegal(uuid, is_rvc); + } + #else + return this->illegal(uuid, is_rvc); + #endif + } if (op == Opcode::R && funct7 == 0x7) { uint32_t imm; if (funct3 == 0x5) { // CZERO.EQZ @@ -667,6 +758,9 @@ Instr::Ptr Decoder::decode(uint32_t code, uint64_t uuid) { auto bit_12 = funct7 >> 6; auto imm12 = (bits_4_1 << 1) | (bit_10_5 << 5) | (bit_11 << 11) | (bit_12 << 12); auto addr = sext(imm12, width_i_imm+1); + if (funct3 == 2 || funct3 == 3) { + return this->illegal(uuid, is_rvc); + } instr->set_op_type(BrType::BR); instr->set_args(IntrBrArgs{funct3, is_rvc, addr}); instr->set_src_reg(0, rs1, RegType::Integer); @@ -787,11 +881,17 @@ Instr::Ptr Decoder::decode(uint32_t code, uint64_t uuid) { case 2: case 6: instr->set_op_type(CsrType::CSRRS); break; case 3: case 7: instr->set_op_type(CsrType::CSRRC); break; default: - std::abort(); + return this->illegal(uuid, is_rvc); } auto imm12 = code >> shift_rs2; { bool csr_write = (funct3 == 1) || (funct3 == 5) || (rs1 != 0); + // CSR addresses 0xC00-0xFFF are read-only + if (csr_write && ((imm12 >> 10) & 0x3) == 0x3) { + return this->illegal(uuid, is_rvc); + } + // mstatus gates the decode of the warp's next instructions + instr->set_wstall(imm12 == VX_CSR_MSTATUS); uint8_t fcsr_regs = 0; if (imm12 == VX_CSR_FFLAGS || imm12 == VX_CSR_FCSR) { fcsr_regs |= Instr::FCSR_FFLAGS; @@ -807,8 +907,13 @@ Instr::Ptr Decoder::decode(uint32_t code, uint64_t uuid) { } else { // zimm instr->set_args(IntrCsrArgs{1, rs1, imm12}); } - } else { // ECALL/EBREACK/URET/SRET/MRET + } else { // ECALL/EBREAK/URET/SRET/MRET/WFI auto imm12 = code >> shift_rs2; + switch (imm12) { + case 0x000: case 0x001: case 0x002: case 0x102: case 0x302: case 0x105: break; + default: + return this->illegal(uuid, is_rvc); + } instr->set_op_type(BrType::SYS); instr->set_args(IntrBrArgs{0, 0, imm12}); instr->set_wstall(true); @@ -825,7 +930,7 @@ Instr::Ptr Decoder::decode(uint32_t code, uint64_t uuid) { bool is_f2f = (group == 0x08); bool is_mv = (group == 0x1e) || (group == 0x1c && funct3 == 0); if (!is_f2f && !(is_mv ? mv_fmt_supported(fmt) : fmt_supported(fmt))) { - std::abort(); + return this->illegal(uuid, is_rvc); } } switch (funct7) { @@ -884,7 +989,7 @@ Instr::Ptr Decoder::decode(uint32_t code, uint64_t uuid) { uint32_t src_fmt = rs2; bool supported = (src_fmt != dst_fmt) && mv_fmt_supported(src_fmt) && mv_fmt_supported(dst_fmt); if (!supported) { - std::abort(); + return this->illegal(uuid, is_rvc); } instr->set_op_type(FpuType::F2F); instr->set_dest_reg(rd, RegType::Float); @@ -894,7 +999,7 @@ Instr::Ptr Decoder::decode(uint32_t code, uint64_t uuid) { #ifdef VX_CFG_EXT_ZFHMIN_ENABLE case 0x72: // FMV.X.H if (funct3 != 0) { - std::abort(); + return this->illegal(uuid, is_rvc); } instr->set_op_type(FpuType::FMVXW); instr->set_dest_reg(rd, RegType::Integer); @@ -902,7 +1007,7 @@ Instr::Ptr Decoder::decode(uint32_t code, uint64_t uuid) { break; case 0x7a: // FMV.H.X if (funct3 != 0) { - std::abort(); + return this->illegal(uuid, is_rvc); } instr->set_op_type(FpuType::FMVWX); instr->set_dest_reg(rd, RegType::Float); @@ -928,7 +1033,7 @@ Instr::Ptr Decoder::decode(uint32_t code, uint64_t uuid) { case 0x61: // FCVT.W.D, FCVT.WU.D, FCVT.L.D, FCVT.LU.D #ifndef VX_CFG_XLEN_64 if (rs2 & 0x2) { // FCVT.L[U] needs RV64 - std::abort(); + return this->illegal(uuid, is_rvc); } #endif instr->set_op_type(FpuType::F2I); @@ -941,7 +1046,7 @@ Instr::Ptr Decoder::decode(uint32_t code, uint64_t uuid) { case 0x69: // FCVT.D.W, FCVT.D.WU, FCVT.D.L, FCVT.D.LU #ifndef VX_CFG_XLEN_64 if (rs2 & 0x2) { // FCVT.*.L[U] needs RV64 - std::abort(); + return this->illegal(uuid, is_rvc); } #endif instr->set_op_type(FpuType::I2F); @@ -953,7 +1058,7 @@ Instr::Ptr Decoder::decode(uint32_t code, uint64_t uuid) { case 0x70: // FCLASS.S, FMV.X.S case 0x71: // FCLASS.D, FMV.X.D if (funct3 > 1) { // reserved - std::abort(); + return this->illegal(uuid, is_rvc); } instr->set_op_type((funct3 != 0) ? FpuType::FCLASS : FpuType::FMVXW); instr->set_dest_reg(rd, RegType::Integer); @@ -962,14 +1067,14 @@ Instr::Ptr Decoder::decode(uint32_t code, uint64_t uuid) { case 0x78: // FMV.S.X case 0x79: // FMV.D.X if (funct3 != 0) { - std::abort(); + return this->illegal(uuid, is_rvc); } instr->set_op_type(FpuType::FMVWX); instr->set_dest_reg(rd, RegType::Float); instr->set_src_reg(0, rs1, RegType::Integer); break; default: - std::abort(); + return this->illegal(uuid, is_rvc); } } break; case Opcode::FMADD: @@ -977,7 +1082,7 @@ Instr::Ptr Decoder::decode(uint32_t code, uint64_t uuid) { case Opcode::FNMADD: case Opcode::FNMSUB: { if (!fmt_supported(funct2)) { - std::abort(); + return this->illegal(uuid, is_rvc); } instr->set_fu_type(FUType::FPU); instr->set_op_type((op == Opcode::FMADD) ? FpuType::FMADD : @@ -1399,8 +1504,20 @@ Instr::Ptr Decoder::decode(uint32_t code, uint64_t uuid) { } break; #endif default: - std::abort(); + return this->illegal(uuid, is_rvc); + } + + // Writing an FP register, or FP CSR state, makes mstatus.FS Dirty. + bool fs_dirty = (instr->get_dest_reg().type == RegType::Float) + || (is_fp && instr->fcsr_writes() != 0); +#ifdef VX_CFG_EXT_TCU_ENABLE + // a tensor MMA writes its result fragment to FP registers + auto op_type = instr->get_op_type(); + if (auto tcu = std::get_if(&op_type)) { + fs_dirty |= (*tcu != TcuType::TCU_LD); } +#endif + instr->set_fs_dirty(fs_dirty); return instr; } diff --git a/sim/simx/decode.h b/sim/simx/decode.h index e66e3945ea..ad107c60f0 100644 --- a/sim/simx/decode.h +++ b/sim/simx/decode.h @@ -29,9 +29,13 @@ class Decoder : public SimObject { // 32-bit RV32I encoding (low2 == 0b11) or a 16-bit RVC in the low half // (low2 != 0b11); the decoder detects RVC, calls rvc_decompress, and // stamps IntrBrArgs.is_rvc accordingly. - Instr::Ptr decode(uint32_t code, uint64_t uuid); + // An illegal encoding, or an F/D instruction while fs_off (mstatus.FS is + // Off), decodes to an illegal-instruction trap. + Instr::Ptr decode(uint32_t code, uint64_t uuid, bool fs_off); private: + Instr::Ptr illegal(uint64_t uuid, bool is_rvc); + PoolAllocator& instr_pool_; }; diff --git a/sim/simx/decompressor.cpp b/sim/simx/decompressor.cpp index 2c98736fc2..03a5416662 100644 --- a/sim/simx/decompressor.cpp +++ b/sim/simx/decompressor.cpp @@ -98,6 +98,9 @@ DecompResult rvc_decompress(uint32_t word) { break; } case 0b001: { // C.FLD (RV32DC/RV64DC) -> FLD rd', uimm(rs1') + #ifndef VX_CFG_FLEN_64 + out.illegal = true; break; + #endif uint32_t rd_ = rcp(bits(h, 4, 2)); uint32_t rs1_ = rcp(bits(h, 9, 7)); uint32_t uimm = (bits(h, 12, 10) << 3) | (bits(h, 6, 5) << 6); @@ -112,6 +115,9 @@ DecompResult rvc_decompress(uint32_t word) { break; } case 0b101: { // C.FSD (RV32DC/RV64DC) -> FSD rs2', uimm(rs1') + #ifndef VX_CFG_FLEN_64 + out.illegal = true; break; + #endif uint32_t rs2_ = rcp(bits(h, 4, 2)); uint32_t rs1_ = rcp(bits(h, 9, 7)); uint32_t uimm = (bits(h, 12, 10) << 3) | (bits(h, 6, 5) << 6); @@ -208,9 +214,9 @@ DecompResult rvc_decompress(uint32_t word) { if (imm == 0) { out.illegal = true; break; } out.instr32 = ENCI(imm & 0xFFF, 2, 0b000, 2, 0b0010011); } else { - // C.LUI -> LUI rd, imm (rd != x0, x2) + // C.LUI -> LUI rd, imm (rd=x0 is a HINT) int32_t imm = static_cast(sext_imm((bit(h,12)<<17) | (bits(h,6,2)<<12), 18)); - if (rd == 0 || rd == 2 || imm == 0) { out.illegal = true; break; } + if (imm == 0) { out.illegal = true; break; } out.instr32 = ENCU((imm >> 12), rd, 0b0110111); } break; @@ -295,9 +301,8 @@ DecompResult rvc_decompress(uint32_t word) { // ---------------- Quadrant 2 (opcode 10) ---------------- case 2: switch (funct3) { - case 0b000: { // C.SLLI + case 0b000: { // C.SLLI (rd=x0 is a HINT) uint32_t rd = bits(h, 11, 7); - if (rd == 0) { out.illegal = true; break; } #ifndef VX_CFG_XLEN_64 if (bit(h, 12)) { out.illegal = true; break; } #endif @@ -306,12 +311,18 @@ DecompResult rvc_decompress(uint32_t word) { break; } case 0b001: { // C.FLDSP (RV32DC/RV64DC) -> FLD rd, uimm(x2) + #ifndef VX_CFG_FLEN_64 + out.illegal = true; break; + #endif uint32_t rd = bits(h, 11, 7); uint32_t uimm = (bit(h, 12) << 5) | (bits(h, 6, 5) << 3) | (bits(h, 4, 2) << 6); out.instr32 = ENCI(uimm, 2, 0b011, rd, 0b0000111); break; } case 0b101: { // C.FSDSP (RV32DC/RV64DC) -> FSD rs2, uimm(x2) + #ifndef VX_CFG_FLEN_64 + out.illegal = true; break; + #endif uint32_t rs2 = bits(h, 6, 2); uint32_t uimm = (bits(h, 12, 10) << 3) | (bits(h, 9, 7) << 6); out.instr32 = ENCS(uimm, rs2, 2, 0b011, 0b0100111); @@ -359,7 +370,7 @@ DecompResult rvc_decompress(uint32_t word) { } } } else { - if (rd == 0) { out.illegal = true; break; } + // rd=x0 is a HINT if (s12 == 0) { // C.MV out.instr32 = ENCR(0b0000000, rs2, 0, 0b000, rd, 0b0110011); @@ -399,10 +410,6 @@ DecompResult rvc_decompress(uint32_t word) { default: out.illegal = true; break; } - if (out.illegal) { - std::cerr << "Illegal 16-bit RVC: word=0x" << std::hex << (word & 0xFFFFu) << std::dec - << ", quadrant=" << quadrant << ", func3=" << funct3 << std::endl; - } return out; } diff --git a/sim/simx/dtm/debug_module.cpp b/sim/simx/dtm/debug_module.cpp index 84edf9360c..6b6b761dc9 100644 --- a/sim/simx/dtm/debug_module.cpp +++ b/sim/simx/dtm/debug_module.cpp @@ -816,7 +816,6 @@ vortex::Word DebugModule::read_register(uint16_t regaddr) dm_log("[DM] READ REG dpc (0x7B1) -> 0x%0*llx\n", (VX_CFG_XLEN == 64) ? 16 : 8, (unsigned long long)value); return value; } - // Helper function to read CSR by number // Helper function to read CSR by number (CSRs are XLEN bits wide) auto read_csr = [this](uint16_t csr_num, uint16_t regaddr) -> vortex::Word { if (csr_num == 0x0301) { diff --git a/sim/simx/instr.h b/sim/simx/instr.h index 0c063c8f87..8196c71dc0 100644 --- a/sim/simx/instr.h +++ b/sim/simx/instr.h @@ -148,6 +148,7 @@ class Instr { , fu_lock_(true) , fu_unlock_(true) , issue_yield_(false) + , fs_dirty_(false) {} void set_fu_type(FUType fu_type) { @@ -247,6 +248,10 @@ class Instr { uint8_t fcsr_reads() const { return fcsr_rd_; } uint8_t fcsr_writes() const { return fcsr_wr_; } + // the instruction may write the F/D state (mstatus.FS becomes Dirty) + void set_fs_dirty(bool value) { fs_dirty_ = value; } + bool fs_dirty() const { return fs_dirty_; } + private: uint64_t uuid_; @@ -267,6 +272,7 @@ class Instr { bool fu_lock_; bool fu_unlock_; bool issue_yield_; + bool fs_dirty_; friend std::ostream &operator<<(std::ostream &, const Instr &); }; diff --git a/sim/simx/scheduler.cpp b/sim/simx/scheduler.cpp index bfd3169841..0fdb2a919c 100644 --- a/sim/simx/scheduler.cpp +++ b/sim/simx/scheduler.cpp @@ -41,7 +41,7 @@ warp_t::warp_t(uint32_t num_threads) { } -void warp_t::reset() { +void warp_t::reset(bool launch) { this->tmask.reset(); // PC is excluded: a slot that has already run the startup resumes at its // dispatch window, which is derived from where the previous CTA left this @@ -49,11 +49,15 @@ void warp_t::reset() { // construction, and every activation path assigns the PC before it is read. this->uuid = 0; this->fcsr = 0; - this->mstatus = 0; - this->mtvec = 0; - this->mepc = 0; - this->mcause = 0; - this->mtval = 0; + // The trap CSRs are device state, as in the RTL: a device reset clears + // them and a launch keeps them. + if (!launch) { + this->mstatus = MSTATUS_FS_RESET; + this->mtvec = 0; + this->mepc = 0; + this->mcause = 0; + this->mtval = 0; + } // Register files live in OpcUnit and are reset there. } @@ -81,8 +85,9 @@ Scheduler::Scheduler(const SimContext& ctx, const char* name, Core* core) Scheduler::~Scheduler() {} void Scheduler::on_reset() { + bool launch = SimPlatform::instance().launch_resetting(); for (auto& warp : warps_) { - warp.reset(); + warp.reset(launch); } stalled_warps_.reset(); @@ -357,8 +362,10 @@ bool Scheduler::wspawn(uint32_t num_warps, Word nextPC) { // RISC-V machine-mode synchronous exception cause codes (mcause). // Standard 0..15; 24..31 reserved for custom by the privileged spec. namespace { - constexpr Word TRAP_CAUSE_BREAKPOINT = 3; - constexpr Word TRAP_CAUSE_ECALL_MMODE = 11; + constexpr Word TRAP_CAUSE_MISALIGNED_FETCH = 0; + constexpr Word TRAP_CAUSE_ILLEGAL_INSTR = 2; + constexpr Word TRAP_CAUSE_BREAKPOINT = 3; + constexpr Word TRAP_CAUSE_ECALL_MMODE = 11; } void Scheduler::raise_trap(uint32_t wid, Word cause, Word trap_pc) { @@ -390,6 +397,27 @@ void Scheduler::trigger_ebreak(uint32_t wid, Word trap_pc) { this->raise_trap(wid, TRAP_CAUSE_BREAKPOINT, trap_pc); } +void Scheduler::trigger_illegal(uint32_t wid, Word trap_pc, uint32_t code) { + if (warps_.at(wid).mtvec == 0) { + std::cerr << "Error: illegal instruction (mcause=" << TRAP_CAUSE_ILLEGAL_INSTR + << ") with no trap handler (mtvec=0): " << core_->name() << ", wid=" << wid + << ", PC=0x" << std::hex << trap_pc << ", instr=0x" << code << std::dec << std::endl; + std::abort(); + } + this->raise_trap(wid, TRAP_CAUSE_ILLEGAL_INSTR, trap_pc); +} + +void Scheduler::trigger_misaligned_fetch(uint32_t wid, Word trap_pc, uint32_t code, Word target) { + if (warps_.at(wid).mtvec == 0) { + std::cerr << "Error: instruction address misaligned (mcause=" << TRAP_CAUSE_MISALIGNED_FETCH + << ") with no trap handler (mtvec=0): " << core_->name() << ", wid=" << wid + << ", PC=0x" << std::hex << trap_pc << ", instr=0x" << code + << ", target=0x" << target << std::dec << std::endl; + std::abort(); + } + this->raise_trap(wid, TRAP_CAUSE_MISALIGNED_FETCH, trap_pc); +} + #ifdef VX_CFG_EXT_RASTER_ENABLE /////////////////////////////////////////////////////////////////////////////// // Fragment Work Distributor (FWD). diff --git a/sim/simx/scheduler.h b/sim/simx/scheduler.h index f2c21b8a01..a2ad54f0c8 100644 --- a/sim/simx/scheduler.h +++ b/sim/simx/scheduler.h @@ -39,7 +39,16 @@ constexpr Word MSTATUS_MIE = Word(1) << VX_CSR_MSTATUS_MIE; constexpr Word MSTATUS_MPIE = Word(1) << VX_CSR_MSTATUS_MPIE; constexpr Word MSTATUS_MPP_M = Word(3) << VX_CSR_MSTATUS_MPP; constexpr Word MSTATUS_FS = Word(3) << VX_CSR_MSTATUS_FS; +constexpr Word MSTATUS_SD = Word(1) << (VX_CFG_XLEN - 1); +// FS resets to Initial so that FP code runs without first writing mstatus. +// Without F, FS is read-only zero. +#ifdef VX_CFG_EXT_F_ENABLE +constexpr Word MSTATUS_FS_RESET = Word(1) << VX_CSR_MSTATUS_FS; constexpr Word MSTATUS_WMASK = MSTATUS_MIE | MSTATUS_MPIE | MSTATUS_FS; +#else +constexpr Word MSTATUS_FS_RESET = 0; +constexpr Word MSTATUS_WMASK = MSTATUS_MIE | MSTATUS_MPIE; +#endif constexpr Word MTVEC_WMASK = ~Word(3); #ifdef VX_CFG_EXT_C_ENABLE constexpr Word MEPC_WMASK = ~Word(1); @@ -102,8 +111,8 @@ struct warp_t { Word mscratch; // Per-warp machine-mode trap CSRs, held masked to their writable bits. - // Synchronous ECALL/EBREAK traps redirect the warp PC to mtvec and - // snapshot the faulting PC/cause here; MRET restores the PC from mepc. + // Synchronous traps redirect the warp PC to mtvec and snapshot the + // faulting PC/cause here; MRET restores the PC from mepc. // See Scheduler::raise_trap. Word mstatus = 0; Word mtvec = 0; @@ -123,7 +132,7 @@ struct warp_t { warp_t(uint32_t num_threads); - void reset(); + void reset(bool launch); }; // Per-core warp lifecycle owner: holds warp register state, barrier state, @@ -198,13 +207,18 @@ class Scheduler : public SimObject { // ----- Trap helpers ----- // Synchronous trap entry: snapshot the faulting PC into mepc, set - // mcause, and redirect the warp PC to mtvec. trap_pc is the PC of the - // faulting instruction (trace->PC), not the decode-advanced warp.PC. + // mcause, clear mtval, and redirect the warp PC to mtvec. trap_pc is the PC + // of the faulting instruction (trace->PC), not the decode-advanced warp.PC. void raise_trap(uint32_t wid, Word cause, Word trap_pc); // Trap return: restore the warp PC from mepc (MRET/SRET/URET). void mret(uint32_t wid); void trigger_ecall(uint32_t wid, Word trap_pc); void trigger_ebreak(uint32_t wid, Word trap_pc); + // A fault taken while the warp has no handler (mtvec=0) aborts the + // simulation and reports the instruction (`code`). + void trigger_illegal(uint32_t wid, Word trap_pc, uint32_t code); + // A taken jump or branch to `target`, which is not instruction-aligned. + void trigger_misaligned_fetch(uint32_t wid, Word trap_pc, uint32_t code, Word target); // ----- Accessors ----- warp_t& warp(uint32_t wid) { return warps_.at(wid); } diff --git a/sim/simx/sfu_unit.cpp b/sim/simx/sfu_unit.cpp index 46d2cd2d9c..a6e44c1151 100644 --- a/sim/simx/sfu_unit.cpp +++ b/sim/simx/sfu_unit.cpp @@ -546,6 +546,12 @@ void SfuUnit::on_tick() { } else if (is_csr) { csr_unit_->process(trace); csr_read_done_.at(b) = nullptr; + // An mstatus access holds its warp from decode; it is released as the + // request fires, not when its result retires. + if (trace->fetch_stall && trace->eop) { + warp_ctl_out.send(WarpCtl{trace->wid, false}, 1); + release_warp = false; + } #ifdef VX_CFG_EXT_DXA_ENABLE } else if (std::get_if(&trace->op_type)) { // process() returns nullptr on backpressure (idempotent retry next diff --git a/sim/simx/types.h b/sim/simx/types.h index 7b71a8a4d4..2dae6df00a 100644 --- a/sim/simx/types.h +++ b/sim/simx/types.h @@ -275,6 +275,7 @@ enum class BrType { JAL, JALR, SYS, + ILLEGAL, }; struct IntrBrArgs { @@ -289,6 +290,7 @@ inline std::ostream &operator<<(std::ostream &os, const BrType& type) { case BrType::JAL: os << "JAL"; break; case BrType::JALR: os << "JALR"; break; case BrType::SYS: os << "SYS"; break; + case BrType::ILLEGAL: os << "ILLEGAL"; break; default: assert(false); } diff --git a/tests/regression/Makefile b/tests/regression/Makefile index 1dabd029c6..d81a8d4912 100644 --- a/tests/regression/Makefile +++ b/tests/regression/Makefile @@ -17,7 +17,7 @@ TESTS := \ async_barrier async_gbarrier gbar_phase bar_slot_phase packld wgather wsync quad_lod \ occupancy multikernel reopen module_reload single_cta \ flush_coherence lazy_fetch sector_merge raw_order cache_ways chase \ - vm_test vm_fault vm_stress + vm_test vm_fault vm_stress trap_illegal # --- common exclude list --------------------------------------------- EXCLUDE := diff --git a/tests/regression/trap_illegal/Makefile b/tests/regression/trap_illegal/Makefile new file mode 100644 index 0000000000..add2f8ef18 --- /dev/null +++ b/tests/regression/trap_illegal/Makefile @@ -0,0 +1,14 @@ +ROOT_DIR := $(realpath ../../..) +include $(ROOT_DIR)/config.mk + +PROJECT := trap_illegal + +SRC_DIR := $(VORTEX_HOME)/tests/regression/$(PROJECT) + +SRCS := $(SRC_DIR)/main.cpp + +VX_SRCS := $(SRC_DIR)/kernel.cpp + +KERNEL_LIB := vortex2 + +include ../common.mk diff --git a/tests/regression/trap_illegal/common.h b/tests/regression/trap_illegal/common.h new file mode 100644 index 0000000000..3d231d8b68 --- /dev/null +++ b/tests/regression/trap_illegal/common.h @@ -0,0 +1,36 @@ +#ifndef _COMMON_H_ +#define _COMMON_H_ + +#include + +// One result per probe: the trap cause (-1 if none) and the faulting pc +// relative to the probed instruction. +enum { + PROBE_FS_OFF_FADD = 0, // FP instructions with mstatus.FS=Off + PROBE_FS_OFF_FLW, + PROBE_FS_OFF_FFLAGS, + PROBE_FS_OFF_FMV, + PROBE_BRANCH_FUNCT3_2, // reserved encodings + PROBE_BRANCH_FUNCT3_3, + PROBE_SYSTEM_RESERVED, + PROBE_SYSTEM_FUNCT3_4, + PROBE_WFI, // runs as a no-op + NUM_PROBES, +}; + +typedef struct { + int64_t cause; + int64_t epc_offset; +} probe_t; + +typedef struct { + probe_t probes[NUM_PROBES]; + uint64_t mstatus_initial; // FS set to Initial, before any FP write + uint64_t mstatus_dirty; // after an FP write +} trap_out_t; + +typedef struct { + uint64_t dst_addr; +} kernel_arg_t; + +#endif diff --git a/tests/regression/trap_illegal/kernel.cpp b/tests/regression/trap_illegal/kernel.cpp new file mode 100644 index 0000000000..c31ce52068 --- /dev/null +++ b/tests/regression/trap_illegal/kernel.cpp @@ -0,0 +1,73 @@ +#include +#include "common.h" + +// Runs insn under a local trap handler, after the setup instructions pre. The +// handler records mcause and mepc relative to insn, then returns past it. FS +// goes back to Initial and mtvec to its old value afterwards. +#define TRAP_PROBE(pre, insn, cause, epc_offset) \ + __asm__ volatile( \ + ".option push\n\t" \ + ".option norvc\n\t" \ + "li %0, -1\n\t" \ + "li %1, -1\n\t" \ + "la t0, 1f\n\t" \ + "csrrw t1, mtvec, t0\n\t" \ + pre \ + "2:\n\t" \ + insn "\n\t" \ + "j 3f\n\t" \ + ".align 2\n" \ + "1:\n\t" \ + "csrr %0, mcause\n\t" \ + "csrr t0, mepc\n\t" \ + "la t2, 2b\n\t" \ + "sub %1, t0, t2\n\t" \ + "addi t0, t0, 4\n\t" \ + "csrw mepc, t0\n\t" \ + "mret\n" \ + "3:\n\t" \ + "csrw mtvec, t1\n\t" \ + "li t2, 0x2000\n\t" \ + "csrs mstatus, t2\n\t" \ + ".option pop" \ + : "=&r"(cause), "=&r"(epc_offset) \ + : \ + : "t0", "t1", "t2", "ft0", "memory") + +__kernel void kernel_main(kernel_arg_t* __UNIFORM__ arg) { + auto out = reinterpret_cast(arg->dst_addr); + + long cause[NUM_PROBES], epc_offset[NUM_PROBES]; +#define FS_OFF "li t2, 0x6000\n\tcsrc mstatus, t2\n" + TRAP_PROBE(FS_OFF, "fadd.s ft0, ft0, ft0", cause[PROBE_FS_OFF_FADD], epc_offset[PROBE_FS_OFF_FADD]); + TRAP_PROBE(FS_OFF, "flw ft0, 0(sp)", cause[PROBE_FS_OFF_FLW], epc_offset[PROBE_FS_OFF_FLW]); + TRAP_PROBE(FS_OFF, "csrr t0, fflags", cause[PROBE_FS_OFF_FFLAGS], epc_offset[PROBE_FS_OFF_FFLAGS]); + TRAP_PROBE(FS_OFF, "fmv.x.w t0, ft0", cause[PROBE_FS_OFF_FMV], epc_offset[PROBE_FS_OFF_FMV]); + // beq-form x0, x0, +8 with funct3 2 and 3 + TRAP_PROBE("", ".word 0x00002463", cause[PROBE_BRANCH_FUNCT3_2], epc_offset[PROBE_BRANCH_FUNCT3_2]); + TRAP_PROBE("", ".word 0x00003463", cause[PROBE_BRANCH_FUNCT3_3], epc_offset[PROBE_BRANCH_FUNCT3_3]); + // SYSTEM, funct3 0, imm12 0x7b2 (DRET, a debug-mode instruction) + TRAP_PROBE("", ".word 0x7b200073", cause[PROBE_SYSTEM_RESERVED], epc_offset[PROBE_SYSTEM_RESERVED]); + // SYSTEM, funct3 4 (HLV.B, a hypervisor load) + TRAP_PROBE("", ".word 0x60004073", cause[PROBE_SYSTEM_FUNCT3_4], epc_offset[PROBE_SYSTEM_FUNCT3_4]); + TRAP_PROBE("", "wfi", cause[PROBE_WFI], epc_offset[PROBE_WFI]); + for (int i = 0; i < NUM_PROBES; ++i) { + out->probes[i].cause = cause[i]; + out->probes[i].epc_offset = epc_offset[i]; + } + + long ms_initial, ms_dirty; + __asm__ volatile( + "li t0, 0x6000\n\t" + "csrc mstatus, t0\n\t" + "li t0, 0x2000\n\t" + "csrs mstatus, t0\n\t" + "csrr %0, mstatus\n\t" + "fadd.s ft0, ft0, ft0\n\t" + "csrr %1, mstatus" + : "=&r"(ms_initial), "=&r"(ms_dirty) + : + : "t0", "ft0"); + out->mstatus_initial = (unsigned long)ms_initial; + out->mstatus_dirty = (unsigned long)ms_dirty; +} diff --git a/tests/regression/trap_illegal/main.cpp b/tests/regression/trap_illegal/main.cpp new file mode 100644 index 0000000000..25a32afaf6 --- /dev/null +++ b/tests/regression/trap_illegal/main.cpp @@ -0,0 +1,123 @@ +// Illegal-instruction traps raised at decode: FP instructions, FP loads, FP +// CSR accesses and FP moves with mstatus.FS=Off, and reserved branch and +// SYSTEM encodings, each at the faulting pc; WFI runs without trapping. An FP +// write moves FS from Initial to Dirty and sets SD. +#include +#include "common.h" +#include +#include +#include +#include +#include + +#define CHECK(expr) do { \ + vx_result_t _r = (expr); \ + if (_r != VX_SUCCESS) { \ + std::fprintf(stderr, "FAIL %s:%d: '%s' returned %s\n", \ + __FILE__, __LINE__, #expr, vx_result_string(_r)); \ + std::exit(1); \ + } \ +} while (0) + +namespace { + +constexpr int64_t CAUSE_ILLEGAL_INSTR = 2; +constexpr uint64_t MSTATUS_FS_SHIFT = 13; +constexpr uint64_t FS_INITIAL = 1, FS_DIRTY = 3; + +const char* probe_names[] = { + "fadd.s with FS=Off", "flw with FS=Off", "csrr fflags with FS=Off", "fmv.x.w with FS=Off", + "branch funct3=2", "branch funct3=3", "reserved SYSTEM", "SYSTEM funct3=4", "wfi", +}; + +} // namespace + +int main(int argc, char** argv) { + const char* kernel_file = "kernel.vxbin"; + int c; + while ((c = getopt(argc, argv, "k:h")) != -1) { + switch (c) { + case 'k': kernel_file = optarg; break; + default: + std::cout << "Usage: [-k kernel] [-h]" << std::endl; + return (c == 'h') ? 0 : -1; + } + } + + vx_device_h dev = nullptr; + CHECK(vx_device_open(0, &dev)); + uint64_t isa = 0; + CHECK(vx_device_query(dev, VX_CAPS_ISA_FLAGS, &isa)); + uint32_t xlen = VX_ISA_ARCH(isa); + + vx_queue_h q = nullptr; + vx_queue_info_t qi = { sizeof(qi), nullptr, VX_QUEUE_PRIORITY_NORMAL, 0 }; + CHECK(vx_queue_create(dev, &qi, &q)); + vx_module_h mod = nullptr; + vx_kernel_h kern = nullptr; + CHECK(vx_module_load_file(dev, kernel_file, &mod)); + CHECK(vx_module_get_kernel(mod, "main", &kern)); + + trap_out_t out{}; + vx_buffer_h dst = nullptr; + kernel_arg_t arg{}; + CHECK(vx_buffer_create(dev, sizeof(out), VX_MEM_WRITE, &dst)); + CHECK(vx_buffer_address(dst, &arg.dst_addr)); + + // One thread: mstatus, mtvec and mepc are per warp. + vx_launch_info_t li{}; + li.struct_size = sizeof(li); + li.kernel = kern; + li.args_host = &arg; + li.args_size = sizeof(arg); + li.ndim = 1; + li.grid_dim[0] = 1; + li.block_dim[0]= 1; + vx_event_h launch_ev = nullptr, read_ev = nullptr; + CHECK(vx_enqueue_launch(q, &li, 0, nullptr, &launch_ev)); + CHECK(vx_enqueue_read(q, &out, dst, 0, sizeof(out), 1, &launch_ev, &read_ev)); + CHECK(vx_event_wait_value(read_ev, 1, VX_TIMEOUT_INFINITE)); + vx_event_release(read_ev); + vx_event_release(launch_ev); + + int errors = 0; + for (int i = 0; i < NUM_PROBES; ++i) { + const probe_t& p = out.probes[i]; + int64_t expected = (i == PROBE_WFI) ? -1 : CAUSE_ILLEGAL_INSTR; + int64_t expected_offset = (i == PROBE_WFI) ? -1 : 0; + if (p.cause != expected || p.epc_offset != expected_offset) { + std::printf("*** %s: expected mcause %lld, mepc offset %lld; actual mcause %lld, mepc offset %lld\n", + probe_names[i], (long long)expected, (long long)expected_offset, + (long long)p.cause, (long long)p.epc_offset); + ++errors; + } + } + + auto fs = [](uint64_t ms) { return (ms >> MSTATUS_FS_SHIFT) & 3; }; + auto sd = [xlen](uint64_t ms) { return (ms >> (xlen - 1)) & 1; }; + if (fs(out.mstatus_initial) != FS_INITIAL || sd(out.mstatus_initial) != 0) { + std::printf("*** before an FP write: expected FS=%llu SD=0, actual FS=%llu SD=%llu (mstatus=0x%llx)\n", + (unsigned long long)FS_INITIAL, (unsigned long long)fs(out.mstatus_initial), + (unsigned long long)sd(out.mstatus_initial), (unsigned long long)out.mstatus_initial); + ++errors; + } + if (fs(out.mstatus_dirty) != FS_DIRTY || sd(out.mstatus_dirty) != 1) { + std::printf("*** after an FP write: expected FS=%llu SD=1, actual FS=%llu SD=%llu (mstatus=0x%llx)\n", + (unsigned long long)FS_DIRTY, (unsigned long long)fs(out.mstatus_dirty), + (unsigned long long)sd(out.mstatus_dirty), (unsigned long long)out.mstatus_dirty); + ++errors; + } + + vx_buffer_release(dst); + vx_kernel_release(kern); + vx_module_release(mod); + vx_queue_release(q); + vx_device_release(dev); + + if (errors) { + std::cout << "Found " << errors << " errors!\nFAILED!" << std::endl; + return 1; + } + std::cout << "PASSED!" << std::endl; + return 0; +} diff --git a/tests/riscv/isa/Makefile b/tests/riscv/isa/Makefile index dd0f6f968d..656c09052b 100644 --- a/tests/riscv/isa/Makefile +++ b/tests/riscv/isa/Makefile @@ -52,19 +52,17 @@ endif # coherence), ma_data (no misaligned-access trap), rv64uc-p-rvc (misaligned # C.FLD the upstream test expects to trap on). ISA_SKIP := *-fence_i|*-ma_data|rv64uc-p-rvc -# The machine-mode suites run mcsr, scall and sbreak. Only ECALL/EBREAK trap -# today, so the tests that need another trap source or an unmodelled CSR are -# skipped: -# illegal, shamt - illegal-instruction trap -# csr - mstatus.FS=0 must make FP stores trap -# ma_fetch, ma_addr, -# *-misaligned - misaligned fetch/load/store traps +# The machine-mode suites run mcsr, scall, sbreak, illegal, shamt and +# ma_fetch. The tests that need a trap source or a CSR Vortex does not model +# are skipped: +# csr - tests 14-15 need misa.U cleared or an +# unimplemented-CSR trap +# ma_addr, *-misaligned - misaligned load/store traps # breakpoint - Sdtrig trigger CSRs (tselect, tdata1/2, tcontrol) # zicntr - user counters cycle/instret (Zicntr) # instret_overflow - writable minstret and mcountinhibit # pmpaddr - PMP -ISA_SKIP := $(ISA_SKIP)|rv*mi-p-illegal|rv*mi-p-shamt|rv*mi-p-csr -ISA_SKIP := $(ISA_SKIP)|rv*mi-p-ma_fetch|rv*mi-p-ma_addr|rv*mi-p-*-misaligned +ISA_SKIP := $(ISA_SKIP)|rv*mi-p-csr|rv*mi-p-ma_addr|rv*mi-p-*-misaligned ISA_SKIP := $(ISA_SKIP)|rv*mi-p-breakpoint|rv*mi-p-zicntr|rv*mi-p-instret_overflow ISA_SKIP := $(ISA_SKIP)|rv*mi-p-pmpaddr diff --git a/tests/unittest/rvc_decompress/main.cpp b/tests/unittest/rvc_decompress/main.cpp index 7c80ec037d..0f8317b62d 100644 --- a/tests/unittest/rvc_decompress/main.cpp +++ b/tests/unittest/rvc_decompress/main.cpp @@ -85,8 +85,26 @@ int main() { expect_illegal("c.addw", 0x9d25); #endif - // C.SLLI with rd=x0 is reserved at both widths. - expect_illegal("c.slli rd=x0", 0x0002); + // Code points with rd=x0 are HINTs: they expand to a write of x0. + expect_expansion("c.slli rd=x0", 0x0002, 0x00001013); + expect_expansion("c.lui rd=x0", 0x6005, 0x00001037); + expect_expansion("c.mv rd=x0", 0x8006, 0x00100033); + expect_expansion("c.add rd=x0", 0x9006, 0x00100033); + + // Reserved code points. + expect_illegal("c.lui imm=0", 0x6081); + expect_illegal("c.addi16sp imm=0", 0x6101); + expect_illegal("c.jr rs1=x0", 0x8002); + + // C.FLD/C.FSD and their SP forms exist only with 64-bit FP registers. +#ifdef VX_CFG_FLEN_64 + expect_expansion("c.fld", 0x2000, 0x00043407); +#else + expect_illegal("c.fld", 0x2000); + expect_illegal("c.fsd", 0xa000); + expect_illegal("c.fldsp", 0x2002); + expect_illegal("c.fsdsp", 0xa002); +#endif if (failures != 0) { printf("FAILED: %d rvc_decompress case(s)\n", failures);