Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
Back to tool/Back to issues
#121016·zephyr

arch: xtensa: boolean registers (BR) are not saved on interrupt entry or context switch, so FP compares can branch wrong under preemption

Author: jtbrewbirdCreated Oct 1, 2026Updated Oct 1, 2026

Describe the bug

On Xtensa cores with the boolean option (XCHAL_HAVE_BOOLEANS, e.g. ESP32 and ESP32-S3), a floating-point comparison is two instructions: olt.s b0, f0, f1 writes boolean register b0, and the following bt b0, … / bf b0, … reads it. The BR special register that holds b0–b15 is never saved or restored by the kernel.

With CONFIG_FPU=y and CONFIG_FPU_SHARING=y, arch/xtensa saves f0–f15, FCR and FSR in the base save area (struct xtensa_irq_base_save_area) on interrupt entry and in xtensa_switch, but BR is not part of the BSA and ODD_REG_SAVE / ODD_REG_RESTORE do not touch it (arch/xtensa/include/xtensa_asm2.inc.S; xtensa_asm2_s.h in v4.2). So if an interrupt — or a context switch to a higher-priority thread — lands between a thread's compare and its branch, and the preempting context executes any FP compare, the preempted thread's branch takes the wrong direction when it resumes. Every value it then logs is correct; only the decision is wrong, which makes this very hard to diagnose from the application side.

This affects any application with FP compares in more than one context (two threads, or a thread and an ISR/timer callback). It is independent of CONFIG_FPU_SHARING in the sense that without it the FP values are also corrupted, but enabling FPU sharing does not fix the compare flags, which is what makes it surprising.

Regression

No. Present in v4.2.0 and in main as of 2026-10-01 (no reference to br/rsr.br anywhere in arch/xtensa). SCOMPARE1 and THREADPTR get the same treatment the fix proposes, so this looks like an omission rather than a decision.

Steps to reproduce

  1. ESP32-S3 (any board), CONFIG_FPU=y, CONFIG_FPU_SHARING=y.
  2. Thread A, in a loop: float x = read(); if (x >= limit) fault(); with x well below limit. (Compiles to olt.s/ult.s + bt/bf.)
  3. A k_timer or a higher-priority thread, firing frequently, that performs any FP comparison whose result is the other way (e.g. if (y < 0.0f) with y positive).
  4. Run for minutes. Thread A eventually calls fault() although x was below limit at the time; logging x and limit in fault() shows values that do not satisfy the condition.

A self-contained ztest can be written on the same pattern (two threads with opposite-sense FP compares and k_yield() between the compare and the branch is enough to show it on real hardware; native_sim cannot reproduce it). We can contribute one if wanted.

Relevant log output

From the field (ESP32-S3, Zephyr v4.2.0, FPU sharing on): a temperature limit check tripped with temp_mC=90280, max_mC=110000 — 90.28 judged ≥ 110. Disassembly of that image: the check is olt.s b0, f0, f1 / bt b0; _handle_excint and xtensa_switch contain 16 FP register saves and no rsr.br/wsr.br. With the patch below applied, the same build ran clean under the same load for the full test session; without it, it faulted within 25 s of boot, repeatably.

Impact

Silent wrong branches in any FP-using code under preemption; in our case a false safety fault that stopped the device, and before that months of misdiagnosis (the symptom had earlier been worked around in application code by comparing IEEE bit patterns as integers). Severity: medium-high for anyone using floats on ESP32/ESP32-S3 with more than one FP context.

Environment

  • Zephyr v4.2.0 (413b789deb3); also inspected main at 2026-10-01
  • Zephyr SDK 0.17.0, xtensa-espressif_esp32s3_zephyr-elf
  • ESP32-S3 (esp32s3/procpu), single core (CONFIG_MP_MAX_NUM_CPUS=1), CONFIG_FPU=y, CONFIG_FPU_SHARING=y, CONFIG_THREAD_MONITOR=y

Additional Context

Proposed fix (tested on hardware): add br to the BSA, guarded by XCHAL_HAVE_BOOLEANS, with an offset symbol, and rsr.br/wsr.br in ODD_REG_SAVE/ODD_REG_RESTORE, so it is preserved wherever SAR/LCOUNT/SCOMPARE1 are — interrupt entry, xtensa_switch, and the userspace syscall path. One word, re-padded to the 16-byte frame alignment. Patch against v4.2.0 below; happy to open a PR against main (the FPU block has moved to the end of the BSA there, same four hunks).

