From e29780fe0901bb97bdb3a19339940d81a108366b Mon Sep 17 00:00:00 2001 From: Aidan MacDonald Date: Mon, 12 Jan 2026 12:35:56 +0000 Subject: [PATCH] firmware: fix thread_exit handling on ARM Cortex-M The "mov lr, pc" instruction doesn't link a proper return address in Thumb mode: bit 0 will be unset, leading to a UsageFault exception when returning from the thread's main function. Cortex-M has the "blx" instruction to automatically branch and link the correct return address, so use that. Change-Id: I4c0ca55b1b2204286343f906f0b53be0c0ddc392 --- firmware/asm/arm/thread-micro.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/firmware/asm/arm/thread-micro.c b/firmware/asm/arm/thread-micro.c index 9ad329b55d..95992c39c3 100644 --- a/firmware/asm/arm/thread-micro.c +++ b/firmware/asm/arm/thread-micro.c @@ -34,10 +34,9 @@ static void __attribute__((naked)) USED_ATTR start_thread(void) "ldr r4, [r0, #40] \n" /* start in r4 since it's non-volatile */ "mov r1, #0 \n" /* Mark thread as running */ "str r1, [r0, #40] \n" - "mov lr, pc \n" /* Call thread function */ - "bx r4 \n" + "blx r4 \n" /* Call thread function */ + "b thread_exit \n" ); /* No clobber list - new thread doesn't care */ - thread_exit(); } /* For startup, place context pointer in r4 slot, start_thread pointer in r5