kernel: force K_FP_REGS in z_setup_new_thread when FPU_SHARING - #10
Merged
Conversation
When CONFIG_FPU and CONFIG_FPU_SHARING are both enabled, GCC >= 9 with -mfloat-abi=hard emits FP instructions in any translation unit for struct copies and register spills, regardless of whether the source code explicitly uses float. This affects all threads, including kernel-internal ones like main and sysworkq whose options are set by the kernel, not the application. On Cortex-M, arch_new_thread() reads K_FP_REGS at creation time to size the MPU stack guard (FP_GUARD_EXTRA_SIZE) and set Z_ARM_MODE_MPU_GUARD_FLOAT_Msk. Without K_FP_REGS at that point, the guard is sized too small. z_arm_mpu_stack_guard_and_fpu_adjust() can retroactively tag K_FP_REGS at the next context switch, but any ISR arriving between the thread's first FP instruction and that switch opens the Cortex-M4 ES0182 lazy-FPU-stacking race window, corrupting the saved PC and causing a compound HARD+USAGE+BUS+MPU fault on exception return. Fix this by unconditionally ORing K_FP_REGS into options at the top of z_setup_new_thread() when CONFIG_FPU && CONFIG_FPU_SHARING, before z_init_thread_base() writes user_options and before arch_new_thread() reads them for guard sizing. This closes the race for main, sysworkq, idle, and any future kernel-internal thread in one place without requiring each call-site to be updated. Fixes zephyrproject-rtos#108793 Signed-off-by: BhuvanChandra DV <[email protected]>
Gowtham1256
approved these changes
Jul 3, 2026
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the ES0182 lazy-FPU-stacking race window on Cortex-M for all kernel-internal threads (main, sysworkq, idle) when
CONFIG_FPU && CONFIG_FPU_SHARINGare enabled.-mfloat-abi=hardemits FP instructions in any thread for struct copies and register spills, even without explicitfloatin sourcearch_new_thread()readsK_FP_REGSat creation time to size the MPU stack guard — if absent, the guard is sized too smallz_arm_mpu_stack_guard_and_fpu_adjust()can only retroactively tagK_FP_REGSat the next context switch, leaving a race window open for any ISR that fires before that switchK_FP_REGSintooptionsat the top ofz_setup_new_thread(), beforez_init_thread_base()andarch_new_thread()— closes the gap for all threads in one placeUpstream issue: zephyrproject-rtos#108793
Changes
kernel/thread.c: addoptions |= K_FP_REGSunder#if defined(CONFIG_FPU) && defined(CONFIG_FPU_SHARING)at the top ofz_setup_new_thread()Test plan
oksbot_base_ctrl_fwwithCONFIG_FPU=y,CONFIG_FPU_SHARING=y,CONFIG_HW_STACK_PROTECTION=y— verify no boot crash duringboot_banner()/ static C++ constructorsK_FP_REGSset via GDBk_thread_foreachafter boot