context_gcc.S 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Copyright (c) 2006-2018, 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. * 2020/11/20 BalanceTWK Add FPU support
  11. * 2022/12/28 WangShun Add macro to distinguish whether FPU is supported
  12. */
  13. #define __ASSEMBLY__
  14. #include "cpuport.h"
  15. #ifdef RT_USING_SMP
  16. #define rt_hw_interrupt_disable rt_hw_local_irq_disable
  17. #define rt_hw_interrupt_enable rt_hw_local_irq_enable
  18. #endif
  19. /*
  20. * rt_base_t rt_hw_interrupt_disable(void);
  21. */
  22. .globl rt_hw_interrupt_disable
  23. rt_hw_interrupt_disable:
  24. csrrci a0, mstatus, 8
  25. ret
  26. /*
  27. * void rt_hw_interrupt_enable(rt_base_t level);
  28. */
  29. .globl rt_hw_interrupt_enable
  30. rt_hw_interrupt_enable:
  31. csrw mstatus, a0
  32. ret
  33. /*
  34. * #ifdef RT_USING_SMP
  35. * void rt_hw_context_switch_to(rt_ubase_t to, stuct rt_thread *to_thread);
  36. * #else
  37. * void rt_hw_context_switch_to(rt_ubase_t to);
  38. * #endif
  39. * a0 --> to
  40. * a1 --> to_thread
  41. */
  42. .globl rt_hw_context_switch_to
  43. rt_hw_context_switch_to:
  44. la t0, __rt_rvstack
  45. csrw mscratch,t0
  46. LOAD sp, (a0)
  47. #ifdef RT_USING_SMP
  48. mv a0, a1
  49. call rt_cpus_lock_status_restore
  50. #endif
  51. LOAD a0, 2 * REGBYTES(sp)
  52. csrw mstatus, a0
  53. j rt_hw_context_switch_exit
  54. /*
  55. * #ifdef RT_USING_SMP
  56. * void rt_hw_context_switch(rt_ubase_t from, rt_ubase_t to, struct rt_thread *to_thread);
  57. * #else
  58. * void rt_hw_context_switch(rt_ubase_t from, rt_ubase_t to);
  59. * #endif
  60. *
  61. * a0 --> from
  62. * a1 --> to
  63. * a2 --> to_thread
  64. */
  65. .globl rt_hw_context_switch
  66. rt_hw_context_switch:
  67. /* saved from thread context
  68. * x1/ra -> sp(0)
  69. * x1/ra -> sp(1)
  70. * mstatus.mie -> sp(2)
  71. * x(i) -> sp(i-4)
  72. */
  73. #ifdef ARCH_RISCV_FPU
  74. addi sp, sp, -32 * FREGBYTES
  75. FSTORE f0, 0 * FREGBYTES(sp)
  76. FSTORE f1, 1 * FREGBYTES(sp)
  77. FSTORE f2, 2 * FREGBYTES(sp)
  78. FSTORE f3, 3 * FREGBYTES(sp)
  79. FSTORE f4, 4 * FREGBYTES(sp)
  80. FSTORE f5, 5 * FREGBYTES(sp)
  81. FSTORE f6, 6 * FREGBYTES(sp)
  82. FSTORE f7, 7 * FREGBYTES(sp)
  83. FSTORE f8, 8 * FREGBYTES(sp)
  84. FSTORE f9, 9 * FREGBYTES(sp)
  85. FSTORE f10, 10 * FREGBYTES(sp)
  86. FSTORE f11, 11 * FREGBYTES(sp)
  87. FSTORE f12, 12 * FREGBYTES(sp)
  88. FSTORE f13, 13 * FREGBYTES(sp)
  89. FSTORE f14, 14 * FREGBYTES(sp)
  90. FSTORE f15, 15 * FREGBYTES(sp)
  91. FSTORE f16, 16 * FREGBYTES(sp)
  92. FSTORE f17, 17 * FREGBYTES(sp)
  93. FSTORE f18, 18 * FREGBYTES(sp)
  94. FSTORE f19, 19 * FREGBYTES(sp)
  95. FSTORE f20, 20 * FREGBYTES(sp)
  96. FSTORE f21, 21 * FREGBYTES(sp)
  97. FSTORE f22, 22 * FREGBYTES(sp)
  98. FSTORE f23, 23 * FREGBYTES(sp)
  99. FSTORE f24, 24 * FREGBYTES(sp)
  100. FSTORE f25, 25 * FREGBYTES(sp)
  101. FSTORE f26, 26 * FREGBYTES(sp)
  102. FSTORE f27, 27 * FREGBYTES(sp)
  103. FSTORE f28, 28 * FREGBYTES(sp)
  104. FSTORE f29, 29 * FREGBYTES(sp)
  105. FSTORE f30, 30 * FREGBYTES(sp)
  106. FSTORE f31, 31 * FREGBYTES(sp)
  107. #endif
  108. addi sp, sp, -32 * REGBYTES
  109. STORE sp, (a0)
  110. STORE x1, 0 * REGBYTES(sp)
  111. STORE x1, 1 * REGBYTES(sp)
  112. csrr a0, mstatus
  113. andi a0, a0, 8
  114. beqz a0, save_mpie
  115. li a0, 0x80
  116. save_mpie:
  117. STORE a0, 2 * REGBYTES(sp)
  118. STORE x4, 4 * REGBYTES(sp)
  119. STORE x5, 5 * REGBYTES(sp)
  120. STORE x6, 6 * REGBYTES(sp)
  121. STORE x7, 7 * REGBYTES(sp)
  122. STORE x8, 8 * REGBYTES(sp)
  123. STORE x9, 9 * REGBYTES(sp)
  124. STORE x10, 10 * REGBYTES(sp)
  125. STORE x11, 11 * REGBYTES(sp)
  126. STORE x12, 12 * REGBYTES(sp)
  127. STORE x13, 13 * REGBYTES(sp)
  128. STORE x14, 14 * REGBYTES(sp)
  129. STORE x15, 15 * REGBYTES(sp)
  130. STORE x16, 16 * REGBYTES(sp)
  131. STORE x17, 17 * REGBYTES(sp)
  132. STORE x18, 18 * REGBYTES(sp)
  133. STORE x19, 19 * REGBYTES(sp)
  134. STORE x20, 20 * REGBYTES(sp)
  135. STORE x21, 21 * REGBYTES(sp)
  136. STORE x22, 22 * REGBYTES(sp)
  137. STORE x23, 23 * REGBYTES(sp)
  138. STORE x24, 24 * REGBYTES(sp)
  139. STORE x25, 25 * REGBYTES(sp)
  140. STORE x26, 26 * REGBYTES(sp)
  141. STORE x27, 27 * REGBYTES(sp)
  142. STORE x28, 28 * REGBYTES(sp)
  143. STORE x29, 29 * REGBYTES(sp)
  144. STORE x30, 30 * REGBYTES(sp)
  145. STORE x31, 31 * REGBYTES(sp)
  146. /* restore to thread context
  147. * sp(0) -> epc;
  148. * sp(1) -> ra;
  149. * sp(i) -> x(i+2)
  150. */
  151. LOAD sp, (a1)
  152. #ifdef RT_USING_SMP
  153. mv a0, a2
  154. call rt_cpus_lock_status_restore
  155. #endif /*RT_USING_SMP*/
  156. j rt_hw_context_switch_exit
  157. #ifdef RT_USING_SMP
  158. /*
  159. * void rt_hw_context_switch_interrupt(void *context, rt_ubase_t from, rt_ubase_t to, struct rt_thread *to_thread);
  160. *
  161. * a0 --> context
  162. * a1 --> from
  163. * a2 --> to
  164. * a3 --> to_thread
  165. */
  166. .globl rt_hw_context_switch_interrupt
  167. rt_hw_context_switch_interrupt:
  168. STORE a0, 0(a1)
  169. LOAD sp, 0(a2)
  170. move a0, a3
  171. call rt_cpus_lock_status_restore
  172. j rt_hw_context_switch_exit
  173. #endif
  174. .global rt_hw_context_switch_exit
  175. rt_hw_context_switch_exit:
  176. #ifdef RT_USING_SMP
  177. #ifdef RT_USING_SIGNALS
  178. mv a0, sp
  179. csrr t0, mhartid
  180. /* switch interrupt stack of current cpu */
  181. la sp, __stack_start__
  182. addi t1, t0, 1
  183. li t2, __STACKSIZE__
  184. mul t1, t1, t2
  185. add sp, sp, t1 /* sp = (cpuid + 1) * __STACKSIZE__ + __stack_start__ */
  186. call rt_signal_check
  187. mv sp, a0
  188. #endif
  189. #endif
  190. /* resw ra to mepc */
  191. LOAD a0, 0 * REGBYTES(sp)
  192. csrw mepc, a0
  193. LOAD x1, 1 * REGBYTES(sp)
  194. #ifdef ARCH_RISCV_FPU
  195. li t0, 0x7800
  196. #else
  197. li t0, 0x1800
  198. #endif
  199. csrw mstatus, t0
  200. LOAD a0, 2 * REGBYTES(sp)
  201. csrs mstatus, a0
  202. LOAD x4, 4 * REGBYTES(sp)
  203. LOAD x5, 5 * REGBYTES(sp)
  204. LOAD x6, 6 * REGBYTES(sp)
  205. LOAD x7, 7 * REGBYTES(sp)
  206. LOAD x8, 8 * REGBYTES(sp)
  207. LOAD x9, 9 * REGBYTES(sp)
  208. LOAD x10, 10 * REGBYTES(sp)
  209. LOAD x11, 11 * REGBYTES(sp)
  210. LOAD x12, 12 * REGBYTES(sp)
  211. LOAD x13, 13 * REGBYTES(sp)
  212. LOAD x14, 14 * REGBYTES(sp)
  213. LOAD x15, 15 * REGBYTES(sp)
  214. LOAD x16, 16 * REGBYTES(sp)
  215. LOAD x17, 17 * REGBYTES(sp)
  216. LOAD x18, 18 * REGBYTES(sp)
  217. LOAD x19, 19 * REGBYTES(sp)
  218. LOAD x20, 20 * REGBYTES(sp)
  219. LOAD x21, 21 * REGBYTES(sp)
  220. LOAD x22, 22 * REGBYTES(sp)
  221. LOAD x23, 23 * REGBYTES(sp)
  222. LOAD x24, 24 * REGBYTES(sp)
  223. LOAD x25, 25 * REGBYTES(sp)
  224. LOAD x26, 26 * REGBYTES(sp)
  225. LOAD x27, 27 * REGBYTES(sp)
  226. LOAD x28, 28 * REGBYTES(sp)
  227. LOAD x29, 29 * REGBYTES(sp)
  228. LOAD x30, 30 * REGBYTES(sp)
  229. LOAD x31, 31 * REGBYTES(sp)
  230. addi sp, sp, 32 * REGBYTES
  231. #ifdef ARCH_RISCV_FPU
  232. FLOAD f0, 0 * FREGBYTES(sp)
  233. FLOAD f1, 1 * FREGBYTES(sp)
  234. FLOAD f2, 2 * FREGBYTES(sp)
  235. FLOAD f3, 3 * FREGBYTES(sp)
  236. FLOAD f4, 4 * FREGBYTES(sp)
  237. FLOAD f5, 5 * FREGBYTES(sp)
  238. FLOAD f6, 6 * FREGBYTES(sp)
  239. FLOAD f7, 7 * FREGBYTES(sp)
  240. FLOAD f8, 8 * FREGBYTES(sp)
  241. FLOAD f9, 9 * FREGBYTES(sp)
  242. FLOAD f10, 10 * FREGBYTES(sp)
  243. FLOAD f11, 11 * FREGBYTES(sp)
  244. FLOAD f12, 12 * FREGBYTES(sp)
  245. FLOAD f13, 13 * FREGBYTES(sp)
  246. FLOAD f14, 14 * FREGBYTES(sp)
  247. FLOAD f15, 15 * FREGBYTES(sp)
  248. FLOAD f16, 16 * FREGBYTES(sp)
  249. FLOAD f17, 17 * FREGBYTES(sp)
  250. FLOAD f18, 18 * FREGBYTES(sp)
  251. FLOAD f19, 19 * FREGBYTES(sp)
  252. FLOAD f20, 20 * FREGBYTES(sp)
  253. FLOAD f21, 21 * FREGBYTES(sp)
  254. FLOAD f22, 22 * FREGBYTES(sp)
  255. FLOAD f23, 23 * FREGBYTES(sp)
  256. FLOAD f24, 24 * FREGBYTES(sp)
  257. FLOAD f25, 25 * FREGBYTES(sp)
  258. FLOAD f26, 26 * FREGBYTES(sp)
  259. FLOAD f27, 27 * FREGBYTES(sp)
  260. FLOAD f28, 28 * FREGBYTES(sp)
  261. FLOAD f29, 29 * FREGBYTES(sp)
  262. FLOAD f30, 30 * FREGBYTES(sp)
  263. FLOAD f31, 31 * FREGBYTES(sp)
  264. addi sp, sp, 32 * FREGBYTES
  265. #endif
  266. mret