portASM.S 10 KB

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