From 5063de64db88188c648870b278de7d17afcad0d5 Mon Sep 17 00:00:00 2001 From: Laukik Hase Date: Mon, 9 Dec 2024 13:05:34 +0530 Subject: [PATCH] refactor(esp_tee): Disable the cache and interrupts in the TEE panic handler --- .../subproject/main/common/panic/esp_tee_panic.c | 16 ++++++++++++++-- .../main/common/panic/panic_helper_riscv.c | 2 -- .../subproject/main/ld/esp32c6/esp_tee.ld.in | 1 + .../main/soc/esp32c6/include/esp_tee_rv_utils.h | 10 +++++++--- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/components/esp_tee/subproject/main/common/panic/esp_tee_panic.c b/components/esp_tee/subproject/main/common/panic/esp_tee_panic.c index 444c357391..1aea490a07 100644 --- a/components/esp_tee/subproject/main/common/panic/esp_tee_panic.c +++ b/components/esp_tee/subproject/main/common/panic/esp_tee_panic.c @@ -10,6 +10,7 @@ #include "esp_macros.h" #include "esp_rom_sys.h" #include "esp_rom_uart.h" +#include "rom/cache.h" #include "riscv/rv_utils.h" #include "riscv/rvruntime-frames.h" @@ -18,6 +19,7 @@ #include "esp_tee.h" #include "esp_tee_apm_intr.h" +#include "esp_tee_rv_utils.h" #include "panic_helper.h" #define RV_FUNC_STK_SZ (32) @@ -26,12 +28,22 @@ static void tee_panic_end(void) { - // make sure all the panic handler output is sent from UART FIFO + // Disable interrupts + rv_utils_tee_intr_global_disable(); + + // Disable the cache + Cache_Disable_ICache(); + + // Clear the interrupt controller configurations + memset((void *)DR_REG_PLIC_MX_BASE, 0x00, (PLIC_MXINT_CLAIM_REG + 4 - DR_REG_PLIC_MX_BASE)); + memset((void *)DR_REG_PLIC_UX_BASE, 0x00, (PLIC_UXINT_CLAIM_REG + 4 - DR_REG_PLIC_UX_BASE)); + + // Make sure all the panic handler output is sent from UART FIFO if (CONFIG_ESP_CONSOLE_UART_NUM >= 0) { esp_rom_output_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM); } - // generate core reset + // Generate system reset esp_rom_software_reset_system(); } diff --git a/components/esp_tee/subproject/main/common/panic/panic_helper_riscv.c b/components/esp_tee/subproject/main/common/panic/panic_helper_riscv.c index db55e7c687..6de24a7a42 100644 --- a/components/esp_tee/subproject/main/common/panic/panic_helper_riscv.c +++ b/components/esp_tee/subproject/main/common/panic/panic_helper_riscv.c @@ -117,9 +117,7 @@ void panic_print_isrcause(const void *f, int core) static const char *pseudo_reason[] = { "Unknown reason", "Interrupt wdt timeout on CPU0", -#if SOC_CPU_NUM > 1 "Interrupt wdt timeout on CPU1", -#endif "Cache error", }; diff --git a/components/esp_tee/subproject/main/ld/esp32c6/esp_tee.ld.in b/components/esp_tee/subproject/main/ld/esp32c6/esp_tee.ld.in index 95646e3867..48a8710655 100644 --- a/components/esp_tee/subproject/main/ld/esp32c6/esp_tee.ld.in +++ b/components/esp_tee/subproject/main/ld/esp32c6/esp_tee.ld.in @@ -105,6 +105,7 @@ SECTIONS *libtee_flash_mgr.a:*(.rodata .srodata .rodata.* .srodata.*) *libbootloader_support.a:bootloader_flash.*(.rodata .srodata .rodata.* .srodata.*) *libmain.a:panic_helper_riscv.*(.rodata .srodata .rodata.* .srodata.*) + *libmain.a:esp_tee_apm_intr.c.*(.rodata .srodata .rodata.* .srodata.*) _rodata_end = ABSOLUTE(.); _tee_dram_end = ABSOLUTE(.); } > dram_tee_seg diff --git a/components/esp_tee/subproject/main/soc/esp32c6/include/esp_tee_rv_utils.h b/components/esp_tee/subproject/main/soc/esp32c6/include/esp_tee_rv_utils.h index 60e588e02b..603a441878 100644 --- a/components/esp_tee/subproject/main/soc/esp32c6/include/esp_tee_rv_utils.h +++ b/components/esp_tee/subproject/main/soc/esp32c6/include/esp_tee_rv_utils.h @@ -21,9 +21,6 @@ extern "C" { #endif -#define SET_BIT(t, n) (t |= (1UL << (n))) -#define CLR_BIT(t, n) (t &= ~(1UL << (n))) - FORCE_INLINE_ATTR void rv_utils_tee_intr_global_enable(void) { /* @@ -50,6 +47,13 @@ FORCE_INLINE_ATTR void rv_utils_tee_intr_global_enable(void) RV_SET_CSR(mstatus, MSTATUS_MIE); } +FORCE_INLINE_ATTR void rv_utils_tee_intr_global_disable(void) +{ + RV_CLEAR_CSR(ustatus, USTATUS_UIE); + RV_CLEAR_CSR(mstatus, MSTATUS_UIE); + RV_CLEAR_CSR(mstatus, MSTATUS_MIE); +} + FORCE_INLINE_ATTR void rv_utils_tee_intr_enable(uint32_t intr_mask) { unsigned old_xstatus;