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

Aperta
#1,536 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 3 giorni

@ChrisX101010 ci sta già lavorando.

Dal 13/11/2026.

  • #1537 di @ChrisX101010 — aperta

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
25/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
c

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.
Lingua principale
C
Stelle
4.6k
Fork
1.6k
Merge medio
2g 22h
PR unite (30g)
4

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di FreeRTOS/FreeRTOS-Kernel

Tutte le issue di FreeRTOS/FreeRTOS-Kernel

Issue simili

Altre issue su C

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.