From eec24193906e3b402a6e329de6aeab16d539f62a Mon Sep 17 00:00:00 2001 From: Marius Vikhammer Date: Thu, 28 Jan 2021 11:32:51 +0800 Subject: [PATCH] system: enable shared stack watchpoint Enable shared stack watchpoint for overflow detection Enable unit tests: * "test printf using shared buffer stack" for C3 * "Test vTaskDelayUntil" for S2 * "UART can do poll()" for C3 --- .gitlab/ci/target-test.yml | 2 +- components/freertos/port/riscv/port.c | 5 ++++- components/freertos/port/xtensa/port.c | 5 ++++- .../freertos/test/test_freertos_task_delay_until.c | 4 ---- components/newlib/test/test_shared_stack_printf.c | 4 ---- components/riscv/expression_with_stack_riscv.c | 11 +++-------- components/spi_flash/test/test_mmap.c | 2 +- components/vfs/test/test_vfs_select.c | 6 ------ components/xtensa/expression_with_stack_xtensa.c | 8 +++----- 9 files changed, 16 insertions(+), 31 deletions(-) diff --git a/.gitlab/ci/target-test.yml b/.gitlab/ci/target-test.yml index 3cee6708a2..d872f3eae7 100644 --- a/.gitlab/ci/target-test.yml +++ b/.gitlab/ci/target-test.yml @@ -551,7 +551,7 @@ UT_046: UT_C3: extends: .unit_test_esp32c3_template - parallel: 28 + parallel: 29 tags: - ESP32C3_IDF - UT_T1_1 diff --git a/components/freertos/port/riscv/port.c b/components/freertos/port/riscv/port.c index 44ace2f48d..4639292e2b 100644 --- a/components/freertos/port/riscv/port.c +++ b/components/freertos/port/riscv/port.c @@ -81,6 +81,7 @@ #include "sdkconfig.h" +#include "soc/soc_caps.h" #include "soc/periph_defs.h" #include "soc/system_reg.h" #include "hal/systimer_hal.h" @@ -302,11 +303,13 @@ void vPortYield(void) } #define STACK_WATCH_AREA_SIZE 32 +#define STACK_WATCH_POINT_NUMBER (SOC_CPU_WATCHPOINTS_NUM - 1) + void vPortSetStackWatchpoint(void *pxStackStart) { uint32_t addr = (uint32_t)pxStackStart; addr = (addr + (STACK_WATCH_AREA_SIZE - 1)) & (~(STACK_WATCH_AREA_SIZE - 1)); - esp_set_watchpoint(7, (char *)addr, STACK_WATCH_AREA_SIZE, ESP_WATCHPOINT_STORE); + esp_set_watchpoint(STACK_WATCH_POINT_NUMBER, (char *)addr, STACK_WATCH_AREA_SIZE, ESP_WATCHPOINT_STORE); } BaseType_t xPortInIsrContext(void) diff --git a/components/freertos/port/xtensa/port.c b/components/freertos/port/xtensa/port.c index ab41a36fc5..438d9dcfe5 100644 --- a/components/freertos/port/xtensa/port.c +++ b/components/freertos/port/xtensa/port.c @@ -416,6 +416,9 @@ void vPortAssertIfInISR(void) configASSERT(xPortInIsrContext()); } +#define STACK_WATCH_AREA_SIZE 32 +#define STACK_WATCH_POINT_NUMBER (SOC_CPU_WATCHPOINTS_NUM - 1) + void vPortSetStackWatchpoint( void* pxStackStart ) { //Set watchpoint 1 to watch the last 32 bytes of the stack. //Unfortunately, the Xtensa watchpoints can't set a watchpoint on a random [base - base+n] region because @@ -425,7 +428,7 @@ void vPortSetStackWatchpoint( void* pxStackStart ) { //This way, we make sure we trigger before/when the stack canary is corrupted, not after. int addr=(int)pxStackStart; addr=(addr+31)&(~31); - esp_set_watchpoint(1, (char*)addr, 32, ESP_WATCHPOINT_STORE); + esp_set_watchpoint(STACK_WATCH_POINT_NUMBER, (char*)addr, 32, ESP_WATCHPOINT_STORE); } uint32_t xPortGetTickRateHz(void) { diff --git a/components/freertos/test/test_freertos_task_delay_until.c b/components/freertos/test/test_freertos_task_delay_until.c index 8bb2ce319f..6f6359f414 100644 --- a/components/freertos/test/test_freertos_task_delay_until.c +++ b/components/freertos/test/test_freertos_task_delay_until.c @@ -24,8 +24,6 @@ #define TICKS_TO_MS(x) (((x)*1000)/TICK_RATE) #define REF_TO_ROUND_MS(x) (((x)+500)/1000) -#if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2) - static SemaphoreHandle_t task_delete_semphr; static void delaying_task(void* arg) @@ -73,5 +71,3 @@ TEST_CASE("Test vTaskDelayUntil", "[freertos]") vSemaphoreDelete(task_delete_semphr); ref_clock_deinit(); } - -#endif // CONFIG_IDF_TARGET_ESP32S2 diff --git a/components/newlib/test/test_shared_stack_printf.c b/components/newlib/test/test_shared_stack_printf.c index 3cf1a44d80..2c6368b3de 100644 --- a/components/newlib/test/test_shared_stack_printf.c +++ b/components/newlib/test/test_shared_stack_printf.c @@ -26,8 +26,6 @@ void another_external_stack_function(void) shared_stack_sp = (StackType_t *)get_sp(); } -#if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32C3) - TEST_CASE("test printf using shared buffer stack", "[newlib]") { portSTACK_TYPE *shared_stack = malloc(SHARED_STACK_SIZE); @@ -60,5 +58,3 @@ TEST_CASE("test printf using shared buffer stack", "[newlib]") vSemaphoreDelete(printf_lock); free(shared_stack); } - -#endif diff --git a/components/riscv/expression_with_stack_riscv.c b/components/riscv/expression_with_stack_riscv.c index 28e492ce00..2581d3d62b 100644 --- a/components/riscv/expression_with_stack_riscv.c +++ b/components/riscv/expression_with_stack_riscv.c @@ -15,6 +15,8 @@ #include #include #include +#include "freertos/FreeRTOS.h" +#include "freertos/portmacro.h" static portMUX_TYPE shared_stack_spinlock = portMUX_INITIALIZER_UNLOCKED; static void *current_task_stack = NULL; @@ -23,11 +25,6 @@ extern void esp_shared_stack_invoke_function(shared_stack_function function, voi static StackType_t *esp_switch_stack_setup(StackType_t *stack, size_t stack_size) { -#if CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK - // TODO ESP32-C3 IDF-2207 - // esp_clear_watchpoint(1); - // uint32_t watchpoint_place = ((uint32_t)stack + 32) & ~0x1f ; -#endif //We need also to tweak current task stackpointer to avoid erroneous //stack overflow indication, so fills the stack with freertos known pattern: memset(stack, 0xa5U, stack_size * sizeof(StackType_t)); @@ -45,10 +42,8 @@ static StackType_t *esp_switch_stack_setup(StackType_t *stack, size_t stack_size adjusted_top_of_stack--; #if CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK - // TODO ESP32-C3 IDF-2207 - //esp_set_watchpoint(1, (uint8_t *)watchpoint_place, 32, ESP_WATCHPOINT_STORE); + vPortSetStackWatchpoint(stack); #endif - return ((StackType_t *)adjusted_top_of_stack); } diff --git a/components/spi_flash/test/test_mmap.c b/components/spi_flash/test/test_mmap.c index 3cf0589145..28ae98c06b 100644 --- a/components/spi_flash/test/test_mmap.c +++ b/components/spi_flash/test/test_mmap.c @@ -215,7 +215,7 @@ TEST_CASE("Can mmap into instruction address space", "[spi_flash][mmap]") } -#endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32C3) +#endif //!DISABLED_FOR_TARGETS(ESP32C3) TEST_CASE("Can mmap unordered pages into contiguous memory", "[spi_flash][mmap]") diff --git a/components/vfs/test/test_vfs_select.c b/components/vfs/test/test_vfs_select.c index 4bb0a64e45..223890d599 100644 --- a/components/vfs/test/test_vfs_select.c +++ b/components/vfs/test/test_vfs_select.c @@ -206,9 +206,6 @@ TEST_CASE("UART can do select()", "[vfs]") deinit(uart_fd, socket_fd); } -#if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32C3) -// TODO ESP32C3 IDF-2457 - TEST_CASE("UART can do poll()", "[vfs]") { int uart_fd; @@ -270,9 +267,6 @@ TEST_CASE("UART can do poll()", "[vfs]") deinit(uart_fd, socket_fd); } -#endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32C3) - - TEST_CASE("socket can do select()", "[vfs]") { int uart_fd; diff --git a/components/xtensa/expression_with_stack_xtensa.c b/components/xtensa/expression_with_stack_xtensa.c index 074210748f..6002c86821 100644 --- a/components/xtensa/expression_with_stack_xtensa.c +++ b/components/xtensa/expression_with_stack_xtensa.c @@ -17,6 +17,8 @@ #include #include #include +#include "freertos/FreeRTOS.h" +#include "freertos/portmacro.h" StackType_t *xtensa_shared_stack; shared_stack_function xtensa_shared_stack_callback; @@ -29,10 +31,6 @@ extern void esp_shared_stack_invoke_function(void); static void esp_switch_stack_setup(StackType_t *stack, size_t stack_size) { -#if CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK - esp_clear_watchpoint(1); - uint32_t watchpoint_place = ((uint32_t)stack + 32) & ~0x1f ; -#endif //We need also to tweak current task stackpointer to avoid erroneous //stack overflow indication, so fills the stack with freertos known pattern: memset(stack, 0xa5U, stack_size * sizeof(StackType_t)); @@ -48,7 +46,7 @@ static void esp_switch_stack_setup(StackType_t *stack, size_t stack_size) top_of_stack = (StackType_t *)(((UBaseType_t)(top_of_stack - 16) & ~0xf)); #if CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK - esp_set_watchpoint(1, (uint8_t *)watchpoint_place, 32, ESP_WATCHPOINT_STORE); + vPortSetStackWatchpoint(stack); #endif xtensa_shared_stack = top_of_stack;