os_cpu_a.S 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. #include "riscv_encoding.h"
  2. #ifndef __riscv_32e
  3. #define portRegNum 32
  4. #else
  5. #define portRegNum 14
  6. #endif
  7. #define portCONTEXT_SIZE ( portRegNum * REGBYTES )
  8. .extern OSTCBCur
  9. .extern OSTCBHighRdy
  10. .extern xPortTaskSwitch
  11. .global prvPortStartFirstTask
  12. .global exc_entry
  13. .global irq_entry
  14. .global default_intexc_handler
  15. .global eclic_msip_handler
  16. .section .text.entry
  17. .align 8
  18. /**
  19. * \brief Global interrupt disabled
  20. * \details
  21. * This function disable global interrupt.
  22. * \remarks
  23. * - All the interrupt requests will be ignored by CPU.
  24. */
  25. .macro DISABLE_MIE
  26. csrc CSR_MSTATUS, MSTATUS_MIE
  27. .endm
  28. /**
  29. * \brief Macro for context save
  30. * \details
  31. * This macro save ABI defined caller saved registers in the stack.
  32. * \remarks
  33. * - This Macro could use to save context when you enter to interrupt
  34. * or exception
  35. */
  36. /* Save caller registers */
  37. .macro SAVE_CONTEXT
  38. #if defined(ECLIC_HW_CTX_AUTO) && defined(CFG_HAS_ECLICV2)
  39. #else
  40. csrrw sp, CSR_MSCRATCHCSWL, sp
  41. /* Allocate stack space for context saving */
  42. #ifndef __riscv_32e
  43. addi sp, sp, -20*REGBYTES
  44. #else
  45. addi sp, sp, -14*REGBYTES
  46. #endif /* __riscv_32e */
  47. STORE x1, 0*REGBYTES(sp)
  48. STORE x4, 1*REGBYTES(sp)
  49. STORE x5, 2*REGBYTES(sp)
  50. STORE x6, 3*REGBYTES(sp)
  51. STORE x7, 4*REGBYTES(sp)
  52. STORE x10, 5*REGBYTES(sp)
  53. STORE x11, 6*REGBYTES(sp)
  54. STORE x12, 7*REGBYTES(sp)
  55. STORE x13, 8*REGBYTES(sp)
  56. STORE x14, 9*REGBYTES(sp)
  57. STORE x15, 10*REGBYTES(sp)
  58. #ifndef __riscv_32e
  59. STORE x16, 14*REGBYTES(sp)
  60. STORE x17, 15*REGBYTES(sp)
  61. STORE x28, 16*REGBYTES(sp)
  62. STORE x29, 17*REGBYTES(sp)
  63. STORE x30, 18*REGBYTES(sp)
  64. STORE x31, 19*REGBYTES(sp)
  65. #endif /* __riscv_32e */
  66. #endif
  67. .endm
  68. /**
  69. * \brief Macro for restore caller registers
  70. * \details
  71. * This macro restore ABI defined caller saved registers from stack.
  72. * \remarks
  73. * - You could use this macro to restore context before you want return
  74. * from interrupt or exeception
  75. */
  76. /* Restore caller registers */
  77. .macro RESTORE_CONTEXT
  78. #if defined(ECLIC_HW_CTX_AUTO) && defined(CFG_HAS_ECLICV2)
  79. #else
  80. LOAD x1, 0*REGBYTES(sp)
  81. LOAD x4, 1*REGBYTES(sp)
  82. LOAD x5, 2*REGBYTES(sp)
  83. LOAD x6, 3*REGBYTES(sp)
  84. LOAD x7, 4*REGBYTES(sp)
  85. LOAD x10, 5*REGBYTES(sp)
  86. LOAD x11, 6*REGBYTES(sp)
  87. LOAD x12, 7*REGBYTES(sp)
  88. LOAD x13, 8*REGBYTES(sp)
  89. LOAD x14, 9*REGBYTES(sp)
  90. LOAD x15, 10*REGBYTES(sp)
  91. #ifndef __riscv_32e
  92. LOAD x16, 14*REGBYTES(sp)
  93. LOAD x17, 15*REGBYTES(sp)
  94. LOAD x28, 16*REGBYTES(sp)
  95. LOAD x29, 17*REGBYTES(sp)
  96. LOAD x30, 18*REGBYTES(sp)
  97. LOAD x31, 19*REGBYTES(sp)
  98. /* De-allocate the stack space */
  99. addi sp, sp, 20*REGBYTES
  100. #else
  101. /* De-allocate the stack space */
  102. addi sp, sp, 14*REGBYTES
  103. #endif /* __riscv_32e */
  104. csrrw sp, CSR_MSCRATCHCSWL, sp
  105. #endif
  106. .endm
  107. /**
  108. * \brief Macro for save necessary CSRs to stack
  109. * \details
  110. * This macro store MCAUSE, MEPC, MSUBM to stack.
  111. */
  112. .macro SAVE_CSR_CONTEXT
  113. #if defined(ECLIC_HW_CTX_AUTO) && defined(CFG_HAS_ECLICV2)
  114. #else
  115. /* Store CSR mcause to stack using pushmcause */
  116. csrrwi x0, CSR_PUSHMCAUSE, 11
  117. /* Store CSR mepc to stack using pushmepc */
  118. csrrwi x0, CSR_PUSHMEPC, 12
  119. /* Store CSR msub to stack using pushmsub */
  120. csrrwi x0, CSR_PUSHMSUBM, 13
  121. #endif
  122. .endm
  123. /**
  124. * \brief Macro for restore necessary CSRs from stack
  125. * \details
  126. * This macro restore MSUBM, MEPC, MCAUSE from stack.
  127. */
  128. .macro RESTORE_CSR_CONTEXT
  129. #if defined(ECLIC_HW_CTX_AUTO) && defined(CFG_HAS_ECLICV2)
  130. #else
  131. LOAD x5, 13*REGBYTES(sp)
  132. csrw CSR_MSUBM, x5
  133. LOAD x5, 12*REGBYTES(sp)
  134. csrw CSR_MEPC, x5
  135. LOAD x5, 11*REGBYTES(sp)
  136. csrw CSR_MCAUSE, x5
  137. #endif
  138. .endm
  139. /**
  140. * \brief Exception/NMI Entry
  141. * \details
  142. * This function provide common entry functions for exception/nmi.
  143. * \remarks
  144. * This function provide a default exception/nmi entry.
  145. * ABI defined caller save register and some CSR registers
  146. * to be saved before enter interrupt handler and be restored before return.
  147. */
  148. .section .text.trap
  149. /* In CLIC mode, the exeception entry must be 64bytes aligned */
  150. .align 6
  151. .type exc_entry, @function
  152. exc_entry:
  153. /* Save the caller saving registers (context) */
  154. SAVE_CONTEXT
  155. /* Save the necessary CSR registers */
  156. SAVE_CSR_CONTEXT
  157. /*
  158. * Set the exception handler function arguments
  159. * argument 1: mcause value
  160. * argument 2: current stack point(SP) value
  161. */
  162. csrr a0, mcause
  163. mv a1, sp
  164. /*
  165. * TODO: Call the exception handler function
  166. * By default, the function template is provided in
  167. * system_Device.c, you can adjust it as you want
  168. */
  169. call core_exception_handler
  170. /* Restore the necessary CSR registers */
  171. RESTORE_CSR_CONTEXT
  172. /* Restore the caller saving registers (context) */
  173. RESTORE_CONTEXT
  174. /* Return to regular code */
  175. #if defined(ECLIC_HW_CTX_AUTO) && defined(CFG_HAS_ECLICV2)
  176. csrrwi x0, CSR_POPXRET, 0
  177. #else
  178. mret
  179. #endif
  180. .size exc_entry, . - exc_entry
  181. /**
  182. * \brief Non-Vector Interrupt Entry
  183. * \details
  184. * This function provide common entry functions for handling
  185. * non-vector interrupts
  186. * \remarks
  187. * This function provide a default non-vector interrupt entry.
  188. * ABI defined caller save register and some CSR registers need
  189. * to be saved before enter interrupt handler and be restored before return.
  190. */
  191. .section .text.irq
  192. /* In CLIC mode, the interrupt entry must be 4bytes aligned */
  193. .align 2
  194. .type irq_entry, @function
  195. /* This label will be set to MTVT2 register */
  196. irq_entry:
  197. /* Save the caller saving registers (context) */
  198. SAVE_CONTEXT
  199. /* Save the necessary CSR registers */
  200. SAVE_CSR_CONTEXT
  201. /* This special CSR read/write operation, which is actually
  202. * claim the CLIC to find its pending highest ID, if the ID
  203. * is not 0, then automatically enable the mstatus.MIE, and
  204. * jump to its vector-entry-label, and update the link register
  205. */
  206. csrrw ra, CSR_JALMNXTI, ra
  207. /* Critical section with interrupts disabled */
  208. DISABLE_MIE
  209. /* Restore the necessary CSR registers */
  210. RESTORE_CSR_CONTEXT
  211. /* Restore the caller saving registers (context) */
  212. RESTORE_CONTEXT
  213. /* Return to regular code */
  214. #if defined(ECLIC_HW_CTX_AUTO) && defined(CFG_HAS_ECLICV2)
  215. csrrwi x0, CSR_POPXRET, 0
  216. #else
  217. mret
  218. #endif
  219. .size irq_entry, . - irq_entry
  220. /* Default Handler for Exceptions / Interrupts */
  221. Undef_Handler:
  222. .type default_intexc_handler, @function
  223. default_intexc_handler:
  224. 1:
  225. j 1b
  226. .size default_intexc_handler, . - default_intexc_handler
  227. /* Start the first task. This also clears the bit that indicates the FPU is
  228. in use in case the FPU was used before the scheduler was started - which
  229. would otherwise result in the unnecessary leaving of space in the stack
  230. for lazy saving of FPU registers. */
  231. .align 3
  232. .type prvPortStartFirstTask, @function
  233. prvPortStartFirstTask:
  234. /* Setup Interrupt Stack using
  235. The stack that was used by main()
  236. before the scheduler is started is
  237. no longer required after the scheduler is started.
  238. Interrupt stack pointer is stored in CSR_MSCRATCH */
  239. la t0, _sp
  240. csrw CSR_MSCRATCH, t0
  241. LOAD sp, OSTCBHighRdy /* Load OSTCBHighRdy. */
  242. LOAD sp, 0x0(sp) /* Read sp from first TCB member */
  243. /* Pop PC from stack and set MEPC */
  244. LOAD t0, 0 * REGBYTES(sp)
  245. csrw CSR_MEPC, t0
  246. /* Pop mstatus from stack and set it */
  247. LOAD t0, (portRegNum - 1) * REGBYTES(sp)
  248. csrw CSR_MSTATUS, t0
  249. /* Interrupt still disable here */
  250. /* Restore Registers from Stack */
  251. LOAD x1, 1 * REGBYTES(sp) /* RA */
  252. LOAD x5, 2 * REGBYTES(sp)
  253. LOAD x6, 3 * REGBYTES(sp)
  254. LOAD x7, 4 * REGBYTES(sp)
  255. LOAD x8, 5 * REGBYTES(sp)
  256. LOAD x9, 6 * REGBYTES(sp)
  257. LOAD x10, 7 * REGBYTES(sp)
  258. LOAD x11, 8 * REGBYTES(sp)
  259. LOAD x12, 9 * REGBYTES(sp)
  260. LOAD x13, 10 * REGBYTES(sp)
  261. LOAD x14, 11 * REGBYTES(sp)
  262. LOAD x15, 12 * REGBYTES(sp)
  263. #ifndef __riscv_32e
  264. LOAD x16, 13 * REGBYTES(sp)
  265. LOAD x17, 14 * REGBYTES(sp)
  266. LOAD x18, 15 * REGBYTES(sp)
  267. LOAD x19, 16 * REGBYTES(sp)
  268. LOAD x20, 17 * REGBYTES(sp)
  269. LOAD x21, 18 * REGBYTES(sp)
  270. LOAD x22, 19 * REGBYTES(sp)
  271. LOAD x23, 20 * REGBYTES(sp)
  272. LOAD x24, 21 * REGBYTES(sp)
  273. LOAD x25, 22 * REGBYTES(sp)
  274. LOAD x26, 23 * REGBYTES(sp)
  275. LOAD x27, 24 * REGBYTES(sp)
  276. LOAD x28, 25 * REGBYTES(sp)
  277. LOAD x29, 26 * REGBYTES(sp)
  278. LOAD x30, 27 * REGBYTES(sp)
  279. LOAD x31, 28 * REGBYTES(sp)
  280. #endif
  281. addi sp, sp, portCONTEXT_SIZE
  282. mret
  283. .size prvPortStartFirstTask, . - prvPortStartFirstTask
  284. .section .text
  285. .align 2
  286. .type eclic_msip_handler, @function
  287. eclic_msip_handler:
  288. addi sp, sp, -portCONTEXT_SIZE
  289. STORE x1, 1 * REGBYTES(sp) /* RA */
  290. STORE x5, 2 * REGBYTES(sp)
  291. STORE x6, 3 * REGBYTES(sp)
  292. STORE x7, 4 * REGBYTES(sp)
  293. STORE x8, 5 * REGBYTES(sp)
  294. STORE x9, 6 * REGBYTES(sp)
  295. STORE x10, 7 * REGBYTES(sp)
  296. STORE x11, 8 * REGBYTES(sp)
  297. STORE x12, 9 * REGBYTES(sp)
  298. STORE x13, 10 * REGBYTES(sp)
  299. STORE x14, 11 * REGBYTES(sp)
  300. STORE x15, 12 * REGBYTES(sp)
  301. #ifndef __riscv_32e
  302. STORE x16, 13 * REGBYTES(sp)
  303. STORE x17, 14 * REGBYTES(sp)
  304. STORE x18, 15 * REGBYTES(sp)
  305. STORE x19, 16 * REGBYTES(sp)
  306. STORE x20, 17 * REGBYTES(sp)
  307. STORE x21, 18 * REGBYTES(sp)
  308. STORE x22, 19 * REGBYTES(sp)
  309. STORE x23, 20 * REGBYTES(sp)
  310. STORE x24, 21 * REGBYTES(sp)
  311. STORE x25, 22 * REGBYTES(sp)
  312. STORE x26, 23 * REGBYTES(sp)
  313. STORE x27, 24 * REGBYTES(sp)
  314. STORE x28, 25 * REGBYTES(sp)
  315. STORE x29, 26 * REGBYTES(sp)
  316. STORE x30, 27 * REGBYTES(sp)
  317. STORE x31, 28 * REGBYTES(sp)
  318. #endif
  319. /* Push mstatus to stack */
  320. csrr t0, CSR_MSTATUS
  321. STORE t0, (portRegNum - 1) * REGBYTES(sp)
  322. /* Push additional registers */
  323. /* Store sp to task stack */
  324. LOAD t0, OSTCBCur
  325. STORE sp, 0(t0)
  326. csrr t0, CSR_MEPC
  327. STORE t0, 0(sp)
  328. jal xPortTaskSwitch
  329. /* Switch task context */
  330. LOAD t0, OSTCBHighRdy /* Load OSTCBHighRdy. */
  331. LOAD sp, 0x0(t0) /* Read sp from first TCB member */
  332. /* Pop PC from stack and set MEPC */
  333. LOAD t0, 0 * REGBYTES(sp)
  334. csrw CSR_MEPC, t0
  335. /* Pop additional registers */
  336. /* Pop mstatus from stack and set it */
  337. LOAD t0, (portRegNum - 1) * REGBYTES(sp)
  338. csrw CSR_MSTATUS, t0
  339. /* Interrupt still disable here */
  340. /* Restore Registers from Stack */
  341. LOAD x1, 1 * REGBYTES(sp) /* RA */
  342. LOAD x5, 2 * REGBYTES(sp)
  343. LOAD x6, 3 * REGBYTES(sp)
  344. LOAD x7, 4 * REGBYTES(sp)
  345. LOAD x8, 5 * REGBYTES(sp)
  346. LOAD x9, 6 * REGBYTES(sp)
  347. LOAD x10, 7 * REGBYTES(sp)
  348. LOAD x11, 8 * REGBYTES(sp)
  349. LOAD x12, 9 * REGBYTES(sp)
  350. LOAD x13, 10 * REGBYTES(sp)
  351. LOAD x14, 11 * REGBYTES(sp)
  352. LOAD x15, 12 * REGBYTES(sp)
  353. #ifndef __riscv_32e
  354. LOAD x16, 13 * REGBYTES(sp)
  355. LOAD x17, 14 * REGBYTES(sp)
  356. LOAD x18, 15 * REGBYTES(sp)
  357. LOAD x19, 16 * REGBYTES(sp)
  358. LOAD x20, 17 * REGBYTES(sp)
  359. LOAD x21, 18 * REGBYTES(sp)
  360. LOAD x22, 19 * REGBYTES(sp)
  361. LOAD x23, 20 * REGBYTES(sp)
  362. LOAD x24, 21 * REGBYTES(sp)
  363. LOAD x25, 22 * REGBYTES(sp)
  364. LOAD x26, 23 * REGBYTES(sp)
  365. LOAD x27, 24 * REGBYTES(sp)
  366. LOAD x28, 25 * REGBYTES(sp)
  367. LOAD x29, 26 * REGBYTES(sp)
  368. LOAD x30, 27 * REGBYTES(sp)
  369. LOAD x31, 28 * REGBYTES(sp)
  370. #endif
  371. addi sp, sp, portCONTEXT_SIZE
  372. mret
  373. .size eclic_msip_handler, . - eclic_msip_handler