context_gcc.S 7.8 KB

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