portasm.S 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. .extern pxCurrentTCB
  30. .extern vTaskSwitchContext
  31. .extern xTaskIncrementTick
  32. .extern vPortISRHandler
  33. .global vPortStartFirstTask
  34. .global vPortYield
  35. .global vPortTickISR
  36. .global vPortISRWrapper
  37. .global vPortSaveFPURegisters
  38. .global vPortRestoreFPURegisters
  39. .set BChainField, 0
  40. .set NextLRField, BChainField + 4
  41. .set MSRField, NextLRField + 4
  42. .set PCField, MSRField + 4
  43. .set LRField, PCField + 4
  44. .set CTRField, LRField + 4
  45. .set XERField, CTRField + 4
  46. .set CRField, XERField + 4
  47. .set USPRG0Field, CRField + 4
  48. .set r0Field, USPRG0Field + 4
  49. .set r2Field, r0Field + 4
  50. .set r3r31Field, r2Field + 4
  51. .set IFrameSize, r3r31Field + ( ( 31 - 3 ) + 1 ) * 4
  52. .macro portSAVE_STACK_POINTER_AND_LR
  53. /* Get the address of the TCB. */
  54. xor R0, R0, R0
  55. addis R2, R0, pxCurrentTCB@ha
  56. lwz R2, pxCurrentTCB@l( R2 )
  57. /* Store the stack pointer into the TCB */
  58. stw SP, 0( R2 )
  59. /* Save the link register */
  60. stwu R1, -24( R1 )
  61. mflr R0
  62. stw R31, 20( R1 )
  63. stw R0, 28( R1 )
  64. mr R31, r1
  65. .endm
  66. .macro portRESTORE_STACK_POINTER_AND_LR
  67. /* Restore the link register */
  68. lwz R11, 0( R1 )
  69. lwz R0, 4( R11 )
  70. mtlr R0
  71. lwz R31, -4( R11 )
  72. mr R1, R11
  73. /* Get the address of the TCB. */
  74. xor R0, R0, R0
  75. addis SP, R0, pxCurrentTCB@ha
  76. lwz SP, pxCurrentTCB@l( R1 )
  77. /* Get the task stack pointer from the TCB. */
  78. lwz SP, 0( SP )
  79. .endm
  80. vPortStartFirstTask:
  81. /* Get the address of the TCB. */
  82. xor R0, R0, R0
  83. addis SP, R0, pxCurrentTCB@ha
  84. lwz SP, pxCurrentTCB@l( SP )
  85. /* Get the task stack pointer from the TCB. */
  86. lwz SP, 0( SP )
  87. /* Restore MSR register to SRR1. */
  88. lwz R0, MSRField(R1)
  89. mtsrr1 R0
  90. /* Restore current PC location to SRR0. */
  91. lwz R0, PCField(R1)
  92. mtsrr0 R0
  93. /* Save USPRG0 register */
  94. lwz R0, USPRG0Field(R1)
  95. mtspr 0x100,R0
  96. /* Restore Condition register */
  97. lwz R0, CRField(R1)
  98. mtcr R0
  99. /* Restore Fixed Point Exception register */
  100. lwz R0, XERField(R1)
  101. mtxer R0
  102. /* Restore Counter register */
  103. lwz R0, CTRField(R1)
  104. mtctr R0
  105. /* Restore Link register */
  106. lwz R0, LRField(R1)
  107. mtlr R0
  108. /* Restore remaining GPR registers. */
  109. lmw R3,r3r31Field(R1)
  110. /* Restore r0 and r2. */
  111. lwz R0, r0Field(R1)
  112. lwz R2, r2Field(R1)
  113. /* Remove frame from stack */
  114. addi R1,R1,IFrameSize
  115. /* Return into the first task */
  116. rfi
  117. vPortYield:
  118. portSAVE_STACK_POINTER_AND_LR
  119. bl vTaskSwitchContext
  120. portRESTORE_STACK_POINTER_AND_LR
  121. blr
  122. vPortTickISR:
  123. portSAVE_STACK_POINTER_AND_LR
  124. bl xTaskIncrementTick
  125. #if configUSE_PREEMPTION == 1
  126. bl vTaskSwitchContext
  127. #endif
  128. /* Clear the interrupt */
  129. lis R0, 2048
  130. mttsr R0
  131. portRESTORE_STACK_POINTER_AND_LR
  132. blr
  133. vPortISRWrapper:
  134. portSAVE_STACK_POINTER_AND_LR
  135. bl vPortISRHandler
  136. portRESTORE_STACK_POINTER_AND_LR
  137. blr
  138. #if configUSE_FPU == 1
  139. vPortSaveFPURegisters:
  140. /* Enable APU and mark FPU as present. */
  141. mfmsr r0
  142. xor r30, r30, r30
  143. oris r30, r30, 512
  144. ori r30, r30, 8192
  145. or r0, r0, r30
  146. mtmsr r0
  147. #ifdef USE_DP_FPU
  148. /* Buffer address is in r3. Save each flop register into an offset from
  149. this buffer address. */
  150. stfd f0, 0(r3)
  151. stfd f1, 8(r3)
  152. stfd f2, 16(r3)
  153. stfd f3, 24(r3)
  154. stfd f4, 32(r3)
  155. stfd f5, 40(r3)
  156. stfd f6, 48(r3)
  157. stfd f7, 56(r3)
  158. stfd f8, 64(r3)
  159. stfd f9, 72(r3)
  160. stfd f10, 80(r3)
  161. stfd f11, 88(r3)
  162. stfd f12, 96(r3)
  163. stfd f13, 104(r3)
  164. stfd f14, 112(r3)
  165. stfd f15, 120(r3)
  166. stfd f16, 128(r3)
  167. stfd f17, 136(r3)
  168. stfd f18, 144(r3)
  169. stfd f19, 152(r3)
  170. stfd f20, 160(r3)
  171. stfd f21, 168(r3)
  172. stfd f22, 176(r3)
  173. stfd f23, 184(r3)
  174. stfd f24, 192(r3)
  175. stfd f25, 200(r3)
  176. stfd f26, 208(r3)
  177. stfd f27, 216(r3)
  178. stfd f28, 224(r3)
  179. stfd f29, 232(r3)
  180. stfd f30, 240(r3)
  181. stfd f31, 248(r3)
  182. /* Also save the FPSCR. */
  183. mffs f31
  184. stfs f31, 256(r3)
  185. #else
  186. /* Buffer address is in r3. Save each flop register into an offset from
  187. this buffer address. */
  188. stfs f0, 0(r3)
  189. stfs f1, 4(r3)
  190. stfs f2, 8(r3)
  191. stfs f3, 12(r3)
  192. stfs f4, 16(r3)
  193. stfs f5, 20(r3)
  194. stfs f6, 24(r3)
  195. stfs f7, 28(r3)
  196. stfs f8, 32(r3)
  197. stfs f9, 36(r3)
  198. stfs f10, 40(r3)
  199. stfs f11, 44(r3)
  200. stfs f12, 48(r3)
  201. stfs f13, 52(r3)
  202. stfs f14, 56(r3)
  203. stfs f15, 60(r3)
  204. stfs f16, 64(r3)
  205. stfs f17, 68(r3)
  206. stfs f18, 72(r3)
  207. stfs f19, 76(r3)
  208. stfs f20, 80(r3)
  209. stfs f21, 84(r3)
  210. stfs f22, 88(r3)
  211. stfs f23, 92(r3)
  212. stfs f24, 96(r3)
  213. stfs f25, 100(r3)
  214. stfs f26, 104(r3)
  215. stfs f27, 108(r3)
  216. stfs f28, 112(r3)
  217. stfs f29, 116(r3)
  218. stfs f30, 120(r3)
  219. stfs f31, 124(r3)
  220. /* Also save the FPSCR. */
  221. mffs f31
  222. stfs f31, 128(r3)
  223. #endif
  224. blr
  225. #endif /* configUSE_FPU. */
  226. #if configUSE_FPU == 1
  227. vPortRestoreFPURegisters:
  228. /* Enable APU and mark FPU as present. */
  229. mfmsr r0
  230. xor r30, r30, r30
  231. oris r30, r30, 512
  232. ori r30, r30, 8192
  233. or r0, r0, r30
  234. mtmsr r0
  235. #ifdef USE_DP_FPU
  236. /* Buffer address is in r3. Restore each flop register from an offset
  237. into this buffer.
  238. First the FPSCR. */
  239. lfs f31, 256(r3)
  240. mtfsf f31, 7
  241. lfd f0, 0(r3)
  242. lfd f1, 8(r3)
  243. lfd f2, 16(r3)
  244. lfd f3, 24(r3)
  245. lfd f4, 32(r3)
  246. lfd f5, 40(r3)
  247. lfd f6, 48(r3)
  248. lfd f7, 56(r3)
  249. lfd f8, 64(r3)
  250. lfd f9, 72(r3)
  251. lfd f10, 80(r3)
  252. lfd f11, 88(r3)
  253. lfd f12, 96(r3)
  254. lfd f13, 104(r3)
  255. lfd f14, 112(r3)
  256. lfd f15, 120(r3)
  257. lfd f16, 128(r3)
  258. lfd f17, 136(r3)
  259. lfd f18, 144(r3)
  260. lfd f19, 152(r3)
  261. lfd f20, 160(r3)
  262. lfd f21, 168(r3)
  263. lfd f22, 176(r3)
  264. lfd f23, 184(r3)
  265. lfd f24, 192(r3)
  266. lfd f25, 200(r3)
  267. lfd f26, 208(r3)
  268. lfd f27, 216(r3)
  269. lfd f28, 224(r3)
  270. lfd f29, 232(r3)
  271. lfd f30, 240(r3)
  272. lfd f31, 248(r3)
  273. #else
  274. /* Buffer address is in r3. Restore each flop register from an offset
  275. into this buffer.
  276. First the FPSCR. */
  277. lfs f31, 128(r3)
  278. mtfsf f31, 7
  279. lfs f0, 0(r3)
  280. lfs f1, 4(r3)
  281. lfs f2, 8(r3)
  282. lfs f3, 12(r3)
  283. lfs f4, 16(r3)
  284. lfs f5, 20(r3)
  285. lfs f6, 24(r3)
  286. lfs f7, 28(r3)
  287. lfs f8, 32(r3)
  288. lfs f9, 36(r3)
  289. lfs f10, 40(r3)
  290. lfs f11, 44(r3)
  291. lfs f12, 48(r3)
  292. lfs f13, 52(r3)
  293. lfs f14, 56(r3)
  294. lfs f15, 60(r3)
  295. lfs f16, 64(r3)
  296. lfs f17, 68(r3)
  297. lfs f18, 72(r3)
  298. lfs f19, 76(r3)
  299. lfs f20, 80(r3)
  300. lfs f21, 84(r3)
  301. lfs f22, 88(r3)
  302. lfs f23, 92(r3)
  303. lfs f24, 96(r3)
  304. lfs f25, 100(r3)
  305. lfs f26, 104(r3)
  306. lfs f27, 108(r3)
  307. lfs f28, 112(r3)
  308. lfs f29, 116(r3)
  309. lfs f30, 120(r3)
  310. lfs f31, 124(r3)
  311. #endif
  312. blr
  313. #endif /* configUSE_FPU. */