Skip to content

boards/arm: Align ARM32 idle stack to AAPCS-32 requirement - #20981

Merged
hartmannathan merged 1 commit into
apache:masterfrom
Swatantra-66:fix/arm32-idle-stack-align
Oct 25, 2026
Merged

hartmannathan merged 1 commit into
apache:masterfrom
Swatantra-66:fix/arm32-idle-stack-align

Conversation

@Swatantra-66

Copy link
Copy Markdown
Contributor

Summary

Fixes #18558.

The ARM Architecture Procedure Call Standard (AAPCS-32, §5.2.1.1) mandates:

  • SP mod 4 = 0: The stack pointer must be word-aligned at all times.
  • SP mod 8 = 0: The stack pointer must be double-word (8-byte) aligned at each external interface (function entry and call boundary).

In NuttX on ARM architectures (armv6-m, armv7-m, armv8-m, armv7-a, armv7-r, armv8-r), the idle thread stack pointer is defined relative to the end of the BSS section:

#define IDLE_STACK (_ebss + CONFIG_IDLETHREAD_STACKSIZE)

On Cortex-M cores, IDLE_STACK is placed at vector table index 0 and loaded directly into the Main Stack Pointer (MSP) by hardware on reset. On Cortex-A and Cortex-R cores, SP is loaded with IDLE_STACK in startup assembly (arm_head.S).

Prior to this change, board linker scripts terminated .bss with:

    *(COMMON)
    . = ALIGN(4);
    _ebss = ABSOLUTE(.);
} > sram

If .bss ended on a 4-byte boundary that was not a multiple of 8, _ebss and the initial idle stack pointer IDLE_STACK were only 4-byte aligned (SP % 8 == 4).

When the idle thread invokes functions with 64-bit varargs (such as syslog() with CONFIG_SYSLOG_TIMESTAMP enabled or formatting 64-bit integers with vsprintf_internal), GCC's va_arg forcibly 8-byte aligns the ap pointer (bic.w r9, r9, #7). On a 4-byte aligned stack, this advances ap by 12 bytes instead of 8 bytes, corrupting the varargs stream and causing subsequent parameter dereferences to fail with Data Abort / Bus Fault exceptions (as documented in issue #18558).

Commit 8409599 (PR #18569) addressed this specifically for boards/arm/stm32*/. This commit extends the fix across all remaining ARM32 boards (and any missed/newly added STM32 scripts) by aligning _ebss to an 8-byte boundary (. = ALIGN(8);).

Impact

  • Architecture: ARM32 (armv6-m, armv7-m, armv8-m, armv7-a, armv7-r, armv8-r).
  • Functionality: Guarantees that the idle task stack is always strictly 8-byte aligned at reset and throughout execution, conforming to AAPCS-32 and preventing bus faults when 64-bit varargs / syslog timestamps are accessed in the idle thread.
  • Compatibility: Safe drop-in alignment enhancement; aligns with existing STM32 implementation in commit 8409599.

Testing

  • Repository Audit: Performed an automated audit of all 558 linker scripts in boards/arm/ to confirm zero remaining 4-byte aligned or unaligned _ebss definitions.
  • Diff Check: Verified clean git diff --check across all 341 modified linker scripts (0 whitespace / newline errors).
  • Prior Art: Aligns directly with merged upstream commit 8409599 (boards/arm/stm32*/: Align ARM STM32 idle stack).

Align the _ebss symbol (used as the base of the idle stack) to an 8-byte
boundary across all remaining ARM32 board linker scripts to conform to
AAPCS-32.

Previously, .bss ended with . = ALIGN(4); before _ebss. If .bss ended on
a non-8-byte boundary, _ebss and the resulting idle stack pointer
(IDLE_STACK = _ebss + CONFIG_IDLETHREAD_STACKSIZE) were only 4-byte
aligned. This caused exceptions during 64-bit vararg accesses (such as
syslog timestamps) in the idle thread when va_arg forcibly 8-byte
aligned the va_list pointer.

Commit 8409599 addressed this for stm32 boards; this commit extends
the fix to all remaining ARM32 boards.

Fixes apache#18558.

Signed-off-by: Swatantra Yadav <[email protected]>
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

stm32-nucleo-f103rb

  • sram: .bss +4 B (+0.2%, 2,452 B / 20,480 B, total: 12% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@hartmannathan
hartmannathan merged commit 2f98854 into apache:master Oct 25, 2026
37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Board: arm Size: L The size of the change in this PR is large

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] ARM32 Idle stack underaligned

4 participants