portASM.s 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 portmacro.inc
  29. IMPORT vApplicationIRQHandler
  30. IMPORT vTaskSwitchContext
  31. IMPORT ulPortYieldRequired
  32. IMPORT ulPortInterruptNesting
  33. IMPORT vTaskSwitchContext
  34. IMPORT ulICCIAR
  35. IMPORT ulICCEOIR
  36. EXPORT FreeRTOS_SWI_Handler
  37. EXPORT FreeRTOS_IRQ_Handler
  38. EXPORT vPortRestoreTaskContext
  39. ARM
  40. AREA PORT_ASM, CODE, READONLY
  41. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  42. ; SVC handler is used to yield a task.
  43. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  44. FreeRTOS_SWI_Handler
  45. PRESERVE8
  46. ; Save the context of the current task and select a new task to run.
  47. portSAVE_CONTEXT
  48. LDR R0, =vTaskSwitchContext
  49. BLX R0
  50. portRESTORE_CONTEXT
  51. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  52. ; vPortRestoreTaskContext is used to start the scheduler.
  53. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  54. vPortRestoreTaskContext
  55. ; Switch to system mode
  56. CPS #SYS_MODE
  57. portRESTORE_CONTEXT
  58. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  59. ; PL390 GIC interrupt handler
  60. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  61. FreeRTOS_IRQ_Handler
  62. ; Return to the interrupted instruction.
  63. SUB lr, lr, #4
  64. ; Push the return address and SPSR
  65. PUSH {lr}
  66. MRS lr, SPSR
  67. PUSH {lr}
  68. ; Change to supervisor mode to allow reentry.
  69. CPS #SVC_MODE
  70. ; Push used registers.
  71. PUSH {r0-r4, r12}
  72. ; Increment nesting count. r3 holds the address of ulPortInterruptNesting
  73. ; for future use. r1 holds the original ulPortInterruptNesting value for
  74. ; future use.
  75. LDR r3, =ulPortInterruptNesting
  76. LDR r1, [r3]
  77. ADD r4, r1, #1
  78. STR r4, [r3]
  79. ; Read value from the interrupt acknowledge register, which is stored in r0
  80. ; for future parameter and interrupt clearing use.
  81. LDR r2, =ulICCIAR
  82. LDR r0, [r2]
  83. ; Ensure bit 2 of the stack pointer is clear. r2 holds the bit 2 value for
  84. ; future use. _RB_ Does this ever actually need to be done provided the
  85. ; start of the stack is 8-byte aligned?
  86. MOV r2, sp
  87. AND r2, r2, #4
  88. SUB sp, sp, r2
  89. ; Call the interrupt handler. r4 is pushed to maintain alignment.
  90. PUSH {r0-r4, lr}
  91. LDR r1, =vApplicationIRQHandler
  92. BLX r1
  93. POP {r0-r4, lr}
  94. ADD sp, sp, r2
  95. CPSID i
  96. ; Write the value read from ICCIAR to ICCEOIR
  97. LDR r4, =ulICCEOIR
  98. STR r0, [r4]
  99. ; Restore the old nesting count
  100. STR r1, [r3]
  101. ; A context switch is never performed if the nesting count is not 0
  102. CMP r1, #0
  103. BNE exit_without_switch
  104. ; Did the interrupt request a context switch? r1 holds the address of
  105. ; ulPortYieldRequired and r0 the value of ulPortYieldRequired for future
  106. ; use.
  107. LDR r1, =ulPortYieldRequired
  108. LDR r0, [r1]
  109. CMP r0, #0
  110. BNE switch_before_exit
  111. exit_without_switch
  112. ; No context switch. Restore used registers, LR_irq and SPSR before
  113. ; returning.
  114. POP {r0-r4, r12}
  115. CPS #IRQ_MODE
  116. POP {LR}
  117. MSR SPSR_cxsf, LR
  118. POP {LR}
  119. MOVS PC, LR
  120. switch_before_exit
  121. ; A context swtich is to be performed. Clear the context switch pending
  122. ; flag.
  123. MOV r0, #0
  124. STR r0, [r1]
  125. ; Restore used registers, LR-irq and SPSR before saving the context
  126. ; to the task stack.
  127. POP {r0-r4, r12}
  128. CPS #IRQ_MODE
  129. POP {LR}
  130. MSR SPSR_cxsf, LR
  131. POP {LR}
  132. portSAVE_CONTEXT
  133. ; Call the function that selects the new task to execute.
  134. ; vTaskSwitchContext() if vTaskSwitchContext() uses LDRD or STRD
  135. ; instructions, or 8 byte aligned stack allocated data. LR does not need
  136. ; saving as a new LR will be loaded by portRESTORE_CONTEXT anyway.
  137. LDR r0, =vTaskSwitchContext
  138. BLX r0
  139. ; Restore the context of, and branch to, the task selected to execute next.
  140. portRESTORE_CONTEXT
  141. END