vectors.S 8.7 KB

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