apache / apache/nuttx

[FEATURE] Support for Arm FPU Lazy Stacking

Open
#18,313 2 comments 0 reactions 0 assignees View on GitHub
Type: Enhancement
Dominant language
C
Stars
4k
Forks
1.7k
Avg merge
1d 13h
Merged PRs (30d)
230

Description

### Is your feature request related to a problem? Please describe.

**Description**

Currently, NuttX saves/restores all FPU registers during every context switch, even when tasks don't use the FPU. This adds unnecessary overhead. Modern Cortex-M processors support FPU Lazy Stacking, which delays FPU register preservation until actual FPU usage is detected.

**Problem**

When FPU is enabled, each context switch saves 17 additional FPU registers (S0-S15 + FPSCR), adding ~100 bytes of stack usage and increasing switch time. This is inefficient for tasks that don't use floating-point operations.
Hardware Support

**Cortex-M4/M7/M33/M55/M85 processors already support Lazy Stacking via:**
1. FPCCR.LSPENbit to enable/disable lazy stacking
2. FPCCR.LSPACTbit indicating pending lazy save
3. Automatic detection of FPU instruction usage

**Proposed Implementation**
- Configuration Option: Add CONFIG_ARCH_FPU_LAZYSTACKINGto enable/disable
- FPU Context Management:
1. Only allocate FPU save area when task actually uses FPU
2. Set CONTROL.FPCA=0initially for all tasks
3. Enable FPCCR.LSPENduring system initialization

**Exception Handling:**
- Monitor UFSR.LSPACT flag for pending lazy saves
- Handle automatic FPU stacking in UsageFault

**Task Creation:**
- Mark tasks as "FPU-unused" by default
- Update flag on first FPU instruction

**Benefits**
- Reduced context switch time​ for non-FPU tasks
- Lower stack memory usage​ (no FPU save area until needed)
- Better real-time performance​ for interrupt handlers
- Compatibility​ with existing code (transparent to applications)

### Describe the solution you'd like

Official support for lazy stacking

### Describe alternatives you've considered

_No response_

### Verification

- [x] I have verified before submitting the report.

Contributor guide

Open the contributing guide

Research direction

Start by locating NuttX's Arm FPU context-switch, task-creation, and UsageFault handling paths, then review how FPCCR, CONTROL.FPCA, and UFSR.LSPACT are currently handled. Done means a configuration option enables lazy stacking, FPU state is preserved only after use, and existing non-FPU task behavior remains compatible.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
operating-systems
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.