1 // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <esp_expression_with_stack.h>
16 #include <riscv/rvruntime-frames.h>
17 #include <string.h>
18 #include "freertos/FreeRTOS.h"
19 #include "freertos/portmacro.h"
20
21 static portMUX_TYPE shared_stack_spinlock = portMUX_INITIALIZER_UNLOCKED;
22 static void *current_task_stack = NULL;
23
24 extern void esp_shared_stack_invoke_function(shared_stack_function function, void *stack);
25
esp_switch_stack_setup(StackType_t * stack,size_t stack_size)26 static StackType_t *esp_switch_stack_setup(StackType_t *stack, size_t stack_size)
27 {
28 //We need also to tweak current task stackpointer to avoid erroneous
29 //stack overflow indication, so fills the stack with freertos known pattern:
30 memset(stack, 0xa5U, stack_size * sizeof(StackType_t));
31
32 StaticTask_t *current = (StaticTask_t *)xTaskGetCurrentTaskHandle();
33 //Then put the fake stack inside of TCB:
34 current_task_stack = current->pxDummy6;
35 current->pxDummy6 = (void *)stack;
36
37 StackType_t *top_of_stack = stack + stack_size;
38
39 //Align stack to a 16byte boundary, as required by CPU specific:
40 top_of_stack = (StackType_t *)(((UBaseType_t)(top_of_stack - 16) & ~0xf));
41 StackType_t *adjusted_top_of_stack = top_of_stack - RV_STK_FRMSZ;
42
43 #if CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK
44 vPortSetStackWatchpoint(stack);
45 #endif
46 return ((StackType_t *)adjusted_top_of_stack);
47 }
48
49
esp_execute_shared_stack_function(SemaphoreHandle_t lock,void * stack,size_t stack_size,shared_stack_function function)50 void esp_execute_shared_stack_function(SemaphoreHandle_t lock, void *stack, size_t stack_size, shared_stack_function function)
51 {
52 assert(lock);
53 assert(stack);
54 assert(stack_size > 0 && stack_size >= CONFIG_ESP_MINIMAL_SHARED_STACK_SIZE);
55 assert(function);
56
57 xSemaphoreTake(lock, portMAX_DELAY);
58 portENTER_CRITICAL(&shared_stack_spinlock);
59 stack = esp_switch_stack_setup(stack, stack_size);
60 portEXIT_CRITICAL(&shared_stack_spinlock);
61
62 esp_shared_stack_invoke_function(function, stack);
63
64 portENTER_CRITICAL(&shared_stack_spinlock);
65 StaticTask_t *current = (StaticTask_t *)xTaskGetCurrentTaskHandle();
66
67 //Restore current task stack:
68 current->pxDummy6 = (StackType_t *)current_task_stack;
69 vPortSetStackWatchpoint(current->pxDummy6);
70 portEXIT_CRITICAL(&shared_stack_spinlock);
71
72 xSemaphoreGive(lock);
73 }
74