interrupt.S 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #include "riscv_encoding.h"
  2. DISABLE_MIE MACRO
  3. csrci CSR_MSTATUS, MSTATUS_MIE
  4. ENDM
  5. SAVE_CONTEXT MACRO
  6. #if defined(ECLIC_HW_CTX_AUTO) && defined(CFG_HAS_ECLICV2)
  7. #else
  8. csrrw sp, CSR_MSCRATCHCSWL, sp
  9. /* Allocate stack space for context saving */
  10. #ifndef __riscv_32e
  11. addi sp, sp, -20*REGBYTES
  12. #else
  13. addi sp, sp, -14*REGBYTES
  14. #endif /* __riscv_32e */
  15. STORE x1, 0*REGBYTES(sp)
  16. STORE x4, 1*REGBYTES(sp)
  17. STORE x5, 2*REGBYTES(sp)
  18. STORE x6, 3*REGBYTES(sp)
  19. STORE x7, 4*REGBYTES(sp)
  20. STORE x10, 5*REGBYTES(sp)
  21. STORE x11, 6*REGBYTES(sp)
  22. STORE x12, 7*REGBYTES(sp)
  23. STORE x13, 8*REGBYTES(sp)
  24. STORE x14, 9*REGBYTES(sp)
  25. STORE x15, 10*REGBYTES(sp)
  26. #ifndef __riscv_32e
  27. STORE x16, 14*REGBYTES(sp)
  28. STORE x17, 15*REGBYTES(sp)
  29. STORE x28, 16*REGBYTES(sp)
  30. STORE x29, 17*REGBYTES(sp)
  31. STORE x30, 18*REGBYTES(sp)
  32. STORE x31, 19*REGBYTES(sp)
  33. #endif /* __riscv_32e */
  34. #endif
  35. ENDM
  36. RESTORE_CONTEXT MACRO
  37. #if defined(ECLIC_HW_CTX_AUTO) && defined(CFG_HAS_ECLICV2)
  38. #else
  39. LOAD x1, 0*REGBYTES(sp)
  40. LOAD x4, 1*REGBYTES(sp)
  41. LOAD x5, 2*REGBYTES(sp)
  42. LOAD x6, 3*REGBYTES(sp)
  43. LOAD x7, 4*REGBYTES(sp)
  44. LOAD x10, 5*REGBYTES(sp)
  45. LOAD x11, 6*REGBYTES(sp)
  46. LOAD x12, 7*REGBYTES(sp)
  47. LOAD x13, 8*REGBYTES(sp)
  48. LOAD x14, 9*REGBYTES(sp)
  49. LOAD x15, 10*REGBYTES(sp)
  50. #ifndef __riscv_32e
  51. LOAD x16, 14*REGBYTES(sp)
  52. LOAD x17, 15*REGBYTES(sp)
  53. LOAD x28, 16*REGBYTES(sp)
  54. LOAD x29, 17*REGBYTES(sp)
  55. LOAD x30, 18*REGBYTES(sp)
  56. LOAD x31, 19*REGBYTES(sp)
  57. /* De-allocate the stack space */
  58. addi sp, sp, 20*REGBYTES
  59. #else
  60. /* De-allocate the stack space */
  61. addi sp, sp, 14*REGBYTES
  62. #endif /* __riscv_32e */
  63. csrrw sp, CSR_MSCRATCHCSWL, sp
  64. #endif
  65. ENDM
  66. SAVE_CSR_CONTEXT MACRO
  67. #if defined(ECLIC_HW_CTX_AUTO) && defined(CFG_HAS_ECLICV2)
  68. #else
  69. /* Store CSR mcause to stack using pushmcause */
  70. csrrwi x0, CSR_PUSHMCAUSE, 11
  71. /* Store CSR mepc to stack using pushmepc */
  72. csrrwi x0, CSR_PUSHMEPC, 12
  73. /* Store CSR msub to stack using pushmsub */
  74. csrrwi x0, CSR_PUSHMSUBM, 13
  75. #endif
  76. ENDM
  77. RESTORE_CSR_CONTEXT MACRO
  78. #if defined(ECLIC_HW_CTX_AUTO) && defined(CFG_HAS_ECLICV2)
  79. #else
  80. LOAD x5, 13*REGBYTES(sp)
  81. csrw CSR_MSUBM, x5
  82. LOAD x5, 12*REGBYTES(sp)
  83. csrw CSR_MEPC, x5
  84. LOAD x5, 11*REGBYTES(sp)
  85. csrw CSR_MCAUSE, x5
  86. #endif
  87. ENDM
  88. PUBLIC exc_entry, irq_entry, default_intexc_handler
  89. PUBLIC Undef_Handler
  90. EXTERN core_exception_handler
  91. SECTION `.text`:CODE:NOROOT(2)
  92. CODE
  93. /**
  94. * \brief Exception/NMI Entry
  95. * \details
  96. * This function provide common entry functions for exception/nmi.
  97. * \remarks
  98. * This function provide a default exception/nmi entry.
  99. * ABI defined caller save register and some CSR registers
  100. * to be saved before enter interrupt handler and be restored before return.
  101. */
  102. /* In CLIC mode, the exeception entry must be 64bytes aligned */
  103. ALIGN 6
  104. exc_entry:
  105. /* Save the caller saving registers (context) */
  106. SAVE_CONTEXT
  107. /* Save the necessary CSR registers */
  108. SAVE_CSR_CONTEXT
  109. /*
  110. * Set the exception handler function arguments
  111. * argument 1: mcause value
  112. * argument 2: current stack point(SP) value
  113. */
  114. csrr a0, mcause
  115. mv a1, sp
  116. /*
  117. * TODO: Call the exception handler function
  118. * By default, the function template is provided in
  119. * system_Device.c, you can adjust it as you want
  120. */
  121. call core_exception_handler
  122. /* Restore the necessary CSR registers */
  123. RESTORE_CSR_CONTEXT
  124. /* Restore the caller saving registers (context) */
  125. RESTORE_CONTEXT
  126. /* Return to regular code */
  127. #if defined(ECLIC_HW_CTX_AUTO) && defined(CFG_HAS_ECLICV2)
  128. csrrwi x0, CSR_POPXRET, 0
  129. #else
  130. mret
  131. #endif
  132. /**
  133. * \brief Non-Vector Interrupt Entry
  134. * \details
  135. * This function provide common entry functions for handling
  136. * non-vector interrupts
  137. * \remarks
  138. * This function provide a default non-vector interrupt entry.
  139. * ABI defined caller save register and some CSR registers need
  140. * to be saved before enter interrupt handler and be restored before return.
  141. */
  142. /* In CLIC mode, the interrupt entry must be 4bytes aligned */
  143. ALIGN 2
  144. /* This label will be set to MTVT2 register */
  145. irq_entry:
  146. /* Save the caller saving registers (context) */
  147. SAVE_CONTEXT
  148. /* Save the necessary CSR registers */
  149. SAVE_CSR_CONTEXT
  150. /* This special CSR read/write operation, which is actually
  151. * claim the CLIC to find its pending highest ID, if the ID
  152. * is not 0, then automatically enable the mstatus.MIE, and
  153. * jump to its vector-entry-label, and update the link register
  154. */
  155. csrrw ra, CSR_JALMNXTI, ra
  156. /* Critical section with interrupts disabled */
  157. DISABLE_MIE
  158. /* Restore the necessary CSR registers */
  159. RESTORE_CSR_CONTEXT
  160. /* Restore the caller saving registers (context) */
  161. RESTORE_CONTEXT
  162. /* Return to regular code */
  163. #if defined(ECLIC_HW_CTX_AUTO) && defined(CFG_HAS_ECLICV2)
  164. csrrwi x0, CSR_POPXRET, 0
  165. #else
  166. mret
  167. #endif
  168. /* Default Handler for Exceptions / Interrupts */
  169. Undef_Handler:
  170. default_intexc_handler:
  171. j Undef_Handler
  172. END