Skip to content

fix(riscv): set FS before fp restore/clear in switch_to#30

Merged
equation314 merged 1 commit intoarceos-org:mainfrom
LetsWalkInLine:fix/rustsbi-boot-failure-main
Mar 23, 2026
Merged

fix(riscv): set FS before fp restore/clear in switch_to#30
equation314 merged 1 commit intoarceos-org:mainfrom
LetsWalkInLine:fix/rustsbi-boot-failure-main

Conversation

@LetsWalkInLine
Copy link
Copy Markdown
Contributor

@LetsWalkInLine LetsWalkInLine commented Mar 23, 2026

Background

When booting downstream StarryOS on riscv64 with RustSBI, the kernel could panic with:

Root Cause

In axcpu RISC-V fp-simd context switching, FP instructions can be executed in restore/clear paths while sstatus.FS is still Off on some boot paths/harts.
When FS=Off, executing FP instructions (e.g. fld / fmv.d.x) traps as IllegalInstruction.
So the issue is not OS entry order itself, but a missing local precondition in the axcpu FP context switch path.

A common trigger is switching from a task that does not use FP (FS=Off) to one that needs FP state restore/initialization, where FP instructions in the switch path can trap before the next task actually runs.

Changes

This PR fixes the issue at the root-cause location in axcpu:

  1. src/riscv/context.rs
    • Move sstatus::set_fs(next_fp_state.fs) so it executes before the restore/clear match.
    • This ensures FP instructions are executable even when firmware leaves FS=Off.
    • The conditional table remains the same: FS::Clean → restore(), FS::Initial → clear(), FS::Off → no-op, with FS::Dirty still marked unreachable.

Validation Performed

  • Local:
    • cargo fmt --all -- --check passes.
  • Behavioral/functional validation is from downstream reproduction:
    • With this fix concept applied, RustSBI boot no longer hits the IllegalInstruction trap in fp-simd switching path.

Impact

  • Makes RISC-V fp-simd context switching robust when firmware leaves sstatus.FS=Off.

Files Included In This PR

  • src/riscv/context.rs

I’m still learning kernel development, so feedback is very welcome.
If any part of this fix should be done differently, I’m happy to revise.

Move sstatus::set_fs(next_fp_state.fs) before the restore/clear match
so FP instructions are executable when firmware leaves FS=Off.
@equation314 equation314 merged commit 58e1018 into arceos-org:main Mar 23, 2026
9 checks passed
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.

2 participants