2022-07-21 07:39:21 -04:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2020-10-07 23:19:23 -04:00
|
|
|
|
2021-01-27 16:03:07 -05:00
|
|
|
#include "riscv/rvruntime-frames.h"
|
2020-10-07 23:19:23 -04:00
|
|
|
#include "esp_private/panic_internal.h"
|
2023-11-27 20:57:44 -05:00
|
|
|
#include "esp_private/panic_reason.h"
|
2020-10-07 23:19:23 -04:00
|
|
|
|
2021-01-27 16:03:07 -05:00
|
|
|
|
2020-10-07 23:19:23 -04:00
|
|
|
extern void esp_panic_handler(panic_info_t *info);
|
2021-01-27 16:03:07 -05:00
|
|
|
volatile bool g_override_illegal_instruction = false;
|
2020-10-07 23:19:23 -04:00
|
|
|
|
|
|
|
void __real_esp_panic_handler(panic_info_t *info);
|
2022-07-21 07:39:21 -04:00
|
|
|
void __real_esp_cpu_stall(int core_id);
|
2020-10-07 23:19:23 -04:00
|
|
|
|
2021-01-27 16:03:07 -05:00
|
|
|
void return_from_panic_handler(RvExcFrame *frm) __attribute__((noreturn));
|
|
|
|
|
2020-10-07 23:19:23 -04: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 16:03:07 -05:00
|
|
|
* Illegal instruction on execution attempt. Such a result is expected
|
2020-10-07 23:19:23 -04: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 16:03:07 -05:00
|
|
|
* NOTE: if Illegal instruction comes from a different source than the testing code
|
2020-10-07 23:19:23 -04:00
|
|
|
* the behavior is undefined
|
|
|
|
* */
|
|
|
|
void __wrap_esp_panic_handler(panic_info_t *info)
|
|
|
|
{
|
2021-01-27 16:03:07 -05: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-07 23:19:23 -04:00
|
|
|
} else {
|
|
|
|
__real_esp_panic_handler(info);
|
|
|
|
}
|
|
|
|
}
|
2022-07-21 07:39:21 -04:00
|
|
|
|
|
|
|
void __wrap_esp_cpu_stall(int core_id)
|
|
|
|
{
|
|
|
|
if ( g_override_illegal_instruction == true ) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
__real_esp_cpu_stall(core_id);
|
|
|
|
}
|
|
|
|
}
|