ISR_Support.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 132
  30. #define portEPC_STACK_LOCATION 124
  31. #define portSTATUS_STACK_LOCATION 128
  32. #ifdef __LANGUAGE_ASSEMBLY__
  33. /******************************************************************/
  34. .macro portSAVE_CONTEXT
  35. /* Make room for the context. First save the current status so it can be
  36. manipulated, and the cause and EPC registers so their original values are
  37. captured. */
  38. mfc0 k0, _CP0_CAUSE
  39. addiu sp, sp, -portCONTEXT_SIZE
  40. mfc0 k1, _CP0_STATUS
  41. /* Also save s6 and s5 so they can be used. Any nesting interrupts should
  42. maintain the values of these registers across the ISR. */
  43. sw s6, 44(sp)
  44. sw s5, 40(sp)
  45. sw k1, portSTATUS_STACK_LOCATION(sp)
  46. /* Prepare to enable interrupts above the current priority.
  47. k0 = k0 >> 10. Moves RIPL[17:10] to [7:0] */
  48. srl k0, k0, 0xa
  49. /* Insert bit field. 7 bits k0[6:0] to k1[16:10] */
  50. ins k1, k0, 10, 7
  51. /* Sets CP0.Status.IPL = CP0.Cause.RIPL
  52. Copy the MSB of the IPL, but it would be an error if it was set anyway. */
  53. srl k0, k0, 0x7
  54. /* MSB of IPL is bit[18] of CP0.Status */
  55. ins k1, k0, 18, 1
  56. /* CP0.Status[5:1] = 0 b[5]=Rsvd, b[4]=UM,
  57. b[3]=Rsvd, b[2]=ERL, b[1]=EXL
  58. Setting EXL=0 allows higher priority interrupts
  59. to preempt this handler */
  60. ins k1, zero, 1, 4
  61. /* s5 is used as the frame pointer. */
  62. add s5, zero, sp
  63. /* Check the nesting count value. */
  64. la k0, uxInterruptNesting
  65. lw s6, (k0)
  66. /* If the nesting count is 0 then swap to the the system stack, otherwise
  67. the system stack is already being used. */
  68. bne s6, zero, 1f
  69. nop
  70. /* Swap to the system stack. */
  71. la sp, xISRStackTop
  72. lw sp, (sp)
  73. /* Increment and save the nesting count. */
  74. 1: addiu s6, s6, 1
  75. sw s6, 0(k0)
  76. /* s6 holds the EPC value, this is saved after interrupts are re-enabled. */
  77. mfc0 s6, _CP0_EPC
  78. /* Re-enable interrupts. */
  79. mtc0 k1, _CP0_STATUS
  80. /* Save the context into the space just created. s6 is saved again
  81. here as it now contains the EPC value. No other s registers need be
  82. saved. */
  83. sw ra, 120(s5) /* Return address (RA=R31) */
  84. sw s8, 116(s5) /* Frame Pointer (FP=R30) */
  85. sw t9, 112(s5)
  86. sw t8, 108(s5)
  87. sw t7, 104(s5)
  88. sw t6, 100(s5)
  89. sw t5, 96(s5)
  90. sw t4, 92(s5)
  91. sw t3, 88(s5)
  92. sw t2, 84(s5)
  93. sw t1, 80(s5)
  94. sw t0, 76(s5)
  95. sw a3, 72(s5)
  96. sw a2, 68(s5)
  97. sw a1, 64(s5)
  98. sw a0, 60(s5)
  99. sw v1, 56(s5)
  100. sw v0, 52(s5)
  101. sw s6, portEPC_STACK_LOCATION(s5)
  102. sw $1, 16(s5)
  103. /* MEC14xx does not have DSP, removed 7 words */
  104. mfhi s6
  105. sw s6, 12(s5)
  106. mflo s6
  107. sw s6, 8(s5)
  108. /* Update the task stack pointer value if nesting is zero. */
  109. la s6, uxInterruptNesting
  110. lw s6, (s6)
  111. addiu s6, s6, -1
  112. bne s6, zero, 1f
  113. nop
  114. /* Save the stack pointer. */
  115. la s6, uxSavedTaskStackPointer
  116. sw s5, (s6)
  117. 1:
  118. .endm
  119. /******************************************************************/
  120. .macro portRESTORE_CONTEXT
  121. /* Restore the stack pointer from the TCB. This is only done if the
  122. nesting count is 1. */
  123. la s6, uxInterruptNesting
  124. lw s6, (s6)
  125. addiu s6, s6, -1
  126. bne s6, zero, 1f
  127. nop
  128. la s6, uxSavedTaskStackPointer
  129. lw s5, (s6)
  130. /* Restore the context.
  131. MCHP MEC14xx does not include DSP */
  132. 1:
  133. lw s6, 8(s5)
  134. mtlo s6
  135. lw s6, 12(s5)
  136. mthi s6
  137. lw $1, 16(s5)
  138. /* s6 is loaded as it was used as a scratch register and therefore saved
  139. as part of the interrupt context. */
  140. lw s6, 44(s5)
  141. lw v0, 52(s5)
  142. lw v1, 56(s5)
  143. lw a0, 60(s5)
  144. lw a1, 64(s5)
  145. lw a2, 68(s5)
  146. lw a3, 72(s5)
  147. lw t0, 76(s5)
  148. lw t1, 80(s5)
  149. lw t2, 84(s5)
  150. lw t3, 88(s5)
  151. lw t4, 92(s5)
  152. lw t5, 96(s5)
  153. lw t6, 100(s5)
  154. lw t7, 104(s5)
  155. lw t8, 108(s5)
  156. lw t9, 112(s5)
  157. lw s8, 116(s5)
  158. lw ra, 120(s5)
  159. /* Protect access to the k registers, and others. */
  160. di
  161. ehb
  162. /* Decrement the nesting count. */
  163. la k0, uxInterruptNesting
  164. lw k1, (k0)
  165. addiu k1, k1, -1
  166. sw k1, 0(k0)
  167. lw k0, portSTATUS_STACK_LOCATION(s5)
  168. lw k1, portEPC_STACK_LOCATION(s5)
  169. /* Leave the stack in its original state. First load sp from s5, then
  170. restore s5 from the stack. */
  171. add sp, zero, s5
  172. lw s5, 40(sp)
  173. addiu sp, sp, portCONTEXT_SIZE
  174. mtc0 k0, _CP0_STATUS
  175. mtc0 k1, _CP0_EPC
  176. ehb
  177. eret
  178. nop
  179. .endm
  180. #endif /* #ifdef __LANGUAGE_ASSEMBLY__ */