expression_with_stack: added a tweak on TCB stackpointers to avoid false trigger of stack overflow

This commit is contained in:
Felipe Neves
2020-04-01 12:10:13 -03:00
committed by bot
parent a700035a85
commit 11f6addc61
3 changed files with 47 additions and 29 deletions

View File

@@ -16,12 +16,14 @@
#include <freertos/xtensa_rtos.h>
#include <freertos/xtensa_context.h>
#include <setjmp.h>
#include <string.h>
StackType_t *shared_stack;
shared_stack_function shared_stack_callback;
jmp_buf shared_stack_env;
bool shared_stack_function_done = false;
portMUX_TYPE shared_stack_spinlock = portMUX_INITIALIZER_UNLOCKED;
StackType_t *xtensa_shared_stack;
shared_stack_function xtensa_shared_stack_callback;
jmp_buf xtensa_shared_stack_env;
bool xtensa_shared_stack_function_done = false;
static portMUX_TYPE xtensa_shared_stack_spinlock = portMUX_INITIALIZER_UNLOCKED;
static void *current_task_stack = NULL;
extern void esp_shared_stack_invoke_function(void);
@@ -31,6 +33,15 @@ static void esp_switch_stack_setup(StackType_t *stack, size_t stack_size)
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));
StaticTask_t *current = (StaticTask_t *)xTaskGetCurrentTaskHandle();
//Then put the fake stack inside of TCB:
current_task_stack = current->pxDummy6;
current->pxDummy6 = (void *)stack;
StackType_t *top_of_stack = stack + stack_size;
//Align stack to a 16byte boundary, as required by CPU specific:
@@ -40,7 +51,7 @@ static void esp_switch_stack_setup(StackType_t *stack, size_t stack_size)
esp_set_watchpoint(1, (uint8_t *)watchpoint_place, 32, ESP_WATCHPOINT_STORE);
#endif
shared_stack = top_of_stack;
xtensa_shared_stack = top_of_stack;
}
@@ -52,21 +63,24 @@ void esp_execute_shared_stack_function(SemaphoreHandle_t lock, void *stack, size
assert(function);
xSemaphoreTake(lock, portMAX_DELAY);
portENTER_CRITICAL(&shared_stack_spinlock);
shared_stack_function_done = false;
portENTER_CRITICAL(&xtensa_shared_stack_spinlock);
xtensa_shared_stack_function_done = false;
esp_switch_stack_setup(stack, stack_size);
shared_stack_callback = function;
portEXIT_CRITICAL(&shared_stack_spinlock);
xtensa_shared_stack_callback = function;
portEXIT_CRITICAL(&xtensa_shared_stack_spinlock);
setjmp(shared_stack_env);
if(!shared_stack_function_done) {
setjmp(xtensa_shared_stack_env);
if(!xtensa_shared_stack_function_done) {
esp_shared_stack_invoke_function();
}
portENTER_CRITICAL(&shared_stack_spinlock);
portENTER_CRITICAL(&xtensa_shared_stack_spinlock);
StaticTask_t *current = (StaticTask_t *)xTaskGetCurrentTaskHandle();
//Restore current task stack:
current->pxDummy6 = (StackType_t *)current_task_stack;
vPortSetStackWatchpoint(current->pxDummy6);
portEXIT_CRITICAL(&shared_stack_spinlock);
portEXIT_CRITICAL(&xtensa_shared_stack_spinlock);
xSemaphoreGive(lock);
}