diff --git a/esp32/builtins.h b/esp32/builtins.h index d9a2db4..1559796 100644 --- a/esp32/builtins.h +++ b/esp32/builtins.h @@ -69,7 +69,13 @@ static cell_t ResizeFile(cell_t fd, cell_t size); YV(internals, heap_caps_malloc, SET heap_caps_malloc(n1, n0); NIP) \ YV(internals, heap_caps_free, heap_caps_free(a0); DROP) \ YV(internals, heap_caps_realloc, \ - tos = (cell_t) heap_caps_realloc(a2, n1, n0); NIPn(2)) + tos = (cell_t) heap_caps_realloc(a2, n1, n0); NIPn(2)) \ + YV(internals, heap_caps_get_total_size, n0 = heap_caps_get_total_size(n0)) \ + YV(internals, heap_caps_get_free_size, n0 = heap_caps_get_free_size(n0)) \ + YV(internals, heap_caps_get_minimum_free_size, \ + n0 = heap_caps_get_minimum_free_size(n0)) \ + YV(internals, heap_caps_get_largest_free_block, \ + n0 = heap_caps_get_largest_free_block(n0)) #define REQUIRED_PLATFORM_SUPPORT \ X("ESP32?", IS_ESP32, PUSH UEFORTH_PLATFORM_IS_ESP32) \ diff --git a/esp32/faults.h b/esp32/faults.h index 9ed3790..76a799d 100644 --- a/esp32/faults.h +++ b/esp32/faults.h @@ -14,6 +14,41 @@ #if defined(ENABLE_ESP32_FORTH_FAULT_HANDLING) +# if defined(CONFIG_IDF_TARGET_ESP32C3) + +#include +#include "riscv/csr.h" +#include "esp_heap_caps.h" + +#define FORTH_VECTOR_TABLE_SIZE 32 + +static __thread jmp_buf g_forth_fault; +static __thread int g_forth_signal; +static void **g_forth_vector_table; +extern void *_vector_table; + +#define FAULT_ENTRY \ + if (setjmp(g_forth_fault)) { THROWIT(g_forth_signal); } + +static void forth_faults_setup(void) { + g_forth_vector_table = (void **) malloc(sizeof(void *) * FORTH_VECTOR_TABLE_SIZE); + //g_forth_vector_table = (void **) heap_caps_malloc(sizeof(void *) * FORTH_VECTOR_TABLE_SIZE, + // MALLOC_CAP_EXEC); + void **vector_table = (void **) &_vector_table; + for (int i = 0; i < FORTH_VECTOR_TABLE_SIZE; ++i) { + g_forth_vector_table[i] = vector_table[i]; + } + // TODO: Actually apply it. +/* + uint32_t mtvec_val = (uint32_t) g_forth_vector_table; + mtvec_val |= 1; + RV_WRITE_CSR(mtvec, mtvec_val); +*/ + //rv_utils_set_mtvec((uint32_t) g_forth_vector_table); +} + +# else + #include #include "soc/soc.h" #include @@ -44,20 +79,38 @@ static void IRAM_ATTR forth_exception_handler(XtExcFrame *frame) { } static void forth_faults_setup(void) { - xt_set_exception_handler(EXCCAUSE_LOAD_STORE_ERROR, forth_exception_handler); - xt_set_exception_handler(EXCCAUSE_PRIVILEGED, forth_exception_handler); - xt_set_exception_handler(EXCCAUSE_UNALIGNED, forth_exception_handler); xt_set_exception_handler(EXCCAUSE_DIVIDE_BY_ZERO, forth_exception_handler); - xt_set_exception_handler(EXCCAUSE_INSTR_ERROR, forth_exception_handler); + xt_set_exception_handler(EXCCAUSE_DTLB_MISS, forth_exception_handler); + xt_set_exception_handler(EXCCAUSE_DTLB_MULTIHIT, forth_exception_handler); + xt_set_exception_handler(EXCCAUSE_EXCLUSIVE_ERROR, forth_exception_handler); + xt_set_exception_handler(EXCCAUSE_EXTREG_PRIVILEGE, forth_exception_handler); xt_set_exception_handler(EXCCAUSE_ILLEGAL, forth_exception_handler); - xt_set_exception_handler(EXCCAUSE_LOAD_PROHIBITED, forth_exception_handler); - xt_set_exception_handler(EXCCAUSE_STORE_PROHIBITED, forth_exception_handler); + xt_set_exception_handler(EXCCAUSE_INSTR_ADDR_ERROR, forth_exception_handler); + xt_set_exception_handler(EXCCAUSE_INSTR_DATA_ERROR, forth_exception_handler); + xt_set_exception_handler(EXCCAUSE_INSTR_ERROR, forth_exception_handler); xt_set_exception_handler(EXCCAUSE_INSTR_PROHIBITED, forth_exception_handler); + xt_set_exception_handler(EXCCAUSE_INSTR_RING, forth_exception_handler); + xt_set_exception_handler(EXCCAUSE_ITLB_MISS, forth_exception_handler); + xt_set_exception_handler(EXCCAUSE_ITLB_MULTIHIT, forth_exception_handler); + xt_set_exception_handler(EXCCAUSE_LOAD_PROHIBITED, forth_exception_handler); + xt_set_exception_handler(EXCCAUSE_LOAD_STORE_ADDR_ERROR, forth_exception_handler); + xt_set_exception_handler(EXCCAUSE_LOAD_STORE_DATA_ERROR, forth_exception_handler); + xt_set_exception_handler(EXCCAUSE_LOAD_STORE_ERROR, forth_exception_handler); + xt_set_exception_handler(EXCCAUSE_LOAD_STORE_RING, forth_exception_handler); + xt_set_exception_handler(EXCCAUSE_PC_ERROR, forth_exception_handler); + xt_set_exception_handler(EXCCAUSE_PRIVILEGED, forth_exception_handler); + xt_set_exception_handler(EXCCAUSE_STORE_PROHIBITED, forth_exception_handler); + xt_set_exception_handler(EXCCAUSE_UNALIGNED, forth_exception_handler); + for (int i = 0; i < 8; ++i) { + xt_set_exception_handler(EXCCAUSE_CP_DISABLED(i), forth_exception_handler); + } uint32_t default_setlevel = XTOS_SET_INTLEVEL(XCHAL_EXCM_LEVEL); XTOS_RESTORE_INTLEVEL(default_setlevel); g_forth_setlevel = default_setlevel; } +# endif + #else #define forth_faults_setup() diff --git a/esp32/options.h b/esp32/options.h index 94cf265..d54bc67 100644 --- a/esp32/options.h +++ b/esp32/options.h @@ -27,6 +27,7 @@ #define ENABLE_LEDC_SUPPORT #define ENABLE_SD_SUPPORT #define ENABLE_SPI_FLASH_SUPPORT +#define ENABLE_ESP32_FORTH_FAULT_HANDLING // SD_MMC does not work on ESP32-S2 / ESP32-C3 #if !defined(CONFIG_IDF_TARGET_ESP32S2) && !defined(CONFIG_IDF_TARGET_ESP32C3) @@ -54,7 +55,6 @@ // ESP32-C3 doesn't support fault handling yet. #if !defined(CONFIG_IDF_TARGET_ESP32C3) -# define ENABLE_ESP32_FORTH_FAULT_HANDLING #endif // Uncomment this #define for OLED Support.