context_gcc.S 7.4 KB

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