stack.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-05-12 RT-Thread init
  9. * 2023-07-13 GuEe-GUI append fpu: Q16 ~ Q31
  10. */
  11. #include <board.h>
  12. #include <rtthread.h>
  13. #include <cpuport.h>
  14. #include <armv8.h>
  15. #define INITIAL_SPSR_EL1 (PSTATE_EL1 | SP_ELx)
  16. /**
  17. * This function will initialize thread stack
  18. *
  19. * @param tentry the entry of thread
  20. * @param parameter the parameter of entry
  21. * @param stack_addr the beginning stack address
  22. * @param texit the function will be called when thread exit
  23. *
  24. * @return stack address
  25. */
  26. rt_uint8_t *rt_hw_stack_init(void *tentry, void *parameter,
  27. rt_uint8_t *stack_addr, void *texit)
  28. {
  29. rt_ubase_t *stk;
  30. /* The AAPCS64 requires 128-bit (16 byte) stack alignment */
  31. stk = (rt_ubase_t*)RT_ALIGN_DOWN((rt_ubase_t)stack_addr, 16);
  32. for (int i = 0; i < 32; ++i)
  33. {
  34. stk -= sizeof(rt_uint128_t) / sizeof(rt_ubase_t);
  35. *(rt_uint128_t *)stk = (rt_uint128_t) { 0 };
  36. }
  37. *(--stk) = (rt_ubase_t)texit; /* X20, 2nd param */
  38. *(--stk) = (rt_ubase_t)tentry; /* X19, 1st param */
  39. *(--stk) = (rt_ubase_t)22; /* X22 */
  40. *(--stk) = (rt_ubase_t)parameter; /* X21, 3rd param */
  41. *(--stk) = (rt_ubase_t)24; /* X24 */
  42. *(--stk) = (rt_ubase_t)23; /* X23 */
  43. *(--stk) = (rt_ubase_t)26; /* X26 */
  44. *(--stk) = (rt_ubase_t)25; /* X25 */
  45. *(--stk) = (rt_ubase_t)28; /* X28 */
  46. *(--stk) = (rt_ubase_t)27; /* X27 */
  47. *(--stk) = (rt_ubase_t)0; /* sp_el0 */
  48. *(--stk) = (rt_ubase_t)0; /* X29 - addr 0 as AAPCS64 specified */
  49. *(--stk) = (rt_ubase_t)0; /* FPSR */
  50. *(--stk) = (rt_ubase_t)0; /* FPCR */
  51. *(--stk) = INITIAL_SPSR_EL1; /* Save Processor States */
  52. *(--stk) = (rt_ubase_t)_thread_start; /* Exception return address. */
  53. /* return task's current stack address */
  54. return (rt_uint8_t *)stk;
  55. }