From e3955f421d10111271dfb6a2774e742ed01c1d7b Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Tue, 6 Aug 2024 16:13:33 +1000 Subject: [PATCH] esp32: Fix thread stack limit margin, change to new cstack API. This change moves that complexity out into the stack checker and fixes the bug where stack margin wasn't set correctly by ESP32-C3 threads. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton --- ports/esp32/main.c | 12 ++---------- ports/esp32/mpconfigport.h | 5 +++++ ports/esp32/mpthreadport.c | 5 +---- 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/ports/esp32/main.c b/ports/esp32/main.c index ccec6c8cd..ea641e668 100644 --- a/ports/esp32/main.c +++ b/ports/esp32/main.c @@ -41,7 +41,7 @@ #include "esp_log.h" #include "esp_psram.h" -#include "py/stackctrl.h" +#include "py/cstack.h" #include "py/nlr.h" #include "py/compile.h" #include "py/runtime.h" @@ -71,13 +71,6 @@ // MicroPython runs as a task under FreeRTOS #define MP_TASK_PRIORITY (ESP_TASK_PRIO_MIN + 1) -// Set the margin for detecting stack overflow, depending on the CPU architecture. -#if CONFIG_IDF_TARGET_ESP32C3 -#define MP_TASK_STACK_LIMIT_MARGIN (2048) -#else -#define MP_TASK_STACK_LIMIT_MARGIN (1024) -#endif - typedef struct _native_code_node_t { struct _native_code_node_t *next; uint32_t data[]; @@ -132,8 +125,7 @@ void mp_task(void *pvParameter) { soft_reset: // initialise the stack pointer for the main thread - mp_stack_set_top((void *)sp); - mp_stack_set_limit(MICROPY_TASK_STACK_SIZE - MP_TASK_STACK_LIMIT_MARGIN); + mp_cstack_init_with_top((void *)sp, MICROPY_TASK_STACK_SIZE); gc_init(mp_task_heap, mp_task_heap + MICROPY_GC_INITIAL_HEAP_SIZE); mp_init(); mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_lib)); diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h index 3349e56e4..40b3f11b4 100644 --- a/ports/esp32/mpconfigport.h +++ b/ports/esp32/mpconfigport.h @@ -62,6 +62,11 @@ // Python internal features #define MICROPY_READER_VFS (1) #define MICROPY_ENABLE_GC (1) +#if CONFIG_IDF_TARGET_ARCH_RISCV // RISC-V SoCs use more stack than Xtensa +#define MICROPY_STACK_CHECK_MARGIN (2048) // This may be unnecessarily conservative +#else +#define MICROPY_STACK_CHECK_MARGIN (1024) +#endif #define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1) #define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ) #define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_NORMAL) diff --git a/ports/esp32/mpthreadport.c b/ports/esp32/mpthreadport.c index 34fef9f7b..eac1e5ea3 100644 --- a/ports/esp32/mpthreadport.c +++ b/ports/esp32/mpthreadport.c @@ -38,7 +38,7 @@ #if MICROPY_PY_THREAD #define MP_THREAD_MIN_STACK_SIZE (4 * 1024) -#define MP_THREAD_DEFAULT_STACK_SIZE (MP_THREAD_MIN_STACK_SIZE + 1024) +#define MP_THREAD_DEFAULT_STACK_SIZE (MP_THREAD_MIN_STACK_SIZE + MICROPY_STACK_CHECK_MARGIN) #define MP_THREAD_PRIORITY (ESP_TASK_PRIO_MIN + 1) #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 0) && !CONFIG_FREERTOS_ENABLE_STATIC_TASK_CLEAN_UP @@ -160,9 +160,6 @@ mp_uint_t mp_thread_create_ex(void *(*entry)(void *), void *arg, size_t *stack_s th->next = thread; thread = th; - // adjust the stack_size to provide room to recover from hitting the limit - *stack_size -= 1024; - mp_thread_mutex_unlock(&thread_mutex); return (mp_uint_t)th->id;