portASM.S 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. .text
  29. .arm
  30. .set SYS_MODE, 0x1f
  31. .set SVC_MODE, 0x13
  32. .set IRQ_MODE, 0x12
  33. /* Hardware registers. */
  34. .extern ulICCIAR
  35. .extern ulICCEOIR
  36. .extern ulICCPMR
  37. /* Variables and functions. */
  38. .extern ulMaxAPIPriorityMask
  39. .extern _freertos_vector_table
  40. .extern pxCurrentTCB
  41. .extern vTaskSwitchContext
  42. .extern vApplicationIRQHandler
  43. .extern ulPortInterruptNesting
  44. .extern ulPortTaskHasFPUContext
  45. .global FreeRTOS_IRQ_Handler
  46. .global FreeRTOS_SWI_Handler
  47. .global vPortRestoreTaskContext
  48. .macro portSAVE_CONTEXT
  49. /* Save the LR and SPSR onto the system mode stack before switching to
  50. system mode to save the remaining system mode registers. */
  51. SRSDB sp!, #SYS_MODE
  52. CPS #SYS_MODE
  53. PUSH {R0-R12, R14}
  54. /* Push the critical nesting count. */
  55. LDR R2, ulCriticalNestingConst
  56. LDR R1, [R2]
  57. PUSH {R1}
  58. /* Does the task have a floating point context that needs saving? If
  59. ulPortTaskHasFPUContext is 0 then no. */
  60. LDR R2, ulPortTaskHasFPUContextConst
  61. LDR R3, [R2]
  62. CMP R3, #0
  63. /* Save the floating point context, if any. */
  64. FMRXNE R1, FPSCR
  65. VPUSHNE {D0-D15}
  66. /*VPUSHNE {D16-D31}*/
  67. PUSHNE {R1}
  68. /* Save ulPortTaskHasFPUContext itself. */
  69. PUSH {R3}
  70. /* Save the stack pointer in the TCB. */
  71. LDR R0, pxCurrentTCBConst
  72. LDR R1, [R0]
  73. STR SP, [R1]
  74. .endm
  75. ; /**********************************************************************/
  76. .macro portRESTORE_CONTEXT
  77. /* Set the SP to point to the stack of the task being restored. */
  78. LDR R0, pxCurrentTCBConst
  79. LDR R1, [R0]
  80. LDR SP, [R1]
  81. /* Is there a floating point context to restore? If the restored
  82. ulPortTaskHasFPUContext is zero then no. */
  83. LDR R0, ulPortTaskHasFPUContextConst
  84. POP {R1}
  85. STR R1, [R0]
  86. CMP R1, #0
  87. /* Restore the floating point context, if any. */
  88. POPNE {R0}
  89. /*VPOPNE {D16-D31}*/
  90. VPOPNE {D0-D15}
  91. VMSRNE FPSCR, R0
  92. /* Restore the critical section nesting depth. */
  93. LDR R0, ulCriticalNestingConst
  94. POP {R1}
  95. STR R1, [R0]
  96. /* Ensure the priority mask is correct for the critical nesting depth. */
  97. LDR R2, ulICCPMRConst
  98. LDR R2, [R2]
  99. CMP R1, #0
  100. MOVEQ R4, #255
  101. LDRNE R4, ulMaxAPIPriorityMaskConst
  102. LDRNE R4, [R4]
  103. STR R4, [R2]
  104. /* Restore all system mode registers other than the SP (which is already
  105. being used). */
  106. POP {R0-R12, R14}
  107. /* Return to the task code, loading CPSR on the way. */
  108. RFEIA sp!
  109. .endm
  110. /******************************************************************************
  111. * SVC handler is used to start the scheduler.
  112. *****************************************************************************/
  113. .align 4
  114. .type FreeRTOS_SWI_Handler, %function
  115. FreeRTOS_SWI_Handler:
  116. /* Save the context of the current task and select a new task to run. */
  117. portSAVE_CONTEXT
  118. LDR R0, vTaskSwitchContextConst
  119. BLX R0
  120. portRESTORE_CONTEXT
  121. /******************************************************************************
  122. * vPortRestoreTaskContext is used to start the scheduler.
  123. *****************************************************************************/
  124. .type vPortRestoreTaskContext, %function
  125. vPortRestoreTaskContext:
  126. /* Switch to system mode. */
  127. CPS #SYS_MODE
  128. portRESTORE_CONTEXT
  129. .align 4
  130. .type FreeRTOS_IRQ_Handler, %function
  131. FreeRTOS_IRQ_Handler:
  132. /* Return to the interrupted instruction. */
  133. SUB lr, lr, #4
  134. /* Push the return address and SPSR. */
  135. PUSH {lr}
  136. MRS lr, SPSR
  137. PUSH {lr}
  138. /* Change to supervisor mode to allow reentry. */
  139. CPS #SVC_MODE
  140. /* Push used registers. */
  141. PUSH {r0-r4, r12}
  142. /* Increment nesting count. r3 holds the address of ulPortInterruptNesting
  143. for future use. r1 holds the original ulPortInterruptNesting value for
  144. future use. */
  145. LDR r3, ulPortInterruptNestingConst
  146. LDR r1, [r3]
  147. ADD r4, r1, #1
  148. STR r4, [r3]
  149. /* Read value from the interrupt acknowledge register, which is stored in r0
  150. for future parameter and interrupt clearing use. */
  151. LDR r2, ulICCIARConst
  152. LDR r2, [r2]
  153. LDR r0, [r2]
  154. /* Ensure bit 2 of the stack pointer is clear. r2 holds the bit 2 value for
  155. future use. _RB_ Is this ever needed provided the start of the stack is
  156. alligned on an 8-byte boundary? */
  157. MOV r2, sp
  158. AND r2, r2, #4
  159. SUB sp, sp, r2
  160. /* Call the interrupt handler. */
  161. PUSH {r0-r4, lr}
  162. LDR r1, vApplicationIRQHandlerConst
  163. BLX r1
  164. POP {r0-r4, lr}
  165. ADD sp, sp, r2
  166. CPSID i
  167. DSB
  168. ISB
  169. /* Write the value read from ICCIAR to ICCEOIR. */
  170. LDR r4, ulICCEOIRConst
  171. LDR r4, [r4]
  172. STR r0, [r4]
  173. /* Restore the old nesting count. */
  174. STR r1, [r3]
  175. /* A context switch is never performed if the nesting count is not 0. */
  176. CMP r1, #0
  177. BNE exit_without_switch
  178. /* Did the interrupt request a context switch? r1 holds the address of
  179. ulPortYieldRequired and r0 the value of ulPortYieldRequired for future
  180. use. */
  181. LDR r1, =ulPortYieldRequired
  182. LDR r0, [r1]
  183. CMP r0, #0
  184. BNE switch_before_exit
  185. exit_without_switch:
  186. /* No context switch. Restore used registers, LR_irq and SPSR before
  187. returning. */
  188. POP {r0-r4, r12}
  189. CPS #IRQ_MODE
  190. POP {LR}
  191. MSR SPSR_cxsf, LR
  192. POP {LR}
  193. MOVS PC, LR
  194. switch_before_exit:
  195. /* A context swtich is to be performed. Clear the context switch pending
  196. flag. */
  197. MOV r0, #0
  198. STR r0, [r1]
  199. /* Restore used registers, LR-irq and SPSR before saving the context
  200. to the task stack. */
  201. POP {r0-r4, r12}
  202. CPS #IRQ_MODE
  203. POP {LR}
  204. MSR SPSR_cxsf, LR
  205. POP {LR}
  206. portSAVE_CONTEXT
  207. /* Call the function that selects the new task to execute.
  208. vTaskSwitchContext() if vTaskSwitchContext() uses LDRD or STRD
  209. instructions, or 8 byte aligned stack allocated data. LR does not need
  210. saving as a new LR will be loaded by portRESTORE_CONTEXT anyway. */
  211. LDR R0, vTaskSwitchContextConst
  212. BLX R0
  213. /* Restore the context of, and branch to, the task selected to execute
  214. next. */
  215. portRESTORE_CONTEXT
  216. ulICCIARConst: .word ulICCIAR
  217. ulICCEOIRConst: .word ulICCEOIR
  218. ulICCPMRConst: .word ulICCPMR
  219. pxCurrentTCBConst: .word pxCurrentTCB
  220. ulCriticalNestingConst: .word ulCriticalNesting
  221. ulPortTaskHasFPUContextConst: .word ulPortTaskHasFPUContext
  222. ulMaxAPIPriorityMaskConst: .word ulMaxAPIPriorityMask
  223. vTaskSwitchContextConst: .word vTaskSwitchContext
  224. vApplicationIRQHandlerConst: .word vApplicationIRQHandler
  225. ulPortInterruptNestingConst: .word ulPortInterruptNesting
  226. .end