Fix S-mode ECALL delegation in medeleg - #3561
Open
124107157-KV wants to merge 4 commits into
Open
124107157-KV wants to merge 4 commits into
124107157-KV wants to merge 4 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why is this PR needed?
The CSR_MEDELEG writable mask currently does not include ENV_CALL_SMODE, corresponding to exception cause 9 (ECALL from S-mode).
The decoder already generates ENV_CALL_SMODE for an ECALL executed in S-mode, and the trap-routing logic already uses medeleg[cause] to determine whether the exception is delegated to S-mode.
However, because ENV_CALL_SMODE is missing from the CSR_MEDELEG writable mask, writing medeleg[9] does not retain the bit. As a result, software cannot enable delegation of an S-mode ECALL even though the existing exception and trap-routing logic supports this path.
This PR adds ENV_CALL_SMODE to the CSR_MEDELEG writable mask so that medeleg[9] can retain writes and the existing S-mode ECALL delegation path can be used.
What does this PR change?
The writable mask for CSR_MEDELEG in core/csr_regfile.sv is extended with:
No decoder or trap-routing changes are required because those paths already generate and handle ENV_CALL_SMODE.
A directed regression is also added to verify both CSR readback and actual trap delegation behavior.
Regression test
A new directed test is added:
The test is registered in:
The regression performs the following sequence:
The regression also uses separate failure values to distinguish a CSR write/readback failure from an exception-routing failure.
Verification
Target:
Spike reference
The directed regression was first executed using Spike as the architectural reference.
The Spike trace confirms that:
Result:
CVA6 before the fix
The same directed-test ELF was executed on the Verilator model built from the original, unfixed CVA6 RTL.
Result:
The regression uses exit value 91 specifically when medeleg[9] does not read back as set.
This confirms the original RTL reproduces issue #3457.
CVA6 after the fix
ENV_CALL_SMODE was then added to the CSR_MEDELEG writable mask and the Verilator model was rebuilt.
The exact same directed-test ELF was executed again without modifying the test binary.
Result:
This confirms that adding ENV_CALL_SMODE to the CSR_MEDELEG writable mask fixes both:
Additional validation
The following checks were completed:
History
git blame shows that ENV_CALL_UMODE was already present in the CSR_MEDELEG writable mask.
Later Hypervisor-extension work added conditional support for ENV_CALL_VSMODE.
ENV_CALL_SMODE remained absent from the writable mask even though S-mode ECALL generation and cause-based exception delegation are already implemented in the core.
The fix is therefore intentionally limited to adding the missing ENV_CALL_SMODE writable-mask entry.
Fixes #3457