[BUG] RISC-V: new tasks inherit the previous task's FPU state (fcsr) because initial mstatus.FS is set to Clean

Đang mở
#1,536 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 3 ngày

@ChrisX101010 đang làm issue này rồi.

Từ ngày 13/11/2026.

  • #1537 của @ChrisX101010 — đang mở

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
25/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
c
Lĩnh vực
operating-systems

Hướng nghiên cứu

Start with the FS setup in pxPortInitialiseStack() and the configENABLE_FPU block in portable/GCC/RISC-V/portASM.S, then trace portcontextSAVE_CONTEXT_INTERNAL and portcontextRESTORE_CONTEXT to see how the saved mstatus is restored. Decide how a new task's initial FS state should be encoded so its FPU state is defined, which the issue notes is a security-relevant design choice. Done means a fresh task reads frm and fflags as zero on RV32GC hardware or an emulator, with the maintainers having agreed on the approach.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

bug

When configENABLE_FPU == 1, pxPortInitialiseStack() forces mstatus.FS to MSTATUS_FS_CLEAN in the initial stack frame of every new task:

/* portable/GCC/RISC-V/portASM.S */
#if( configENABLE_FPU == 1 )
    /* Mark the FPU as clean in the mstatus value. */
    li t1, ~MSTATUS_FS_MASK
    and t0, t0, t1
    li t1, MSTATUS_FS_CLEAN
    or t0, t0, t1
#endif

No FPU context area is reserved in that initial stack frame, and neither fcsr nor f0-f31 is initialised. When a task that used floating point is switched out, portcontexSAVE_FPU_CONTEXT stores the FPU state but only reads the physical registers (it does not clear them), and portcontextSAVE_CONTEXT_INTERNAL writes FS=Clean back to the hardware mstatus. When the new task is restored, portcontextRESTORE_CONTEXT loads the new task's saved mstatus into t3, writes it to mstatus, and then skips portcontextRESTORE_FPU_CONTEXT because the saved FS is not Dirty:

load_x t3, 1 * portWORD_SIZE( sp )
csrw mstatus, t3
...
srl t1, t3, MSTATUS_FS_OFFSET
andi t1, t1, 3
addi t2, x0, 3
bne t1, t2, 6f      /* If FPU status is not dirty, do not restore FPU registers. */
portcontextRESTORE_FPU_CONTEXT
6:

Consequently, a task that has never used the FPU starts with mstatus.FS = Clean while the physical fcsr and f0-f31 still hold the previous task's values. This contradicts the Privileged Specification definition of Clean (section 3.1.6.6, RISC-V Privileged Architecture 20211203):

When the status is Clean, the corresponding state is potentially different from the initial value, but matches the last value stored on a context swap.

A newly created task has no "last value stored on a context swap", so the Clean encoding is semantically invalid for it. The same section also states:

Changing the setting of FS has no effect on the contents of the floating-point register state.

If the status is Initial, the context must be set to an initial constant value on context restore to avoid a security hole, but this can be done without accessing memory.

Target

  • Development board: hpm6750evk2
  • Instruction Set Architecture: RV32GC
  • Toolchain and version: GNU GCC V13.2.0

Host

  • Host OS: Windows11
  • Version: 25H2

** Impact **

  • A new task inherits the previous task's frm (rounding mode) and fflags (accrued exceptions). Its first floating-point operations may silently use a different rounding mode and non-zero exception flags.
  • This is a cross-task information leak. The spec explicitly describes an uninitialised Initial state as a "security hole"; using Clean without a saved image is at least as problematic.

Steps to reproduce

  1. Build with configENABLE_FPU = 1 for an RV32/RV64 core with F/D.
  2. Create Task A that sets a non-default rounding mode (for example frm = RTZ via csrw frm) and performs floating-point arithmetic, so mstatus.FS becomes Dirty.
  3. Force a context switch (tick interrupt or taskYIELD()) so Task A's FPU context is saved.
  4. Create/start Task B. On its first run, read frm / fcsr, or perform an FP operation that assumes the default RNE mode.
  5. Observe that Task B sees RTZ and Task A's fflags instead of the expected fcsr = 0.
Ngôn ngữ chính
C
Star
4.6k
Fork
1.6k
Merge trung bình
2 ngày 22 giờ
Pull request đã merge (30 ngày)
4

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của FreeRTOS/FreeRTOS-Kernel

Tất cả issue của FreeRTOS/FreeRTOS-Kernel

Issue tương tự

Thêm issue về C

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.