expression_with_stack: added a tweak on TCB stackpointers to avoid false trigger of stack overflow
This commit is contained in:
@@ -15,29 +15,72 @@
|
||||
#include <esp_expression_with_stack.h>
|
||||
#include <freertos/xtensa_rtos.h>
|
||||
#include <freertos/xtensa_context.h>
|
||||
#include <setjmp.h>
|
||||
#include <string.h>
|
||||
|
||||
StackType_t * esp_switch_stack_setup(StackType_t *stack, size_t stack_size)
|
||||
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);
|
||||
|
||||
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
|
||||
StackType_t *top_of_stack = (StackType_t *)&stack[0] +
|
||||
((stack_size * sizeof(StackType_t)) / sizeof(StackType_t));
|
||||
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:
|
||||
top_of_stack = (StackType_t *)(((UBaseType_t)(top_of_stack - 31) -
|
||||
ALIGNUP(0x10, sizeof(XtSolFrame) )) &
|
||||
~0xf);
|
||||
|
||||
//Fake stack frame to do not break the backtrace
|
||||
XtSolFrame *frame = (XtSolFrame *)top_of_stack;
|
||||
frame->a0 = 0;
|
||||
frame->a1 = (UBaseType_t)top_of_stack;
|
||||
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);
|
||||
esp_set_watchpoint(1, (uint8_t *)watchpoint_place, 32, ESP_WATCHPOINT_STORE);
|
||||
#endif
|
||||
|
||||
return top_of_stack;
|
||||
}
|
||||
xtensa_shared_stack = top_of_stack;
|
||||
}
|
||||
|
||||
|
||||
void esp_execute_shared_stack_function(SemaphoreHandle_t lock, void *stack, size_t stack_size, shared_stack_function function)
|
||||
{
|
||||
assert(lock);
|
||||
assert(stack);
|
||||
assert(stack_size > 0 && stack_size >= CONFIG_ESP_MINIMAL_SHARED_STACK_SIZE);
|
||||
assert(function);
|
||||
|
||||
xSemaphoreTake(lock, portMAX_DELAY);
|
||||
portENTER_CRITICAL(&xtensa_shared_stack_spinlock);
|
||||
xtensa_shared_stack_function_done = false;
|
||||
esp_switch_stack_setup(stack, stack_size);
|
||||
xtensa_shared_stack_callback = function;
|
||||
portEXIT_CRITICAL(&xtensa_shared_stack_spinlock);
|
||||
|
||||
setjmp(xtensa_shared_stack_env);
|
||||
if(!xtensa_shared_stack_function_done) {
|
||||
esp_shared_stack_invoke_function();
|
||||
}
|
||||
|
||||
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(&xtensa_shared_stack_spinlock);
|
||||
|
||||
xSemaphoreGive(lock);
|
||||
}
|
||||
|
||||
@@ -13,52 +13,39 @@
|
||||
// limitations under the License.
|
||||
|
||||
#include <freertos/xtensa_context.h>
|
||||
|
||||
.extern esp_clear_watchpoint
|
||||
|
||||
.extern xtensa_shared_stack
|
||||
.extern xtensa_shared_stack_callback
|
||||
.extern xtensa_shared_stack_function_done
|
||||
.extern xtensa_shared_stack_env
|
||||
.extern longjmp
|
||||
.text
|
||||
|
||||
/**
|
||||
* extern void switch_stack_enter(portSTACK_TYPE *stack, portSTACK_TYPE *backup_stack);
|
||||
*/
|
||||
.globl esp_switch_stack_enter
|
||||
.type esp_switch_stack_enter,@function
|
||||
|
||||
/* extern void esp_shared_stack_invoke_function(void) */
|
||||
|
||||
.globl esp_shared_stack_invoke_function
|
||||
.type esp_shared_stack_invoke_function,@function
|
||||
.align 4
|
||||
esp_switch_stack_enter:
|
||||
esp_shared_stack_invoke_function:
|
||||
|
||||
#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
|
||||
#else
|
||||
#error "this code is written for Window ABI"
|
||||
#endif
|
||||
|
||||
/**
|
||||
* extern void switch_stack_exit(portSTACK_TYPE *backup_stack);
|
||||
*/
|
||||
.globl esp_switch_stack_exit
|
||||
.type esp_switch_stack_exit,@function
|
||||
.align 4
|
||||
esp_switch_stack_exit:
|
||||
|
||||
#ifndef __XTENSA_CALL0_ABI__
|
||||
movi a0, 0 /* no need to rotate window, it will be destroyed anyway */
|
||||
movi a6, shared_stack
|
||||
movi a0, 0 /* must not rotate the window here, */
|
||||
/* the state of execution for shared stack */
|
||||
/* functions will be completely destroyed at end */
|
||||
movi a6, xtensa_shared_stack
|
||||
l32i sp, a6, 0 /* load shared stack pointer */
|
||||
movi a12, shared_stack_callback
|
||||
movi a12, xtensa_shared_stack_callback
|
||||
l32i a12, a12, 0
|
||||
callx4 a12 /* call user function */
|
||||
movi a6, shared_stack_function_done
|
||||
movi a7, 1
|
||||
movi a6, xtensa_shared_stack_function_done
|
||||
movi a7, 1
|
||||
s32i a7, a6, 0 /* hint the function was finished */
|
||||
movi a6, shared_stack_env
|
||||
movi a6, xtensa_shared_stack_env
|
||||
movi a7, 0
|
||||
movi a12, longjmp
|
||||
movi a12, longjmp
|
||||
callx4 a12 /* jump to last clean state previously saved */
|
||||
ret
|
||||
#else
|
||||
#else
|
||||
#error "this code is written for Window ABI"
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user