CONFIG_FPU_SHARING's help text says "preservation of the hardware floating point registers"; once BR is included that becomes true in the sense users need.

Proposed patch (against v4.2.0)

diff
diff --git a/arch/xtensa/core/offsets/offsets.c b/arch/xtensa/core/offsets/offsets.c
index f0b75c9..fb7ce03 100644
--- a/arch/xtensa/core/offsets/offsets.c
+++ b/arch/xtensa/core/offsets/offsets.c
@@ -36,6 +36,10 @@ GEN_OFFSET_SYM(_xtensa_irq_bsa_t, lend);
 GEN_OFFSET_SYM(_xtensa_irq_bsa_t, scompare1);
 #endif
 
+#if XCHAL_HAVE_BOOLEANS
+GEN_OFFSET_SYM(_xtensa_irq_bsa_t, br);
+#endif
+
 #if XCHAL_HAVE_THREADPTR
 GEN_OFFSET_SYM(_xtensa_irq_bsa_t, threadptr);
 #endif
diff --git a/arch/xtensa/include/xtensa_asm2_context.h b/arch/xtensa/include/xtensa_asm2_context.h
index ac13a61..f1c5eb4 100644
--- a/arch/xtensa/include/xtensa_asm2_context.h
+++ b/arch/xtensa/include/xtensa_asm2_context.h
@@ -115,6 +115,12 @@
 # define _BSA_PADDING_LOOPS		(0)
 #endif
 
+#if XCHAL_HAVE_BOOLEANS
+# define _BSA_PADDING_BOOLEANS		(sizeof(uintptr_t))
+#else
+# define _BSA_PADDING_BOOLEANS		(0)
+#endif
+
 /* Must have fields regardless of features. */
 #define _BSA_PADDING_COMMON		(sizeof(uintptr_t) * 12U)
 
@@ -125,6 +131,7 @@
 	 _BSA_PADDING_THREADPTR + \
 	 _BSA_PADDING_S32C1I + \
 	 _BSA_PADDING_LOOPS + \
+	 _BSA_PADDING_BOOLEANS + \
 	 _BSA_PADDING_COMMON)
 
 /* Each stack frame always has a pointer to BSA so we add
@@ -195,6 +202,13 @@ struct xtensa_irq_base_save_area {
 
 	uintptr_t exccause;
 
+#if XCHAL_HAVE_BOOLEANS
+	/* Boolean registers b0-b15. Floating-point compares write them and the
+	 * following bt/bf reads them, so an interrupt between the two that runs
+	 * any FP compare corrupts the branch unless BR is preserved. */
+	uintptr_t br;
+#endif
+
 #if XCHAL_HAVE_LOOPS
 	uintptr_t lcount;
 	uintptr_t lend;
diff --git a/arch/xtensa/include/xtensa_asm2_s.h b/arch/xtensa/include/xtensa_asm2_s.h
index a189d60..3cf50d9 100644
--- a/arch/xtensa/include/xtensa_asm2_s.h
+++ b/arch/xtensa/include/xtensa_asm2_s.h
@@ -178,6 +178,10 @@
 #endif
 	rsr.exccause \SCRATCH_REG
 	s32i \SCRATCH_REG, \BSA_PTR, ___xtensa_irq_bsa_t_exccause_OFFSET
+#if XCHAL_HAVE_BOOLEANS
+	rsr.br \SCRATCH_REG
+	s32i \SCRATCH_REG, \BSA_PTR, ___xtensa_irq_bsa_t_br_OFFSET
+#endif
 #if XCHAL_HAVE_S32C1I
 	rsr.scompare1 \SCRATCH_REG
 	s32i \SCRATCH_REG, \BSA_PTR, ___xtensa_irq_bsa_t_scompare1_OFFSET
@@ -213,6 +217,10 @@
 #endif
 	l32i \SCRATCH_REG, \BSA_PTR, ___xtensa_irq_bsa_t_exccause_OFFSET
 	wsr.exccause \SCRATCH_REG
+#if XCHAL_HAVE_BOOLEANS
+	l32i \SCRATCH_REG, \BSA_PTR, ___xtensa_irq_bsa_t_br_OFFSET
+	wsr.br \SCRATCH_REG
+#endif
 #if XCHAL_HAVE_S32C1I
 	l32i \SCRATCH_REG, \BSA_PTR, ___xtensa_irq_bsa_t_scompare1_OFFSET
 	wsr.scompare1 \SCRATCH_REG

Source: zephyrproject-rtos/zephyr

View original on GitHubView discussion on GitHub