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, -8
  18. STORE \reg, (sp)
  19. .endm
  20. .macro POP_8 reg
  21. LOAD \reg, (sp)
  22. addi sp, sp, 8
  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. .endm
  44. .macro RESTORE_CONTEXT
  45. POP_8 s11
  46. csrw sstatus, s11
  47. POP_8 s11
  48. POP_8 s10
  49. POP_8 s9
  50. POP_8 s8
  51. POP_8 s7
  52. POP_8 s6
  53. POP_8 s5
  54. POP_8 s4
  55. POP_8 s3
  56. POP_8 s2
  57. POP_8 s1
  58. POP_8 s0
  59. POP_8 ra
  60. POP_8 tp
  61. csrw sepc, ra
  62. .endm
  63. /*
  64. * void rt_hw_context_switch_to(rt_ubase_t to);
  65. *
  66. * a0 --> to SP pointer
  67. */
  68. .globl rt_hw_context_switch_to
  69. rt_hw_context_switch_to:
  70. LOAD sp, (a0)
  71. la s0, rt_current_thread
  72. LOAD s1, (s0)
  73. #ifdef RT_USING_SMART
  74. mv a0, s1
  75. jal lwp_aspace_switch
  76. #endif
  77. RESTORE_CONTEXT
  78. sret
  79. /*
  80. * void rt_hw_context_switch(rt_ubase_t from, rt_ubase_t to);
  81. *
  82. * a0 --> from SP pointer
  83. * a1 --> to SP pointer
  84. *
  85. * It should only be used on local interrupt disable
  86. */
  87. .globl rt_hw_context_switch
  88. rt_hw_context_switch:
  89. RESERVE_CONTEXT
  90. STORE sp, (a0)
  91. // restore to thread SP
  92. LOAD sp, (a1)
  93. // restore Address Space
  94. la s0, rt_current_thread
  95. LOAD s1, (s0)
  96. #ifdef RT_USING_SMART
  97. mv a0, s1
  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 */