vectors.S 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * Copyright (C) 2017-2019 Alibaba Group Holding Limited
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-08-20 zx.chen define default vector handlers.
  9. */
  10. #include <csi_config.h>
  11. /* Enable interrupts when returning from the handler */
  12. #define MSTATUS_PRV1 0x1880
  13. .section .bss
  14. .align 2
  15. .globl g_trapstackalloc
  16. .global g_trapstackbase
  17. .global g_top_trapstack
  18. g_trapstackalloc:
  19. g_trapstackbase:
  20. .space 768
  21. g_top_trapstack:
  22. .align 2
  23. .globl g_trap_sp
  24. .type g_trap_sp, object
  25. g_trap_sp:
  26. .long 0
  27. .size g_trap_sp, .-g_trap_sp
  28. irq_nested_level:
  29. .long 0
  30. #ifdef ARCH_RISCV_FPU
  31. irq_mstatus_fs_flag:
  32. .long 0
  33. #endif
  34. .text
  35. .align 2
  36. .global Default_IRQHandler
  37. .weak Default_IRQHandler
  38. .type Default_IRQHandler, %function
  39. Default_IRQHandler:
  40. ipush
  41. #ifdef ARCH_RISCV_FPU
  42. csrr t1, mstatus
  43. srli t1, t1, 13
  44. andi t1, t1, 0x3
  45. la t3, irq_mstatus_fs_flag
  46. sw t1, (t3)
  47. li t0, 0x3
  48. bne t1, t0, .F_RegNotSave1
  49. addi sp, sp, -(20 * FREGBYTES)
  50. FSTORE ft0, 0 * FREGBYTES(sp)
  51. FSTORE ft1, 1 * FREGBYTES(sp)
  52. FSTORE ft2, 2 * FREGBYTES(sp)
  53. FSTORE ft3, 3 * FREGBYTES(sp)
  54. FSTORE ft4, 4 * FREGBYTES(sp)
  55. FSTORE ft5, 5 * FREGBYTES(sp)
  56. FSTORE ft6, 6 * FREGBYTES(sp)
  57. FSTORE ft7, 7 * FREGBYTES(sp)
  58. FSTORE fa0, 8 * FREGBYTES(sp)
  59. FSTORE fa1, 9 * FREGBYTES(sp)
  60. FSTORE fa2, 10 * FREGBYTES(sp)
  61. FSTORE fa3, 11 * FREGBYTES(sp)
  62. FSTORE fa4, 12 * FREGBYTES(sp)
  63. FSTORE fa5, 13 * FREGBYTES(sp)
  64. FSTORE fa6, 14 * FREGBYTES(sp)
  65. FSTORE fa7, 15 * FREGBYTES(sp)
  66. FSTORE ft8, 16 * FREGBYTES(sp)
  67. FSTORE ft9, 17 * FREGBYTES(sp)
  68. FSTORE ft10, 18 * FREGBYTES(sp)
  69. FSTORE ft11, 19 * FREGBYTES(sp)
  70. .F_RegNotSave1:
  71. #endif
  72. csrr t1, mcause
  73. andi t1, t1, 0x3FF
  74. slli t1, t1, 2
  75. la t0, g_irqvector
  76. add t0, t0, t1
  77. lw t2, (t0)
  78. jalr t2
  79. li t0, MSTATUS_PRV1
  80. csrs mstatus, t0
  81. #ifdef ARCH_RISCV_FPU
  82. la t0, irq_mstatus_fs_flag
  83. lw t1, (t0)
  84. li t0, 0x3
  85. bne t1, t0, .F_RegNotLoad
  86. FLOAD ft0, 0 * FREGBYTES(sp)
  87. FLOAD ft1, 1 * FREGBYTES(sp)
  88. FLOAD ft2, 2 * FREGBYTES(sp)
  89. FLOAD ft3, 3 * FREGBYTES(sp)
  90. FLOAD ft4, 4 * FREGBYTES(sp)
  91. FLOAD ft5, 5 * FREGBYTES(sp)
  92. FLOAD ft6, 6 * FREGBYTES(sp)
  93. FLOAD ft7, 7 * FREGBYTES(sp)
  94. FLOAD fa0, 8 * FREGBYTES(sp)
  95. FLOAD fa1, 9 * FREGBYTES(sp)
  96. FLOAD fa2, 10 * FREGBYTES(sp)
  97. FLOAD fa3, 11 * FREGBYTES(sp)
  98. FLOAD fa4, 12 * FREGBYTES(sp)
  99. FLOAD fa5, 13 * FREGBYTES(sp)
  100. FLOAD fa6, 14 * FREGBYTES(sp)
  101. FLOAD fa7, 15 * FREGBYTES(sp)
  102. FLOAD ft8, 16 * FREGBYTES(sp)
  103. FLOAD ft9, 17 * FREGBYTES(sp)
  104. FLOAD ft10,18 * FREGBYTES(sp)
  105. FLOAD ft11,19 * FREGBYTES(sp)
  106. addi sp, sp, (20 * FREGBYTES)
  107. .F_RegNotLoad:
  108. #endif
  109. ipop
  110. /******************************************************************************
  111. * Functions:
  112. * void trap(void);
  113. * default exception handler
  114. ******************************************************************************/
  115. .align 2
  116. .global trap
  117. .type trap, %function
  118. trap:
  119. /* Check for interrupt */
  120. addi sp, sp, -4
  121. sw t0, 0x0(sp)
  122. csrr t0, mcause
  123. blt t0, x0, .Lirq
  124. addi sp, sp, 4
  125. la t0, g_trap_sp
  126. addi t0, t0, -132
  127. sw x1, 0(t0)
  128. sw x2, 4(t0)
  129. sw x3, 8(t0)
  130. sw x4, 12(t0)
  131. sw x6, 20(t0)
  132. sw x7, 24(t0)
  133. sw x8, 28(t0)
  134. sw x9, 32(t0)
  135. sw x10, 36(t0)
  136. sw x11, 40(t0)
  137. sw x12, 44(t0)
  138. sw x13, 48(t0)
  139. sw x14, 52(t0)
  140. sw x15, 56(t0)
  141. sw x16, 60(t0)
  142. sw x17, 64(t0)
  143. sw x18, 68(t0)
  144. sw x19, 72(t0)
  145. sw x20, 76(t0)
  146. sw x21, 80(t0)
  147. sw x22, 84(t0)
  148. sw x23, 88(t0)
  149. sw x24, 92(t0)
  150. sw x25, 96(t0)
  151. sw x26, 100(t0)
  152. sw x27, 104(t0)
  153. sw x28, 108(t0)
  154. sw x29, 112(t0)
  155. sw x30, 116(t0)
  156. sw x31, 120(t0)
  157. csrr a0, mepc
  158. sw a0, 124(t0)
  159. csrr a0, mstatus
  160. sw a0, 128(t0)
  161. mv a0, t0
  162. lw t0, -4(sp)
  163. mv sp, a0
  164. sw t0, 16(sp)
  165. jal trap_c
  166. .Lirq:
  167. lw t0, 0x0(sp)
  168. addi sp, sp, 4
  169. j Default_IRQHandler
  170. .align 6
  171. .weak Default_Handler
  172. .global Default_Handler
  173. .type Default_Handler, %function
  174. Default_Handler:
  175. /* Check for nmi */
  176. addi sp, sp, -8
  177. sw t0, 0x0(sp)
  178. sw t1, 0x4(sp)
  179. csrr t0, mcause
  180. andi t0, t0, 0x3FF
  181. li t1, 24
  182. beq t0, t1, .NMI_Handler
  183. lw t0, 0x0(sp)
  184. lw t1, 0x4(sp)
  185. addi sp, sp, 8
  186. j trap
  187. .NMI_Handler:
  188. lw t0, 0x0(sp)
  189. lw t1, 0x4(sp)
  190. addi sp, sp, 8
  191. addi sp, sp, -64
  192. sw ra, 0(sp)
  193. sw t0, 4(sp)
  194. sw t1, 8(sp)
  195. sw t2, 12(sp)
  196. sw a0, 16(sp)
  197. sw a1, 20(sp)
  198. sw a2, 24(sp)
  199. sw a3, 28(sp)
  200. sw a4, 32(sp)
  201. sw a5, 36(sp)
  202. sw a6, 40(sp)
  203. sw a7, 44(sp)
  204. sw t3, 48(sp)
  205. sw t4, 52(sp)
  206. sw t5, 56(sp)
  207. sw t6, 60(sp)
  208. #ifdef ARCH_RISCV_FPU
  209. addi sp, sp, -(20*FREGBYTES)
  210. FSTORE ft0, 0 * FREGBYTES(sp)
  211. FSTORE ft1, 1 * FREGBYTES(sp)
  212. FSTORE ft2, 2 * FREGBYTES(sp)
  213. FSTORE ft3, 3 * FREGBYTES(sp)
  214. FSTORE ft4, 4 * FREGBYTES(sp)
  215. FSTORE ft5, 5 * FREGBYTES(sp)
  216. FSTORE ft6, 6 * FREGBYTES(sp)
  217. FSTORE ft7, 7 * FREGBYTES(sp)
  218. FSTORE fa0, 8 * FREGBYTES(sp)
  219. FSTORE fa1, 9 * FREGBYTES(sp)
  220. FSTORE fa2, 10 * FREGBYTES(sp)
  221. FSTORE fa3, 11 * FREGBYTES(sp)
  222. FSTORE fa4, 12 * FREGBYTES(sp)
  223. FSTORE fa5, 13 * FREGBYTES(sp)
  224. FSTORE fa6, 14 * FREGBYTES(sp)
  225. FSTORE fa7, 15 * FREGBYTES(sp)
  226. FSTORE ft8, 16 * FREGBYTES(sp)
  227. FSTORE ft9, 17 * FREGBYTES(sp)
  228. FSTORE ft10, 18 * FREGBYTES(sp)
  229. FSTORE ft11, 19 * FREGBYTES(sp)
  230. #endif
  231. la t0, g_nmivector
  232. lw t0, (t0)
  233. jalr t0
  234. #ifdef ARCH_RISCV_FPU
  235. FLOAD ft0, 0 * FREGBYTES(sp)
  236. FLOAD ft1, 1 * FREGBYTES(sp)
  237. FLOAD ft2, 2 * FREGBYTES(sp)
  238. FLOAD ft3, 3 * FREGBYTES(sp)
  239. FLOAD ft4, 4 * FREGBYTES(sp)
  240. FLOAD ft5, 5 * FREGBYTES(sp)
  241. FLOAD ft6, 6 * FREGBYTES(sp)
  242. FLOAD ft7, 7 * FREGBYTES(sp)
  243. FLOAD fa0, 8 * FREGBYTES(sp)
  244. FLOAD fa1, 9 * FREGBYTES(sp)
  245. FLOAD fa2, 10 * FREGBYTES(sp)
  246. FLOAD fa3, 11 * FREGBYTES(sp)
  247. FLOAD fa4, 12 * FREGBYTES(sp)
  248. FLOAD fa5, 13 * FREGBYTES(sp)
  249. FLOAD fa6, 14 * FREGBYTES(sp)
  250. FLOAD fa7, 15 * FREGBYTES(sp)
  251. FLOAD ft8, 16 * FREGBYTES(sp)
  252. FLOAD ft9, 17 * FREGBYTES(sp)
  253. FLOAD ft10, 18 * FREGBYTES(sp)
  254. FLOAD ft11, 19 * FREGBYTES(sp)
  255. addi sp, sp, (20 * FREGBYTES)
  256. #endif
  257. lw ra, 0(sp)
  258. lw t0, 4(sp)
  259. lw t1, 8(sp)
  260. lw t2, 12(sp)
  261. lw a0, 16(sp)
  262. lw a1, 20(sp)
  263. lw a2, 24(sp)
  264. lw a3, 28(sp)
  265. lw a4, 32(sp)
  266. lw a5, 36(sp)
  267. lw a6, 40(sp)
  268. lw a7, 44(sp)
  269. lw t3, 48(sp)
  270. lw t4, 52(sp)
  271. lw t5, 56(sp)
  272. lw t6, 60(sp)
  273. addi sp, sp, 64
  274. mret
  275. .size Default_Handler, . - Default_Handler
  276. /* Macro to define default handlers. Default handler
  277. * will be weak symbol and just dead loops. They can be
  278. * overwritten by other handlers */
  279. .macro def_irq_handler handler_name
  280. .weak \handler_name
  281. .globl \handler_name
  282. .set \handler_name, Default_Handler
  283. .endm
  284. def_irq_handler PendSV_Handler
  285. def_irq_handler SysTick_Handler
  286. def_irq_handler STIM0_IRQHandler
  287. def_irq_handler STIM1_IRQHandler
  288. def_irq_handler STIM2_IRQHandler
  289. def_irq_handler STIM3_IRQHandler
  290. def_irq_handler TIM0_IRQHandler
  291. def_irq_handler TIM1_IRQHandler
  292. def_irq_handler TIM2_IRQHandler
  293. def_irq_handler TIM3_IRQHandler
  294. def_irq_handler USART_IRQHandler
  295. def_irq_handler GPIO0_IRQHandler
  296. def_irq_handler GPIO1_IRQHandler
  297. def_irq_handler GPIO2_IRQHandler
  298. def_irq_handler GPIO3_IRQHandler
  299. def_irq_handler GPIO4_IRQHandler
  300. def_irq_handler GPIO5_IRQHandler
  301. def_irq_handler GPIO6_IRQHandler
  302. def_irq_handler GPIO7_IRQHandler
  303. def_irq_handler PAD_IRQHandler
  304. def_irq_handler TIM6_IRQHandler
  305. def_irq_handler TIM7_IRQHandler
  306. def_irq_handler TIM8_IRQHandler
  307. def_irq_handler TIM9_IRQHandler
  308. def_irq_handler TIM10_IRQHandler
  309. def_irq_handler TIM11_IRQHandler