From 7a598cd56179d903e96e72a297ea1706bad24b5f 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 | 7 ++++- components/espcoredump/src/core_dump_common.c | 26 ++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) 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 154c5a28e6..d8be12233c 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 @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -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 f4e44d48c2..801a34be2c 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"; @@ -72,12 +75,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 } /** @@ -112,10 +128,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