ISR_Support.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. * FreeRTOS Kernel V10.4.6
  3. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * SPDX-License-Identifier: MIT
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  8. * this software and associated documentation files (the "Software"), to deal in
  9. * the Software without restriction, including without limitation the rights to
  10. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  11. * the Software, and to permit persons to whom the Software is furnished to do so,
  12. * subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  19. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  20. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  21. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * https://www.FreeRTOS.org
  25. * https://github.com/FreeRTOS
  26. *
  27. */
  28. #include "FreeRTOSConfig.h"
  29. #define portCONTEXT_SIZE 160
  30. #define portEPC_STACK_LOCATION 152
  31. #define portSTATUS_STACK_LOCATION 156
  32. #define portFPCSR_STACK_LOCATION 0
  33. #define portTASK_HAS_FPU_STACK_LOCATION 0
  34. #define portFPU_CONTEXT_SIZE 264
  35. /******************************************************************/
  36. .macro portSAVE_FPU_REGS offset, base
  37. /* Macro to assist with saving just the FPU registers to the
  38. * specified address and base offset,
  39. * offset is a constant, base is the base pointer register */
  40. sdc1 $f31, \offset + 248(\base)
  41. sdc1 $f30, \offset + 240(\base)
  42. sdc1 $f29, \offset + 232(\base)
  43. sdc1 $f28, \offset + 224(\base)
  44. sdc1 $f27, \offset + 216(\base)
  45. sdc1 $f26, \offset + 208(\base)
  46. sdc1 $f25, \offset + 200(\base)
  47. sdc1 $f24, \offset + 192(\base)
  48. sdc1 $f23, \offset + 184(\base)
  49. sdc1 $f22, \offset + 176(\base)
  50. sdc1 $f21, \offset + 168(\base)
  51. sdc1 $f20, \offset + 160(\base)
  52. sdc1 $f19, \offset + 152(\base)
  53. sdc1 $f18, \offset + 144(\base)
  54. sdc1 $f17, \offset + 136(\base)
  55. sdc1 $f16, \offset + 128(\base)
  56. sdc1 $f15, \offset + 120(\base)
  57. sdc1 $f14, \offset + 112(\base)
  58. sdc1 $f13, \offset + 104(\base)
  59. sdc1 $f12, \offset + 96(\base)
  60. sdc1 $f11, \offset + 88(\base)
  61. sdc1 $f10, \offset + 80(\base)
  62. sdc1 $f9, \offset + 72(\base)
  63. sdc1 $f8, \offset + 64(\base)
  64. sdc1 $f7, \offset + 56(\base)
  65. sdc1 $f6, \offset + 48(\base)
  66. sdc1 $f5, \offset + 40(\base)
  67. sdc1 $f4, \offset + 32(\base)
  68. sdc1 $f3, \offset + 24(\base)
  69. sdc1 $f2, \offset + 16(\base)
  70. sdc1 $f1, \offset + 8(\base)
  71. sdc1 $f0, \offset + 0(\base)
  72. .endm
  73. /******************************************************************/
  74. .macro portLOAD_FPU_REGS offset, base
  75. /* Macro to assist with loading just the FPU registers from the
  76. * specified address and base offset, offset is a constant,
  77. * base is the base pointer register */
  78. ldc1 $f0, \offset + 0(\base)
  79. ldc1 $f1, \offset + 8(\base)
  80. ldc1 $f2, \offset + 16(\base)
  81. ldc1 $f3, \offset + 24(\base)
  82. ldc1 $f4, \offset + 32(\base)
  83. ldc1 $f5, \offset + 40(\base)
  84. ldc1 $f6, \offset + 48(\base)
  85. ldc1 $f7, \offset + 56(\base)
  86. ldc1 $f8, \offset + 64(\base)
  87. ldc1 $f9, \offset + 72(\base)
  88. ldc1 $f10, \offset + 80(\base)
  89. ldc1 $f11, \offset + 88(\base)
  90. ldc1 $f12, \offset + 96(\base)
  91. ldc1 $f13, \offset + 104(\base)
  92. ldc1 $f14, \offset + 112(\base)
  93. ldc1 $f15, \offset + 120(\base)
  94. ldc1 $f16, \offset + 128(\base)
  95. ldc1 $f17, \offset + 136(\base)
  96. ldc1 $f18, \offset + 144(\base)
  97. ldc1 $f19, \offset + 152(\base)
  98. ldc1 $f20, \offset + 160(\base)
  99. ldc1 $f21, \offset + 168(\base)
  100. ldc1 $f22, \offset + 176(\base)
  101. ldc1 $f23, \offset + 184(\base)
  102. ldc1 $f24, \offset + 192(\base)
  103. ldc1 $f25, \offset + 200(\base)
  104. ldc1 $f26, \offset + 208(\base)
  105. ldc1 $f27, \offset + 216(\base)
  106. ldc1 $f28, \offset + 224(\base)
  107. ldc1 $f29, \offset + 232(\base)
  108. ldc1 $f30, \offset + 240(\base)
  109. ldc1 $f31, \offset + 248(\base)
  110. .endm
  111. /******************************************************************/
  112. .macro portSAVE_CONTEXT
  113. /* Make room for the context. First save the current status so it can be
  114. manipulated, and the cause and EPC registers so their original values are
  115. captured. */
  116. mfc0 k0, _CP0_CAUSE
  117. addiu sp, sp, -portCONTEXT_SIZE
  118. #if ( __mips_hard_float == 1 ) && ( configUSE_TASK_FPU_SUPPORT == 1 )
  119. /* Test if we are already using the system stack. Only tasks may use the
  120. FPU so if we are already in a nested interrupt then the FPU context does
  121. not require saving. */
  122. la k1, uxInterruptNesting
  123. lw k1, 0(k1)
  124. bne k1, zero, 2f
  125. nop
  126. /* Test if the current task needs the FPU context saving. */
  127. la k1, ulTaskHasFPUContext
  128. lw k1, 0(k1)
  129. beq k1, zero, 1f
  130. nop
  131. /* Adjust the stack to account for the additional FPU context.*/
  132. addiu sp, sp, -portFPU_CONTEXT_SIZE
  133. 1:
  134. /* Save the ulTaskHasFPUContext flag. */
  135. sw k1, portTASK_HAS_FPU_STACK_LOCATION(sp)
  136. 2:
  137. #endif
  138. mfc0 k1, _CP0_STATUS
  139. /* Also save s7, s6 and s5 so they can be used. Any nesting interrupts
  140. should maintain the values of these registers across the ISR. */
  141. sw s7, 48(sp)
  142. sw s6, 44(sp)
  143. sw s5, 40(sp)
  144. sw k1, portSTATUS_STACK_LOCATION(sp)
  145. /* Prepare to enable interrupts above the current priority. */
  146. srl k0, k0, 0xa
  147. ins k1, k0, 10, 7
  148. srl k0, k0, 0x7 /* This copies the MSB of the IPL, but it would be an error if it was set anyway. */
  149. ins k1, k0, 18, 1
  150. ins k1, zero, 1, 4
  151. /* s5 is used as the frame pointer. */
  152. add s5, zero, sp
  153. /* Check the nesting count value. */
  154. la k0, uxInterruptNesting
  155. lw s6, (k0)
  156. /* If the nesting count is 0 then swap to the the system stack, otherwise
  157. the system stack is already being used. */
  158. bne s6, zero, 1f
  159. nop
  160. /* Swap to the system stack. */
  161. la sp, xISRStackTop
  162. lw sp, (sp)
  163. /* Increment and save the nesting count. */
  164. 1: addiu s6, s6, 1
  165. sw s6, 0(k0)
  166. /* s6 holds the EPC value, this is saved after interrupts are re-enabled. */
  167. mfc0 s6, _CP0_EPC
  168. /* Re-enable interrupts. */
  169. mtc0 k1, _CP0_STATUS
  170. /* Save the context into the space just created. s6 is saved again
  171. here as it now contains the EPC value. No other s registers need be
  172. saved. */
  173. sw ra, 120(s5)
  174. sw s8, 116(s5)
  175. sw t9, 112(s5)
  176. sw t8, 108(s5)
  177. sw t7, 104(s5)
  178. sw t6, 100(s5)
  179. sw t5, 96(s5)
  180. sw t4, 92(s5)
  181. sw t3, 88(s5)
  182. sw t2, 84(s5)
  183. sw t1, 80(s5)
  184. sw t0, 76(s5)
  185. sw a3, 72(s5)
  186. sw a2, 68(s5)
  187. sw a1, 64(s5)
  188. sw a0, 60(s5)
  189. sw v1, 56(s5)
  190. sw v0, 52(s5)
  191. sw s6, portEPC_STACK_LOCATION(s5)
  192. sw $1, 16(s5)
  193. /* Save the AC0, AC1, AC2, AC3 registers from the DSP. s6 is used as a
  194. scratch register. */
  195. mfhi s6, $ac1
  196. sw s6, 128(s5)
  197. mflo s6, $ac1
  198. sw s6, 124(s5)
  199. mfhi s6, $ac2
  200. sw s6, 136(s5)
  201. mflo s6, $ac2
  202. sw s6, 132(s5)
  203. mfhi s6, $ac3
  204. sw s6, 144(s5)
  205. mflo s6, $ac3
  206. sw s6, 140(s5)
  207. /* Save the DSP Control register */
  208. rddsp s6
  209. sw s6, 148(s5)
  210. /* ac0 is done separately to match the MX port. */
  211. mfhi s6, $ac0
  212. sw s6, 12(s5)
  213. mflo s6, $ac0
  214. sw s6, 8(s5)
  215. /* Save the FPU context if the nesting count was zero. */
  216. #if ( __mips_hard_float == 1 ) && ( configUSE_TASK_FPU_SUPPORT == 1 )
  217. la s6, uxInterruptNesting
  218. lw s6, 0(s6)
  219. addiu s6, s6, -1
  220. bne s6, zero, 1f
  221. nop
  222. /* Test if the current task needs the FPU context saving. */
  223. lw s6, portTASK_HAS_FPU_STACK_LOCATION(s5)
  224. beq s6, zero, 1f
  225. nop
  226. /* Save the FPU registers. */
  227. portSAVE_FPU_REGS ( portCONTEXT_SIZE + 8 ), s5
  228. /* Save the FPU status register */
  229. cfc1 s6, $f31
  230. sw s6, (portCONTEXT_SIZE + portFPCSR_STACK_LOCATION)(s5)
  231. 1:
  232. #endif
  233. /* Update the task stack pointer value if nesting is zero. */
  234. la s6, uxInterruptNesting
  235. lw s6, (s6)
  236. addiu s6, s6, -1
  237. bne s6, zero, 1f
  238. nop
  239. /* Save the stack pointer. */
  240. la s6, uxSavedTaskStackPointer
  241. sw s5, (s6)
  242. 1:
  243. .endm
  244. /******************************************************************/
  245. .macro portRESTORE_CONTEXT
  246. /* Restore the stack pointer from the TCB. This is only done if the
  247. nesting count is 1. */
  248. la s6, uxInterruptNesting
  249. lw s6, (s6)
  250. addiu s6, s6, -1
  251. bne s6, zero, 1f
  252. nop
  253. la s6, uxSavedTaskStackPointer
  254. lw s5, (s6)
  255. #if ( __mips_hard_float == 1 ) && ( configUSE_TASK_FPU_SUPPORT == 1 )
  256. /* Restore the FPU context if required. */
  257. lw s6, portTASK_HAS_FPU_STACK_LOCATION(s5)
  258. beq s6, zero, 1f
  259. nop
  260. /* Restore the FPU registers. */
  261. portLOAD_FPU_REGS ( portCONTEXT_SIZE + 8 ), s5
  262. /* Restore the FPU status register. */
  263. lw s6, ( portCONTEXT_SIZE + portFPCSR_STACK_LOCATION )(s5)
  264. ctc1 s6, $f31
  265. #endif
  266. 1:
  267. /* Restore the context. */
  268. lw s6, 128(s5)
  269. mthi s6, $ac1
  270. lw s6, 124(s5)
  271. mtlo s6, $ac1
  272. lw s6, 136(s5)
  273. mthi s6, $ac2
  274. lw s6, 132(s5)
  275. mtlo s6, $ac2
  276. lw s6, 144(s5)
  277. mthi s6, $ac3
  278. lw s6, 140(s5)
  279. mtlo s6, $ac3
  280. /* Restore DSPControl. */
  281. lw s6, 148(s5)
  282. wrdsp s6
  283. lw s6, 8(s5)
  284. mtlo s6, $ac0
  285. lw s6, 12(s5)
  286. mthi s6, $ac0
  287. lw $1, 16(s5)
  288. /* s6 is loaded as it was used as a scratch register and therefore saved
  289. as part of the interrupt context. */
  290. lw s7, 48(s5)
  291. lw s6, 44(s5)
  292. lw v0, 52(s5)
  293. lw v1, 56(s5)
  294. lw a0, 60(s5)
  295. lw a1, 64(s5)
  296. lw a2, 68(s5)
  297. lw a3, 72(s5)
  298. lw t0, 76(s5)
  299. lw t1, 80(s5)
  300. lw t2, 84(s5)
  301. lw t3, 88(s5)
  302. lw t4, 92(s5)
  303. lw t5, 96(s5)
  304. lw t6, 100(s5)
  305. lw t7, 104(s5)
  306. lw t8, 108(s5)
  307. lw t9, 112(s5)
  308. lw s8, 116(s5)
  309. lw ra, 120(s5)
  310. /* Protect access to the k registers, and others. */
  311. di
  312. ehb
  313. /* Decrement the nesting count. */
  314. la k0, uxInterruptNesting
  315. lw k1, (k0)
  316. addiu k1, k1, -1
  317. sw k1, 0(k0)
  318. #if ( __mips_hard_float == 1 ) && ( configUSE_TASK_FPU_SUPPORT == 1 )
  319. /* If the nesting count is now zero then the FPU context may be restored. */
  320. bne k1, zero, 1f
  321. nop
  322. /* Restore the value of ulTaskHasFPUContext */
  323. la k0, ulTaskHasFPUContext
  324. lw k1, 0(s5)
  325. sw k1, 0(k0)
  326. /* If the task does not have an FPU context then adjust the stack normally. */
  327. beq k1, zero, 1f
  328. nop
  329. /* Restore the STATUS and EPC registers */
  330. lw k0, portSTATUS_STACK_LOCATION(s5)
  331. lw k1, portEPC_STACK_LOCATION(s5)
  332. /* Leave the stack in its original state. First load sp from s5, then
  333. restore s5 from the stack. */
  334. add sp, zero, s5
  335. lw s5, 40(sp)
  336. /* Adjust the stack pointer to remove the FPU context */
  337. addiu sp, sp, portFPU_CONTEXT_SIZE
  338. beq zero, zero, 2f
  339. nop
  340. 1: /* Restore the STATUS and EPC registers */
  341. lw k0, portSTATUS_STACK_LOCATION(s5)
  342. lw k1, portEPC_STACK_LOCATION(s5)
  343. /* Leave the stack in its original state. First load sp from s5, then
  344. restore s5 from the stack. */
  345. add sp, zero, s5
  346. lw s5, 40(sp)
  347. 2: /* Adjust the stack pointer */
  348. addiu sp, sp, portCONTEXT_SIZE
  349. #else
  350. /* Restore the frame when there is no hardware FP support. */
  351. lw k0, portSTATUS_STACK_LOCATION(s5)
  352. lw k1, portEPC_STACK_LOCATION(s5)
  353. /* Leave the stack in its original state. First load sp from s5, then
  354. restore s5 from the stack. */
  355. add sp, zero, s5
  356. lw s5, 40(sp)
  357. addiu sp, sp, portCONTEXT_SIZE
  358. #endif // ( __mips_hard_float == 1 ) && ( configUSE_TASK_FPU_SUPPORT == 1 )
  359. mtc0 k0, _CP0_STATUS
  360. mtc0 k1, _CP0_EPC
  361. ehb
  362. eret
  363. nop
  364. .endm