fix(espcoredump): Reconfigure the stack guard when using custom coredump stack

This commit is contained in:
Omar Chebib 2024-03-01 12:26:08 +08:00
parent 6020d08074
commit 0f852fcd5f
2 changed files with 32 additions and 3 deletions

View File

@ -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;
/**

View File

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@ -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