expression_with_stack_riscv.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #include <esp_expression_with_stack.h>
  15. #include <riscv/rvruntime-frames.h>
  16. #include <string.h>
  17. #include "freertos/FreeRTOS.h"
  18. #include "freertos/portmacro.h"
  19. static portMUX_TYPE shared_stack_spinlock = portMUX_INITIALIZER_UNLOCKED;
  20. static void *current_task_stack = NULL;
  21. extern void esp_shared_stack_invoke_function(shared_stack_function function, void *stack);
  22. static StackType_t *esp_switch_stack_setup(StackType_t *stack, size_t stack_size)
  23. {
  24. //We need also to tweak current task stackpointer to avoid erroneous
  25. //stack overflow indication, so fills the stack with freertos known pattern:
  26. memset(stack, 0xa5U, stack_size * sizeof(StackType_t));
  27. StaticTask_t *current = (StaticTask_t *)xTaskGetCurrentTaskHandle();
  28. //Then put the fake stack inside of TCB:
  29. current_task_stack = current->pxDummy6;
  30. current->pxDummy6 = (void *)stack;
  31. StackType_t *top_of_stack = stack + stack_size;
  32. //Align stack to a 16byte boundary, as required by CPU specific:
  33. top_of_stack = (StackType_t *)(((UBaseType_t)(top_of_stack - 16) & ~0xf));
  34. RvExcFrame *adjusted_top_of_stack = (RvExcFrame *) top_of_stack;
  35. adjusted_top_of_stack--;
  36. #if CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK
  37. vPortSetStackWatchpoint(stack);
  38. #endif
  39. return ((StackType_t *)adjusted_top_of_stack);
  40. }
  41. void esp_execute_shared_stack_function(SemaphoreHandle_t lock, void *stack, size_t stack_size, shared_stack_function function)
  42. {
  43. assert(lock);
  44. assert(stack);
  45. assert(stack_size > 0 && stack_size >= CONFIG_ESP_MINIMAL_SHARED_STACK_SIZE);
  46. assert(function);
  47. xSemaphoreTake(lock, portMAX_DELAY);
  48. portENTER_CRITICAL(&shared_stack_spinlock);
  49. stack = esp_switch_stack_setup(stack, stack_size);
  50. portEXIT_CRITICAL(&shared_stack_spinlock);
  51. esp_shared_stack_invoke_function(function, stack);
  52. portENTER_CRITICAL(&shared_stack_spinlock);
  53. StaticTask_t *current = (StaticTask_t *)xTaskGetCurrentTaskHandle();
  54. //Restore current task stack:
  55. current->pxDummy6 = (StackType_t *)current_task_stack;
  56. vPortSetStackWatchpoint(current->pxDummy6);
  57. portEXIT_CRITICAL(&shared_stack_spinlock);
  58. xSemaphoreGive(lock);
  59. }