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

Abierto
#1,536 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 3 días

@ChrisX101010 ya está trabajando en esto.

Desde el 13/11/2026.

  • #1537 de @ChrisX101010 — abierto

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
25/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
c

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.
Lenguaje dominante
C
Estrellas
4.6k
Forks
1.6k
Merge medio
2 d 22 h
PR fusionados (30 d)
4

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de FreeRTOS/FreeRTOS-Kernel

Todos los issues de FreeRTOS/FreeRTOS-Kernel

Issues similares

Más issues de C

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.