Skip to content

Conversation

@aggarg
Copy link
Member

@aggarg aggarg commented Oct 27, 2025

Description

According to the RISC-V Privileged Architecture Specification (20211203), writing Initial or Clean to the FS field of mstatus may result in the FS value getting set to Dirty in some implementations. This means we cannot rely on reading back the same FS value after writing to mstatus.

Previously, the context restore code would:

  1. Write an FS value to mstatus
  2. Read mstatus again at a later point
  3. Use the read FS value to determine FPU status

This change updates the context restore code to use the mstatus value from the saved context instead of re-reading mstatus after writing to it. This required changing the location of the mstatus slot in the context.

Test Steps

Tested using RISC-V_RV32_QEMU_VIRT_GCC demo.

Checklist:

  • I have tested my changes. No regression in existing tests.
  • [NA] I have modified and/or added unit-tests to cover the code changes in this Pull Request.

Related Issue

#1327

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

According to the RISC-V Privileged Architecture Specification (20211203),
writing Initial or Clean to the FS field of mstatus may result in the FS
value getting set to Dirty in some implementations. This means we cannot
rely on reading back the same FS value after writing to mstatus.

Previously, the context restore code would:
1. Write an FS value to mstatus
2. Read mstatus again at a later point
3. Use the read FS value to determine FPU status

This change updates the context restore code to use the mstatus value
from the saved context instead of re-reading mstatus after writing to
it. This required chaning the location of the mstatus slot in the
context.

Fixes: FreeRTOS#1327

Signed-off-by: Gaurav Aggarwal <[email protected]>
@sonarqubecloud
Copy link

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.

1 participant