From 9046bf1645da1a8d49d7927b9836a6c380ec3e3c Mon Sep 17 00:00:00 2001 From: Omar Chebib Date: Fri, 1 Mar 2024 12:26:08 +0800 Subject: [PATCH] fix(espcoredump): Reconfigure the stack guard when using custom coredump stack --- .../port/riscv/esp_core_dump_port_impl.h | 5 ++++ components/espcoredump/src/core_dump_common.c | 26 ++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/components/espcoredump/include_core_dump/port/riscv/esp_core_dump_port_impl.h b/components/espcoredump/include_core_dump/port/riscv/esp_core_dump_port_impl.h index 4efcb06578..fa2db73afb 100644 --- a/components/espcoredump/include_core_dump/port/riscv/esp_core_dump_port_impl.h +++ b/components/espcoredump/include_core_dump/port/riscv/esp_core_dump_port_impl.h @@ -11,6 +11,7 @@ * @brief Core dump port interface implementation for RISC-V. */ +#include "sdkconfig.h" #include "esp_core_dump_types.h" #include "esp_app_format.h" @@ -24,6 +25,10 @@ extern "C" { */ typedef struct { uint32_t sp; +#if CONFIG_ESP_SYSTEM_HW_STACK_GUARD + uint32_t sp_min; + uint32_t sp_max; +#endif // CONFIG_ESP_SYSTEM_HW_STACK_GUARD } core_dump_stack_context_t; /** diff --git a/components/espcoredump/src/core_dump_common.c b/components/espcoredump/src/core_dump_common.c index 2030ce4eae..250db58d1e 100644 --- a/components/espcoredump/src/core_dump_common.c +++ b/components/espcoredump/src/core_dump_common.c @@ -12,6 +12,9 @@ #include "esp_rom_sys.h" #include "esp_core_dump_port.h" #include "esp_core_dump_common.h" +#if CONFIG_ESP_SYSTEM_HW_STACK_GUARD +#include "esp_private/hw_stack_guard.h" +#endif // CONFIG_ESP_SYSTEM_HW_STACK_GUARD const static char TAG[] __attribute__((unused)) = "esp_core_dump_common"; @@ -71,12 +74,25 @@ FORCE_INLINE_ATTR void esp_core_dump_setup_stack(void) //esp_cpu_clear_watchpoint(1); //esp_cpu_set_watchpoint(1, s_coredump_stack, 1, ESP_WATCHPOINT_STORE); +#if CONFIG_ESP_SYSTEM_HW_STACK_GUARD + /* Save the current area we are watching to restore it later */ + esp_hw_stack_guard_get_bounds(&s_stack_context.sp_min, &s_stack_context.sp_max); + /* Since the stack is going to change, make sure we disable protection or an exception would be triggered */ + esp_hw_stack_guard_monitor_stop(); +#endif // CONFIG_ESP_SYSTEM_HW_STACK_GUARD + /* Replace the stack pointer depending on the architecture, but save the * current stack pointer, in order to be able too restore it later. * This function must be inlined. */ esp_core_dump_replace_sp(s_core_dump_sp, &s_stack_context); ESP_COREDUMP_LOGI("Backing up stack @ %p and use core dump stack @ %p", s_stack_context.sp, esp_cpu_get_sp()); + +#if CONFIG_ESP_SYSTEM_HW_STACK_GUARD + /* Re-enable the stack guard to check if the stack is big enough for coredump generation */ + esp_hw_stack_guard_set_bounds((uint32_t) s_coredump_stack, (uint32_t) s_core_dump_sp); + esp_hw_stack_guard_monitor_start(); +#endif // CONFIG_ESP_SYSTEM_HW_STACK_GUARD } /** @@ -110,10 +126,18 @@ FORCE_INLINE_ATTR void esp_core_dump_report_stack_usage(void) /* Restore the stack pointer. */ ESP_COREDUMP_LOGI("Restoring stack @ %p", s_stack_context.sp); +#if CONFIG_ESP_SYSTEM_HW_STACK_GUARD + esp_hw_stack_guard_monitor_stop(); +#endif // CONFIG_ESP_SYSTEM_HW_STACK_GUARD esp_core_dump_restore_sp(&s_stack_context); +#if CONFIG_ESP_SYSTEM_HW_STACK_GUARD + /* Monitor the same stack area that was set before replacing the stack pointer */ + esp_hw_stack_guard_set_bounds(s_stack_context.sp_min, s_stack_context.sp_max); + esp_hw_stack_guard_monitor_start(); +#endif // CONFIG_ESP_SYSTEM_HW_STACK_GUARD } -#else // CONFIG_ESP_COREDUMP_STACK_SIZE > 0 +#else // CONFIG_ESP_COREDUMP_STACK_SIZE == 0 /* Here, we are not going to use a custom stack for coredump. Make sure the current configuration doesn't require one. */ #if CONFIG_ESP_COREDUMP_USE_STACK_SIZE