fix: riscv PC advance for compressed instructions and fetch faults - #2400
Closed
Kreijstal wants to merge 1 commit into
Closed
fix: riscv PC advance for compressed instructions and fetch faults#2400Kreijstal wants to merge 1 commit into
Kreijstal wants to merge 1 commit into
Conversation
Kreijstal
marked this pull request as draft
August 19, 2026 09:48
Kreijstal
force-pushed
the
fix/riscv-exception-pc-advance
branch
2 times, most recently
from
August 19, 2026 11:25
e3c2656 to
132a5ac
Compare
Kreijstal
marked this pull request as ready for review
August 19, 2026 17:23
cpu_handle_exception unconditionally does env->pc += 4 for RISC-V. That corrupts the PC for 16-bit compressed instructions and for instruction access/page faults, where env->pc already holds the faulting address and must not be advanced. Move the logic into a RISC-V helper, riscv_cpu_prepare_exception_pc(), which mirrors the trap state riscv_cpu_do_interrupt would set up: - load mbadaddr from badaddr for every address-bearing exception so the UC_HOOK_INTR callback can inspect the faulting address - advance the PC by the real instruction size (2 or 4 bytes, from the low two opcode bits) for execution-time exceptions - leave the PC untouched for INST_ADDR_MIS / INST_ACCESS_FAULT / INST_PAGE_FAULT Extract the address-bearing exception list into a shared riscv_cpu_exception_has_badaddr() predicate used by both do_interrupt and the new helper, instead of duplicating the switch.
Kreijstal
force-pushed
the
fix/riscv-exception-pc-advance
branch
from
August 19, 2026 17:24
132a5ac to
9e66abe
Compare
Author
|
Closing this as superseded by #2384. That PR preserves the faulting PC while the hook runs and advances to the translator-recorded next PC only after a handled One part is missing from #2384. For instruction, load, and store address faults, RISC-V places the faulting address in I built that commit with RISC-V enabled. All 30 tests from #2384 pass. |
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.
cpu_handle_exceptionunconditionally doesenv->pc += 4for RISC-V. This corrupts the PC for 16-bit compressed instructions and for instruction access/page faults (whereenv->pcalready holds the faulting address and must not be advanced). It also leaves the faulting address unavailable toUC_HOOK_INTRcallbacks.Changes:
env->pcby the real instruction size (2 or 4 bytes, determined by the low two opcode bits) for execution-time exceptions.INST_ADDR_MIS,INST_ACCESS_FAULT,INST_PAGE_FAULT.riscv_cpu_do_interruptand setenv->mbadaddr = env->badaddrfor every address-bearing exception, so the callback can recover from e.g. PMP faults.Verified by booting a RISC-V firmware under Unicorn whose syscall/return path is full of compressed instructions and relies on lazy PMP fault recovery.