context_gcc.S 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018/10/28 Bernard The unify RISC-V porting implementation
  9. * 2018/12/27 Jesven Add SMP support
  10. * 2021/02/02 lizhirui Add userspace support
  11. * 2022/10/22 WangXiaoyao Support User mode RVV;
  12. * Trimming process switch context
  13. */
  14. #include "cpuport.h"
  15. #include "stackframe.h"
  16. .macro PUSH_8 reg
  17. addi sp, sp, -REGBYTES
  18. STORE \reg, (sp)
  19. .endm
  20. .macro POP_8 reg
  21. LOAD \reg, (sp)
  22. addi sp, sp, REGBYTES
  23. .endm
  24. .macro RESERVE_CONTEXT
  25. PUSH_8 tp
  26. PUSH_8 ra
  27. PUSH_8 s0
  28. PUSH_8 s1
  29. PUSH_8 s2
  30. PUSH_8 s3
  31. PUSH_8 s4
  32. PUSH_8 s5
  33. PUSH_8 s6
  34. PUSH_8 s7
  35. PUSH_8 s8
  36. PUSH_8 s9
  37. PUSH_8 s10
  38. PUSH_8 s11
  39. csrr s11, sstatus
  40. li s10, (SSTATUS_SPP)
  41. or s11, s11, s10
  42. PUSH_8 s11
  43. addi sp, sp, -REGBYTES
  44. .endm
  45. .macro RESTORE_CONTEXT
  46. addi sp, sp, REGBYTES
  47. POP_8 s11
  48. csrw sstatus, s11
  49. POP_8 s11
  50. POP_8 s10
  51. POP_8 s9
  52. POP_8 s8
  53. POP_8 s7
  54. POP_8 s6
  55. POP_8 s5
  56. POP_8 s4
  57. POP_8 s3
  58. POP_8 s2
  59. POP_8 s1
  60. POP_8 s0
  61. POP_8 ra
  62. POP_8 tp
  63. csrw sepc, ra
  64. .endm
  65. /*
  66. * void rt_hw_context_switch_to(rt_ubase_t to);
  67. *
  68. * a0 --> to SP pointer
  69. */
  70. .globl rt_hw_context_switch_to
  71. rt_hw_context_switch_to:
  72. LOAD sp, (a0)
  73. jal rt_thread_self
  74. mv s1, a0
  75. #ifdef RT_USING_SMART
  76. jal lwp_aspace_switch
  77. #endif
  78. RESTORE_CONTEXT
  79. sret
  80. /*
  81. * void rt_hw_context_switch(rt_ubase_t from, rt_ubase_t to);
  82. *
  83. * a0 --> from SP pointer
  84. * a1 --> to SP pointer
  85. *
  86. * It should only be used on local interrupt disable
  87. */
  88. .globl rt_hw_context_switch
  89. rt_hw_context_switch:
  90. RESERVE_CONTEXT
  91. STORE sp, (a0)
  92. // restore to thread SP
  93. LOAD sp, (a1)
  94. // restore Address Space
  95. jal rt_thread_self
  96. mv s1, a0
  97. #ifdef RT_USING_SMART
  98. jal lwp_aspace_switch
  99. #endif
  100. RESTORE_CONTEXT
  101. sret
  102. #ifdef ENABLE_VECTOR
  103. /**
  104. * @param a0 pointer to frame bottom
  105. */
  106. .global rt_hw_vector_ctx_save
  107. rt_hw_vector_ctx_save:
  108. SAVE_VECTOR a0
  109. ret
  110. /**
  111. * @param a0 pointer to frame bottom
  112. */
  113. .global rt_hw_vector_ctx_restore
  114. rt_hw_vector_ctx_restore:
  115. RESTORE_VECTOR a0
  116. ret
  117. .global rt_hw_disable_vector
  118. rt_hw_disable_vector:
  119. li t0, SSTATUS_VS
  120. csrc sstatus, t0
  121. ret
  122. .global rt_hw_enable_vector
  123. rt_hw_enable_vector:
  124. li t0, SSTATUS_VS
  125. csrs sstatus, t0
  126. ret
  127. #endif /* ENABLE_VECTOR */