context_gcc.S 7.5 KB

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