context_gcc.S 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2013-07-05 Bernard the first version
  9. */
  10. #include "rtconfig.h"
  11. .section .text, "ax"
  12. #ifdef RT_USING_SMP
  13. #define rt_hw_interrupt_disable rt_hw_local_irq_disable
  14. #define rt_hw_interrupt_enable rt_hw_local_irq_enable
  15. #endif
  16. /*
  17. * rt_base_t rt_hw_interrupt_disable();
  18. */
  19. .globl rt_hw_interrupt_disable
  20. rt_hw_interrupt_disable:
  21. mrs r0, cpsr
  22. cpsid i
  23. bx lr
  24. /*
  25. * void rt_hw_interrupt_enable(rt_base_t level);
  26. */
  27. .globl rt_hw_interrupt_enable
  28. rt_hw_interrupt_enable:
  29. msr cpsr, r0
  30. bx lr
  31. /*
  32. * void rt_hw_context_switch_to(rt_uint32 to, struct rt_thread *to_thread);
  33. * r0 --> to (thread stack)
  34. * r1 --> to_thread
  35. */
  36. .globl rt_hw_context_switch_to
  37. rt_hw_context_switch_to:
  38. ldr sp, [r0] @ get new task stack pointer
  39. #ifdef RT_USING_SMP
  40. mov r0, r1
  41. bl rt_cpus_lock_status_restore
  42. bl rt_thread_self
  43. bl lwp_user_setting_restore
  44. #else
  45. bl rt_thread_self
  46. #ifdef RT_USING_USERSPACE
  47. mov r4, r0
  48. bl lwp_mmu_switch
  49. mov r0, r4
  50. #endif
  51. bl lwp_user_setting_restore
  52. #endif /*RT_USING_SMP*/
  53. b rt_hw_context_switch_exit
  54. .section .bss.share.isr
  55. _guest_switch_lvl:
  56. .word 0
  57. .globl vmm_virq_update
  58. .section .text.isr, "ax"
  59. /*
  60. * void rt_hw_context_switch(rt_uint32 from, rt_uint32 to, struct rt_thread *to_thread);
  61. * r0 --> from (from_thread stack)
  62. * r1 --> to (to_thread stack)
  63. * r2 --> to_thread
  64. */
  65. .globl rt_hw_context_switch
  66. rt_hw_context_switch:
  67. stmfd sp!, {lr} @ push pc (lr should be pushed in place of PC)
  68. stmfd sp!, {r0-r12, lr} @ push lr & register file
  69. mrs r4, cpsr
  70. tst lr, #0x01
  71. orrne r4, r4, #0x20 @ it's thumb code
  72. stmfd sp!, {r4} @ push cpsr
  73. #ifdef RT_USING_LWP
  74. stmfd sp, {r13, r14}^ @ push usr_sp usr_lr
  75. sub sp, #8
  76. #endif
  77. #ifdef RT_USING_FPU
  78. /* fpu context */
  79. vmrs r6, fpexc
  80. tst r6, #(1<<30)
  81. beq 1f
  82. vstmdb sp!, {d0-d15}
  83. vstmdb sp!, {d16-d31}
  84. vmrs r5, fpscr
  85. stmfd sp!, {r5}
  86. 1:
  87. stmfd sp!, {r6}
  88. #endif
  89. str sp, [r0] @ store sp in preempted tasks TCB
  90. ldr sp, [r1] @ get new task stack pointer
  91. #ifdef RT_USING_SMP
  92. mov r0, r2
  93. bl rt_cpus_lock_status_restore
  94. bl rt_thread_self
  95. bl lwp_user_setting_restore
  96. #else
  97. bl rt_thread_self
  98. #ifdef RT_USING_USERSPACE
  99. mov r4, r0
  100. bl lwp_mmu_switch
  101. mov r0, r4
  102. #endif
  103. bl lwp_user_setting_restore
  104. #endif /*RT_USING_SMP*/
  105. b rt_hw_context_switch_exit
  106. /*
  107. * void rt_hw_context_switch_interrupt(rt_uint32 from, rt_uint32 to);
  108. */
  109. .equ Mode_USR, 0x10
  110. .equ Mode_FIQ, 0x11
  111. .equ Mode_IRQ, 0x12
  112. .equ Mode_SVC, 0x13
  113. .equ Mode_ABT, 0x17
  114. .equ Mode_UND, 0x1B
  115. .equ Mode_SYS, 0x1F
  116. .equ I_Bit, 0x80 @ when I bit is set, IRQ is disabled
  117. .equ F_Bit, 0x40 @ when F bit is set, FIQ is disabled
  118. .globl rt_thread_switch_interrupt_flag
  119. .globl rt_interrupt_from_thread
  120. .globl rt_interrupt_to_thread
  121. .globl rt_hw_context_switch_interrupt
  122. rt_hw_context_switch_interrupt:
  123. #ifdef RT_USING_SMP
  124. /* r0 :svc_mod context
  125. * r1 :addr of from_thread's sp
  126. * r2 :addr of to_thread's sp
  127. * r3 :to_thread's tcb
  128. */
  129. #ifdef RT_USING_LWP
  130. push {r0 - r3, lr}
  131. bl rt_thread_self
  132. bl lwp_user_setting_save
  133. pop {r0 - r3, lr}
  134. #endif
  135. str r0, [r1]
  136. ldr sp, [r2]
  137. mov r0, r3
  138. bl rt_cpus_lock_status_restore
  139. b rt_hw_context_switch_exit
  140. #else /*RT_USING_SMP*/
  141. /* r0 :addr of from_thread's sp
  142. * r1 :addr of to_thread's sp
  143. * r2 :from_thread's tcb
  144. * r3 :to_thread's tcb
  145. */
  146. #ifdef RT_USING_LWP
  147. /* now to_thread(r3) not used */
  148. ldr ip, =rt_thread_switch_interrupt_flag
  149. ldr r3, [ip]
  150. cmp r3, #1
  151. beq _reswitch
  152. ldr r3, =rt_interrupt_from_thread @ set rt_interrupt_from_thread
  153. str r0, [r3]
  154. mov r3, #1 @ set rt_thread_switch_interrupt_flag to 1
  155. str r3, [ip]
  156. push {r1, lr}
  157. mov r0, r2
  158. bl lwp_user_setting_save
  159. pop {r1, lr}
  160. _reswitch:
  161. ldr ip, =rt_interrupt_to_thread @ set rt_interrupt_to_thread
  162. str r1, [ip]
  163. bx lr
  164. #else
  165. /* now from_thread(r2) to_thread(r3) not used */
  166. ldr ip, =rt_thread_switch_interrupt_flag
  167. ldr r3, [ip]
  168. cmp r3, #1
  169. beq _reswitch
  170. ldr r3, =rt_interrupt_from_thread @ set rt_interrupt_from_thread
  171. str r0, [r3]
  172. mov r3, #1 @ set rt_thread_switch_interrupt_flag to 1
  173. str r3, [ip]
  174. _reswitch:
  175. ldr ip, =rt_interrupt_to_thread @ set rt_interrupt_to_thread
  176. str r1, [ip]
  177. bx lr
  178. #endif
  179. #endif /*RT_USING_SMP*/
  180. .global rt_hw_context_switch_exit
  181. rt_hw_context_switch_exit:
  182. #ifdef RT_USING_SMP
  183. #ifdef RT_USING_SIGNALS
  184. mov r0, sp
  185. cps #Mode_IRQ
  186. bl rt_signal_check
  187. cps #Mode_SVC
  188. mov sp, r0
  189. #endif
  190. #endif
  191. #ifdef RT_USING_FPU
  192. /* fpu context */
  193. ldmfd sp!, {r6}
  194. vmsr fpexc, r6
  195. tst r6, #(1<<30)
  196. beq 1f
  197. ldmfd sp!, {r5}
  198. vmsr fpscr, r5
  199. vldmia sp!, {d16-d31}
  200. vldmia sp!, {d0-d15}
  201. 1:
  202. #endif
  203. #ifdef RT_USING_LWP
  204. ldmfd sp, {r13, r14}^ /* usr_sp, usr_lr */
  205. add sp, #8
  206. #endif
  207. ldmfd sp!, {r1}
  208. msr spsr_cxsf, r1 /* original mode */
  209. #ifdef RT_USING_GDBSERVER
  210. bl lwp_check_debug
  211. #endif
  212. #ifdef RT_USING_LWP
  213. bl lwp_check_exit
  214. #endif
  215. #ifdef RT_USING_LWP
  216. and r1, #0x1f
  217. cmp r1, #0x10
  218. bne 1f
  219. ldmfd sp!, {r0-r12,lr}
  220. ldmfd sp!, {lr}
  221. b ret_to_user
  222. 1:
  223. #endif
  224. ldmfd sp!, {r0-r12,lr,pc}^ /* irq return */
  225. #ifdef RT_USING_FPU
  226. .global set_fpexc
  227. set_fpexc:
  228. vmsr fpexc, r0
  229. bx lr
  230. #endif