From a4b2aa8a579143641634c6813644cc528c86ea25 Mon Sep 17 00:00:00 2001 From: Omar Chebib Date: Wed, 16 Nov 2022 12:28:40 +0800 Subject: [PATCH] RISC-V: fix PC not saved when using backtrace --- .../freertos/FreeRTOS-Kernel-SMP/portable/riscv/port.c | 9 +++------ .../freertos/FreeRTOS-Kernel/portable/riscv/port.c | 6 +++--- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/components/freertos/FreeRTOS-Kernel-SMP/portable/riscv/port.c b/components/freertos/FreeRTOS-Kernel-SMP/portable/riscv/port.c index 070cc3cf68..42c2cc0f83 100644 --- a/components/freertos/FreeRTOS-Kernel-SMP/portable/riscv/port.c +++ b/components/freertos/FreeRTOS-Kernel-SMP/portable/riscv/port.c @@ -509,14 +509,12 @@ void vApplicationGetTimerTaskMemory(StaticTask_t **ppxTimerTaskTCBBuffer, * Wrapper to allow task functions to return. Force the optimization option -O1 on that function to make sure there * is no tail-call. Indeed, we need the compiler to keep the return address to this function when calling `panic_abort`. * - * Thanks to the `naked` attribute, GDB stub backtrace doesn't print: - * #2 0x00000000 in ?? () - * - * However, this also means that when calculating vPortTaskWrapper's call frame in a backtrace, return address - * register `ra`, will NOT contain 0x00000000. + * Thanks to `naked` attribute, the compiler won't generate a prologue and epilogue for the function, which saves time + * and stack space. */ static void __attribute__((optimize("O1"), naked)) vPortTaskWrapper(TaskFunction_t pxCode, void *pvParameters) { + asm volatile(".cfi_undefined ra\n"); extern void __attribute__((noreturn)) panic_abort(const char *details); static char DRAM_ATTR msg[80] = "FreeRTOS: FreeRTOS Task \"\0"; pxCode(pvParameters); @@ -526,7 +524,6 @@ static void __attribute__((optimize("O1"), naked)) vPortTaskWrapper(TaskFunction strcat(msg, pcTaskName); strcat(msg, "\" should not return, Aborting now!"); panic_abort(msg); - } #endif // CONFIG_FREERTOS_TASK_FUNCTION_WRAPPER diff --git a/components/freertos/FreeRTOS-Kernel/portable/riscv/port.c b/components/freertos/FreeRTOS-Kernel/portable/riscv/port.c index ae36e950fa..183b05ca1b 100644 --- a/components/freertos/FreeRTOS-Kernel/portable/riscv/port.c +++ b/components/freertos/FreeRTOS-Kernel/portable/riscv/port.c @@ -118,11 +118,12 @@ void vPortEndScheduler(void) * Wrapper to allow task functions to return. Force the optimization option -O1 on that function to make sure there * is no tail-call. Indeed, we need the compiler to keep the return address to this function when calling `panic_abort`. * - * Thanks to the `naked` attribute, GDB stub backtrace doesn't print: - * #2 0x00000000 in ?? () + * Thanks to `naked` attribute, the compiler won't generate a prologue and epilogue for the function, which saves time + * and stack space. */ static void __attribute__((optimize("O1"), naked)) vPortTaskWrapper(TaskFunction_t pxCode, void *pvParameters) { + asm volatile(".cfi_undefined ra\n"); extern void __attribute__((noreturn)) panic_abort(const char *details); static char DRAM_ATTR msg[80] = "FreeRTOS: FreeRTOS Task \"\0"; pxCode(pvParameters); @@ -132,7 +133,6 @@ static void __attribute__((optimize("O1"), naked)) vPortTaskWrapper(TaskFunction strcat(msg, pcTaskName); strcat(msg, "\" should not return, Aborting now!"); panic_abort(msg); - } #endif // CONFIG_FREERTOS_TASK_FUNCTION_WRAPPER