Skip to content

Commit 9f0bd5c

Browse files
fatikiCopilot
andauthored
SCB_DisableDCache: always clean/invalidate stack locals after D-Cache disable (#288)
## Summary Always perform the cache clean/invalidate sequence for the stack-resident local variables in `SCB_DisableDCache()`, independent of the optimization level. ## Motivation / Problem The current implementation only does this when `__OPTIMIZE__` is not defined. However, even with optimization enabled (e.g. `-Og`), compilers may still spill locals to the stack depending on register pressure and code generation. If D-Cache is disabled while relevant locals still reside in cacheable stack memory, stale values may later be read from RAM. This can lead to data inconsistency and, in some cases, endless loops. ## Change Remove the `!defined(__OPTIMIZE__)` guard so the local-variable cache maintenance is always executed. Related to #286 --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 4097ab7 commit 9f0bd5c

1 file changed

Lines changed: 17 additions & 16 deletions

File tree

CMSIS/Core/Include/m-profile/armv7m_cachel1.h

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -195,19 +195,21 @@ __STATIC_FORCEINLINE void SCB_DisableDCache (void)
195195
SCB->CCR &= ~(uint32_t)SCB_CCR_DC_Msk; /* disable D-Cache */
196196
__DSB();
197197

198-
#if !defined(__OPTIMIZE__)
199-
/*
200-
* For the endless loop issue with no optimization builds.
201-
* More details, see https://github.com/ARM-software/CMSIS_5/issues/620
202-
*
203-
* The issue only happens when local variables are in stack. If
204-
* local variables are saved in general purpose register, then the function
205-
* is OK.
206-
*
207-
* When local variables are in stack, after disabling the cache, flush the
208-
* local variables cache line for data consistency.
209-
*/
210-
/* Clean and invalidate the local variable cache. */
198+
/*
199+
* Work around an endless loop issue that can occur when local variables
200+
* are stored on the stack. More details, see
201+
* https://github.com/ARM-software/CMSIS_5/issues/620 and
202+
* https://github.com/ARM-software/CMSIS_6/issues/286.
203+
*
204+
* The issue only happens when local variables are in stack memory. If
205+
* local variables are kept in general purpose registers only, then the
206+
* function is OK.
207+
*
208+
* When local variables are in stack memory, after disabling the cache,
209+
* flush the cache line(s) covering the local variables for data
210+
* consistency.
211+
*/
212+
/* Clean and invalidate the local variable cache. */
211213
#if defined(__ICCARM__)
212214
/* As we can't align the stack to the cache line size, invalidate each of the variables */
213215
SCB->DCCIMVAC = (uint32_t)&locals.sets;
@@ -216,9 +218,8 @@ __STATIC_FORCEINLINE void SCB_DisableDCache (void)
216218
#else
217219
SCB->DCCIMVAC = (uint32_t)&locals;
218220
#endif
219-
__DSB();
220-
__ISB();
221-
#endif
221+
__DSB();
222+
__ISB();
222223

223224
locals.ccsidr = SCB->CCSIDR;
224225
/* clean & invalidate D-Cache */

0 commit comments

Comments
 (0)