ISR_Support.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. /******************************************************************/
  33. .macro portSAVE_CONTEXT
  34. /* Make room for the context. First save the current status so it can be
  35. manipulated, and the cause and EPC registers so their original values are
  36. captured. */
  37. mfc0 k0, _CP0_CAUSE
  38. addiu sp, sp, -portCONTEXT_SIZE
  39. mfc0 k1, _CP0_STATUS
  40. /* Also save s6 and s5 so they can be used. Any nesting interrupts should
  41. maintain the values of these registers across the ISR. */
  42. sw s6, 44(sp)
  43. sw s5, 40(sp)
  44. sw k1, portSTATUS_STACK_LOCATION(sp)
  45. /* Prepare to enable interrupts above the current priority. */
  46. srl k0, k0, 0xa
  47. ins k1, k0, 10, 6
  48. ins k1, zero, 1, 4
  49. /* s5 is used as the frame pointer. */
  50. add s5, zero, sp
  51. /* Check the nesting count value. */
  52. la k0, uxInterruptNesting
  53. lw s6, (k0)
  54. /* If the nesting count is 0 then swap to the the system stack, otherwise
  55. the system stack is already being used. */
  56. bne s6, zero, 1f
  57. nop
  58. /* Swap to the system stack. */
  59. la sp, xISRStackTop
  60. lw sp, (sp)
  61. /* Increment and save the nesting count. */
  62. 1: addiu s6, s6, 1
  63. sw s6, 0(k0)
  64. /* s6 holds the EPC value, this is saved after interrupts are re-enabled. */
  65. mfc0 s6, _CP0_EPC
  66. /* Re-enable interrupts. */
  67. mtc0 k1, _CP0_STATUS
  68. /* Save the context into the space just created. s6 is saved again
  69. here as it now contains the EPC value. No other s registers need be
  70. saved. */
  71. sw ra, 120(s5)
  72. sw s8, 116(s5)
  73. sw t9, 112(s5)
  74. sw t8, 108(s5)
  75. sw t7, 104(s5)
  76. sw t6, 100(s5)
  77. sw t5, 96(s5)
  78. sw t4, 92(s5)
  79. sw t3, 88(s5)
  80. sw t2, 84(s5)
  81. sw t1, 80(s5)
  82. sw t0, 76(s5)
  83. sw a3, 72(s5)
  84. sw a2, 68(s5)
  85. sw a1, 64(s5)
  86. sw a0, 60(s5)
  87. sw v1, 56(s5)
  88. sw v0, 52(s5)
  89. sw s6, portEPC_STACK_LOCATION(s5)
  90. sw $1, 16(s5)
  91. /* s6 is used as a scratch register. */
  92. mfhi s6
  93. sw s6, 12(s5)
  94. mflo s6
  95. sw s6, 8(s5)
  96. /* Update the task stack pointer value if nesting is zero. */
  97. la s6, uxInterruptNesting
  98. lw s6, (s6)
  99. addiu s6, s6, -1
  100. bne s6, zero, 1f
  101. nop
  102. /* Save the stack pointer. */
  103. la s6, uxSavedTaskStackPointer
  104. sw s5, (s6)
  105. 1:
  106. .endm
  107. /******************************************************************/
  108. .macro portRESTORE_CONTEXT
  109. /* Restore the stack pointer from the TCB. This is only done if the
  110. nesting count is 1. */
  111. la s6, uxInterruptNesting
  112. lw s6, (s6)
  113. addiu s6, s6, -1
  114. bne s6, zero, 1f
  115. nop
  116. la s6, uxSavedTaskStackPointer
  117. lw s5, (s6)
  118. /* Restore the context. */
  119. 1: lw s6, 8(s5)
  120. mtlo s6
  121. lw s6, 12(s5)
  122. mthi s6
  123. lw $1, 16(s5)
  124. /* s6 is loaded as it was used as a scratch register and therefore saved
  125. as part of the interrupt context. */
  126. lw s6, 44(s5)
  127. lw v0, 52(s5)
  128. lw v1, 56(s5)
  129. lw a0, 60(s5)
  130. lw a1, 64(s5)
  131. lw a2, 68(s5)
  132. lw a3, 72(s5)
  133. lw t0, 76(s5)
  134. lw t1, 80(s5)
  135. lw t2, 84(s5)
  136. lw t3, 88(s5)
  137. lw t4, 92(s5)
  138. lw t5, 96(s5)
  139. lw t6, 100(s5)
  140. lw t7, 104(s5)
  141. lw t8, 108(s5)
  142. lw t9, 112(s5)
  143. lw s8, 116(s5)
  144. lw ra, 120(s5)
  145. /* Protect access to the k registers, and others. */
  146. di
  147. ehb
  148. /* Decrement the nesting count. */
  149. la k0, uxInterruptNesting
  150. lw k1, (k0)
  151. addiu k1, k1, -1
  152. sw k1, 0(k0)
  153. lw k0, portSTATUS_STACK_LOCATION(s5)
  154. lw k1, portEPC_STACK_LOCATION(s5)
  155. /* Leave the stack in its original state. First load sp from s5, then
  156. restore s5 from the stack. */
  157. add sp, zero, s5
  158. lw s5, 40(sp)
  159. addiu sp, sp, portCONTEXT_SIZE
  160. mtc0 k0, _CP0_STATUS
  161. mtc0 k1, _CP0_EPC
  162. ehb
  163. eret
  164. nop
  165. .endm