From 33c982637268b5f80173f0e1bde6c38bab7cfdf6 Mon Sep 17 00:00:00 2001 From: Felipe Neves Date: Thu, 6 Feb 2020 16:24:39 -0300 Subject: [PATCH 1/2] shared_stack: fixed watchpoint placement on shared_stack --- .../xtensa/expression_with_stack_xtensa.c | 5 ++-- .../xtensa/expression_with_stack_xtensa_asm.S | 29 ++++++++----------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/components/xtensa/expression_with_stack_xtensa.c b/components/xtensa/expression_with_stack_xtensa.c index 74829d7e8b..b612824272 100644 --- a/components/xtensa/expression_with_stack_xtensa.c +++ b/components/xtensa/expression_with_stack_xtensa.c @@ -19,7 +19,8 @@ StackType_t * esp_switch_stack_setup(StackType_t *stack, size_t stack_size) { #if CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK - int watchpoint_place = (((int)stack + 31) & ~31); + esp_clear_watchpoint(0); + uint32_t watchpoint_place = ((uint32_t)stack + 32) & 0x1f ; #endif StackType_t *top_of_stack = (StackType_t *)&stack[0] + ((stack_size * sizeof(StackType_t)) / sizeof(StackType_t)); @@ -35,7 +36,7 @@ StackType_t * esp_switch_stack_setup(StackType_t *stack, size_t stack_size) frame->a1 = (UBaseType_t)top_of_stack; #if CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK - esp_set_watchpoint(2, (char*)watchpoint_place, 32, ESP_WATCHPOINT_STORE); + esp_set_watchpoint(0, (uint8_t *)watchpoint_place, 32, ESP_WATCHPOINT_STORE); #endif return top_of_stack; diff --git a/components/xtensa/expression_with_stack_xtensa_asm.S b/components/xtensa/expression_with_stack_xtensa_asm.S index f868131ba0..b01cc5280d 100644 --- a/components/xtensa/expression_with_stack_xtensa_asm.S +++ b/components/xtensa/expression_with_stack_xtensa_asm.S @@ -26,14 +26,14 @@ esp_switch_stack_enter: #ifndef __XTENSA_CALL0_ABI__ - entry sp, 0x10 - mov a4, a1 - s32i a4, a3, 0 /* on a3 there is a safe place to save the current stack */ - l32i a4, a2, 0 /* obtains the user allocated stack buffer */ - mov a1, a4 /* sp register now contains caller specified stack */ - retw + entry sp, 0x10 + mov a4, a1 + s32i a4, a3, 0 /* on a3 there is a safe place to save the current stack */ + l32i a4, a2, 0 /* obtains the user allocated stack buffer */ + mov a1, a4 /* sp register now contains caller specified stack */ + retw #else - #error "this code is written for Window ABI" + #error "this code is written for Window ABI" #endif /** @@ -45,17 +45,12 @@ esp_switch_stack_enter: esp_switch_stack_exit: #ifndef __XTENSA_CALL0_ABI__ - entry sp, 0x10 + entry sp, 0x10 -#if CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK - movi a6, 2 - movi a4, esp_clear_watchpoint - callx4 a4 /* clear the watchpoint before releasing stack */ -#endif + l32i a4, a2, 0 /* recover the original task stack */ + mov a1, a4 /* put it on sp register again */ + retw - l32i a4, a2, 0 /* recover the original task stack */ - mov a1, a4 /* put it on sp register again */ - retw #else - #error "this code is written for Window ABI" + #error "this code is written for Window ABI" #endif From 9c8289b0d911a2c7c0c2d180fae5808c3592a769 Mon Sep 17 00:00:00 2001 From: Felipe Neves Date: Mon, 10 Feb 2020 12:03:24 -0300 Subject: [PATCH 2/2] shared_stack: using watchpoint 1 to monitor the shared_stack instead of watchpoint 0 --- components/esp_common/include/esp_expression_with_stack.h | 5 +++++ components/freertos/xtensa/include/freertos/portmacro.h | 1 + components/xtensa/expression_with_stack_xtensa.c | 4 ++-- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/components/esp_common/include/esp_expression_with_stack.h b/components/esp_common/include/esp_expression_with_stack.h index cd1e4765a8..3c94f9d3e3 100644 --- a/components/esp_common/include/esp_expression_with_stack.h +++ b/components/esp_common/include/esp_expression_with_stack.h @@ -15,6 +15,7 @@ #include "freertos/FreeRTOS.h" #include "freertos/semphr.h" +#include "freertos/task.h" #include "esp_debug_helpers.h" #include "esp_log.h" @@ -42,6 +43,10 @@ extern "C" { expression; \ } \ esp_switch_stack_exit(&backup); \ + StaticTask_t *current = (StaticTask_t *)xTaskGetCurrentTaskHandle(); \ + /* pxDummy6 is the stack base of current thread defined in TCB_t */ \ + /* place the watchpoint on current task stack after function execution*/ \ + vPortSetStackWatchpoint(current->pxDummy6); \ xSemaphoreGive(lock); \ }) diff --git a/components/freertos/xtensa/include/freertos/portmacro.h b/components/freertos/xtensa/include/freertos/portmacro.h index 5c2c5f8dca..979ffecab9 100644 --- a/components/freertos/xtensa/include/freertos/portmacro.h +++ b/components/freertos/xtensa/include/freertos/portmacro.h @@ -386,6 +386,7 @@ extern void esp_vApplicationTickHook( void ); void _xt_coproc_release(volatile void * coproc_sa_base); void vApplicationSleep( TickType_t xExpectedIdleTime ); +void vPortSetStackWatchpoint( void* pxStackStart ); #define portSUPPRESS_TICKS_AND_SLEEP( idleTime ) vApplicationSleep( idleTime ) diff --git a/components/xtensa/expression_with_stack_xtensa.c b/components/xtensa/expression_with_stack_xtensa.c index b612824272..dd5542b605 100644 --- a/components/xtensa/expression_with_stack_xtensa.c +++ b/components/xtensa/expression_with_stack_xtensa.c @@ -19,7 +19,7 @@ StackType_t * esp_switch_stack_setup(StackType_t *stack, size_t stack_size) { #if CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK - esp_clear_watchpoint(0); + esp_clear_watchpoint(1); uint32_t watchpoint_place = ((uint32_t)stack + 32) & 0x1f ; #endif StackType_t *top_of_stack = (StackType_t *)&stack[0] + @@ -36,7 +36,7 @@ StackType_t * esp_switch_stack_setup(StackType_t *stack, size_t stack_size) frame->a1 = (UBaseType_t)top_of_stack; #if CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK - esp_set_watchpoint(0, (uint8_t *)watchpoint_place, 32, ESP_WATCHPOINT_STORE); + esp_set_watchpoint(1, (uint8_t *)watchpoint_place, 32, ESP_WATCHPOINT_STORE); #endif return top_of_stack;