arch/risc-v: preserve ra in up_idle() for fp-chain backtrace

up_idle() is a leaf function; with frame pointers enabled the compiler
skips spilling ra, corrupting the fp chain.  Clobber "ra" in the WFI
inline asm so backtrace can walk past the idle frame correctly.

Assisted-by: Claude Code:claude-sonnet-5
Signed-off-by: liang.huang <liang.huang@houmo.ai>
This commit is contained in:
liang.huang 2026-07-18 08:12:10 +08:00 committed by Alan C. Assis
parent eea880f1ac
commit 7c75dbae2b

View file

@ -71,7 +71,18 @@ void up_idle(void)
* sleep in a reduced power mode until an interrupt occurs to save power
*/
asm("WFI");
/* up_idle() never calls another function, so with frame pointers
* enabled the compiler has no need to spill ra and skips it, leaving
* its slot in the fp-chain holding whatever was on the stack before.
* Clobbering "ra" forces it to be spilled/reloaded around WFI so the
* fp-chain backtrace sched_backtrace() relies on can find it.
*/
#if defined(CONFIG_FRAME_POINTER) && defined(CONFIG_SCHED_BACKTRACE)
asm volatile ("WFI" : : : "ra");
#else
asm volatile ("WFI");
#endif
#endif
}