context_gcc.S 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. jal rt_thread_self
  72. mv s1, a0
  73. #ifdef RT_USING_SMART
  74. jal lwp_aspace_switch
  75. #endif
  76. RESTORE_CONTEXT
  77. sret
  78. /*
  79. * void rt_hw_context_switch(rt_ubase_t from, rt_ubase_t to);
  80. *
  81. * a0 --> from SP pointer
  82. * a1 --> to SP pointer
  83. *
  84. * It should only be used on local interrupt disable
  85. */
  86. .globl rt_hw_context_switch
  87. rt_hw_context_switch:
  88. RESERVE_CONTEXT
  89. STORE sp, (a0)
  90. // restore to thread SP
  91. LOAD sp, (a1)
  92. // restore Address Space
  93. jal rt_thread_self
  94. mv s1, a0
  95. #ifdef RT_USING_SMART
  96. jal lwp_aspace_switch
  97. #endif
  98. RESTORE_CONTEXT
  99. sret
  100. #ifdef ENABLE_VECTOR
  101. /**
  102. * @param a0 pointer to frame bottom
  103. */
  104. .global rt_hw_vector_ctx_save
  105. rt_hw_vector_ctx_save:
  106. SAVE_VECTOR a0
  107. ret
  108. /**
  109. * @param a0 pointer to frame bottom
  110. */
  111. .global rt_hw_vector_ctx_restore
  112. rt_hw_vector_ctx_restore:
  113. RESTORE_VECTOR a0
  114. ret
  115. .global rt_hw_disable_vector
  116. rt_hw_disable_vector:
  117. li t0, SSTATUS_VS
  118. csrc sstatus, t0
  119. ret
  120. .global rt_hw_enable_vector
  121. rt_hw_enable_vector:
  122. li t0, SSTATUS_VS
  123. csrs sstatus, t0
  124. ret
  125. #endif /* ENABLE_VECTOR */