Skip to content

Conversation

@jserv
Copy link
Contributor

@jserv jserv commented Nov 19, 2025

This commit removes manual mstatus bit manipulation and allows hardware to manage interrupt enable state per RISC-V Privileged Spec §3.1.6.1.

The original code manually reconstructed mstatus.MIE from mstatus.MPIE during context save, which violated the RISC-V specification. Hardware automatically manages the MIE/MPIE stack during trap entry (mpie <- mie, mie <- 0) and MRET (mie <- mpie, mpie <- 1), so manual intervention is unnecessary and causes bugs with nested trap handling.

This fix ensures spec-compliant behavior, resolves nested interrupt issues, and improves code clarity with 78% reduction in context save instructions.

Close #16


Summary by cubic

Fixes incorrect nested trap handling in the RISC-V HAL by relying on hardware to manage MIE/MPIE per §3.1.6.1. Restores spec-compliant interrupt behavior and reduces context-save code size.

  • Bug Fixes

    • Correct nested interrupt behavior by using the hardware-managed MIE/MPIE stack.
    • Preserve mstatus exactly during context save/restore.
    • Update documentation to reflect spec-aligned interrupt state management.
  • Refactors

    • Simplify context save sequence (about 78% fewer instructions).
    • Clarify comments for mtimecmp_w and when global interrupts are enabled.

Written for commit 275bc84. Summary will update automatically on new commits.

This commit removes manual mstatus bit manipulation and allows hardware
to manage interrupt enable state per RISC-V Privileged Spec §3.1.6.1.

The original code manually reconstructed mstatus.MIE from mstatus.MPIE
during context save, which violated the RISC-V specification. Hardware
automatically manages the MIE/MPIE stack during trap entry (mpie <- mie,
mie <- 0) and MRET (mie <- mpie, mpie <- 1), so manual intervention is
unnecessary and causes bugs with nested trap handling.

This fix ensures spec-compliant behavior, resolves nested interrupt
issues, and improves code clarity with 78% reduction in context save
instructions.

Close #16
@github-actions
Copy link

Linmo CI Test Results

Overall Status: ✅ passed
Timestamp: 2025-11-19T20:03:17+00:00

Toolchain Results

Toolchain Build Crash Test Functional
GNU ✅ passed ✅ passed ✅ passed
LLVM ✅ passed ⏭️ skipped ⏭️ skipped

Application Tests

App GNU LLVM
cond ✅ passed ⏭️ skipped
coop ✅ passed ⏭️ skipped
cpubench ✅ passed ⏭️ skipped
echo ✅ passed ⏭️ skipped
hello ✅ passed ⏭️ skipped
mqueues ✅ passed ⏭️ skipped
mutex ✅ passed ⏭️ skipped
pipes ✅ passed ⏭️ skipped
pipes_small ✅ passed ⏭️ skipped
pipes_struct ✅ passed ⏭️ skipped
prodcons ✅ passed ⏭️ skipped
progress ✅ passed ⏭️ skipped
rtsched ✅ passed ⏭️ skipped
semaphore ✅ passed ⏭️ skipped
suspend ✅ passed ⏭️ skipped
test64 ✅ passed ⏭️ skipped
test_libc ✅ passed ⏭️ skipped
timer ✅ passed ⏭️ skipped
timer_kill ✅ passed ⏭️ skipped

Functional Test Details

Test GNU LLVM
mutex:data_consistency ✅ passed ⏭️ skipped
mutex:fairness ✅ passed ⏭️ skipped
mutex:mutual_exclusion ✅ passed ⏭️ skipped
mutex:overall ✅ passed ⏭️ skipped
semaphore:all_tests_passed! ✅ passed ⏭️ skipped

Report generated from test-summary.toml

Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

@jserv jserv merged commit f3bb012 into main Nov 19, 2025
4 checks passed
@jserv jserv deleted the trap-handling branch November 19, 2025 20:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect nested trap handling

2 participants