context_gcc.S 7.9 KB

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