portASM.S 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. .file "portASM.S"
  29. #include "FreeRTOSConfig.h"
  30. #include "ISR_Support.h"
  31. .extern pxCurrentTCB
  32. .extern vTaskSwitchContext
  33. .extern vPortCentralInterruptHandler
  34. .extern xTaskIncrementTick
  35. .extern vPortAPICErrorHandler
  36. .extern pucPortTaskFPUContextBuffer
  37. .extern ulPortYieldPending
  38. .global vPortStartFirstTask
  39. .global vPortCentralInterruptWrapper
  40. .global vPortAPICErrorHandlerWrapper
  41. .global vPortTimerHandler
  42. .global vPortYieldCall
  43. .global vPortAPICSpuriousHandler
  44. .text
  45. /*-----------------------------------------------------------*/
  46. .align 4
  47. .func vPortYieldCall
  48. vPortYieldCall:
  49. /* Save general purpose registers. */
  50. pusha
  51. .if configSUPPORT_FPU == 1
  52. /* If the task has a buffer allocated to save the FPU context then save
  53. the FPU context now. */
  54. movl pucPortTaskFPUContextBuffer, %eax
  55. test %eax, %eax
  56. je 1f
  57. fnsave ( %eax )
  58. fwait
  59. 1:
  60. /* Save the address of the FPU context, if any. */
  61. push pucPortTaskFPUContextBuffer
  62. .endif /* configSUPPORT_FPU */
  63. /* Find the TCB. */
  64. movl pxCurrentTCB, %eax
  65. /* Stack location is first item in the TCB. */
  66. movl %esp, (%eax)
  67. call vTaskSwitchContext
  68. /* Find the location of pxCurrentTCB again - a callee saved register could
  69. be used in place of eax to prevent this second load, but that then relies
  70. on the compiler and other asm code. */
  71. movl pxCurrentTCB, %eax
  72. movl (%eax), %esp
  73. .if configSUPPORT_FPU == 1
  74. /* Restore address of task's FPU context buffer. */
  75. pop pucPortTaskFPUContextBuffer
  76. /* If the task has a buffer allocated in which its FPU context is saved,
  77. then restore it now. */
  78. movl pucPortTaskFPUContextBuffer, %eax
  79. test %eax, %eax
  80. je 1f
  81. frstor ( %eax )
  82. 1:
  83. .endif
  84. popa
  85. iret
  86. .endfunc
  87. /*-----------------------------------------------------------*/
  88. .align 4
  89. .func vPortStartFirstTask
  90. vPortStartFirstTask:
  91. /* Find the TCB. */
  92. movl pxCurrentTCB, %eax
  93. /* Stack location is first item in the TCB. */
  94. movl (%eax), %esp
  95. /* Restore FPU context flag. */
  96. .if configSUPPORT_FPU == 1
  97. pop pucPortTaskFPUContextBuffer
  98. .endif /* configSUPPORT_FPU */
  99. /* Restore general purpose registers. */
  100. popa
  101. iret
  102. .endfunc
  103. /*-----------------------------------------------------------*/
  104. .align 4
  105. .func vPortAPICErrorHandlerWrapper
  106. vPortAPICErrorHandlerWrapper:
  107. pusha
  108. call vPortAPICErrorHandler
  109. popa
  110. /* EOI. */
  111. movl $0x00, (0xFEE000B0)
  112. iret
  113. .endfunc
  114. /*-----------------------------------------------------------*/
  115. .align 4
  116. .func vPortTimerHandler
  117. vPortTimerHandler:
  118. /* Save general purpose registers. */
  119. pusha
  120. /* Interrupts are not nested, so save the rest of the task context. */
  121. .if configSUPPORT_FPU == 1
  122. /* If the task has a buffer allocated to save the FPU context then save the
  123. FPU context now. */
  124. movl pucPortTaskFPUContextBuffer, %eax
  125. test %eax, %eax
  126. je 1f
  127. fnsave ( %eax ) /* Save FLOP context into ucTempFPUBuffer array. */
  128. fwait
  129. 1:
  130. /* Save the address of the FPU context, if any. */
  131. push pucPortTaskFPUContextBuffer
  132. .endif /* configSUPPORT_FPU */
  133. /* Find the TCB. */
  134. movl pxCurrentTCB, %eax
  135. /* Stack location is first item in the TCB. */
  136. movl %esp, (%eax)
  137. /* Switch stacks. */
  138. movl ulTopOfSystemStack, %esp
  139. movl %esp, %ebp
  140. /* Increment nesting count. */
  141. add $1, ulInterruptNesting
  142. call xTaskIncrementTick
  143. sti
  144. /* Is a switch to another task required? */
  145. test %eax, %eax
  146. je _skip_context_switch
  147. cli
  148. call vTaskSwitchContext
  149. _skip_context_switch:
  150. cli
  151. /* Decrement the variable used to determine if a switch to a system
  152. stack is necessary. */
  153. sub $1, ulInterruptNesting
  154. /* Stack location is first item in the TCB. */
  155. movl pxCurrentTCB, %eax
  156. movl (%eax), %esp
  157. .if configSUPPORT_FPU == 1
  158. /* Restore address of task's FPU context buffer. */
  159. pop pucPortTaskFPUContextBuffer
  160. /* If the task has a buffer allocated in which its FPU context is saved,
  161. then restore it now. */
  162. movl pucPortTaskFPUContextBuffer, %eax
  163. test %eax, %eax
  164. je 1f
  165. frstor ( %eax )
  166. 1:
  167. .endif
  168. popa
  169. /* EOI. */
  170. movl $0x00, (0xFEE000B0)
  171. iret
  172. .endfunc
  173. /*-----------------------------------------------------------*/
  174. .if configUSE_COMMON_INTERRUPT_ENTRY_POINT == 1
  175. .align 4
  176. .func vPortCentralInterruptWrapper
  177. vPortCentralInterruptWrapper:
  178. portFREERTOS_INTERRUPT_ENTRY
  179. movl $0xFEE00170, %eax /* Highest In Service Register (ISR) long word. */
  180. movl $8, %ecx /* Loop counter. */
  181. next_isr_long_word:
  182. test %ecx, %ecx /* Loop counter reached 0? */
  183. je wrapper_epilogue /* Looked at all ISR registers without finding a bit set. */
  184. sub $1, %ecx /* Sub 1 from loop counter. */
  185. movl (%eax), %ebx /* Load next ISR long word. */
  186. sub $0x10, %eax /* Point to next ISR long word in case no bits are set in the current long word. */
  187. test %ebx, %ebx /* Are there any bits set? */
  188. je next_isr_long_word /* Look at next ISR long word if no bits were set. */
  189. sti
  190. bsr %ebx, %ebx /* A bit was set, which one? */
  191. movl $32, %eax /* Destination operand for following multiplication. */
  192. mul %ecx /* Calculate base vector for current register, 32 vectors per register. */
  193. add %ebx, %eax /* Add bit offset into register to get final vector number. */
  194. push %eax /* Vector number is function parameter. */
  195. call vPortCentralInterruptHandler
  196. pop %eax /* Remove parameter. */
  197. wrapper_epilogue:
  198. portFREERTOS_INTERRUPT_EXIT
  199. .endfunc
  200. .endif /* configUSE_COMMON_INTERRUPT_ENTRY_POINT */
  201. /*-----------------------------------------------------------*/
  202. .align 4
  203. .func vPortAPISpuriousHandler
  204. vPortAPICSpuriousHandler:
  205. iret
  206. .endfunc
  207. .end