From 9effee27b675a0bb7628bbdc85af59fc5d94032c Mon Sep 17 00:00:00 2001 From: jel221 Date: Tue, 17 Nov 2026 00:32:18 -0800 Subject: [PATCH 1/2] Order trap CSR writes before ECALL/EBREAK/MRET; hold mstatus/mtvec once per core, narrow mcause, make mtval read-only zero --- CHANGELOG.md | 7 + ci/testcases/core.yaml | 24 ++++ docs/designs/trap_and_exception_foundation.md | 45 +++--- hw/rtl/VX_gpu_pkg.sv | 19 ++- hw/rtl/core/VX_alu_int.sv | 4 +- hw/rtl/core/VX_compute_slice.sv | 14 +- hw/rtl/core/VX_core.sv | 20 +++ hw/rtl/core/VX_csr_data.sv | 37 +++-- hw/rtl/core/VX_decode.sv | 6 + hw/rtl/core/VX_frontend_xbar.sv | 13 +- hw/rtl/core/VX_scheduler.sv | 29 ++++ hw/rtl/core/VX_warp_sched.sv | 31 ++-- hw/rtl/interfaces/VX_branch_ctl_if.sv | 2 +- hw/rtl/interfaces/VX_sched_csr_if.sv | 31 ++-- hw/rtl/interfaces/VX_slice_sched_if.sv | 5 + sim/simx/csr_unit.cpp | 22 +-- sim/simx/decode.cpp | 4 + sim/simx/instr.h | 7 +- sim/simx/scheduler.cpp | 13 +- sim/simx/scheduler.h | 22 ++- tests/regression/Makefile | 2 +- tests/regression/trap/Makefile | 16 +++ tests/regression/trap/common.h | 16 +++ tests/regression/trap/kernel.cpp | 53 +++++++ tests/regression/trap/main.cpp | 134 ++++++++++++++++++ 25 files changed, 470 insertions(+), 106 deletions(-) create mode 100644 tests/regression/trap/Makefile create mode 100644 tests/regression/trap/common.h create mode 100644 tests/regression/trap/kernel.cpp create mode 100644 tests/regression/trap/main.cpp diff --git a/CHANGELOG.md b/CHANGELOG.md index 57c58a52cb..282c5de693 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -59,6 +59,9 @@ follows the version pins recorded in [VERSION](VERSION) (`VORTEX_VERSION`, ### Changed +- **Machine-mode trap CSRs.** `mstatus` and `mtvec` are one per core, held by the core scheduler and shared by every warp; `mcause` + holds the 4-bit exception codes the core raises; `mtval` reads as zero. `mepc` and `mcause` stay per warp. Per-warp trap state + drops from 160 to 36 bits at XLEN 32. - **Toolchain pinned to v3.0.2** (`TOOLCHAIN_REV`): LLVM f44a6bb2af4c, with PoCL rebuilt on it. Wide loads/stores and 64-bit atomics stay behind `+xvortexldstwide` and `+xvortexamod`, so code built with plain `+xvortex` uses no new instructions. - **Dcache read-merge pairs realign.** A pair of same-word merge ports whose parts a bank served a cycle apart waits for its partner, @@ -107,6 +110,10 @@ follows the version pins recorded in [VERSION](VERSION) (`VORTEX_VERSION`, ### Fixed +- **Trap CSR writes before ECALL, EBREAK and MRET.** A `csrw mepc` in a trap handler could still be pending when the following `mret` + read `mepc`, returning the warp to the trapping instruction. ECALL, EBREAK and MRET now wait for pending writes to `mstatus`, `mtvec` + and `mepc`, in SimX and RTL. New regression test `trap` and parity cases + `core:parity-trap-fw4`, `core:parity-trap-cs4` and `core:parity-trap-fw2-sfu4`. - **ASIC core and top at 800 MHz.** On ASAP7 the core reached 655 MHz and top 654 MHz against the 800 MHz catalog clock, held by three datapaths: the FDIV/FSQRT result stage, the FMA's accumulate and rounding stages, and the integer multiplier. Each is retimed with no change in latency, so SimX and every cycle count are unchanged. FDIV/FSQRT decides the subnormal shift and tests diff --git a/ci/testcases/core.yaml b/ci/testcases/core.yaml index 2855bed755..97bb62b41b 100644 --- a/ci/testcases/core.yaml +++ b/ci/testcases/core.yaml @@ -273,6 +273,30 @@ tests: via: blackbox app: printf configs: -DVX_CFG_NUM_THREADS=16 -DVX_CFG_NUM_WARPS=16 -DVX_CFG_ISSUE_WIDTH=4 -DVX_CFG_FETCH_WIDTH=2 +# Synchronous traps from every warp across four frontend slices: each +# handler's mepc write must be seen by its MRET, and each warp keeps its own +# return PC and cause. +- id: parity-trap-fw4 + check: model_parity + via: blackbox + app: trap + args: -n512 -i8 + configs: -DVX_CFG_NUM_THREADS=4 -DVX_CFG_NUM_WARPS=16 -DVX_CFG_ISSUE_WIDTH=4 -DVX_CFG_FETCH_WIDTH=4 +# The per-core mtvec written by one warp and read by all, across four compute +# slices with an SFU block each, and across two frontend slices served by four +# SFU blocks. +- id: parity-trap-cs4 + check: model_parity + via: blackbox + app: trap + args: -n512 -i8 + configs: -DVX_CFG_NUM_THREADS=4 -DVX_CFG_NUM_WARPS=16 -DVX_CFG_ISSUE_WIDTH=4 -DVX_CFG_FETCH_WIDTH=4 -DVX_CFG_NUM_SFU_BLOCKS=4 +- id: parity-trap-fw2-sfu4 + check: model_parity + via: blackbox + app: trap + args: -n512 -i8 + configs: -DVX_CFG_NUM_THREADS=4 -DVX_CFG_NUM_WARPS=16 -DVX_CFG_ISSUE_WIDTH=4 -DVX_CFG_FETCH_WIDTH=2 -DVX_CFG_NUM_SFU_BLOCKS=4 # The same fences on a data cache shared by two cores, below an L2: each core's # token rides its own flush injection, and every fence walks the data cache. - id: parity-printf-ss2 diff --git a/docs/designs/trap_and_exception_foundation.md b/docs/designs/trap_and_exception_foundation.md index e53820d83a..13a5bbb033 100644 --- a/docs/designs/trap_and_exception_foundation.md +++ b/docs/designs/trap_and_exception_foundation.md @@ -22,31 +22,40 @@ 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`, +The M-mode trap CSRs are held by the schedulers. Every warp runs the same +kernel and trap handler, so `mstatus` and `mtvec` are one per core, in the +core scheduler ([`VX_scheduler.sv`](../../hw/rtl/core/VX_scheduler.sv)); +the CSR unit reads and writes them in the fire cycle, as it does `satp`. +`mepc` and `mcause`, which concurrent traps in different warps need, stay +per warp in the warp schedulers +([`VX_warp_sched.sv`](../../hw/rtl/core/VX_warp_sched.sv)); `mcause` holds +only the 4-bit exception codes the core raises. `mtval` reads as zero: no +exception the core raises carries a trap value. Addresses: `MSTATUS=0x300`, `MTVEC=0x305`, `MSCRATCH=0x340`, `MEPC=0x341`, `MCAUSE=0x342`, `MTVAL=0x343` ([`VX_types.toml:325-334`](../../VX_types.toml#L325)). The branch unit is the trap producer -([`VX_alu_int.sv:310-339`](../../hw/rtl/core/VX_alu_int.sv#L310)): it -raises `is_trap_entry` / `is_mret_op` with a `br_trap_cause` (`EBREAK=3`, +([`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)). +warp scheduler ([`VX_warp_sched.sv`](../../hw/rtl/core/VX_warp_sched.sv)) +redirects the warp: a trap goes to the core's `mtvec & ~3`, an `mret` +returns to the warp's `mepc`. On trap entry the hardware snapshots +`mepc`/`mcause`; a same-cycle software `csrw` and a hardware trap entry are +ordered hardware-wins. The CSR file +([`VX_csr_data.sv`](../../hw/rtl/core/VX_csr_data.sv)) does the read mux +and routes writes back via `sched_csr_if`. + +ECALL, EBREAK and MRET depend on the warp's pending writes to `mstatus`, +`mtvec` and `mepc` through the scoreboard, so a handler's `csrw mepc` lands +before its `mret` reads `mepc`, and a warp's own `mtvec` write before its +trap redirects through it. 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 +[`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. The old "kill all warps" (`active_warps_.reset()`) hack is gone. @@ -90,7 +99,7 @@ The named **preemption** capability is future work: fault causes (2/4/6). 3. **Supervisor mode** — `stvec`/`sepc`/`scause` + `medeleg`-driven redirect. -4. **Interrupts** — `mie`/`mip`, async injection, `mcause` MSB. +4. **Interrupts** — `mie`/`mip`, async injection, `mcause` MSB (widening `mcause` past the 4 exception-code bits it holds today). 5. **Register-file / IPDOM-stack save-restore** — required for true context switch (only the trap CSRs are saved today). 6. **`tests/regression/csr_smoke/`** unit test — absent. diff --git a/hw/rtl/VX_gpu_pkg.sv b/hw/rtl/VX_gpu_pkg.sv index 9025804269..b1208602e0 100644 --- a/hw/rtl/VX_gpu_pkg.sv +++ b/hw/rtl/VX_gpu_pkg.sv @@ -70,7 +70,8 @@ package VX_gpu_pkg; // these bits. localparam XREG_0 = 0; localparam XREG_1 = 1; - localparam NUM_XREGS = 2; + localparam XREG_2 = 2; + localparam NUM_XREGS = 3; localparam DV_STACK_SIZE = `UP(`VX_CFG_NUM_THREADS-1); localparam DV_STACK_SIZEW = `LOG2UP(DV_STACK_SIZE); @@ -639,8 +640,15 @@ package VX_gpu_pkg; localparam ISSUE_WIS_BITS = `CLOG2(PER_ISSUE_WARPS); localparam ISSUE_WIS_W = `UP(ISSUE_WIS_BITS); - // Machine-mode trap CSRs stored per warp in the scheduler: mstatus, mtvec, mepc, mcause, mtval. - localparam NUM_TRAP_CSRS = 5; + // Per-warp machine-mode trap CSRs, held by the warp schedulers: the return + // PC and cause, which concurrent traps in different warps need. Every warp + // runs the same kernel and trap handler, so mstatus and mtvec are one per + // core, held by the core scheduler. + localparam NUM_TRAP_CSRS = 2; + localparam TRAP_CSR_MEPC = 0; + localparam TRAP_CSR_MCAUSE = 1; + // mcause holds only the exception codes the core raises (ECALL, EBREAK). + localparam TRAP_CAUSE_BITS = 4; localparam DISPATCH_QSIZE = `VX_CFG_DISPATCH_QUEUE_SIZE; @@ -1362,7 +1370,7 @@ package VX_gpu_pkg; logic [PC_BITS-1:0] dest; logic is_trap; logic is_mret; - logic [3:0] trap_cause; + logic [TRAP_CAUSE_BITS-1:0] trap_cause; } sched_branch_t; // SFU warp control @@ -1390,11 +1398,8 @@ package VX_gpu_pkg; typedef struct packed { logic [`VX_CFG_NUM_THREADS-1:0] thread_mask; logic [`VX_CFG_MEM_ADDR_WIDTH-1:0] mscratch; - logic [`VX_CFG_XLEN-1:0] mstatus; - logic [`VX_CFG_XLEN-1:0] mtvec; logic [`VX_CFG_XLEN-1:0] mepc; logic [`VX_CFG_XLEN-1:0] mcause; - logic [`VX_CFG_XLEN-1:0] mtval; logic [NW_WIDTH-1:0] cta_rank; cta_lane_t [`VX_CFG_NUM_THREADS-1:0] cta_lane; } sched_csr_rd_t; diff --git a/hw/rtl/core/VX_alu_int.sv b/hw/rtl/core/VX_alu_int.sv index e6c05158a6..7b10a4cf4b 100644 --- a/hw/rtl/core/VX_alu_int.sv +++ b/hw/rtl/core/VX_alu_int.sv @@ -343,7 +343,7 @@ module VX_alu_int import VX_gpu_pkg::*; #( // 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 [TRAP_CAUSE_BITS-1:0] br_trap_cause = (br_op_r == INST_BR_EBREAK) ? TRAP_CAUSE_BITS'(3) : TRAP_CAUSE_BITS'(11); // EBREAK / ECALL_M wire [`VX_CFG_XLEN-1:0] br_result = alu_result_r[last_tid_r]; wire is_less = br_result[0]; @@ -363,7 +363,7 @@ module VX_alu_int import VX_gpu_pkg::*; #( end VX_pipe_register #( - .DATAW (1 + NW_WIDTH + 1 + PC_BITS + 1 + 1 + 4), + .DATAW (1 + NW_WIDTH + 1 + PC_BITS + 1 + 1 + TRAP_CAUSE_BITS), .RESETW (1) ) branch_reg ( .clk (clk), diff --git a/hw/rtl/core/VX_compute_slice.sv b/hw/rtl/core/VX_compute_slice.sv index 4d8c0b6619..1d45212ff8 100644 --- a/hw/rtl/core/VX_compute_slice.sv +++ b/hw/rtl/core/VX_compute_slice.sv @@ -57,6 +57,11 @@ import VX_tcu_pkg::*; output wire satp_wr_valid, output wire [`VX_CFG_XLEN-1:0] satp_wr_data, `endif + input wire [`VX_CFG_XLEN-1:0] mstatus, + input wire [`VX_CFG_XLEN-1:0] mtvec, + output wire mstatus_wr_valid, + output wire mtvec_wr_valid, + output wire [`VX_CFG_XLEN-1:0] mtrap_wr_data, // LSU `ifdef PERF_ENABLE @@ -159,6 +164,7 @@ import VX_tcu_pkg::*; assign fs_sched_if.bar_unlock_valid = slice_sched_if.bar_unlock_valid; assign fs_sched_if.bar_unlock_mask = slice_sched_if.bar_unlock_mask; assign fs_sched_if.slice_ibuf_full = slice_sched_if.slice_ibuf_full; + assign fs_sched_if.mtvec = slice_sched_if.mtvec; `ifdef EXT_SCHED_TIMEOUT_DUMP assign fs_sched_if.timeout_dump = slice_sched_if.timeout_dump; `endif @@ -255,11 +261,8 @@ import VX_tcu_pkg::*; assign sched_csr_if.active_warps = active_warps; assign sched_csr_if.thread_mask = csr_rd[0].thread_mask; assign sched_csr_if.mscratch = csr_rd[0].mscratch; - assign sched_csr_if.csr_mstatus = csr_rd[0].mstatus; - assign sched_csr_if.csr_mtvec = csr_rd[0].mtvec; assign sched_csr_if.csr_mepc = csr_rd[0].mepc; assign sched_csr_if.csr_mcause = csr_rd[0].mcause; - assign sched_csr_if.csr_mtval = csr_rd[0].mtval; assign sched_csr_if.cta_rank = csr_rd[0].cta_rank; assign sched_csr_if.cta_lane = csr_rd[0].cta_lane; `ifdef VX_CFG_VM_ENABLE @@ -267,6 +270,11 @@ import VX_tcu_pkg::*; assign satp_wr_valid = sched_csr_if.satp_wr_valid; assign satp_wr_data = sched_csr_if.satp_wr_data; `endif + assign sched_csr_if.csr_mstatus = mstatus; + assign sched_csr_if.csr_mtvec = mtvec; + assign mstatus_wr_valid = sched_csr_if.mstatus_wr_valid; + assign mtvec_wr_valid = sched_csr_if.mtvec_wr_valid; + assign mtrap_wr_data = sched_csr_if.mtrap_wr_data; `ifdef VX_CFG_EXT_RTU_ENABLE assign rtu_unlock_valid[0] = sched_unlock_if.valid; diff --git a/hw/rtl/core/VX_core.sv b/hw/rtl/core/VX_core.sv index 14696e0825..3141aead5e 100644 --- a/hw/rtl/core/VX_core.sv +++ b/hw/rtl/core/VX_core.sv @@ -295,6 +295,11 @@ import VX_tcu_pkg::*; wire [`VX_CFG_NUM_SFU_BLOCKS-1:0] satp_wr_valid; wire [`VX_CFG_NUM_SFU_BLOCKS-1:0][`VX_CFG_XLEN-1:0] satp_wr_data; `endif + wire [`VX_CFG_NUM_SFU_BLOCKS-1:0][`VX_CFG_XLEN-1:0] sched_mstatus; + wire [`VX_CFG_NUM_SFU_BLOCKS-1:0][`VX_CFG_XLEN-1:0] sched_mtvec; + wire [`VX_CFG_NUM_SFU_BLOCKS-1:0] mstatus_wr_valid; + wire [`VX_CFG_NUM_SFU_BLOCKS-1:0] mtvec_wr_valid; + wire [`VX_CFG_NUM_SFU_BLOCKS-1:0][`VX_CFG_XLEN-1:0] mtrap_wr_data; cta_bcast_t cta_bcast; @@ -324,6 +329,11 @@ import VX_tcu_pkg::*; .satp_wr_valid (satp_wr_valid), .satp_wr_data (satp_wr_data), `endif + .mstatus (sched_mstatus), + .mtvec (sched_mtvec), + .mstatus_wr_valid (mstatus_wr_valid), + .mtvec_wr_valid (mtvec_wr_valid), + .mtrap_wr_data (mtrap_wr_data), .busy (sched_busy) ); @@ -463,6 +473,11 @@ import VX_tcu_pkg::*; .satp_wr_valid (satp_wr_valid[i]), .satp_wr_data (satp_wr_data[i]), `endif + .mstatus (sched_mstatus[i]), + .mtvec (sched_mtvec[i]), + .mstatus_wr_valid (mstatus_wr_valid[i]), + .mtvec_wr_valid (mtvec_wr_valid[i]), + .mtrap_wr_data (mtrap_wr_data[i]), `ifdef PERF_ENABLE .lsu_perf_rd_req_fire (lsu_perf_rd_req_fire[i]), .lsu_perf_wr_req_fire (lsu_perf_wr_req_fire[i]), @@ -559,6 +574,11 @@ import VX_tcu_pkg::*; .satp_wr_valid (satp_wr_valid), .satp_wr_data (satp_wr_data), `endif + .mstatus (sched_mstatus), + .mtvec (sched_mtvec), + .mstatus_wr_valid (mstatus_wr_valid), + .mtvec_wr_valid (mtvec_wr_valid), + .mtrap_wr_data (mtrap_wr_data), .fs_branch (fs_branch), .fs_wctl (fs_wctl), .fs_dvstack_wid (fs_dvstack_wid), diff --git a/hw/rtl/core/VX_csr_data.sv b/hw/rtl/core/VX_csr_data.sv index 0214f50e99..0b54f5f844 100644 --- a/hw/rtl/core/VX_csr_data.sv +++ b/hw/rtl/core/VX_csr_data.sv @@ -80,7 +80,9 @@ import VX_fpu_pkg::*; localparam LIVE_CLUSTER = 21; localparam LIVE_ENTRY = 22; localparam LIVE_SATP = 23; - localparam NUM_LIVE = 24; + localparam LIVE_MSTATUS = 24; + localparam LIVE_MTVEC = 25; + localparam NUM_LIVE = 26; // Writable CSRs localparam WR_MSCRATCH = 0; @@ -88,7 +90,9 @@ import VX_fpu_pkg::*; localparam WR_FFLAGS = 2; localparam WR_FRM = 3; localparam WR_FCSR = 4; - localparam WR_TRAP = 5; + localparam WR_MSTATUS = 5; + localparam WR_MTVEC = 6; + localparam WR_TRAP = 7; localparam NUM_WR = WR_TRAP + NUM_TRAP_CSRS; function automatic logic [`VX_CFG_XLEN-1:0] csr_rmw( @@ -165,12 +169,14 @@ import VX_fpu_pkg::*; `VX_CSR_SATP : begin live_sel_w[LIVE_SATP] = 1; wr_sel_w[WR_SATP] = 1; end `endif - // Machine-mode trap CSRs (stored in the scheduler). - `VX_CSR_MSTATUS : begin stable_w = sched_csr_if.csr_mstatus; wr_sel_w[WR_TRAP + 0] = 1; end - `VX_CSR_MTVEC : begin stable_w = sched_csr_if.csr_mtvec; wr_sel_w[WR_TRAP + 1] = 1; end - `VX_CSR_MEPC : begin stable_w = sched_csr_if.csr_mepc; wr_sel_w[WR_TRAP + 2] = 1; end - `VX_CSR_MCAUSE : begin stable_w = sched_csr_if.csr_mcause; wr_sel_w[WR_TRAP + 3] = 1; end - `VX_CSR_MTVAL : begin stable_w = sched_csr_if.csr_mtval; wr_sel_w[WR_TRAP + 4] = 1; end + // Machine-mode trap CSRs: mstatus and mtvec per core, mepc and + // mcause per warp, all held by the schedulers. + `VX_CSR_MSTATUS : begin live_sel_w[LIVE_MSTATUS] = 1; wr_sel_w[WR_MSTATUS] = 1; end + `VX_CSR_MTVEC : begin live_sel_w[LIVE_MTVEC] = 1; wr_sel_w[WR_MTVEC] = 1; end + `VX_CSR_MEPC : begin stable_w = sched_csr_if.csr_mepc; wr_sel_w[WR_TRAP + TRAP_CSR_MEPC] = 1; end + `VX_CSR_MCAUSE : begin stable_w = sched_csr_if.csr_mcause; wr_sel_w[WR_TRAP + TRAP_CSR_MCAUSE] = 1; end + // mtval is read-only zero: no exception the core raises carries a trap value. + `VX_CSR_MTVAL : begin stable_w = '0; end // The MPM counters are only reachable from the host; everything // else reads as zero. @@ -261,6 +267,8 @@ import VX_fpu_pkg::*; `else assign live_src[LIVE_SATP] = '0; `endif + assign live_src[LIVE_MSTATUS] = sched_csr_if.csr_mstatus; + assign live_src[LIVE_MTVEC] = sched_csr_if.csr_mtvec; reg [`VX_CFG_XLEN-1:0] live_data; always @(*) begin @@ -284,6 +292,15 @@ import VX_fpu_pkg::*; assign sched_csr_if.trap_csr_wr_valid = write_sel[WR_TRAP +: NUM_TRAP_CSRS]; assign sched_csr_if.trap_csr_wr_data = wr_data_r; + // The per-core CSRs (mstatus, mtvec, SATP) live in the core scheduler, + // shared by every SFU block, so their read-modify-write stays in the fire + // cycle. + wire [`VX_CFG_XLEN-1:0] live_wr_data = csr_rmw(req_op, live_data, req_src); + + assign sched_csr_if.mstatus_wr_valid = write_sel[WR_MSTATUS]; + assign sched_csr_if.mtvec_wr_valid = write_sel[WR_MTVEC]; + assign sched_csr_if.mtrap_wr_data = live_wr_data; + `ifdef VX_CFG_EXT_F_ENABLE // The FP flags keep moving under in-flight FP instructions, so their // read-modify-write stays in the fire cycle. @@ -294,10 +311,8 @@ import VX_fpu_pkg::*; `endif `ifdef VX_CFG_VM_ENABLE - // The per-core SATP CSR lives in the scheduler, shared by every SFU block, - // so its read-modify-write stays in the fire cycle. assign sched_csr_if.satp_wr_valid = write_sel[WR_SATP]; - assign sched_csr_if.satp_wr_data = csr_rmw(req_op, live_data, req_src); + assign sched_csr_if.satp_wr_data = live_wr_data; `else `UNUSED_VAR (write_sel[WR_SATP]) `endif diff --git a/hw/rtl/core/VX_decode.sv b/hw/rtl/core/VX_decode.sv index c2a5a9adba..a567b69900 100644 --- a/hw/rtl/core/VX_decode.sv +++ b/hw/rtl/core/VX_decode.sv @@ -118,6 +118,9 @@ module VX_decode import wire is_csr_fflags = (u_12 == `VX_CSR_FFLAGS); wire is_csr_frm = (u_12 == `VX_CSR_FRM); wire is_csr_fcsr = (u_12 == `VX_CSR_FCSR); + // Trap CSRs that ECALL/EBREAK/MRET read when they execute in the ALU, + // while the CSR unit writes them: XREG_2 orders the two. + wire is_csr_trap = (u_12 == `VX_CSR_MSTATUS) || (u_12 == `VX_CSR_MTVEC) || (u_12 == `VX_CSR_MEPC); wire frm_is_dyn = (funct3 == 3'b111); wire is_rd_zero = (rd == 0); `UNUSED_VAR (frm_is_dyn) @@ -478,6 +481,8 @@ 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); + rd_xregs[XREG_2] = is_csr_trap; + wr_xregs[XREG_2] = csr_write && is_csr_trap; `USED_IREG (rd); `USED_REG (REG_TYPE_I, rs1, ~funct3[2]); end else begin @@ -491,6 +496,7 @@ module VX_decode import op_args.br.imm20 = 20'd4; op_args.br.is_rvc = decode_is_rvc; is_wstall = 1; + rd_xregs[XREG_2] = 1'b1; `USED_IREG (rd); end end diff --git a/hw/rtl/core/VX_frontend_xbar.sv b/hw/rtl/core/VX_frontend_xbar.sv index b3e7abb273..bb1fe61b45 100644 --- a/hw/rtl/core/VX_frontend_xbar.sv +++ b/hw/rtl/core/VX_frontend_xbar.sv @@ -50,6 +50,11 @@ module VX_frontend_xbar import VX_gpu_pkg::*; ( output wire [`VX_CFG_NUM_SFU_BLOCKS-1:0] satp_wr_valid, output wire [`VX_CFG_NUM_SFU_BLOCKS-1:0][`VX_CFG_XLEN-1:0] satp_wr_data, `endif + input wire [`VX_CFG_NUM_SFU_BLOCKS-1:0][`VX_CFG_XLEN-1:0] mstatus, + input wire [`VX_CFG_NUM_SFU_BLOCKS-1:0][`VX_CFG_XLEN-1:0] mtvec, + output wire [`VX_CFG_NUM_SFU_BLOCKS-1:0] mstatus_wr_valid, + output wire [`VX_CFG_NUM_SFU_BLOCKS-1:0] mtvec_wr_valid, + output wire [`VX_CFG_NUM_SFU_BLOCKS-1:0][`VX_CFG_XLEN-1:0] mtrap_wr_data, // frontend slices output sched_branch_t [`VX_CFG_FETCH_WIDTH-1:0][FETCH_ALU_PORTS-1:0] fs_branch, @@ -205,11 +210,8 @@ module VX_frontend_xbar import VX_gpu_pkg::*; ( assign sched_csr_if[b].active_warps = active_warps; assign sched_csr_if[b].thread_mask = csr_rd.thread_mask; assign sched_csr_if[b].mscratch = csr_rd.mscratch; - assign sched_csr_if[b].csr_mstatus = csr_rd.mstatus; - assign sched_csr_if[b].csr_mtvec = csr_rd.mtvec; assign sched_csr_if[b].csr_mepc = csr_rd.mepc; assign sched_csr_if[b].csr_mcause = csr_rd.mcause; - assign sched_csr_if[b].csr_mtval = csr_rd.mtval; assign sched_csr_if[b].cta_rank = csr_rd.cta_rank; assign sched_csr_if[b].cta_lane = csr_rd.cta_lane; `ifdef VX_CFG_VM_ENABLE @@ -217,6 +219,11 @@ module VX_frontend_xbar import VX_gpu_pkg::*; ( assign satp_wr_valid[b] = sched_csr_if[b].satp_wr_valid; assign satp_wr_data[b] = sched_csr_if[b].satp_wr_data; `endif + assign sched_csr_if[b].csr_mstatus = mstatus[b]; + assign sched_csr_if[b].csr_mtvec = mtvec[b]; + assign mstatus_wr_valid[b] = sched_csr_if[b].mstatus_wr_valid; + assign mtvec_wr_valid[b] = sched_csr_if[b].mtvec_wr_valid; + assign mtrap_wr_data[b] = sched_csr_if[b].mtrap_wr_data; end endmodule diff --git a/hw/rtl/core/VX_scheduler.sv b/hw/rtl/core/VX_scheduler.sv index c16017a9d9..361ea45c30 100644 --- a/hw/rtl/core/VX_scheduler.sv +++ b/hw/rtl/core/VX_scheduler.sv @@ -51,6 +51,11 @@ module VX_scheduler import VX_gpu_pkg::*; #( input wire [`VX_CFG_NUM_SFU_BLOCKS-1:0] satp_wr_valid, input wire [`VX_CFG_NUM_SFU_BLOCKS-1:0][`VX_CFG_XLEN-1:0] satp_wr_data, `endif + output wire [`VX_CFG_NUM_SFU_BLOCKS-1:0][`VX_CFG_XLEN-1:0] mstatus, + output wire [`VX_CFG_NUM_SFU_BLOCKS-1:0][`VX_CFG_XLEN-1:0] mtvec, + input wire [`VX_CFG_NUM_SFU_BLOCKS-1:0] mstatus_wr_valid, + input wire [`VX_CFG_NUM_SFU_BLOCKS-1:0] mtvec_wr_valid, + input wire [`VX_CFG_NUM_SFU_BLOCKS-1:0][`VX_CFG_XLEN-1:0] mtrap_wr_data, output wire busy ); @@ -194,6 +199,29 @@ module VX_scheduler import VX_gpu_pkg::*; #( end `endif + // Per-core mstatus and mtvec, written by any SFU block's CSR unit in block + // order, as SATP is. Trap redirection reads the registered vector: a warp's + // own mtvec write has committed before its ECALL or EBREAK issues. + reg [`VX_CFG_XLEN-1:0] mstatus_r, mtvec_r; + wire [`VX_CFG_NUM_SFU_BLOCKS:0][`VX_CFG_XLEN-1:0] mstatus_chain, mtvec_chain; + assign mstatus_chain[0] = mstatus_r; + assign mtvec_chain[0] = mtvec_r; + for (genvar b = 0; b < `VX_CFG_NUM_SFU_BLOCKS; ++b) begin : g_mtrap + assign mstatus[b] = mstatus_chain[b]; + assign mtvec[b] = mtvec_chain[b]; + assign mstatus_chain[b+1] = mstatus_wr_valid[b] ? mtrap_wr_data[b] : mstatus_chain[b]; + assign mtvec_chain[b+1] = mtvec_wr_valid[b] ? mtrap_wr_data[b] : mtvec_chain[b]; + end + always @(posedge clk) begin + if (reset) begin + mstatus_r <= '0; + mtvec_r <= '0; + end else begin + mstatus_r <= mstatus_chain[`VX_CFG_NUM_SFU_BLOCKS]; + mtvec_r <= mtvec_chain[`VX_CFG_NUM_SFU_BLOCKS]; + end + end + // per-slice view of the core-wide events wire [FETCH_FSW_W-1:0] wspawn_fsw = wid_to_fsw(wspawn_wid); @@ -201,6 +229,7 @@ module VX_scheduler import VX_gpu_pkg::*; #( for (genvar p = 0; p < FW; ++p) begin : g_slices localparam [FETCH_FSW_W-1:0] FSW = FETCH_FSW_W'(p); + assign slice_if[p].mtvec = mtvec_r; assign slice_if[p].cta_fire = cta_fire[p]; assign slice_if[p].cta_lid = wid_to_wfs(cta_wid[p]); assign slice_if[p].cta_PC = cta_PC; diff --git a/hw/rtl/core/VX_warp_sched.sv b/hw/rtl/core/VX_warp_sched.sv index fca7e336b1..c3f32df701 100644 --- a/hw/rtl/core/VX_warp_sched.sv +++ b/hw/rtl/core/VX_warp_sched.sv @@ -141,7 +141,8 @@ module VX_warp_sched import VX_gpu_pkg::*; #( wire sched_fire; 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][`VX_CFG_XLEN-1:0] mepc_r; + reg [SW-1:0][TRAP_CAUSE_BITS-1:0] mcause_r; for (genvar l = 0; l < SW; ++l) begin : g_warps localparam [LW-1:0] LID = LW'(l); @@ -233,7 +234,7 @@ 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(sched_if.mtvec & ~`VX_CFG_XLEN'(3)); end else if (branch[QA].is_mret) begin // MRET/SRET/URET: restore the saved PC from mepc. pc_n = from_fullPC(mepc_r[l]); @@ -274,11 +275,8 @@ module VX_warp_sched import VX_gpu_pkg::*; #( tmask_r <= '0; pc_r <= '0; mscratch_r[l] <= '0; - mstatus_r[l] <= '0; - mtvec_r[l] <= '0; mepc_r[l] <= '0; mcause_r[l] <= '0; - mtval_r[l] <= '0; end else begin active_r <= active_n; stalled_r <= stalled_n; @@ -299,28 +297,18 @@ module VX_warp_sched import VX_gpu_pkg::*; #( end if (csr_wr_lid[QS] == LID) begin - if (csr_wr[QS].trap_valid[0]) begin - mstatus_r[l] <= csr_wr[QS].trap_data; - end - if (csr_wr[QS].trap_valid[1]) begin - mtvec_r[l] <= csr_wr[QS].trap_data; - end - if (csr_wr[QS].trap_valid[2]) begin + if (csr_wr[QS].trap_valid[TRAP_CSR_MEPC]) begin mepc_r[l] <= csr_wr[QS].trap_data; end - if (csr_wr[QS].trap_valid[3]) begin - mcause_r[l] <= csr_wr[QS].trap_data; - end - if (csr_wr[QS].trap_valid[4]) begin - mtval_r[l] <= csr_wr[QS].trap_data; + if (csr_wr[QS].trap_valid[TRAP_CSR_MCAUSE]) begin + mcause_r[l] <= TRAP_CAUSE_BITS'(csr_wr[QS].trap_data); end end // Hardware trap entry (ECALL/EBREAK) wins a same-cycle software write. if (br_hit && branch[QA].is_trap) begin mepc_r[l] <= to_fullPC(branch[QA].dest); - mcause_r[l] <= `VX_CFG_XLEN'(branch[QA].trap_cause); - mtval_r[l] <= '0; + mcause_r[l] <= branch[QA].trap_cause; end end end @@ -425,11 +413,8 @@ 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].mcause = mcause_r[csr_rd_lid[q]]; - assign csr_rd[q].mtval = mtval_r[csr_rd_lid[q]]; + assign csr_rd[q].mcause = `VX_CFG_XLEN'(mcause_r[csr_rd_lid[q]]); assign dvstack_mscratch[q] = mscratch_r[wid_to_wfs(dvstack_wid[q])]; end diff --git a/hw/rtl/interfaces/VX_branch_ctl_if.sv b/hw/rtl/interfaces/VX_branch_ctl_if.sv index 664dd56502..c41818606f 100644 --- a/hw/rtl/interfaces/VX_branch_ctl_if.sv +++ b/hw/rtl/interfaces/VX_branch_ctl_if.sv @@ -26,7 +26,7 @@ interface VX_branch_ctl_if import VX_gpu_pkg::*; (); // override the normal taken/dest path in the scheduler. wire is_trap; wire is_mret; - wire [3:0] trap_cause; + wire [TRAP_CAUSE_BITS-1:0] trap_cause; modport master ( output valid, diff --git a/hw/rtl/interfaces/VX_sched_csr_if.sv b/hw/rtl/interfaces/VX_sched_csr_if.sv index cc88a4d0e2..d3cd3ae786 100644 --- a/hw/rtl/interfaces/VX_sched_csr_if.sv +++ b/hw/rtl/interfaces/VX_sched_csr_if.sv @@ -40,19 +40,22 @@ interface VX_sched_csr_if import VX_gpu_pkg::*; (); logic [NW_WIDTH-1:0] csr_wr_wid; logic [`VX_CFG_MEM_ADDR_WIDTH-1:0] csr_wr_data; - // Per-warp machine-mode trap CSRs live in the scheduler (alongside - // mscratch) because the scheduler owns warp PC redirection. Reads are - // returned already selected by csr_rd_wid; csrw writes are forwarded - // here as a one-hot {mtval, mcause, mepc, mtvec, mstatus}. csr_wr_wid is - // shared with mscratch. - logic [`VX_CFG_XLEN-1:0] csr_mstatus; - logic [`VX_CFG_XLEN-1:0] csr_mtvec; + // The per-warp trap CSRs (mepc, mcause) live in the warp schedulers, which + // own warp PC redirection. Reads are returned already selected by + // csr_rd_wid; csrw writes are forwarded here as a one-hot {mcause, mepc}. + // csr_wr_wid is shared with mscratch. logic [`VX_CFG_XLEN-1:0] csr_mepc; logic [`VX_CFG_XLEN-1:0] csr_mcause; - 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; + // The per-core mstatus and mtvec live in the core scheduler, as SATP does. + logic [`VX_CFG_XLEN-1:0] csr_mstatus; + logic [`VX_CFG_XLEN-1:0] csr_mtvec; + logic mstatus_wr_valid; + logic mtvec_wr_valid; + logic [`VX_CFG_XLEN-1:0] mtrap_wr_data; + modport master ( output cycles, output instret, @@ -65,7 +68,6 @@ interface VX_sched_csr_if import VX_gpu_pkg::*; (); output csr_mtvec, output csr_mepc, output csr_mcause, - output csr_mtval, `ifdef VX_CFG_VM_ENABLE output csr_satp, input satp_wr_valid, @@ -76,7 +78,10 @@ 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 mstatus_wr_valid, + input mtvec_wr_valid, + input mtrap_wr_data ); modport slave ( @@ -91,7 +96,6 @@ interface VX_sched_csr_if import VX_gpu_pkg::*; (); input csr_mtvec, input csr_mepc, input csr_mcause, - input csr_mtval, `ifdef VX_CFG_VM_ENABLE input csr_satp, output satp_wr_valid, @@ -102,7 +106,10 @@ 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 mstatus_wr_valid, + output mtvec_wr_valid, + output mtrap_wr_data ); endinterface diff --git a/hw/rtl/interfaces/VX_slice_sched_if.sv b/hw/rtl/interfaces/VX_slice_sched_if.sv index 95d8c79d6d..efb74bb6ec 100644 --- a/hw/rtl/interfaces/VX_slice_sched_if.sv +++ b/hw/rtl/interfaces/VX_slice_sched_if.sv @@ -56,6 +56,9 @@ interface VX_slice_sched_if import VX_gpu_pkg::*; (); // every slice's ibuf_full; the slice ANDs them into its core-wide view logic [FW-1:0] slice_ibuf_full; + + // the per-core trap vector, where ECALL and EBREAK redirect + logic [`VX_CFG_XLEN-1:0] mtvec; `ifdef EXT_SCHED_TIMEOUT_DUMP // the core-wide stall timeout is about to fire logic timeout_dump; @@ -99,6 +102,7 @@ interface VX_slice_sched_if import VX_gpu_pkg::*; (); output retire_valid, output retire_lid, output slice_ibuf_full, + output mtvec, `ifdef EXT_SCHED_TIMEOUT_DUMP output timeout_dump, `endif @@ -138,6 +142,7 @@ interface VX_slice_sched_if import VX_gpu_pkg::*; (); input retire_valid, input retire_lid, input slice_ibuf_full, + input mtvec, `ifdef EXT_SCHED_TIMEOUT_DUMP input timeout_dump, `endif diff --git a/sim/simx/csr_unit.cpp b/sim/simx/csr_unit.cpp index 9291bf8a86..0d47d05805 100644 --- a/sim/simx/csr_unit.cpp +++ b/sim/simx/csr_unit.cpp @@ -59,12 +59,14 @@ Word CsrUnit::get_csr(uint32_t addr, uint32_t wid, uint32_t tid) { case VX_CSR_MNSTATUS: return 0; - // Per-warp machine-mode trap CSRs (see warp_t). - case VX_CSR_MSTATUS: return warp.mstatus; - case VX_CSR_MTVEC: return warp.mtvec; + // Machine-mode trap CSRs: mstatus/mtvec core-wide, mepc/mcause per warp + // (see warp_t). mtval is read-only zero: no exception the core raises + // carries a trap value. + case VX_CSR_MSTATUS: return sched.mstatus(); + case VX_CSR_MTVEC: return sched.mtvec(); case VX_CSR_MEPC: return warp.mepc; case VX_CSR_MCAUSE: return warp.mcause; - case VX_CSR_MTVAL: return warp.mtval; + case VX_CSR_MTVAL: return 0; case VX_CSR_SATP: #ifdef VX_CFG_VM_ENABLE return (Word)satp_; @@ -336,7 +338,8 @@ Word CsrUnit::get_csr(uint32_t addr, uint32_t wid, uint32_t tid) { void CsrUnit::set_csr(uint32_t addr, Word value, uint32_t wid, uint32_t tid) { __unused(tid); - auto& warp = core_->scheduler().warp(wid); + auto& sched = core_->scheduler(); + auto& warp = sched.warp(wid); switch (addr) { case VX_CSR_FFLAGS: warp.fcsr = (warp.fcsr & ~0x1F) | (value & 0x1F); @@ -359,21 +362,20 @@ void CsrUnit::set_csr(uint32_t addr, Word value, uint32_t wid, uint32_t tid) { satp_ = value; #endif break; - // Per-warp machine-mode trap CSRs (see warp_t). + // Machine-mode trap CSRs: mstatus/mtvec core-wide, mepc/mcause per warp. case VX_CSR_MSTATUS: - warp.mstatus = value; + sched.set_mstatus(value); break; case VX_CSR_MTVEC: - warp.mtvec = value; + sched.set_mtvec(value); break; case VX_CSR_MEPC: warp.mepc = value; break; case VX_CSR_MCAUSE: - warp.mcause = value; + warp.mcause = value & Scheduler::TRAP_CAUSE_MASK; break; case VX_CSR_MTVAL: - warp.mtval = value; break; case VX_CSR_MEDELEG: case VX_CSR_MIDELEG: diff --git a/sim/simx/decode.cpp b/sim/simx/decode.cpp index d75e1a6c46..f33a1d33ed 100644 --- a/sim/simx/decode.cpp +++ b/sim/simx/decode.cpp @@ -797,6 +797,9 @@ Instr::Ptr Decoder::decode(uint32_t code, uint64_t uuid) { if (imm12 == VX_CSR_FRM || imm12 == VX_CSR_FCSR) { fcsr_regs |= Instr::FCSR_FRM; } + if (imm12 == VX_CSR_MSTATUS || imm12 == VX_CSR_MTVEC || imm12 == VX_CSR_MEPC) { + fcsr_regs |= Instr::TRAP_CSRS; + } instr->set_fcsr_use(fcsr_regs, csr_write ? fcsr_regs : 0); } if (funct3 < 5) { @@ -810,6 +813,7 @@ Instr::Ptr Decoder::decode(uint32_t code, uint64_t uuid) { instr->set_op_type(BrType::SYS); instr->set_args(IntrBrArgs{0, 0, imm12}); instr->set_wstall(true); + instr->set_fcsr_use(Instr::TRAP_CSRS, 0); } } break; case Opcode::FCI: { diff --git a/sim/simx/instr.h b/sim/simx/instr.h index 0c063c8f87..861d8a9a26 100644 --- a/sim/simx/instr.h +++ b/sim/simx/instr.h @@ -234,11 +234,14 @@ class Instr { void set_dst_bytesel(uint8_t value) { dst_bytesel_ = value; } uint8_t get_dst_bytesel() const { return dst_bytesel_; } - // Floating-point CSR fields the scoreboard orders like GPRs: an instruction - // naming one, read or written, waits for an older writer to commit. + // CSR fields the scoreboard orders like GPRs: an instruction naming one, + // read or written, waits for an older writer to commit. Besides the FP + // control fields, TRAP_CSRS stands for mstatus/mtvec/mepc, which + // ECALL/EBREAK/MRET read outside the CSR unit. enum : uint8_t { FCSR_FFLAGS = 1u << 0, FCSR_FRM = 1u << 1, + TRAP_CSRS = 1u << 2, }; void set_fcsr_use(uint8_t rd_mask, uint8_t wr_mask) { fcsr_rd_ = rd_mask; diff --git a/sim/simx/scheduler.cpp b/sim/simx/scheduler.cpp index 5781fbd816..1fc601775d 100644 --- a/sim/simx/scheduler.cpp +++ b/sim/simx/scheduler.cpp @@ -49,11 +49,8 @@ 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; // Register files live in OpcUnit and are reset there. } @@ -85,6 +82,9 @@ void Scheduler::on_reset() { warp.reset(); } + mstatus_ = 0; + mtvec_ = 0; + stalled_warps_.reset(); stalled_warps_next_.reset(); active_warps_.reset(); @@ -364,13 +364,12 @@ namespace { void Scheduler::raise_trap(uint32_t wid, Word cause, Word trap_pc) { auto& warp = warps_.at(wid); warp.mepc = trap_pc; - warp.mcause = cause; - warp.mtval = 0; + warp.mcause = cause & TRAP_CAUSE_MASK; // 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 = mtvec_ & ~Word(3); DT(3, core_->name() << " trap: wid=" << wid << ", cause=" << cause - << ", mepc=0x" << std::hex << trap_pc << ", mtvec=0x" << warp.mtvec << std::dec); + << ", mepc=0x" << std::hex << trap_pc << ", mtvec=0x" << mtvec_ << std::dec); } void Scheduler::mret(uint32_t wid) { diff --git a/sim/simx/scheduler.h b/sim/simx/scheduler.h index a594a660f7..95813ac1d4 100644 --- a/sim/simx/scheduler.h +++ b/sim/simx/scheduler.h @@ -85,14 +85,11 @@ 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. - Word mstatus = 0; - Word mtvec = 0; + // Per-warp trap return PC and cause: synchronous ECALL/EBREAK traps snapshot + // the faulting PC/cause here and MRET restores the PC from mepc. mstatus and + // mtvec are core-wide (Scheduler); see Scheduler::raise_trap. Word mepc = 0; Word mcause = 0; - Word mtval = 0; // CTA CSR values set at dispatch time cta_csrs_t cta_csrs; @@ -180,6 +177,8 @@ class Scheduler : public SimObject { // core_->csr_unit(). // ----- Trap helpers ----- + // mcause holds only the exception codes the core raises (TRAP_CAUSE_BITS in RTL). + static constexpr Word TRAP_CAUSE_MASK = 0xF; // 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. @@ -189,6 +188,14 @@ class Scheduler : public SimObject { void trigger_ecall(uint32_t wid, Word trap_pc); void trigger_ebreak(uint32_t wid, Word trap_pc); + // ----- Core-wide trap CSRs ----- + // Every warp runs the same kernel and trap handler, so mstatus and mtvec are + // one per core: a write by any warp updates them for all. + Word mstatus() const { return mstatus_; } + Word mtvec() const { return mtvec_; } + void set_mstatus(Word value) { mstatus_ = value; } + void set_mtvec(Word value) { mtvec_ = value; } + // ----- Accessors ----- warp_t& warp(uint32_t wid) { return warps_.at(wid); } uint32_t ipdom_size() const { return ipdom_size_; } @@ -199,6 +206,9 @@ class Scheduler : public SimObject { void on_reset(); private: + Word mstatus_ = 0; + Word mtvec_ = 0; + struct wspawn_t { bool valid; uint32_t num_warps; diff --git a/tests/regression/Makefile b/tests/regression/Makefile index 1dabd029c6..dbe17b405a 100644 --- a/tests/regression/Makefile +++ b/tests/regression/Makefile @@ -3,7 +3,7 @@ include $(ROOT_DIR)/config.mk # --- master list ------------------------------------------------------ TESTS := \ - minimal \ + minimal trap \ basic demo dogfood dropout dotproduct dotproduct2 mstress io_addr \ arith printf diverge diverge_large sort fence fence_mp ldst_order \ vecadd vecadd_v1 sgemm sgemmx sgemm_v1 conv3 relu sgemv \ diff --git a/tests/regression/trap/Makefile b/tests/regression/trap/Makefile new file mode 100644 index 0000000000..3839c4cfc3 --- /dev/null +++ b/tests/regression/trap/Makefile @@ -0,0 +1,16 @@ +ROOT_DIR := $(realpath ../../..) +include $(ROOT_DIR)/config.mk + +PROJECT := trap + +SRC_DIR := $(VORTEX_HOME)/tests/regression/$(PROJECT) + +SRCS := $(SRC_DIR)/main.cpp + +VX_SRCS := $(SRC_DIR)/kernel.cpp + +OPTS ?= -n256 + +KERNEL_LIB := vortex2 + +include ../common.mk diff --git a/tests/regression/trap/common.h b/tests/regression/trap/common.h new file mode 100644 index 0000000000..5cd440936b --- /dev/null +++ b/tests/regression/trap/common.h @@ -0,0 +1,16 @@ +#ifndef _COMMON_H_ +#define _COMMON_H_ + +// Per-thread result bits; zero means every check passed. +#define TRAP_BAD_CAUSE 0x1u // mcause is not this warp's own exception code +#define TRAP_BAD_EPC 0x2u // mepc did not return to this warp's own trap site +#define TRAP_BAD_TVEC 0x4u // mtvec does not read back the block's handler address +#define TRAP_BAD_TVAL 0x8u // mtval is not zero + +typedef struct { + uint32_t num_points; + uint32_t iters; + uint64_t dst_addr; +} kernel_arg_t; + +#endif diff --git a/tests/regression/trap/kernel.cpp b/tests/regression/trap/kernel.cpp new file mode 100644 index 0000000000..5f3b71d818 --- /dev/null +++ b/tests/regression/trap/kernel.cpp @@ -0,0 +1,53 @@ +#include +#include +#include "common.h" + +// Steps mepc past the trapping instruction and returns. Uses only t6, which +// the trap sites below declare clobbered. +asm(".section .text\n" + ".align 4\n" + ".global trap_handler\n" + "trap_handler:\n" + " csrr t6, mepc\n" + " addi t6, t6, 4\n" + " csrw mepc, t6\n" + " mret\n"); +extern "C" void trap_handler(); + +// Even warps trap with ECALL and odd warps with EBREAK, at different sites, so +// a return PC or cause shared between warps shows up as a mismatch. +__kernel void kernel_main(kernel_arg_t* __UNIFORM__ arg) { + auto dst_ptr = reinterpret_cast(arg->dst_addr); + uint32_t idx = blockIdx.x * blockDim.x + threadIdx.x; + uintptr_t handler = (uintptr_t)trap_handler; + + // mtvec is one per core: only the block's first warp installs the handler, + // and every warp of the block, across the issue slices, must read it back. + // A warp that does not see it reports the mismatch instead of trapping. + if (threadIdx.x < (uint32_t)vx_num_threads()) { + csr_write(VX_CSR_MTVEC, handler); + } + __syncthreads(); + uint32_t bad = (csr_read(VX_CSR_MTVEC) != handler) ? TRAP_BAD_TVEC : 0; + + bool odd = (vx_warp_id() & 1) != 0; + for (uint32_t i = 0; bad == 0 && i < arg->iters; ++i) { + uintptr_t site, epc; + uint32_t cause; + if (odd) { + __asm__ volatile("la %0, 1f\n\tebreak\n1:\n\tcsrr %1, mepc\n\tcsrr %2, mcause" + : "=&r"(site), "=&r"(epc), "=&r"(cause) :: "t6", "memory"); + bad |= (cause != 3) ? TRAP_BAD_CAUSE : 0; + } else { + __asm__ volatile("la %0, 1f\n\tecall\n1:\n\tcsrr %1, mepc\n\tcsrr %2, mcause" + : "=&r"(site), "=&r"(epc), "=&r"(cause) :: "t6", "memory"); + bad |= (cause != 11) ? TRAP_BAD_CAUSE : 0; + } + bad |= (epc != site) ? TRAP_BAD_EPC : 0; + } + bad |= (csr_read(VX_CSR_MTVAL) != 0) ? TRAP_BAD_TVAL : 0; + + if (idx < arg->num_points) { + dst_ptr[idx] = bad; + } +} diff --git a/tests/regression/trap/main.cpp b/tests/regression/trap/main.cpp new file mode 100644 index 0000000000..9141f56a72 --- /dev/null +++ b/tests/regression/trap/main.cpp @@ -0,0 +1,134 @@ +// Copyright © 2019-2023 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 + +// trap — synchronous traps taken by many warps at once. +// +// Each block's first warp installs the handler in the core's one mtvec, and +// every warp of the block reads it back, then traps in a loop: even warps with +// ECALL, odd warps with EBREAK, each at its own site. The handler steps mepc +// past the trap, so an MRET that misses that write traps again. Each thread +// reports whether its warp saw its own cause and return PC, so a return PC or +// cause shared between warps fails the test. + +#include +#include "common.h" + +#include +#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 { +const char* kernel_file = "kernel.vxbin"; +uint32_t size = 256; +uint32_t iters = 8; + +void parse_args(int argc, char** argv) { + int c; + while ((c = getopt(argc, argv, "n:i:k:h")) != -1) { + switch (c) { + case 'n': size = std::atoi(optarg); break; + case 'i': iters = std::atoi(optarg); break; + case 'k': kernel_file = optarg; break; + default: + std::cout << "Usage: [-k kernel] [-n threads] [-i traps per warp] [-h]" << std::endl; + std::exit(c == 'h' ? 0 : -1); + } + } +} +} // namespace + +int main(int argc, char** argv) { + parse_args(argc, argv); + + const uint32_t num_points = size; + const uint64_t buf_size = num_points * sizeof(uint32_t); + std::cout << "trap: n=" << num_points << " iters=" << iters << std::endl; + + vx_device_h dev = nullptr; + CHECK(vx_device_open(0, &dev)); + + vx_queue_info_t qi = { sizeof(qi), nullptr, VX_QUEUE_PRIORITY_NORMAL, 0 }; + vx_queue_h q = nullptr; + CHECK(vx_queue_create(dev, &qi, &q)); + + vx_buffer_h dst_buf = nullptr; + CHECK(vx_buffer_create(dev, buf_size, VX_MEM_WRITE, &dst_buf)); + + kernel_arg_t kernel_arg{}; + kernel_arg.num_points = num_points; + kernel_arg.iters = iters; + CHECK(vx_buffer_address(dst_buf, &kernel_arg.dst_addr)); + + 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)); + + // 0xDEADBEEF has bits outside the result mask, so an unwritten word fails. + std::vector h_src(num_points, 0xDEADBEEFu); + std::vector h_dst(num_points, 0xDEADBEEFu); + CHECK(vx_enqueue_write(q, dst_buf, 0, h_src.data(), buf_size, 0, nullptr, nullptr)); + + uint32_t grid[1], block[1]; + CHECK(vx_device_max_occupancy_grid(dev, 1, &num_points, grid, block)); + + vx_launch_info_t li{}; + li.struct_size = sizeof(li); + li.kernel = kern; + li.args_host = &kernel_arg; + li.args_size = sizeof(kernel_arg); + li.ndim = 1; + li.grid_dim[0] = grid[0]; + li.block_dim[0] = block[0]; + + vx_event_h launch_ev = nullptr, read_ev = nullptr; + CHECK(vx_enqueue_launch(q, &li, 0, nullptr, &launch_ev)); + CHECK(vx_enqueue_read(q, h_dst.data(), dst_buf, 0, buf_size, 1, &launch_ev, &read_ev)); + CHECK(vx_event_wait_value(read_ev, 1, VX_TIMEOUT_INFINITE)); + + int errors = 0; + for (uint32_t i = 0; i < num_points; ++i) { + if (h_dst[i] != 0) { + if (errors < 16) { + std::printf("*** [%u] result=0x%08x%s%s%s%s\n", i, h_dst[i], + (h_dst[i] & TRAP_BAD_CAUSE) ? " cause" : "", + (h_dst[i] & TRAP_BAD_EPC) ? " epc" : "", + (h_dst[i] & TRAP_BAD_TVEC) ? " tvec" : "", + (h_dst[i] & TRAP_BAD_TVAL) ? " tval" : ""); + } + ++errors; + } + } + + vx_event_release(read_ev); + vx_event_release(launch_ev); + vx_buffer_release(dst_buf); + vx_kernel_release(kern); + vx_module_release(mod); + vx_queue_release(q); + vx_device_dump_perf(dev, stdout); + vx_device_release(dev); + + if (errors) { + std::cout << "Found " << errors << " errors!\nFAILED!" << std::endl; + return 1; + } + std::cout << "PASSED!" << std::endl; + return 0; +} From 5ce4e6cc74912979b883e2c0a05cdb37020b2871 Mon Sep 17 00:00:00 2001 From: jel221 Date: Tue, 17 Nov 2026 04:42:35 -0800 Subject: [PATCH 2/2] Split the per-core mstatus/mtvec write chain so Verilator -O0 sees no combinational loop --- hw/rtl/core/VX_scheduler.sv | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/hw/rtl/core/VX_scheduler.sv b/hw/rtl/core/VX_scheduler.sv index 361ea45c30..cd2838788a 100644 --- a/hw/rtl/core/VX_scheduler.sv +++ b/hw/rtl/core/VX_scheduler.sv @@ -202,23 +202,29 @@ module VX_scheduler import VX_gpu_pkg::*; #( // Per-core mstatus and mtvec, written by any SFU block's CSR unit in block // order, as SATP is. Trap redirection reads the registered vector: a warp's // own mtvec write has committed before its ECALL or EBREAK issues. + // The value a block reads and the value after its write are separate + // vectors: one vector holding both is a combinational loop to Verilator + // when it does not split variables (-O0). reg [`VX_CFG_XLEN-1:0] mstatus_r, mtvec_r; - wire [`VX_CFG_NUM_SFU_BLOCKS:0][`VX_CFG_XLEN-1:0] mstatus_chain, mtvec_chain; - assign mstatus_chain[0] = mstatus_r; - assign mtvec_chain[0] = mtvec_r; + wire [`VX_CFG_NUM_SFU_BLOCKS-1:0][`VX_CFG_XLEN-1:0] mstatus_wr, mtvec_wr; for (genvar b = 0; b < `VX_CFG_NUM_SFU_BLOCKS; ++b) begin : g_mtrap - assign mstatus[b] = mstatus_chain[b]; - assign mtvec[b] = mtvec_chain[b]; - assign mstatus_chain[b+1] = mstatus_wr_valid[b] ? mtrap_wr_data[b] : mstatus_chain[b]; - assign mtvec_chain[b+1] = mtvec_wr_valid[b] ? mtrap_wr_data[b] : mtvec_chain[b]; + if (b == 0) begin : g_first + assign mstatus[b] = mstatus_r; + assign mtvec[b] = mtvec_r; + end else begin : g_next + assign mstatus[b] = mstatus_wr[b-1]; + assign mtvec[b] = mtvec_wr[b-1]; + end + assign mstatus_wr[b] = mstatus_wr_valid[b] ? mtrap_wr_data[b] : mstatus[b]; + assign mtvec_wr[b] = mtvec_wr_valid[b] ? mtrap_wr_data[b] : mtvec[b]; end always @(posedge clk) begin if (reset) begin mstatus_r <= '0; mtvec_r <= '0; end else begin - mstatus_r <= mstatus_chain[`VX_CFG_NUM_SFU_BLOCKS]; - mtvec_r <= mtvec_chain[`VX_CFG_NUM_SFU_BLOCKS]; + mstatus_r <= mstatus_wr[`VX_CFG_NUM_SFU_BLOCKS-1]; + mtvec_r <= mtvec_wr[`VX_CFG_NUM_SFU_BLOCKS-1]; end end