context_gcc.S 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (c) 2006-2024, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-03-01 Wangyuqiang first version
  9. */
  10. #include "rtconfig.h"
  11. .syntax unified
  12. .text
  13. .globl rt_thread_switch_interrupt_flag
  14. .globl rt_interrupt_from_thread
  15. .globl rt_interrupt_to_thread
  16. .globl rt_interrupt_enter
  17. .globl rt_interrupt_leave
  18. .globl rt_hw_trap_irq
  19. /*
  20. * rt_base_t rt_hw_interrupt_disable();
  21. */
  22. .globl rt_hw_interrupt_disable
  23. rt_hw_interrupt_disable:
  24. mrs r0, cpsr
  25. cpsid i
  26. bx lr
  27. /*
  28. * void rt_hw_interrupt_enable(rt_base_t level);
  29. */
  30. .globl rt_hw_interrupt_enable
  31. rt_hw_interrupt_enable:
  32. msr cpsr, r0
  33. bx lr
  34. /*
  35. * void rt_hw_context_switch(rt_uint32 from, rt_uint32 to)
  36. * r0 --> from
  37. * r1 --> to
  38. */
  39. .globl rt_hw_context_switch
  40. rt_hw_context_switch:
  41. clrex
  42. stmfd sp!, {lr} @ push pc (lr should be pushed in place of PC)
  43. stmfd sp!, {r0-r12, lr} @ push lr & register file
  44. mrs r4, cpsr
  45. tst lr, #0x01
  46. orrne r4, r4, #0x20 @ it's thumb code
  47. stmfd sp!, {r4} @ push cpsr
  48. #ifdef RT_USING_FPU
  49. /* fpu context */
  50. vmrs r6, fpexc
  51. tst r6, #(1<<30)
  52. beq __no_vfp_frame1
  53. vstmdb sp!, {d0-d15}
  54. vstmdb sp!, {d16-d31}
  55. vmrs r5, fpscr
  56. stmfd sp!, {r5}
  57. __no_vfp_frame1:
  58. stmfd sp!, {r6}
  59. #endif
  60. str sp, [r0] @ store sp in preempted tasks TCB
  61. ldr sp, [r1] @ get new task stack pointer
  62. #ifdef RT_USING_FPU
  63. /* fpu context */
  64. ldmfd sp!, {r6}
  65. vmsr fpexc, r6
  66. tst r6, #(1<<30)
  67. beq __no_vfp_frame2
  68. ldmfd sp!, {r5}
  69. vmsr fpscr, r5
  70. vldmia sp!, {d16-d31}
  71. vldmia sp!, {d0-d15}
  72. __no_vfp_frame2:
  73. #endif
  74. ldmfd sp!, {r1}
  75. msr spsr_cxsf, r1 /* original mode */
  76. ldmfd sp!, {r0-r12,lr,pc}^ /* irq return */
  77. /*
  78. * void rt_hw_context_switch_to(rt_uint32 to)
  79. * r0 --> to
  80. */
  81. .globl rt_hw_context_switch_to
  82. rt_hw_context_switch_to:
  83. LDR sp, [r0] @ get new task stack pointer
  84. #ifdef RT_USING_FPU
  85. ldmfd sp!, {r6}
  86. vmsr fpexc, r6
  87. tst r6, #(1<<30)
  88. beq __no_vfp_frame_to
  89. ldmfd sp!, {r5}
  90. vmsr fpscr, r5
  91. vldmia sp!, {d0-d15}
  92. __no_vfp_frame_to:
  93. #endif
  94. LDMIA sp!, {r4} @ pop new task cpsr to spsr
  95. MSR spsr_cxsf, r4
  96. ldmfd sp!, {r0-r12,lr,pc}^ /* irq return */
  97. /*
  98. * void rt_hw_context_switch_interrupt(rt_uint32 from, rt_uint32 to)@
  99. */
  100. .globl rt_hw_context_switch_interrupt
  101. rt_hw_context_switch_interrupt:
  102. LDR r2, =rt_thread_switch_interrupt_flag
  103. LDR r3, [r2]
  104. CMP r3, #1
  105. BEQ _reswitch
  106. MOV r3, #1 @ set rt_thread_switch_interrupt_flag to 1
  107. STR r3, [r2]
  108. LDR r2, =rt_interrupt_from_thread @ set rt_interrupt_from_thread
  109. STR r0, [r2]
  110. _reswitch:
  111. LDR r2, =rt_interrupt_to_thread @ set rt_interrupt_to_thread
  112. STR r1, [r2]
  113. BX lr
  114. .globl IRQ_Handler
  115. IRQ_Handler:
  116. STMDB sp!, {r0-r12,lr}
  117. #ifdef RT_USING_FPU
  118. VMRS r0, fpexc
  119. TST r0, #0x40000000
  120. BEQ __no_vfp_frame_str_irq
  121. VSTMDB sp!, {d0-d15}
  122. VMRS r1, fpscr
  123. @ TODO: add support for Common VFPv3.
  124. @ Save registers like FPINST, FPINST2
  125. STMDB sp!, {r1}
  126. __no_vfp_frame_str_irq:
  127. STMDB sp!, {r0}
  128. #endif
  129. BL rt_interrupt_enter
  130. BL rt_hw_trap_irq
  131. BL rt_interrupt_leave
  132. @ if rt_thread_switch_interrupt_flag set, jump to
  133. @ rt_hw_context_switch_interrupt_do and don't return
  134. LDR r0, =rt_thread_switch_interrupt_flag
  135. LDR r1, [r0]
  136. CMP r1, #1
  137. BEQ rt_hw_context_switch_interrupt_do
  138. #ifdef RT_USING_FPU
  139. LDMIA sp!, {r0} @ get fpexc
  140. VMSR fpexc, r0
  141. TST r0, #0x40000000
  142. BEQ __no_vfp_frame_ldr_irq
  143. LDMIA sp!, {r1} @ get fpscr
  144. VMSR fpscr, r1
  145. VLDMIA sp!, {d0-d15}
  146. __no_vfp_frame_ldr_irq:
  147. #endif
  148. LDMIA sp!, {r0-r12,lr}
  149. SUBS pc, lr, #4
  150. /*
  151. * void rt_hw_context_switch_interrupt_do(rt_base_t flag)
  152. */
  153. .globl rt_hw_context_switch_interrupt_do
  154. rt_hw_context_switch_interrupt_do:
  155. MOV r1, #0 @ clear flag
  156. STR r1, [r0]
  157. #ifdef RT_USING_FPU
  158. LDMIA sp!, {r0} @ get fpexc
  159. VMSR fpexc, r0
  160. TST r0, #0x40000000
  161. BEQ __no_vfp_frame_do1
  162. LDMIA sp!, {r1} @ get fpscr
  163. VMSR fpscr, r1
  164. VLDMIA sp!, {d0-d15}
  165. __no_vfp_frame_do1:
  166. #endif
  167. LDMIA sp!, {r0-r12,lr} @ reload saved registers
  168. STMDB sp, {r0-r3} @ save r0-r3. We will restore r0-r3 in the SVC
  169. @ mode so there is no need to update SP.
  170. SUB r1, sp, #16 @ save the right SP value in r1, so we could restore r0-r3.
  171. SUB r2, lr, #4 @ save old task's pc to r2
  172. MRS r3, spsr @ get cpsr of interrupt thread
  173. @ switch to SVC mode and no interrupt
  174. CPSID IF, #0x13
  175. STMDB sp!, {r2} @ push old task's pc
  176. STMDB sp!, {r4-r12,lr} @ push old task's lr,r12-r4
  177. LDMIA r1!, {r4-r7} @ restore r0-r3 of the interrupted thread
  178. STMDB sp!, {r4-r7} @ push old task's r3-r0. We don't need to push/pop them to
  179. @ r0-r3 because we just want to transfer the data and don't
  180. @ use them here.
  181. STMDB sp!, {r3} @ push old task's cpsr
  182. #ifdef RT_USING_FPU
  183. VMRS r0, fpexc
  184. TST r0, #0x40000000
  185. BEQ __no_vfp_frame_do2
  186. VSTMDB sp!, {d0-d15}
  187. VMRS r1, fpscr
  188. @ TODO: add support for Common VFPv3.
  189. @ Save registers like FPINST, FPINST2
  190. STMDB sp!, {r1}
  191. __no_vfp_frame_do2:
  192. STMDB sp!, {r0}
  193. #endif
  194. LDR r4, =rt_interrupt_from_thread
  195. LDR r5, [r4]
  196. STR sp, [r5] @ store sp in preempted tasks's TCB
  197. LDR r6, =rt_interrupt_to_thread
  198. LDR r6, [r6]
  199. LDR sp, [r6] @ get new task's stack pointer
  200. #ifdef RT_USING_FPU
  201. ldmfd sp!, {r6}
  202. vmsr fpexc, r6
  203. tst r6, #(1<<30)
  204. beq __no_vfp_frame_do3
  205. ldmfd sp!, {r5}
  206. vmsr fpscr, r5
  207. vldmia sp!, {d0-d15}
  208. __no_vfp_frame_do3:
  209. #endif
  210. LDMIA sp!, {r4} @ pop new task's cpsr to spsr
  211. MSR spsr_cxsf, r4
  212. ldmfd sp!, {r0-r12,lr,pc}^ /* irq return */