context_gcc.S 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. vmrs r5, fpscr
  55. stmfd sp!, {r5}
  56. __no_vfp_frame1:
  57. stmfd sp!, {r6}
  58. #endif
  59. str sp, [r0] @ store sp in preempted tasks TCB
  60. ldr sp, [r1] @ get new task stack pointer
  61. #ifdef RT_USING_FPU
  62. /* fpu context */
  63. ldmfd sp!, {r6}
  64. vmsr fpexc, r6
  65. tst r6, #(1<<30)
  66. beq __no_vfp_frame2
  67. ldmfd sp!, {r5}
  68. vmsr fpscr, r5
  69. vldmia sp!, {d0-d15}
  70. __no_vfp_frame2:
  71. #endif
  72. ldmfd sp!, {r1}
  73. msr spsr_cxsf, r1 /* original mode */
  74. ldmfd sp!, {r0-r12,lr,pc}^ /* irq return */
  75. /*
  76. * void rt_hw_context_switch_to(rt_uint32 to)
  77. * r0 --> to
  78. */
  79. .globl rt_hw_context_switch_to
  80. rt_hw_context_switch_to:
  81. LDR sp, [r0] @ get new task stack pointer
  82. #ifdef RT_USING_FPU
  83. ldmfd sp!, {r6}
  84. vmsr fpexc, r6
  85. tst r6, #(1<<30)
  86. beq __no_vfp_frame_to
  87. ldmfd sp!, {r5}
  88. vmsr fpscr, r5
  89. vldmia sp!, {d0-d15}
  90. __no_vfp_frame_to:
  91. #endif
  92. LDMIA sp!, {r4} @ pop new task cpsr to spsr
  93. MSR spsr_cxsf, r4
  94. ldmfd sp!, {r0-r12,lr,pc}^ /* irq return */
  95. /*
  96. * void rt_hw_context_switch_interrupt(rt_uint32 from, rt_uint32 to)@
  97. */
  98. .globl rt_hw_context_switch_interrupt
  99. rt_hw_context_switch_interrupt:
  100. LDR r2, =rt_thread_switch_interrupt_flag
  101. LDR r3, [r2]
  102. CMP r3, #1
  103. BEQ _reswitch
  104. MOV r3, #1 @ set rt_thread_switch_interrupt_flag to 1
  105. STR r3, [r2]
  106. LDR r2, =rt_interrupt_from_thread @ set rt_interrupt_from_thread
  107. STR r0, [r2]
  108. _reswitch:
  109. LDR r2, =rt_interrupt_to_thread @ set rt_interrupt_to_thread
  110. STR r1, [r2]
  111. BX lr
  112. .globl IRQ_Handler
  113. IRQ_Handler:
  114. STMDB sp!, {r0-r12,lr}
  115. #ifdef RT_USING_FPU
  116. VMRS r0, fpexc
  117. TST r0, #0x40000000
  118. BEQ __no_vfp_frame_str_irq
  119. VSTMDB sp!, {d0-d15}
  120. VMRS r1, fpscr
  121. @ TODO: add support for Common VFPv3.
  122. @ Save registers like FPINST, FPINST2
  123. STMDB sp!, {r1}
  124. __no_vfp_frame_str_irq:
  125. STMDB sp!, {r0}
  126. #endif
  127. BL rt_interrupt_enter
  128. BL rt_hw_trap_irq
  129. BL rt_interrupt_leave
  130. @ if rt_thread_switch_interrupt_flag set, jump to
  131. @ rt_hw_context_switch_interrupt_do and don't return
  132. LDR r0, =rt_thread_switch_interrupt_flag
  133. LDR r1, [r0]
  134. CMP r1, #1
  135. BEQ rt_hw_context_switch_interrupt_do
  136. #ifdef RT_USING_FPU
  137. LDMIA sp!, {r0} @ get fpexc
  138. VMSR fpexc, r0
  139. TST r0, #0x40000000
  140. BEQ __no_vfp_frame_ldr_irq
  141. LDMIA sp!, {r1} @ get fpscr
  142. VMSR fpscr, r1
  143. VLDMIA sp!, {d0-d15}
  144. __no_vfp_frame_ldr_irq:
  145. #endif
  146. LDMIA sp!, {r0-r12,lr}
  147. SUBS pc, lr, #4
  148. /*
  149. * void rt_hw_context_switch_interrupt_do(rt_base_t flag)
  150. */
  151. .globl rt_hw_context_switch_interrupt_do
  152. rt_hw_context_switch_interrupt_do:
  153. MOV r1, #0 @ clear flag
  154. STR r1, [r0]
  155. #ifdef RT_USING_FPU
  156. LDMIA sp!, {r0} @ get fpexc
  157. VMSR fpexc, r0
  158. TST r0, #0x40000000
  159. BEQ __no_vfp_frame_do1
  160. LDMIA sp!, {r1} @ get fpscr
  161. VMSR fpscr, r1
  162. VLDMIA sp!, {d0-d15}
  163. __no_vfp_frame_do1:
  164. #endif
  165. LDMIA sp!, {r0-r12,lr} @ reload saved registers
  166. STMDB sp, {r0-r3} @ save r0-r3. We will restore r0-r3 in the SVC
  167. @ mode so there is no need to update SP.
  168. SUB r1, sp, #16 @ save the right SP value in r1, so we could restore r0-r3.
  169. SUB r2, lr, #4 @ save old task's pc to r2
  170. MRS r3, spsr @ get cpsr of interrupt thread
  171. @ switch to SVC mode and no interrupt
  172. CPSID IF, #0x13
  173. STMDB sp!, {r2} @ push old task's pc
  174. STMDB sp!, {r4-r12,lr} @ push old task's lr,r12-r4
  175. LDMIA r1!, {r4-r7} @ restore r0-r3 of the interrupted thread
  176. STMDB sp!, {r4-r7} @ push old task's r3-r0. We don't need to push/pop them to
  177. @ r0-r3 because we just want to transfer the data and don't
  178. @ use them here.
  179. STMDB sp!, {r3} @ push old task's cpsr
  180. #ifdef RT_USING_FPU
  181. VMRS r0, fpexc
  182. TST r0, #0x40000000
  183. BEQ __no_vfp_frame_do2
  184. VSTMDB sp!, {d0-d15}
  185. VMRS r1, fpscr
  186. @ TODO: add support for Common VFPv3.
  187. @ Save registers like FPINST, FPINST2
  188. STMDB sp!, {r1}
  189. __no_vfp_frame_do2:
  190. STMDB sp!, {r0}
  191. #endif
  192. LDR r4, =rt_interrupt_from_thread
  193. LDR r5, [r4]
  194. STR sp, [r5] @ store sp in preempted tasks's TCB
  195. LDR r6, =rt_interrupt_to_thread
  196. LDR r6, [r6]
  197. LDR sp, [r6] @ get new task's stack pointer
  198. #ifdef RT_USING_FPU
  199. ldmfd sp!, {r6}
  200. vmsr fpexc, r6
  201. tst r6, #(1<<30)
  202. beq __no_vfp_frame_do3
  203. ldmfd sp!, {r5}
  204. vmsr fpscr, r5
  205. vldmia sp!, {d0-d15}
  206. __no_vfp_frame_do3:
  207. #endif
  208. LDMIA sp!, {r4} @ pop new task's cpsr to spsr
  209. MSR spsr_cxsf, r4
  210. ldmfd sp!, {r0-r12,lr,pc}^ /* irq return */