2022-07-21 19:39:21 +08:00
|
|
|
/*
|
2025-01-27 17:48:09 +01:00
|
|
|
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
|
2022-07-21 19:39:21 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2020-10-08 11:19:23 +08:00
|
|
|
|
2021-01-27 22:03:07 +01:00
|
|
|
#include "riscv/rvruntime-frames.h"
|
2020-10-08 11:19:23 +08:00
|
|
|
#include "esp_private/panic_internal.h"
|
2023-11-28 09:57:44 +08:00
|
|
|
#include "esp_private/panic_reason.h"
|
2025-01-27 17:48:09 +01:00
|
|
|
#include "hal/wdt_hal.h"
|
2020-10-08 11:19:23 +08:00
|
|
|
|
2021-01-27 22:03:07 +01:00
|
|
|
|
2020-10-08 11:19:23 +08:00
|
|
|
extern void esp_panic_handler(panic_info_t *info);
|
2021-01-27 22:03:07 +01:00
|
|
|
volatile bool g_override_illegal_instruction = false;
|
2020-10-08 11:19:23 +08:00
|
|
|
|
|
|
|
void __real_esp_panic_handler(panic_info_t *info);
|
2025-01-27 17:48:09 +01:00
|
|
|
void __real_esp_panic_handler_feed_wdts(void);
|
|
|
|
void __real_esp_panic_handler_enable_rtc_wdt(uint32_t timeout_ms);
|
|
|
|
void __real_esp_panic_handler_increment_entry_count(void);
|
2022-07-21 19:39:21 +08:00
|
|
|
void __real_esp_cpu_stall(int core_id);
|
2020-10-08 11:19:23 +08:00
|
|
|
|
2025-01-27 17:48:09 +01:00
|
|
|
static void disable_all_wdts(void)
|
|
|
|
{
|
|
|
|
wdt_hal_context_t wdt0_context = {.inst = WDT_MWDT0, .mwdt_dev = &TIMERG0};
|
|
|
|
#if SOC_TIMER_GROUPS >= 2
|
|
|
|
wdt_hal_context_t wdt1_context = {.inst = WDT_MWDT1, .mwdt_dev = &TIMERG1};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//Todo: Refactor to use Interrupt or Task Watchdog API, and a system level WDT context
|
|
|
|
//Task WDT is the Main Watchdog Timer of Timer Group 0
|
|
|
|
wdt_hal_write_protect_disable(&wdt0_context);
|
|
|
|
wdt_hal_disable(&wdt0_context);
|
|
|
|
wdt_hal_write_protect_enable(&wdt0_context);
|
|
|
|
|
|
|
|
#if SOC_TIMER_GROUPS >= 2
|
|
|
|
//Interrupt WDT is the Main Watchdog Timer of Timer Group 1
|
|
|
|
wdt_hal_write_protect_disable(&wdt1_context);
|
|
|
|
wdt_hal_disable(&wdt1_context);
|
|
|
|
wdt_hal_write_protect_enable(&wdt1_context);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//Disable RTC WDT
|
|
|
|
wdt_hal_context_t rtc_wdt_ctx = RWDT_HAL_CONTEXT_DEFAULT();
|
|
|
|
wdt_hal_write_protect_disable(&rtc_wdt_ctx);
|
|
|
|
wdt_hal_disable(&rtc_wdt_ctx);
|
|
|
|
wdt_hal_write_protect_enable(&rtc_wdt_ctx);
|
|
|
|
}
|
|
|
|
|
2021-01-27 22:03:07 +01:00
|
|
|
void return_from_panic_handler(RvExcFrame *frm) __attribute__((noreturn));
|
|
|
|
|
2020-10-08 11:19:23 +08:00
|
|
|
/* Memprot test specific IllegalInstruction exception handler:
|
|
|
|
* when testing the protection against a code execution, sample code
|
|
|
|
* is being injected into various memory regions which produces
|
2021-01-27 22:03:07 +01:00
|
|
|
* Illegal instruction on execution attempt. Such a result is expected
|
2020-10-08 11:19:23 +08:00
|
|
|
* but it causes system reboot in the standard panic handler.
|
|
|
|
* The following variant of panic handling simply returns back to the
|
|
|
|
* next instruction and continues normal execution.
|
|
|
|
*
|
2021-01-27 22:03:07 +01:00
|
|
|
* NOTE: if Illegal instruction comes from a different source than the testing code
|
2020-10-08 11:19:23 +08:00
|
|
|
* the behavior is undefined
|
|
|
|
* */
|
|
|
|
void __wrap_esp_panic_handler(panic_info_t *info)
|
|
|
|
{
|
2021-01-27 22:03:07 +01:00
|
|
|
RvExcFrame *frm = (RvExcFrame *)info->frame;
|
|
|
|
if ( frm->mcause == MCAUSE_ILLEGAL_INSTRUCTION && g_override_illegal_instruction == true ) {
|
|
|
|
/* Return from exception to the return address that called the faulting function.
|
|
|
|
*/
|
|
|
|
frm->mepc = frm->ra;
|
|
|
|
|
|
|
|
/* Restore the CPU state and return from the exception.
|
|
|
|
*/
|
|
|
|
return_from_panic_handler(frm);
|
2020-10-08 11:19:23 +08:00
|
|
|
} else {
|
|
|
|
__real_esp_panic_handler(info);
|
|
|
|
}
|
|
|
|
}
|
2022-07-21 19:39:21 +08:00
|
|
|
|
2025-01-27 17:48:09 +01:00
|
|
|
void __wrap_esp_panic_handler_feed_wdts(void)
|
|
|
|
{
|
|
|
|
if ( g_override_illegal_instruction == true ) {
|
|
|
|
disable_all_wdts();
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
__real_esp_panic_handler_feed_wdts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void __wrap_esp_panic_handler_enable_rtc_wdt(uint32_t timeout_ms)
|
|
|
|
{
|
|
|
|
if ( g_override_illegal_instruction == true ) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
__real_esp_panic_handler_enable_rtc_wdt(timeout_ms);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void __wrap_esp_panic_handler_increment_entry_count(void)
|
|
|
|
{
|
|
|
|
if ( g_override_illegal_instruction == true ) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
__real_esp_panic_handler_increment_entry_count();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-21 19:39:21 +08:00
|
|
|
void __wrap_esp_cpu_stall(int core_id)
|
|
|
|
{
|
|
|
|
if ( g_override_illegal_instruction == true ) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
__real_esp_cpu_stall(core_id);
|
|
|
|
}
|
|
|
|
}
|