-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Segmentation Fault Caused by mhpmcounter10 Access Not Implemented in DUT and Spike #129
Comments
Hi,
I would say, better focus on getting things ok with RVLS, and omit this one.
So, when i have crashed happening in RVLS (when called from SpinalSim), then what i do, is to ask the testbench to generate the trace.log file (--trace argument should do it), which contains all the execution trace in the format RVLS is able to read. Just be sure to compile riscv-isa-sim in debug mode by adding : So in short, figuring out why RVLS crash is the first thing to do :) The implement intends of NaxRiscv is to match the RISC-V spec, but there may be a few missmatches. |
Problem DescriptionCurrently, NaxRiscv allows access to all decodable CSRs, including unimplemented HPM counters. This behavior causes inconsistencies with Spike when the number of HPM counters (
The number of implemented HPM counters is defined by the NaxRiscv/src/main/scala/naxriscv/Gen.scala Line 316 in 5211920
NaxRiscv/src/main/scala/naxriscv/misc/PerformanceCounterPlugin.scala Lines 49 to 55 in 5211920
For example, when accessing
However, NaxRiscv still attempts to commit the instruction, as shown in
This discrepancy occurs because the current implementation does not restrict CSR access to the actual list of implemented HPM counters. Root CauseThe issue lies in the CSR filtering logic, which currently grants access to a broader range of CSRs than those explicitly implemented. The relevant code is located in NaxRiscv/src/main/scala/naxriscv/misc/PerformanceCounterPlugin.scala Lines 54 to 59 in 5211920
val decodeCsrFilter = CsrListFilter((3 to 31).flatMap(e => List(e + 0xB00, e + 0xB80, e + 0x320)))
csr.allowCsr(decodeCsrFilter) This code allows access to all decodable counters, leading to NaxRiscv committing instructions for unimplemented HPM counters, which results in invalid behavior. Proposed SolutionTo resolve this issue, the filtering logic for CSR access should be updated to restrict access only to implemented counters. Replace the above logic with: val csrFilter = CsrListFilter(csrList.toList)
if(priv.implementSupervisor) csr.allowCsr(CSR.SCOUNTEREN)
csr.allowCsr(CSR.MCOUNTEREN)
csr.allowCsr(csrFilter) This modification : csr.allowCsr(csrFilter) Ensures that NaxRiscv behaves consistently with Spike by only allowing access to the CSRs explicitly declared in Expected BehaviorAfter the proposed fix:
This fix improves compatibility with Spike and ensures correctness when handling unimplemented HPM counters. |
Hi ^^ Hmm i'm not sur, thing is, the RISC-V spec says : Are you sure the spike N_HPMCOUNTERS is meant to be modified ? and isn't just to reflect how many counter they could be maximum ? |
Hi Dolu1990, Thank you for bringing this up! Based on my understanding of the RISC-V specification, the behavior for unimplemented CSRs is clearly defined:
If I keep the initial behavior, the VexRiscv commits on a write access to an HPM counter that is not implemented, whereas Spike correctly triggers a trap. Knowing that the NaxRiscv read access returns 0, which complies with the specification, but Spike raises an exception, this behavior does not adhere to the specifications. For reference, here’s a related discussion on a similar issue: CSR reads fail for unimplemented features like HPM counters. Best regards, |
Hmmm right, should read zero and trap on write. |
Hi,
The attached riscv-dv generated code attempts to access mhpmcounter10, which is not implemented in either the DUT or Spike. This results in a segmentation fault and the code fails. Below is the specific line of code causing the issue:
After this issue #121 (comment) I modified the number of HPM counters in Spike to ensure consistency between the DUT and Spike but that doesn't change anything:
https://github.com/SpinalHDL/riscv-isa-sim/blob/5b37ab699f6176ea7f2d2b3187f75625eca7e511/riscv/processor.h#L22
Results:
failed_test.zip
Could you please provide guidance on how to handle this mismatch or segmentation fault when accessing unimplemented HPM counters?
Thank you!
The text was updated successfully, but these errors were encountered: