interrupt_gcc.S 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (c) 2019-Present Nuclei Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020/03/26 Huaqi First Nuclei RISC-V porting implementation
  9. */
  10. #include "riscv_encoding.h"
  11. .section .text.entry
  12. .align 8
  13. /**
  14. * \brief Global interrupt disabled
  15. * \details
  16. * This function disable global interrupt.
  17. * \remarks
  18. * - All the interrupt requests will be ignored by CPU.
  19. */
  20. .macro DISABLE_XIE
  21. csrc CSR_XSTATUS, XSTATUS_XIE
  22. .endm
  23. /**
  24. * \brief Macro for context save
  25. * \details
  26. * This macro save ABI defined caller saved registers in the stack.
  27. * \remarks
  28. * - This Macro could use to save context when you enter to interrupt
  29. * or exception
  30. */
  31. /* Save caller registers */
  32. .macro SAVE_CONTEXT
  33. #if defined(ECLIC_HW_CTX_AUTO) && defined(CFG_HAS_ECLICV2)
  34. #else
  35. csrrw sp, CSR_XSCRATCHCSWL, sp
  36. /* Allocate stack space for context saving */
  37. #ifndef __riscv_32e
  38. addi sp, sp, -20*REGBYTES
  39. #else
  40. addi sp, sp, -14*REGBYTES
  41. #endif /* __riscv_32e */
  42. STORE x1, 0*REGBYTES(sp)
  43. STORE x4, 1*REGBYTES(sp)
  44. STORE x5, 2*REGBYTES(sp)
  45. STORE x6, 3*REGBYTES(sp)
  46. STORE x7, 4*REGBYTES(sp)
  47. STORE x10, 5*REGBYTES(sp)
  48. STORE x11, 6*REGBYTES(sp)
  49. STORE x12, 7*REGBYTES(sp)
  50. STORE x13, 8*REGBYTES(sp)
  51. STORE x14, 9*REGBYTES(sp)
  52. STORE x15, 10*REGBYTES(sp)
  53. #ifndef __riscv_32e
  54. STORE x16, 14*REGBYTES(sp)
  55. STORE x17, 15*REGBYTES(sp)
  56. STORE x28, 16*REGBYTES(sp)
  57. STORE x29, 17*REGBYTES(sp)
  58. STORE x30, 18*REGBYTES(sp)
  59. STORE x31, 19*REGBYTES(sp)
  60. #endif /* __riscv_32e */
  61. #endif
  62. .endm
  63. /**
  64. * \brief Macro for restore caller registers
  65. * \details
  66. * This macro restore ABI defined caller saved registers from stack.
  67. * \remarks
  68. * - You could use this macro to restore context before you want return
  69. * from interrupt or exeception
  70. */
  71. /* Restore caller registers */
  72. .macro RESTORE_CONTEXT
  73. #if defined(ECLIC_HW_CTX_AUTO) && defined(CFG_HAS_ECLICV2)
  74. #else
  75. LOAD x1, 0*REGBYTES(sp)
  76. LOAD x4, 1*REGBYTES(sp)
  77. LOAD x5, 2*REGBYTES(sp)
  78. LOAD x6, 3*REGBYTES(sp)
  79. LOAD x7, 4*REGBYTES(sp)
  80. LOAD x10, 5*REGBYTES(sp)
  81. LOAD x11, 6*REGBYTES(sp)
  82. LOAD x12, 7*REGBYTES(sp)
  83. LOAD x13, 8*REGBYTES(sp)
  84. LOAD x14, 9*REGBYTES(sp)
  85. LOAD x15, 10*REGBYTES(sp)
  86. #ifndef __riscv_32e
  87. LOAD x16, 14*REGBYTES(sp)
  88. LOAD x17, 15*REGBYTES(sp)
  89. LOAD x28, 16*REGBYTES(sp)
  90. LOAD x29, 17*REGBYTES(sp)
  91. LOAD x30, 18*REGBYTES(sp)
  92. LOAD x31, 19*REGBYTES(sp)
  93. /* De-allocate the stack space */
  94. addi sp, sp, 20*REGBYTES
  95. #else
  96. /* De-allocate the stack space */
  97. addi sp, sp, 14*REGBYTES
  98. #endif /* __riscv_32e */
  99. csrrw sp, CSR_XSCRATCHCSWL, sp
  100. #endif
  101. .endm
  102. /**
  103. * \brief Macro for save necessary CSRs to stack
  104. * \details
  105. * This macro store MCAUSE, MEPC, MSUBM to stack.
  106. */
  107. .macro SAVE_CSR_CONTEXT
  108. #if defined(ECLIC_HW_CTX_AUTO) && defined(CFG_HAS_ECLICV2)
  109. #else
  110. /* Store CSR mcause to stack using pushmcause */
  111. csrrwi x0, CSR_PUSHXCAUSE, 11
  112. /* Store CSR mepc to stack using pushmepc */
  113. csrrwi x0, CSR_PUSHXEPC, 12
  114. #if (!defined(SMODE_RTOS)) || defined(CFG_HAS_ECLICV2)
  115. /* Store CSR msub to stack using pushmsub */
  116. csrrwi x0, CSR_PUSHXSUBM, 13
  117. #endif
  118. #endif
  119. .endm
  120. /**
  121. * \brief Macro for restore necessary CSRs from stack
  122. * \details
  123. * This macro restore MSUBM, MEPC, MCAUSE from stack.
  124. */
  125. .macro RESTORE_CSR_CONTEXT
  126. #if defined(ECLIC_HW_CTX_AUTO) && defined(CFG_HAS_ECLICV2)
  127. #else
  128. #if (!defined(SMODE_RTOS)) || defined(CFG_HAS_ECLICV2)
  129. LOAD x5, 13*REGBYTES(sp)
  130. csrw CSR_XSUBM, x5
  131. #endif
  132. LOAD x5, 12*REGBYTES(sp)
  133. csrw CSR_XEPC, x5
  134. LOAD x5, 11*REGBYTES(sp)
  135. csrw CSR_XCAUSE, x5
  136. #endif
  137. .endm
  138. /**
  139. * \brief Exception/NMI Entry
  140. * \details
  141. * This function provide common entry functions for exception/nmi.
  142. * \remarks
  143. * This function provide a default exception/nmi entry.
  144. * ABI defined caller save register and some CSR registers
  145. * to be saved before enter interrupt handler and be restored before return.
  146. */
  147. .section .text.trap
  148. /* In CLIC mode, the exeception entry must be 64bytes aligned */
  149. .align 6
  150. .global x_exc_entry
  151. .type x_exc_entry, @function
  152. x_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. XRET
  179. #endif
  180. .size x_exc_entry, . - x_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. .global x_irq_entry
  195. .type x_irq_entry, @function
  196. /* This label will be set to MTVT2 register */
  197. x_irq_entry:
  198. /* Save the caller saving registers (context) */
  199. SAVE_CONTEXT
  200. /* Save the necessary CSR registers */
  201. SAVE_CSR_CONTEXT
  202. /* This special CSR read/write operation, which is actually
  203. * claim the CLIC to find its pending highest ID, if the ID
  204. * is not 0, then automatically enable the mstatus.MIE, and
  205. * jump to its vector-entry-label, and update the link register
  206. */
  207. csrrw ra, CSR_JALXNXTI, ra
  208. /* Critical section with interrupts disabled */
  209. DISABLE_XIE
  210. /* Restore the necessary CSR registers */
  211. RESTORE_CSR_CONTEXT
  212. /* Restore the caller saving registers (context) */
  213. RESTORE_CONTEXT
  214. /* Return to regular code */
  215. #if defined(ECLIC_HW_CTX_AUTO) && defined(CFG_HAS_ECLICV2)
  216. csrrwi x0, CSR_POPXRET, 0
  217. #else
  218. XRET
  219. #endif
  220. .size x_irq_entry, . - x_irq_entry
  221. /* Default Handler for Exceptions / Interrupts */
  222. .global default_intexc_handler
  223. .type default_intexc_handler, @function
  224. Undef_Handler:
  225. default_intexc_handler:
  226. 1:
  227. j 1b
  228. .size default_intexc_handler, . - default_intexc_handler