context_gcc.S 8.5 KB

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