From 8944ea85f3c8a4aaa815427646cf248f4b53e213 Mon Sep 17 00:00:00 2001 From: Blaise Tine Date: Tue, 1 Dec 2026 20:27:46 -0800 Subject: [PATCH] core: a misaligned atomic traps precisely (address-misaligned) instead of touching memory MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An atomic (AMO, LR, SC, compare-and-swap) whose active lane names an address not aligned to its size now raises the RISC-V address-misaligned exception: store/AMO address misaligned (6), load address misaligned (4) for LR, mepc the atomic, mtval read-only zero. On master the RTL asserts in simulation (on hardware it writes through a misaligned word) and SimX aborts. The trap is precise: no instruction after the atomic runs before the handler, and the handler may return to mepc + 4. It uses the pipeline's extension points only; no shared stage changes (coding guidelines ยง13). - Decode marks every atomic wstall (EXT_A only), as it does a branch. - The uop sequencer (VX_uop_amo) puts an alignment check ahead of every atomic: check [release fence] atomic [acquire fence]. The check is an ALU branch-port uop on rs1 (INST_BR_AMOCHK, INST_BR_LRCHK, two spare branch codes) that targets PC + 4 and reads the trap pseudo-register, as ECALL does, so an older mtvec write has landed when it traps. - VX_alu_int (EXT_A only) raises is_trap with cause 6 or 4 and dest = the atomic's PC when an active lane is misaligned. The check's SIMD slices and packets reach a block back to back, so one flop holds a misaligned lane of an earlier one until the last. The branch port's existing trap path writes mepc, mcause and mstatus and redirects the stalled warp; its br_hit releases the warp's fetch. - VX_lsu_block (EXT_A only) decides the same from rs1 on its own: from the packet holding the first misaligned lane on, the atomic's packets retire through the no-response path writing nothing (as a fence's skipped packets do); packets ahead of it have gone to memory. A faulted end-of-instruction packet waits for those packets' responses (fault_hold), so the next packet does not start in a slot still counting them. - SimX in lockstep: decode marks the atomic macro wstall; LsuUopGen emits the check (BrType::AMOCHK) as uop 0; the sequencer gives a wstall uop its macro's fetch release; AluUnit holds a misaligned lane per block and calls Scheduler::raise_trap; LsuUnit drops the atomic from the faulted packet on and holds behind a partly faulted one. - No VX_types change (the causes are local literals, as the ALU's other trap causes), so the resolved config and every perf golden are unchanged. Cost: the atomic stalls its warp's fetch until the check resolves, as a branch does. x32 cycles, control = master 3fb83fda5 with this commit's amo test text, so only the hardware and model differ: case simx rtlsim amoadd 5875 -> 8211 (+39.8%) 5888 -> 8224 (+39.7%) contended 7505 -> 8721 (+16.2%) 7522 -> 8738 (+16.2%) contended-l2 37487 -> 33539 (-10.5%) 37491 -> 37641 (+0.4%) zacas-simd 20239 -> 22129 (+9.3%) 21523 -> 21337 (-0.9%) amo list w/o trap 58579 -> 61244 (+4.5%) 62238 -> 66286 (+6.5%) sgemm 113803 -> 113803 (0.0%) 113814 -> 113814 (0.0%) With EXT_A off (the default) nothing changes: Beacon (v3.0.3) core and lmem with A off are identical to master on U55C (LUT 120383, FF 137138, 15 levels) and ASIC (723387 cells, 94018 FF, 30 stages). With A on: U55C core LUT 127131 -> 127084, FF 140274 -> 140277, 15 -> 15 levels; ASIC core 745720 -> 745233 cells, FF 95842 -> 95845, 30 -> 30 stages. Trigger test: tests/regression/amo amo_misaligned (amoadd.w, lr.w, sc.w, amocas.w at misaligned addresses; lane 1 alone misaligned; a misaligned lane opening the second LSU packet and the second SIMD slice; an aligned atomic), checking mcause, mepc ^ mtval, rd, every word of memory, and that the instruction after the atomic runs exactly once, after the handler. CI cases parity-amo-misaligned and misaligned-simd-packets (8 threads, SIMD 4, ALU and LSU lanes 2); the base, zacas, wb, threads8 and mc amo cases run it too. On master the RTL asserts "misaligned memory access" and SimX aborts. Co-Authored-By: Claude Opus 5.5 --- ci/testcases/isa/amo.yaml | 23 ++- docs/designs/atomic_memory_operations.md | 22 +++ docs/designs/lsu_pipeline_design.md | 8 +- docs/designs/trap_and_exception_foundation.md | 1 + hw/rtl/VX_gpu_pkg.sv | 6 + hw/rtl/VX_trace_pkg.sv | 2 + hw/rtl/core/VX_alu_int.sv | 49 ++++++- hw/rtl/core/VX_decode.sv | 3 + hw/rtl/core/VX_lsu_block.sv | 66 ++++++++- hw/rtl/core/VX_uop_amo.sv | 48 +++++-- hw/rtl/core/VX_uop_sequencer.sv | 11 +- sim/simx/alu_unit.cpp | 24 +++- sim/simx/alu_unit.h | 7 +- sim/simx/decode.cpp | 10 +- sim/simx/lsu_unit.cpp | 98 ++++++++++++- sim/simx/lsu_unit.h | 17 +++ sim/simx/scheduler.cpp | 36 +++-- sim/simx/scheduler.h | 1 + sim/simx/sequencer.cpp | 7 + sim/simx/types.h | 9 +- tests/regression/amo/common.h | 10 ++ tests/regression/amo/kernel.cpp | 135 ++++++++++++++++++ tests/regression/amo/testcases.h | 52 +++++++ 23 files changed, 589 insertions(+), 56 deletions(-) diff --git a/ci/testcases/isa/amo.yaml b/ci/testcases/isa/amo.yaml index cb866e70a4..dbf1896f98 100644 --- a/ci/testcases/isa/amo.yaml +++ b/ci/testcases/isa/amo.yaml @@ -7,7 +7,7 @@ defaults: configs: "-DVX_CFG_EXT_A_ENABLE" xlen: [32, 64] tier: smoke - touches: [hw/rtl/cache, hw/rtl/mem, hw/rtl/core/VX_decode.sv, hw/rtl/core/VX_lsu_block.sv, sim/simx/amo, sim/simx/mem] + touches: [hw/rtl/cache, hw/rtl/mem, hw/rtl/core/VX_decode.sv, hw/rtl/core/VX_lsu_block.sv, hw/rtl/core/VX_uop_amo.sv, hw/rtl/core/VX_alu_int.sv, sim/simx/amo, sim/simx/mem, sim/simx/lsu_unit.cpp, sim/simx/alu_unit.cpp] tests: # RISC-V 'A' ISA conformance (rv{XLEN}ua-p-{amo*,lrsc}); shares the A-built sim. - id: isa-conformance @@ -62,6 +62,27 @@ tests: app: amo xlen: [32] args: "-n32 -t amoadd_scatter" + # The misaligned-atomic trap in cycles: the atomic's alignment check (an ALU + # branch-port uop) resolves its warp's fetch stall and traps; the LSU drops + # the atomic. SimX must trap and release the warp at the RTL's point. + - id: parity-amo-misaligned + check: model_parity + via: blackbox + app: amo + xlen: [32] + configs+: "-DVX_CFG_EXT_ZACAS_ENABLE" + args: "-n32 -t amo_misaligned" + # Two SIMD slices of two ALU and two LSU packets each (8 threads, SIMD 4, + # 2 lanes): the check holds a misaligned lane of an earlier packet or slice + # until the last, and the LSU drops every packet from the misaligned one on + # while the ones ahead of it perform. + - id: misaligned-simd-packets + app: amo + drivers: [simx, rtlsim] + xlen: [32] + configs+: "-DVX_CFG_EXT_ZACAS_ENABLE -DVX_CFG_SIMD_WIDTH=4 -DVX_CFG_NUM_ALU_LANES=2 -DVX_CFG_NUM_LSU_LANES=2" + shape: {threads: 8} + args: "-n8 -t amo_misaligned" # Four operand collectors and two SIMD packets per warp (32 warps x 8 threads, # SIMD 4): a three-operand compare-and-swap stalls between its packets on # bank conflicts, and the operand arbiter must keep an instruction's packets diff --git a/docs/designs/atomic_memory_operations.md b/docs/designs/atomic_memory_operations.md index c8014786e8..c5d6810031 100644 --- a/docs/designs/atomic_memory_operations.md +++ b/docs/designs/atomic_memory_operations.md @@ -86,6 +86,27 @@ so the operation field stays four bits wide. is the `funct5` bits and is otherwise unused โ€” so the comparand reaches the LSU as `rs3_data` with no new operand path. +A misaligned atomic (`.W` not on 4 bytes, `.D` not on 8) is never split: it +raises the RISC-V address-misaligned exception, load (`mcause` 4) for `LR`, +store/AMO (6) for every other atomic, with `mepc` the atomic. `mtval` stays +read-only zero. The trap is precise: no instruction after the atomic runs +before the handler, and the handler may return to `mepc + 4`. + +- **The check.** The uop sequencer (`VX_uop_amo`) puts an alignment check + ahead of every atomic: a branch-port uop on `rs1` (`INST_BR_AMOCHK`, + `INST_BR_LRCHK`) that the ALU resolves like a branch, not taken when every + active lane is aligned, a trap otherwise. The ALU holds a misaligned lane of + an earlier SIMD slice or packet until the instruction's last, which resolves + the check. The atomic stalls its warp's fetch at decode, as a branch does, + until the check resolves. +- **The drop.** The LSU decides the same from `rs1` on its own: from the packet + holding the first misaligned lane on, the atomic's packets perform no access + and retire through the no-response path writing nothing. Packets ahead of it + have gone to memory, so with several LSU packets per warp their lanes + perform their atomics. + +The stall costs an atomic a branch's fetch bubble. + --- ## 3. The LSU sideband @@ -429,6 +450,7 @@ is the LLC. | 15, 16 | `cas_ladder`, `cas_lmem` | every hart races compare-and-swap for each slot; compiled only with Zacas | | 17, 18 | `cas_counter`, `cas_counter_lmem` | compare-and-swap increment from a nonzero base | | `amoadd_scatter`, `amo_line_burst` | atomics into more lines than the dcache holds; groups of harts adding into distinct words of cold lines | the counts, with missing atomics replaying back to back into the LLC | +| last | `amo_misaligned` | `amoadd.w`, `lr.w`, `sc.w`, `amocas.w` at misaligned addresses; a warp whose lane 1 alone is misaligned; a misaligned lane opening the second LSU packet, and the second SIMD slice: `mcause`, `mepc` (and `mtval` 0), the instruction after the atomic running once and only after the handler, `rd` and every word of memory | The `amo` suite in [`ci/testcases/isa/amo.yaml`](../../ci/testcases/isa/amo.yaml) runs them on both simulators at both `XLEN`s, with and without Zacas, on a diff --git a/docs/designs/lsu_pipeline_design.md b/docs/designs/lsu_pipeline_design.md index 276ffd39f1..7acc0136cc 100644 --- a/docs/designs/lsu_pipeline_design.md +++ b/docs/designs/lsu_pipeline_design.md @@ -146,7 +146,11 @@ level needs to know the aperture exists. | request direction | store, unless the instruction is an atomic โ€” atomics always travel as `rw = 0` | **Misaligned access is not supported.** An address that is not a multiple of -the access size is a runtime assertion, not a handled fault. +the access size is a runtime assertion, not a handled fault -- except for an +atomic: its warp traps through the atomic's alignment check, and the block +drops it from the packet holding its first misaligned lane on, retiring those +packets through the no-response path with an empty mask +(atomic_memory_operations.md). ### 3.3 Fence ordering @@ -458,7 +462,7 @@ into a failure at the slot that lost it. ## 10. Not implemented - **Misaligned access.** No splitting; the software contract requires - aligned addresses. + aligned addresses (a misaligned atomic traps instead). - **Prefetching.** Every request is demand-driven. - **Address-ranged fences.** A fence is a total barrier on its slice; there is no ordering between specific address ranges. diff --git a/docs/designs/trap_and_exception_foundation.md b/docs/designs/trap_and_exception_foundation.md index 3f5248f2a0..f8c5ca3804 100644 --- a/docs/designs/trap_and_exception_foundation.md +++ b/docs/designs/trap_and_exception_foundation.md @@ -147,6 +147,7 @@ misaligned, so only there does `ma_fetch` reach the trap. | 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 | +| 4 load address misaligned (`LR`), 6 store/AMO address misaligned | branch unit, the atomic's alignment check uop | the atomic | 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 diff --git a/hw/rtl/VX_gpu_pkg.sv b/hw/rtl/VX_gpu_pkg.sv index 2224ba0721..1d9ab47ff4 100644 --- a/hw/rtl/VX_gpu_pkg.sv +++ b/hw/rtl/VX_gpu_pkg.sv @@ -518,6 +518,12 @@ package VX_gpu_pkg; /////////////////////////////////////////////////////////////////////////// localparam INST_BR_BEQ = 4'b0000; + // An atomic's alignment check (VX_uop_amo): a store/AMO (AMOCHK) or load + // (LRCHK) address-misaligned trap when an active lane's rs1 is not aligned + // to the access size. It targets PC + 4, and its br_args.is_rvc bit says + // the access is 8 bytes (an atomic is never compressed). + localparam INST_BR_AMOCHK = 4'b0001; + localparam INST_BR_LRCHK = 4'b0011; localparam INST_BR_BNE = 4'b0010; localparam INST_BR_BLTU = 4'b0100; localparam INST_BR_BGEU = 4'b0110; diff --git a/hw/rtl/VX_trace_pkg.sv b/hw/rtl/VX_trace_pkg.sv index 48626a3bf5..c4dba13fe8 100644 --- a/hw/rtl/VX_trace_pkg.sv +++ b/hw/rtl/VX_trace_pkg.sv @@ -129,6 +129,8 @@ package VX_trace_pkg; ALU_TYPE_BRANCH: begin case (INST_BR_BITS'(op_type)) INST_BR_BEQ: `TRACE(level, ("BEQ")) + INST_BR_AMOCHK:`TRACE(level, ("AMOCHK")) + INST_BR_LRCHK: `TRACE(level, ("LRCHK")) INST_BR_BNE: `TRACE(level, ("BNE")) INST_BR_BLT: `TRACE(level, ("BLT")) INST_BR_BGE: `TRACE(level, ("BGE")) diff --git a/hw/rtl/core/VX_alu_int.sv b/hw/rtl/core/VX_alu_int.sv index b7ea7866ee..66fdb33309 100644 --- a/hw/rtl/core/VX_alu_int.sv +++ b/hw/rtl/core/VX_alu_int.sv @@ -275,6 +275,30 @@ module VX_alu_int import VX_gpu_pkg::*; #( // uuid). wire is_wgather = (execute_if.data.op_args.alu.xtype == ALU_TYPE_OTHER) && alu_op[3]; wire is_jmp_misaligned = br_misaligned && inst_br_is_static(br_op); + +`ifdef VX_CFG_EXT_A_ENABLE + // An atomic's alignment check traps when an active lane's address is not + // aligned to the access size. Its SIMD slices and packets reach the block + // back to back, so a misaligned lane of an earlier one is held until the + // last, which resolves the check. + wire is_chk = is_br_op && (br_op[3:2] == 2'b00) && br_op[0]; + wire chk_is_d = execute_if.data.op_args.br.is_rvc; + wire [NUM_LANES-1:0] chk_lane_mis; + for (genvar i = 0; i < NUM_LANES; ++i) begin : g_chk_lane_mis + assign chk_lane_mis[i] = execute_if.data.header.tmask[i] + && ((alu_in1[i][1:0] != 2'b00) || (chk_is_d && alu_in1[i][2])); + end + reg chk_mis_r; + wire chk_mis = chk_mis_r || (| chk_lane_mis); + always @(posedge clk) begin + if (reset) begin + chk_mis_r <= 1'b0; + end else if (execute_if.valid && execute_if.ready && is_chk) begin + chk_mis_r <= chk_mis && ~execute_if.data.header.eop; + end + end + wire chk_trap = is_chk && chk_mis; +`endif wire [NUM_LANES-1:0] alu_tmask = is_jmp_misaligned ? NUM_LANES'(0) : is_wgather ? ~wg_src_mask : execute_if.data.header.tmask; @@ -291,21 +315,27 @@ module VX_alu_int import VX_gpu_pkg::*; #( wire is_rvc_r; `endif wire br_misaligned_r; + wire chk_trap_r; VX_elastic_buffer #( - .DATAW ($bits(header_t) + (NUM_LANES * `VX_CFG_XLEN) + PC_BITS + 1 + INST_BR_BITS + LANE_WIDTH + 1 + `VX_CFG_EXT_C_ENABLED) + .DATAW ($bits(header_t) + (NUM_LANES * `VX_CFG_XLEN) + PC_BITS + 1 + INST_BR_BITS + LANE_WIDTH + 1 + `VX_CFG_EXT_C_ENABLED + `VX_CFG_EXT_A_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, br_misaligned - `ifdef VX_CFG_EXT_C_ENABLE , is_rvc_in `endif }), + `ifdef VX_CFG_EXT_C_ENABLE , is_rvc_in `endif + `ifdef VX_CFG_EXT_A_ENABLE , chk_trap `endif }), .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 }), + `ifdef VX_CFG_EXT_C_ENABLE , is_rvc_r `endif + `ifdef VX_CFG_EXT_A_ENABLE , chk_trap_r `endif }), .valid_out (result_if.valid), .ready_out (result_if.ready) ); +`ifndef VX_CFG_EXT_A_ENABLE + assign chk_trap_r = 1'b0; +`endif `UNUSED_VAR (br_op_r) wire is_br_neg = inst_br_is_neg(br_op_r); @@ -320,16 +350,17 @@ module VX_alu_int import VX_gpu_pkg::*; #( 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; - // Synchronous traps: ECALL/EBREAK, an illegal instruction and a taken - // transfer to a misaligned target enter a trap (redirect to mtvec); + // Synchronous traps: ECALL/EBREAK, an illegal instruction, a taken + // transfer to a misaligned target and a misaligned atomic's check 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; + wire is_trap_entry = is_trap_op || is_misaligned || chk_trap_r; // 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 + wire [PC_BITS-1:0] br_dest = (is_trap_op || br_misaligned_r || chk_trap_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); @@ -341,6 +372,10 @@ module VX_alu_int import VX_gpu_pkg::*; #( case (br_op_r) INST_BR_EBREAK: br_trap_cause = TRAP_CAUSE_BITS'(3); // breakpoint INST_BR_ILLEGAL: br_trap_cause = TRAP_CAUSE_BITS'(2); // illegal instruction + `ifdef VX_CFG_EXT_A_ENABLE + INST_BR_LRCHK: br_trap_cause = TRAP_CAUSE_BITS'(4); // load address misaligned + INST_BR_AMOCHK: br_trap_cause = TRAP_CAUSE_BITS'(6); // store/AMO address misaligned + `endif default: br_trap_cause = TRAP_CAUSE_BITS'(11); // ECALL from M-mode endcase end diff --git a/hw/rtl/core/VX_decode.sv b/hw/rtl/core/VX_decode.sv index 12539f0b74..120d08b2b0 100644 --- a/hw/rtl/core/VX_decode.sv +++ b/hw/rtl/core/VX_decode.sv @@ -532,6 +532,9 @@ module VX_decode import op_args.lsu.amo_unsigned = amo_unsigned; op_args.lsu.amo_aq = instr[26]; op_args.lsu.amo_rl = instr[25]; + // The warp fetches on once the atomic's alignment check + // (VX_uop_amo) resolves through the branch port. + is_wstall = 1; `USED_IREG (rd); `USED_IREG (rs1); // LR has rs2=x0; the LSU just sees rs2_data=0 as rhs. diff --git a/hw/rtl/core/VX_lsu_block.sv b/hw/rtl/core/VX_lsu_block.sv index 8758f3e1a8..92e2a8e5a1 100644 --- a/hw/rtl/core/VX_lsu_block.sv +++ b/hw/rtl/core/VX_lsu_block.sv @@ -249,6 +249,37 @@ module VX_lsu_block import VX_gpu_pkg::*; #( wire no_rsp_buf_valid, no_rsp_buf_ready; wire [LSUQ_SIZEW-1:0] pkt_waddr, pkt_raddr; + // requests of the packet still being sent (its slot not yet acquired) are + // out at memory + wire pkt_open; + + // misaligned atomics + + // An atomic with an active lane whose address (rs1) is not aligned to the + // access size performs no access: its warp traps through the atomic's + // alignment check (VX_uop_amo), and this packet and the instruction's + // later ones retire writing nothing. The instruction's earlier packets + // have gone to memory. + wire req_fault; +`ifdef VX_CFG_EXT_A_ENABLE + wire req_is_d = (inst_lsu_wsize(execute_if.data.op_type) == 2'd3); + wire [NUM_LANES-1:0] req_lane_mis; + for (genvar i = 0; i < NUM_LANES; ++i) begin : g_req_lane_mis + assign req_lane_mis[i] = execute_if.data.header.tmask[i] + && ((execute_if.data.rs1_data[i][1:0] != 2'b00) || (req_is_d && execute_if.data.rs1_data[i][2])); + end + reg fault_open_r; + assign req_fault = execute_if.data.op_args.lsu.amo_valid && (fault_open_r || (| req_lane_mis)); + always @(posedge clk) begin + if (reset) begin + fault_open_r <= 1'b0; + end else if (execute_if.valid && execute_if.ready) begin + fault_open_r <= req_fault && ~execute_if.data.header.eop; + end + end +`else + assign req_fault = 1'b0; +`endif // fence handling @@ -270,20 +301,30 @@ module VX_lsu_block import VX_gpu_pkg::*; #( end wire req_skip = req_is_fence && ~execute_if.data.header.eop; - wire no_rsp_buf_enable = (mem_req_rw && ~execute_if.data.header.wb) || req_skip; + // A faulted packet that ends its instruction, while packets before it + // have gone to memory, waits for their responses: they opened the packet + // slot its end-of-packet request would have acquired, so the next + // instruction's packet could otherwise start in a slot still counting + // them, and the instruction's end would retire ahead of their data. + wire fault_hold = req_fault && execute_if.data.header.eop && pkt_open; + // retires through the no-response path without a memory request + wire req_bypass = req_skip || req_fault; + wire no_rsp_buf_enable = (mem_req_rw && ~execute_if.data.header.wb) || req_bypass; assign mem_req_valid = execute_if.valid - && ~req_skip + && ~req_bypass && ~(no_rsp_buf_enable && ~no_rsp_buf_ready) && ~fence_lock; assign no_rsp_buf_valid = execute_if.valid && no_rsp_buf_enable - && (req_skip || mem_req_ready) + && (req_bypass || mem_req_ready) + && ~fault_hold && ~fence_lock; - assign execute_if.ready = (mem_req_ready || req_skip) + assign execute_if.ready = (mem_req_ready || req_bypass) && ~(no_rsp_buf_enable && ~no_rsp_buf_ready) + && ~fault_hold && ~fence_lock; // A fence is sent on every lane, whatever the thread mask: the block's @@ -337,10 +378,10 @@ module VX_lsu_block import VX_gpu_pkg::*; #( assign mem_req_byteen[i] = mem_req_byteen_w; end - // memory misalignment not supported! + // memory misalignment not supported! (a misaligned atomic traps instead) for (genvar i = 0; i < NUM_LANES; ++i) begin : g_missalign wire lsu_req_fire = execute_if.valid && execute_if.ready; - `RUNTIME_ASSERT((~lsu_req_fire || ~execute_if.data.header.tmask[i] || req_is_fence || (full_addr[i] % (1 << inst_lsu_wsize(execute_if.data.op_type))) == 0), + `RUNTIME_ASSERT((~lsu_req_fire || ~execute_if.data.header.tmask[i] || req_is_fence || req_fault || (full_addr[i] % (1 << inst_lsu_wsize(execute_if.data.op_type))) == 0), ("%t: misaligned memory access, wid=%0d, PC=0x%0h, addr=0x%0h, wsize=%0d! (#%0d)", $time, execute_if.data.header.wid, to_fullPC(execute_if.data.header.PC), full_addr[i], inst_lsu_wsize(execute_if.data.op_type), execute_if.data.header.uuid)) end @@ -423,6 +464,7 @@ module VX_lsu_block import VX_gpu_pkg::*; #( end assign mem_rsp_sop_pkt = pkt_sop[pkt_raddr]; + assign pkt_open = (pkt_ctr[pkt_waddr] != 0); assign mem_rsp_eop_pkt = mem_rsp_eop && pkt_eop[pkt_raddr] && (pkt_ctr[pkt_raddr] == 1); `RUNTIME_ASSERT(~(mem_req_rd_fire && full), ("allocator full!")) `RUNTIME_ASSERT(~(mem_req_rd_sop_fire && pkt_ctr[pkt_waddr] != 0), ("oops! broken sop request!")) @@ -434,6 +476,7 @@ module VX_lsu_block import VX_gpu_pkg::*; #( `UNUSED_VAR (mem_rsp_sop) end else begin : g_no_pid assign pkt_waddr = 0; + assign pkt_open = 1'b0; assign mem_rsp_sop_pkt = mem_rsp_sop; assign mem_rsp_eop_pkt = mem_rsp_eop; `UNUSED_VAR (pkt_raddr) @@ -580,6 +623,15 @@ module VX_lsu_block import VX_gpu_pkg::*; #( .ready_out (result_rsp_if.ready) ); + // a faulted atomic retires writing nothing + header_t no_rsp_hdr; + always @(*) begin + no_rsp_hdr = execute_if.data.header; + if (req_fault) begin + no_rsp_hdr.tmask = '0; + end + end + VX_elastic_buffer #( .DATAW ($bits(header_t)), .SIZE (2) @@ -588,7 +640,7 @@ module VX_lsu_block import VX_gpu_pkg::*; #( .reset (reset), .valid_in (no_rsp_buf_valid), .ready_in (no_rsp_buf_ready), - .data_in (execute_if.data.header), + .data_in (no_rsp_hdr), .data_out (result_no_rsp_if.data.header), .valid_out (result_no_rsp_if.valid), .ready_out (result_no_rsp_if.ready) diff --git a/hw/rtl/core/VX_uop_amo.sv b/hw/rtl/core/VX_uop_amo.sv index aaa92cdf1c..038f90da54 100644 --- a/hw/rtl/core/VX_uop_amo.sv +++ b/hw/rtl/core/VX_uop_amo.sv @@ -13,14 +13,23 @@ `include "VX_define.vh" -// VX_uop_amo: orders an atomic with the hart's other accesses. +// VX_uop_amo: checks an atomic's alignment and orders it with the hart's +// other accesses. +// +// check [release fence] atomic [acquire fence] +// +// The check is an ALU branch-port uop (INST_BR_AMOCHK, INST_BR_LRCHK on rs1) +// that raises the address-misaligned trap when an active lane's address is not +// aligned to the access size. Its warp stalls at decode until the check +// resolves, so the trap is precise; the LSU drops a misaligned atomic itself. +// The check reads the trap pseudo-register, as ECALL does, so that an older +// mtvec write has landed when it traps. // // An AMO, LR or SC with .rl is preceded by a release fence (earlier writes // become visible first) and one with .aq is followed by an acquire fence -// (later reads see what the atomic observed): -// [release fence] atomic [acquire fence] -// The fences are plain LSU fence ops carrying the acquire or release fence -// operation; they read and write no register. +// (later reads see what the atomic observed), unless the dcache is itself the +// point of coherence. The fences are plain LSU fence ops carrying the acquire +// or release fence operation; they read and write no register. module VX_uop_amo import VX_gpu_pkg::*; ( input clk, @@ -36,18 +45,35 @@ module VX_uop_amo import VX_gpu_pkg::*; ( ); `UNUSED_VAR ({clk, reset, start, advance}) - wire aq = ibuf_in.op_args.lsu.amo_aq; - wire rl = ibuf_in.op_args.lsu.amo_rl; - assign uop_count = UOP_CTR_W'(1) + UOP_CTR_W'(aq) + UOP_CTR_W'(rl); + wire aq = ibuf_in.op_args.lsu.amo_aq && !DCACHE_SHARED_LLC; + wire rl = ibuf_in.op_args.lsu.amo_rl && !DCACHE_SHARED_LLC; + assign uop_count = UOP_CTR_W'(2) + UOP_CTR_W'(aq) + UOP_CTR_W'(rl); - wire [UOP_CTR_W-1:0] amo_idx = UOP_CTR_W'(rl); - wire is_rel = rl && (uop_idx == '0); + wire [UOP_CTR_W-1:0] amo_idx = UOP_CTR_W'(1) + UOP_CTR_W'(rl); + wire is_chk = (uop_idx == '0); + wire is_rel = rl && (uop_idx == UOP_CTR_W'(1)); wire is_acq = aq && (uop_idx == amo_idx + UOP_CTR_W'(1)); + wire is_lr = (ibuf_in.op_args.lsu.amo_op == AMO_OP_LR); + wire is_d = (inst_lsu_wsize(INST_LSU_BITS'(ibuf_in.op_type)) == 2'd3); ibuffer_t ibuf_r; always_comb begin ibuf_r = ibuf_in; - if (is_rel || is_acq) begin + if (is_chk) begin + ibuf_r.ex_type = EX_ALU; + ibuf_r.op_type = INST_OP_BITS'(is_lr ? INST_BR_LRCHK : INST_BR_AMOCHK); + ibuf_r.op_args = '0; + ibuf_r.op_args.br.xtype = ALU_TYPE_BRANCH; + ibuf_r.op_args.br.use_PC = 1'b1; + ibuf_r.op_args.br.use_imm = 1'b1; + ibuf_r.op_args.br.imm20 = 20'd4; + ibuf_r.op_args.br.is_rvc = is_d; + ibuf_r.wb = 1'b0; + ibuf_r.rd_xregs = '0; + ibuf_r.rd_xregs[XREG_2] = 1'b1; + ibuf_r.wr_xregs = '0; + ibuf_r.used_rs = NUM_SRC_OPDS'(1); + end else if (is_rel || is_acq) begin ibuf_r.op_type = INST_OP_BITS'(INST_LSU_FENCE); ibuf_r.op_args = '0; ibuf_r.op_args.lsu.amo_op = amo_op_e'(fence_op_t'{io: 1'b0, sys: 1'b0, rel: is_rel, acq: is_acq}); diff --git a/hw/rtl/core/VX_uop_sequencer.sv b/hw/rtl/core/VX_uop_sequencer.sv index 2b888a8cce..c6f92fd869 100644 --- a/hw/rtl/core/VX_uop_sequencer.sv +++ b/hw/rtl/core/VX_uop_sequencer.sv @@ -195,14 +195,11 @@ module VX_uop_sequencer import `ifdef VX_CFG_EXT_A_ENABLE // ------------------------------------------------------------------ - // Atomic ordering uop expander: fences around an AMO/LR/SC with .aq/.rl + // Atomic uop expander: the alignment check, and fences around an + // AMO/LR/SC with .aq/.rl // ------------------------------------------------------------------ - // A dcache that is itself the point of coherence orders the atomic with - // the hart's other accesses already: no fence is owed. - assign uop_in_valid[UOP_AMO] = !DCACHE_SHARED_LLC - && (uop_in_data.ex_type == EX_LSU) - && uop_in_data.op_args.lsu.amo_valid - && (uop_in_data.op_args.lsu.amo_aq || uop_in_data.op_args.lsu.amo_rl); + assign uop_in_valid[UOP_AMO] = (uop_in_data.ex_type == EX_LSU) + && uop_in_data.op_args.lsu.amo_valid; VX_uop_amo uop_amo ( .clk (clk), .reset (reset), diff --git a/sim/simx/alu_unit.cpp b/sim/simx/alu_unit.cpp index cbcfa82ff5..8564b361e7 100644 --- a/sim/simx/alu_unit.cpp +++ b/sim/simx/alu_unit.cpp @@ -74,6 +74,7 @@ uint32_t AluUnit::latency_of(const instr_trace_t* trace) const { case BrType::JALR: case BrType::SYS: case BrType::ILLEGAL: + case BrType::AMOCHK: return 0; default: std::abort(); @@ -100,7 +101,7 @@ uint32_t AluUnit::latency_of(const instr_trace_t* trace) const { std::abort(); } -void AluUnit::execute(instr_trace_t *trace) { +void AluUnit::execute(uint32_t b, instr_trace_t *trace) { // Use trace->tmask captured at issue for per-thread active checks. // The warp's live thread mask may change due to divergent // control flow before this trace executes; the rest of the pipeline @@ -463,6 +464,24 @@ void AluUnit::execute(instr_trace_t *trace) { ++branches_; } break; + case BrType::AMOCHK: { + // The atomic traps when an active lane's address is not aligned to + // its size; the LSU drops the atomic itself. + bool mis = chk_mis_.at(b); + for (uint32_t t = thread_start; t < num_threads; ++t) { + if (tmask.test(t) && (rs1_data[t].u % brArgs.offset) != 0) { + mis = true; + } + } + chk_mis_.at(b) = mis && !trace->eop; + if (trace->eop) { + if (mis) { + // LR traps as a load, every other atomic as a store/AMO. + branch_ctl_.op = brArgs.cmp ? BranchCtl::LOAD_MISALIGNED : BranchCtl::AMO_MISALIGNED; + } + ++branches_; + } + } break; default: std::abort(); } @@ -608,6 +627,7 @@ void AluUnit::on_reset() { pending.clear(); } out_free_.fill(0); + chk_mis_.fill(false); } uint32_t AluUnit::output_wait(uint32_t b, uint64_t ready) { @@ -641,7 +661,7 @@ bool AluUnit::issue(uint32_t b, instr_trace_t*& trace) { if (output.full()) { return false; } - this->execute(trace); + this->execute(b, trace); uint32_t latency = this->latency_of(trace) + (kGather ? 1 : 0); uint32_t wait = this->output_wait(b, SimPlatform::instance().cycles() + latency); // A branch releases its warp as it resolves, not when its result diff --git a/sim/simx/alu_unit.h b/sim/simx/alu_unit.h index 42154b0ef2..b84b4ded5d 100644 --- a/sim/simx/alu_unit.h +++ b/sim/simx/alu_unit.h @@ -44,7 +44,7 @@ class AluUnit : public FuncUnit { // Per-unit functional execution. Called only from this unit's tick(). // Reads operands from trace->src_data, populates trace->dst_data, and // for a branch, fills branch_ctl_ with its outcome. - void execute(instr_trace_t* trace); + void execute(uint32_t b, instr_trace_t* trace); // The outcome of the branch execute() last resolved. BranchCtl branch_ctl_; @@ -62,6 +62,11 @@ class AluUnit : public FuncUnit { // Per block: the first cycle the output is free after the results already // gone from out_ready_. std::array out_free_{}; + + // Per block: an atomic's alignment check found a misaligned lane in an + // earlier packet of the instruction; its packets reach the block back to + // back, and the last one resolves the check. + std::array chk_mis_{}; }; } diff --git a/sim/simx/decode.cpp b/sim/simx/decode.cpp index 066005c29e..e49c531157 100644 --- a/sim/simx/decode.cpp +++ b/sim/simx/decode.cpp @@ -227,6 +227,7 @@ static op_string_t op_string(const Instr &instr) { std::abort(); } case BrType::ILLEGAL: return {"ILLEGAL", ""}; + case BrType::AMOCHK: return {brArgs.cmp ? "LRCHK" : "AMOCHK", ""}; default: std::abort(); } @@ -858,11 +859,10 @@ Instr::Ptr Decoder::decode(uint32_t code, uint64_t uuid, bool fs_off) { std::abort(); } instr->set_args(IntrAmoArgs{funct3, aq, rl}); - // .aq/.rl order the atomic with fences around it (LsuUopGen), unless the - // dcache is itself the point of coherence - if ((aq || rl) && !DCACHE_SHARED_LLC) { - instr->set_macro_op(); - } + // An atomic expands into its alignment check and, with .aq/.rl, the fences + // around it (LsuUopGen). The warp fetches on once the check resolves. + instr->set_macro_op(); + instr->set_wstall(true); instr->set_dest_reg(rd, RegType::Integer); instr->set_src_reg(0, rs1, RegType::Integer); instr->set_src_reg(1, rs2, RegType::Integer); diff --git a/sim/simx/lsu_unit.cpp b/sim/simx/lsu_unit.cpp index 5013fe7131..5c29fefba8 100644 --- a/sim/simx/lsu_unit.cpp +++ b/sim/simx/lsu_unit.cpp @@ -32,10 +32,16 @@ using namespace vortex; +// An atomic: check [release fence] atomic [acquire fence]. The fences order a +// .aq/.rl atomic with the hart's other accesses, unless the dcache is itself +// the point of coherence. +static bool amo_fenced(uint32_t bit) { + return bit && !DCACHE_SHARED_LLC; +} + uint32_t LsuUopGen::uop_count(const Instr& instr) { - // An atomic with .aq/.rl: [release fence] atomic [acquire fence] if (auto amo = std::get_if(&instr.get_args())) { - return 1 + amo->aq + amo->rl; + return 2 + amo_fenced(amo->aq) + amo_fenced(amo->rl); } // PACKLB.F: width=0 (LB), 4 byte-elements // PACKLH.F: width=1 (LH), 2 halfword-elements @@ -60,9 +66,24 @@ Instr::Ptr LsuUopGen::get(const Instr& macro_instr, uint32_t uop_index) { uint64_t uop_uuid = make_uop_uuid(parent_uuid, uop_index); if (auto amo = std::get_if(¯o_instr.get_args())) { + if (uop_index == 0) { + // The alignment check, a branch-port uop on rs1: it traps when an active + // lane's address is misaligned, and releases the warp as it resolves. + // It reads the trap state so that an older mtvec write has landed. + auto chk_instr = std::allocate_shared(pool_, uop_uuid, FUType::ALU); + chk_instr->set_parent_uuid(parent_uuid); + bool is_lr = (std::get(macro_instr.get_op_type()) == AmoType::LR); + chk_instr->set_op_type(BrType::AMOCHK); + chk_instr->set_args(IntrBrArgs{is_lr ? 1u : 0u, 0, 1u << (amo->width & 0x3)}); + auto rs1 = macro_instr.get_src_reg(0); + chk_instr->set_src_reg(0, rs1.idx, rs1.type); + chk_instr->set_fcsr_use(Instr::TRAP_CSRS, 0); + chk_instr->set_wstall(true); + return chk_instr; + } auto uop_instr = std::allocate_shared(pool_, uop_uuid, FUType::LSU); uop_instr->set_parent_uuid(parent_uuid); - uint32_t amo_index = amo->rl; + uint32_t amo_index = 1 + amo_fenced(amo->rl); if (uop_index == amo_index) { uop_instr->set_op_type(macro_instr.get_op_type()); uop_instr->set_args(*amo); @@ -346,6 +367,43 @@ void LsuUnit::compute_addrs(uint32_t b, instr_trace_t* trace) { state.remain_addrs = state.addr_list.size(); } +// An atomic with an active lane whose address (rs1) is not aligned to the +// access size performs no access from that lane's packet on: its warp traps +// through the atomic's alignment check, and the instruction's later packets +// fault too. Its earlier packets go to memory. The faulted lanes leave the +// mask, so they write nothing. +void LsuUnit::check_amo(uint32_t b, instr_trace_t* trace) { + auto& state = states_.at(b); + state.amo_checked = trace; + state.amo_partial = false; + state.amo_full = false; + auto amo_args = std::get(trace->instr_ptr->get_args()); + uint32_t size = 1u << (amo_args.width & 0x3); + auto& rs1_data = trace->src_data[0]; + uint32_t num_threads = VX_CFG_NUM_THREADS; + uint32_t from = num_threads; + if (state.amo_fault_open) { + from = 0; + } else { + for (uint32_t t = 0; t < num_threads; ++t) { + if (trace->tmask.test(t) && (rs1_data[t].u % size) != 0) { + from = t - (t % VX_CFG_NUM_LSU_LANES); + break; + } + } + } + bool faulted = (from < num_threads); + state.amo_fault_open = faulted && !trace->eop; + if (!faulted) + return; + for (uint32_t t = from; t < num_threads; ++t) { + trace->tmask.reset(t); + } + state.amo_partial = trace->tmask.any(); + state.amo_full = !state.amo_partial; + DT(3, this->name() << " amo-fault: from=" << from << ", " << *trace); +} + // Per-cycle response handler. Consumes at most one response packet // from lmem_switch and forwards through to writeback when the // originating request retires. Includes the per-lane response @@ -696,6 +754,16 @@ void LsuUnit::process_request_step(uint32_t b) { return; } + // A partially faulted atomic holds the block until its requests are + // answered. + if (state.fault_hold != nullptr) { + for (uint32_t i = 0; i < VX_CFG_LSU_PENDING_SIZE; ++i) { + if (state.pending_reqs.contains(i) && state.pending_reqs.at(i).trace == state.fault_hold) + return; + } + state.fault_hold = nullptr; + } + if (state.req_queue.empty()) return; @@ -728,6 +796,23 @@ void LsuUnit::process_request_step(uint32_t b) { return; } + if (is_amo && state.amo_checked != trace) { + this->check_amo(b, trace); + } + if (is_amo && state.amo_full) { + // performs no access: retires writing nothing + if (no_rsp_bufs_.at(b).full()) + return; // stall + assert(state.remain_addrs == 0); + // the read slot reserved at entry + --state.staged_reads; + state.amo_full = false; + state.amo_checked = nullptr; + no_rsp_bufs_.at(b).push(trace, 1); + state.pop_req(); + return; + } + // check pending queue capacity. AMO always needs a return โ†’ reserve // like a load, even though it carries write-bearing semantics. if ((!is_write || is_amo) && state.pending_reqs.full()) { @@ -900,6 +985,13 @@ void LsuUnit::process_request_step(uint32_t b) { } if (state.remain_addrs == 0) { + if (is_amo) { + if (state.amo_partial) { + state.fault_hold = trace; + } + state.amo_partial = false; + state.amo_checked = nullptr; + } if (direct_commit) { no_rsp_bufs_.at(b).push(trace, 1); } diff --git a/sim/simx/lsu_unit.h b/sim/simx/lsu_unit.h index 74b1ffe545..8edab33c42 100644 --- a/sim/simx/lsu_unit.h +++ b/sim/simx/lsu_unit.h @@ -127,6 +127,7 @@ class LsuUnit : public FuncUnit { PerfStats perf_; void compute_addrs(uint32_t b, instr_trace_t* trace); + void check_amo(uint32_t b, instr_trace_t* trace); // Per-cycle handlers. void process_response_step(uint32_t b); @@ -202,6 +203,17 @@ class LsuUnit : public FuncUnit { bool fence_token = false; std::vector addr_list; uint32_t remain_addrs = 0; + // Misaligned atomics (check_amo): the atomic last checked, whether a + // packet of the open instruction faulted (its later packets fault + // too), and whether the checked one faulted from a later packet on or + // in all of them. + instr_trace_t* amo_checked = nullptr; + bool amo_fault_open = false; + bool amo_partial = false; + bool amo_full = false; + // A partially faulted atomic whose requests are all sent: nothing + // passes it until they are answered, as its faulted end waits for them. + instr_trace_t* fault_hold = nullptr; void push_req(instr_trace_t* trace) { this->req_queue.push(trace); @@ -225,6 +237,11 @@ class LsuUnit : public FuncUnit { this->fence_token = false; this->addr_list.clear(); this->remain_addrs = 0; + this->amo_checked = nullptr; + this->amo_fault_open = false; + this->amo_partial = false; + this->amo_full = false; + this->fault_hold = nullptr; this->rsp_tracks.clear(); this->open_track = kNoTrack; } diff --git a/sim/simx/scheduler.cpp b/sim/simx/scheduler.cpp index dcfc0dad70..0190ff7d44 100644 --- a/sim/simx/scheduler.cpp +++ b/sim/simx/scheduler.cpp @@ -319,6 +319,17 @@ void Scheduler::set_tmask(uint32_t wid, const ThreadMask &tmask) { warp.tmask = tmask; } +// 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_MISALIGNED_FETCH = 0; + constexpr Word TRAP_CAUSE_ILLEGAL_INSTR = 2; + constexpr Word TRAP_CAUSE_BREAKPOINT = 3; + constexpr Word TRAP_CAUSE_LOAD_MISALIGNED = 4; + constexpr Word TRAP_CAUSE_AMO_MISALIGNED = 6; + constexpr Word TRAP_CAUSE_ECALL_MMODE = 11; +} + void Scheduler::branch_ctl(const BranchCtl &ctl) { switch (ctl.op) { case BranchCtl::RELEASE: @@ -341,6 +352,12 @@ void Scheduler::branch_ctl(const BranchCtl &ctl) { case BranchCtl::ILLEGAL: this->trigger_illegal(ctl.wid, ctl.trap_pc, ctl.code); break; + case BranchCtl::LOAD_MISALIGNED: + this->trigger_misaligned_amo(ctl.wid, ctl.trap_pc, TRAP_CAUSE_LOAD_MISALIGNED); + break; + case BranchCtl::AMO_MISALIGNED: + this->trigger_misaligned_amo(ctl.wid, ctl.trap_pc, TRAP_CAUSE_AMO_MISALIGNED); + break; } this->resume(ctl.wid); } @@ -408,15 +425,6 @@ void Scheduler::warp_ctl(const WarpCtl &ctl) { // Barrier handling lives in BarrierUnit. See barrier_unit.{h,cpp}. -// 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_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) { auto& warp = warps_.at(wid); warp.mstatus = (warp.mstatus & ~(MSTATUS_MIE | MSTATUS_MPIE)) @@ -455,6 +463,16 @@ void Scheduler::trigger_illegal(uint32_t wid, Word trap_pc, uint32_t code) { this->raise_trap(wid, TRAP_CAUSE_ILLEGAL_INSTR, trap_pc); } +void Scheduler::trigger_misaligned_amo(uint32_t wid, Word trap_pc, Word cause) { + if (csr_unit_->mtvec() == 0) { + std::cerr << "Error: misaligned atomic (mcause=" << cause + << ") with no trap handler (mtvec=0): " << core_->name() << ", wid=" << wid + << ", PC=0x" << std::hex << trap_pc << std::dec << std::endl; + std::abort(); + } + this->raise_trap(wid, cause, trap_pc); +} + void Scheduler::trigger_misaligned_fetch(uint32_t wid, Word trap_pc, uint32_t code, Word target) { if (csr_unit_->mtvec() == 0) { std::cerr << "Error: instruction address misaligned (mcause=" << TRAP_CAUSE_MISALIGNED_FETCH diff --git a/sim/simx/scheduler.h b/sim/simx/scheduler.h index 23187c8276..b00c52d5f4 100644 --- a/sim/simx/scheduler.h +++ b/sim/simx/scheduler.h @@ -185,6 +185,7 @@ class Scheduler : public SimObject { // 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); + void trigger_misaligned_amo(uint32_t wid, Word trap_pc, Word cause); // 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); diff --git a/sim/simx/sequencer.cpp b/sim/simx/sequencer.cpp index a9bb558b77..2763303546 100644 --- a/sim/simx/sequencer.cpp +++ b/sim/simx/sequencer.cpp @@ -120,6 +120,13 @@ instr_trace_t* Sequencer::get(instr_trace_t* trace) { uop_trace->src_regs[i] = uop_instr->get_src_reg(i); } uop_trace->wb = (uop_instr->get_dest_reg().type != RegType::None); + // A uop that stalls fetch takes over its macro's fetch stall: the warp + // fetches on as that uop resolves, not once the macro's last uop issues. + if (uop_instr->is_wstall()) { + uop_trace->fetch_stall = true; + uop_trace->resume_warp = true; + trace->instr_ptr->set_wstall(false); + } state_.current_uop = uop_trace; return uop_trace; diff --git a/sim/simx/types.h b/sim/simx/types.h index a5b80799e3..f5fbf3bc14 100644 --- a/sim/simx/types.h +++ b/sim/simx/types.h @@ -278,6 +278,10 @@ enum class BrType { JALR, SYS, ILLEGAL, + // An atomic's alignment check: traps when an active lane's rs1 is not a + // multiple of `offset` (the access size), with the load (cmp = 1, LR) or + // the store/AMO address-misaligned cause. + AMOCHK, }; struct IntrBrArgs { @@ -293,6 +297,7 @@ inline std::ostream &operator<<(std::ostream &os, const BrType& type) { case BrType::JALR: os << "JALR"; break; case BrType::SYS: os << "SYS"; break; case BrType::ILLEGAL: os << "ILLEGAL"; break; + case BrType::AMOCHK: os << "AMOCHK"; break; default: assert(false); } @@ -1248,7 +1253,9 @@ struct BranchCtl { ECALL, EBREAK, MRET, - ILLEGAL }; + ILLEGAL, + LOAD_MISALIGNED, + AMO_MISALIGNED }; uint32_t wid = 0; Op op = RELEASE; // JUMP and MISALIGNED: the target. diff --git a/tests/regression/amo/common.h b/tests/regression/amo/common.h index 578b9e36a7..70177a3f08 100644 --- a/tests/regression/amo/common.h +++ b/tests/regression/amo/common.h @@ -34,6 +34,16 @@ static inline uint32_t amo_burst_line(uint32_t i, uint32_t g) { // Initial word of the local-memory swap chain in the AMO op sweep. #define AMO_OPS_SWAP_INIT 0x5EED5EEDu +// amo_misaligned: each hart's words at shared + hid * AMO_MIS_BYTES, the value +// an atomic that does not perform leaves in rd, and the RISC-V trap causes. +#define AMO_MIS_BYTES 64 +#define AMO_MIS_RD 0x7777u +#define AMO_MIS_CAUSE_LOAD 4u // load address misaligned (LR) +#define AMO_MIS_CAUSE_AMO 6u // store/AMO address misaligned +static inline uint32_t amo_mis_word(uint32_t h, uint32_t k) { + return 0xa5000000u | ((h & 0xffffu) << 8) | k; +} + // Args shared between host (main.cpp) and the GPU kernel (kernel.cpp). // Layout matches dogfood's, with `iters` added for the contention loops. typedef struct { diff --git a/tests/regression/amo/kernel.cpp b/tests/regression/amo/kernel.cpp index c461a2d75b..2f87ae6c8e 100644 --- a/tests/regression/amo/kernel.cpp +++ b/tests/regression/amo/kernel.cpp @@ -550,6 +550,140 @@ void kernel_amo_line_burst(kernel_arg_t* __UNIFORM__ arg) { } +// Misaligned atomics trap precisely: store/AMO address misaligned (6), load +// address misaligned (4) for LR, mepc the atomic, mtval 0 (read-only zero). +// No instruction after the atomic runs before the handler: the handler +// records mcause in t1, mepc ^ mtval in t2 and the count of the instruction +// following the atomic in t6, and returns to mepc + 4, so that instruction +// runs exactly once, after it. A misaligned atomic writes no rd, and none of +// its lanes from the LSU packet holding its first misaligned lane on performs; +// lanes of earlier packets have gone to memory. +extern "C" void amo_misaligned_handler(); +asm (".align 4\n" + "amo_misaligned_handler:\n" + " csrr t0, mepc\n" + " csrr t2, mtval\n" + " xor t2, t2, t0\n" + " csrr t1, mcause\n" + " mv t6, t5\n" + " addi t0, t0, 4\n" + " csrw mepc, t0\n" + " mret\n"); + +// epc_ok: a trap's mepc is this atomic and its mtval is 0; precise: the +// instruction after the atomic ran once, and not before the trap +#define AMO_MIS_TRY(insn, cause, epc_ok, precise, ...) \ + do { \ + register uintptr_t _c asm("t1") = 0; \ + register uintptr_t _v asm("t2") = 0; \ + register uintptr_t _p asm("t4") = 0; \ + register uintptr_t _n asm("t5") = 0; \ + register uintptr_t _s asm("t6") = 0; \ + asm volatile ("la t4, 1f\n1: " insn "\n addi t5, t5, 1\n" \ + : "+r"(_c), "+r"(_v), "+r"(_p), "+r"(_n), "+r"(_s), \ + ##__VA_ARGS__); \ + cause = (uint32_t)_c; \ + epc_ok = (_c == 0) || (_v == _p); \ + precise = precise && (_n == 1) && (_s == 0); \ + } while (0) + +void kernel_amo_misaligned(kernel_arg_t* __UNIFORM__ arg) { + auto per_hart = (uint32_t*)arg->per_hart_addr; + const uint32_t hid = hart_id(); + const uint32_t lane = hid % VX_CFG_NUM_THREADS; + const uintptr_t base = (uintptr_t)arg->shared_addr + hid * AMO_MIS_BYTES; + uintptr_t saved_mtvec; + // mtvec is one per core and a block fills its core: every warp reads the + // old value before any installs the handler, and restores it only once all + // have trapped. + asm volatile ("csrr %0, mtvec" : "=r"(saved_mtvec)); + __syncthreads(); + asm volatile ("csrw mtvec, %0" :: "r"(&amo_misaligned_handler)); + + uint32_t ca = 0, cb = 0, cc = 0, cd = 0, cf = 0, cg = 0, ch = 0, cj = 0; + bool ok = false, precise = true; + uint32_t flags = 0; + { + register uintptr_t a asm("a4") = base + 2; + register uint32_t rd asm("a2") = AMO_MIS_RD; + register uint32_t rs asm("a5") = 1; + AMO_MIS_TRY("amoadd.w a2, a5, (a4)", ca, ok, precise, "+r"(rd) : "r"(a), "r"(rs) : "t0", "memory"); + flags |= (ok && rd == AMO_MIS_RD) ? 1u : 0u; + } + { + register uintptr_t a asm("a4") = base + 5; + register uint32_t rd asm("a2") = AMO_MIS_RD; + AMO_MIS_TRY("lr.w a2, (a4)", cb, ok, precise, "+r"(rd) : "r"(a) : "t0", "memory"); + flags |= (ok && rd == AMO_MIS_RD) ? 2u : 0u; + } + { + register uintptr_t a asm("a4") = base + 6; + register uint32_t rd asm("a2") = AMO_MIS_RD; + register uint32_t rs asm("a5") = 9; + AMO_MIS_TRY("sc.w a2, a5, (a4)", cc, ok, precise, "+r"(rd) : "r"(a), "r"(rs) : "t0", "memory"); + flags |= (ok && rd == AMO_MIS_RD) ? 4u : 0u; + } +#if VX_CFG_EXT_ZACAS_ENABLED + { + register uintptr_t a asm("a4") = base + 10; + register uint32_t rd asm("a2") = AMO_MIS_RD; + register uint32_t rs asm("a5") = 9; + AMO_MIS_TRY(".insn r 0x2f, 2, 0x14, a2, a4, a5", cd, ok, precise, "+r"(rd) // amocas.w + : "r"(a), "r"(rs) : "t0", "memory"); + flags |= (ok && rd == AMO_MIS_RD) ? 8u : 0u; + } +#else + flags |= 8u; +#endif + { + // lane 1 alone misaligned: the whole warp traps, and lane 1's packet and + // every later one perform nothing (word 4) + register uintptr_t a asm("a4") = base + ((lane == 1) ? 18 : 16); + register uint32_t rd asm("a2") = AMO_MIS_RD; + register uint32_t rs asm("a5") = 1; + AMO_MIS_TRY("amoadd.w a2, a5, (a4)", cf, ok, precise, "+r"(rd) : "r"(a), "r"(rs) : "t0", "memory"); + flags |= (ok && rd == AMO_MIS_RD) ? 16u : 0u; + } + { + // aligned: performs (word 3) and does not trap + register uintptr_t a asm("a4") = base + 12; + register uint32_t rd asm("a2") = AMO_MIS_RD; + register uint32_t rs asm("a5") = 1; + AMO_MIS_TRY("amoadd.w a2, a5, (a4)", cg, ok, precise, "+r"(rd) : "r"(a), "r"(rs) : "t0", "memory"); + flags |= (ok && rd == amo_mis_word(hid, 3)) ? 32u : 0u; + } + { + // the misaligned lane opens the warp's second LSU packet: the first + // packet's lanes perform (word 5), the rest do not (a warp of one packet + // has no misaligned lane here) + register uintptr_t a asm("a4") = base + ((lane == VX_CFG_NUM_LSU_LANES) ? 22 : 20); + register uint32_t rd asm("a2") = AMO_MIS_RD; + register uint32_t rs asm("a5") = 1; + AMO_MIS_TRY("amoadd.w a2, a5, (a4)", ch, ok, precise, "+r"(rd) : "r"(a), "r"(rs) : "t0", "memory"); + const bool done = (lane < VX_CFG_NUM_LSU_LANES); + flags |= (ok && rd == (done ? amo_mis_word(hid, 5) : AMO_MIS_RD)) ? 64u : 0u; + } + { + // the misaligned lane opens the warp's second SIMD slice: the first + // slice's lanes perform (word 8), the rest do not (a warp of one slice + // has no misaligned lane here) + register uintptr_t a asm("a4") = base + ((lane == VX_CFG_SIMD_WIDTH) ? 34 : 32); + register uint32_t rd asm("a2") = AMO_MIS_RD; + register uint32_t rs asm("a5") = 1; + AMO_MIS_TRY("amoadd.w a2, a5, (a4)", cj, ok, precise, "+r"(rd) : "r"(a), "r"(rs) : "t0", "memory"); + const bool done = (lane < VX_CFG_SIMD_WIDTH); + flags |= (ok && rd == (done ? amo_mis_word(hid, 8) : AMO_MIS_RD)) ? 128u : 0u; + } + flags |= precise ? 256u : 0u; + __syncthreads(); + asm volatile ("csrw mtvec, %0" :: "r"(saved_mtvec)); + // causes in 4-bit fields: amoadd, lr, sc, amocas, lane 1, aligned, packet, + // slice; the checks in the hart's last word + per_hart[hid] = (ca & 15) | ((cb & 15) << 4) | ((cc & 15) << 8) | ((cd & 15) << 12) + | ((cf & 15) << 16) | ((cg & 15) << 20) | ((ch & 15) << 24) | ((cj & 15) << 28); + ((volatile uint32_t*)base)[AMO_MIS_BYTES / 4 - 1] = flags; +} + static const PFN_Kernel sc_tests[] = { kernel_amoadd, // 0 kernel_amoor, // 1 @@ -575,6 +709,7 @@ static const PFN_Kernel sc_tests[] = { #endif kernel_amoadd_scatter, // atomics spread over more lines than the dcache holds kernel_amo_line_burst, // atomics to distinct words of cold lines, in bursts + kernel_amo_misaligned, // misaligned atomics trap precisely }; __kernel void kernel_main(kernel_arg_t* __UNIFORM__ arg) { diff --git a/tests/regression/amo/testcases.h b/tests/regression/amo/testcases.h index 505644037a..40be3e9818 100644 --- a/tests/regression/amo/testcases.h +++ b/tests/regression/amo/testcases.h @@ -611,6 +611,57 @@ class Test_AMO_LINE_BURST : public ITestCase { } }; +// Misaligned atomics trap precisely and change nothing from the LSU packet +// holding the first misaligned lane on; aligned ones still run. +class Test_AMO_MISALIGNED : public ITestCase { +public: + Test_AMO_MISALIGNED(TestSuite* s, uint32_t num_threads_per_warp) + : ITestCase(s, "amo_misaligned"), num_threads_(num_threads_per_warp) {} + void setup(uint32_t n, uint32_t* shared, uint32_t*) override { + for (uint32_t h = 0; h < n; ++h) + for (uint32_t k = 0; k < AMO_MIS_BYTES / 4; ++k) + shared[h * (AMO_MIS_BYTES / 4) + k] = amo_mis_word(h, k); + } + int verify(uint32_t n, uint32_t, + const uint32_t* shared, const uint32_t* per_hart) override { + const uint32_t st = AMO_MIS_CAUSE_AMO, ld = AMO_MIS_CAUSE_LOAD; + const uint32_t cas = VX_CFG_EXT_ZACAS_ENABLED ? st : 0u; + // a warp of one LSU packet, or of one SIMD slice, has no misaligned lane + // in the packet and slice tries + const bool packets = num_threads_ > VX_CFG_NUM_LSU_LANES; + const bool slices = num_threads_ > VX_CFG_SIMD_WIDTH; + const uint32_t expected = st | (ld << 4) | (st << 8) | (cas << 12) | (st << 16) + | ((packets ? st : 0u) << 24) | ((slices ? st : 0u) << 28); + const uint32_t last = AMO_MIS_BYTES / 4 - 1; + int errors = 0; + for (uint32_t h = 0; h < n; ++h) { + if (per_hart[h] != expected && errors++ < 4) { + std::cout << " hart " << h << " causes=0x" << std::hex << per_hart[h] + << " expected=0x" << expected << std::dec << std::endl; + } + const uint32_t lane = h % num_threads_; + for (uint32_t k = 0; k < AMO_MIS_BYTES / 4; ++k) { + // word 3 takes the aligned atomic; words 5 and 8 the lanes ahead of + // the misaligned packet and slice + const bool add = (k == 3) + || (k == 5 && lane < VX_CFG_NUM_LSU_LANES) + || (k == 8 && lane < VX_CFG_SIMD_WIDTH); + const uint32_t want = (k == last) ? 0x1ffu : amo_mis_word(h, k) + (add ? 1u : 0u); + if (shared[h * (AMO_MIS_BYTES / 4) + k] != want && errors++ < 4) { + std::cout << " hart " << h << " word " << k << "=0x" << std::hex + << shared[h * (AMO_MIS_BYTES / 4) + k] << " expected=0x" << want + << std::dec << std::endl; + } + } + } + return errors; + } + +private: + uint32_t num_threads_; +}; + + inline TestSuite::TestSuite(vx_device_h device) : device_(device) { // Test_ATOMIC_CRITICAL needs to know VX_CFG_NUM_THREADS to compute the // expected count; query it from the device caps. @@ -644,6 +695,7 @@ inline TestSuite::TestSuite(vx_device_h device) : device_(device) { #endif add_test(new Test_AMOADD_SCATTER(this)); add_test(new Test_AMO_LINE_BURST(this)); + add_test(new Test_AMO_MISALIGNED(this, (uint32_t)num_threads)); } inline TestSuite::~TestSuite() {