context_gcc.S 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * File : context.S
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2013, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2013-07-05 Bernard the first version
  23. * 2018-11-22 Jesven in the smp version, using macro to
  24. * define rt_hw_interrupt_enable and rt_hw_interrupt_disable
  25. * rt_hw_context_switch_interrupt switches to the new thread directly
  26. */
  27. #include "rtconfig.h"
  28. .section .text, "ax"
  29. #ifdef RT_USING_SMP
  30. #define rt_hw_interrupt_disable rt_hw_local_irq_disable
  31. #define rt_hw_interrupt_enable rt_hw_local_irq_enable
  32. #endif
  33. /*
  34. * rt_base_t rt_hw_interrupt_disable();
  35. */
  36. .globl rt_hw_interrupt_disable
  37. rt_hw_interrupt_disable:
  38. mrs r0, cpsr
  39. cpsid i
  40. bx lr
  41. /*
  42. * void rt_hw_interrupt_enable(rt_base_t level);
  43. */
  44. .globl rt_hw_interrupt_enable
  45. rt_hw_interrupt_enable:
  46. msr cpsr, r0
  47. bx lr
  48. /*
  49. * void rt_hw_context_switch_to(rt_uint32 to);
  50. * r0 --> to
  51. */
  52. .globl rt_hw_context_switch_to
  53. rt_hw_context_switch_to:
  54. ldr sp, [r0] @ get new task stack pointer
  55. #ifdef RT_USING_SMP
  56. mov r0, r1
  57. bl rt_cpus_lock_status_restore
  58. #endif /*RT_USING_SMP*/
  59. #ifdef RT_USING_LWP
  60. ldmfd sp, {r13, r14}^ @ pop usr_sp usr_lr
  61. add sp, #8
  62. #endif
  63. ldmfd sp!, {r4} @ pop new task spsr
  64. msr spsr_cxsf, r4
  65. ldmfd sp!, {r0-r12, lr, pc}^ @ pop new task r0-r12, lr & pc
  66. .section .bss.share.isr
  67. _guest_switch_lvl:
  68. .word 0
  69. .section .text.isr, "ax"
  70. /*
  71. * void rt_hw_context_switch(rt_uint32 from, rt_uint32 to);
  72. * r0 --> from
  73. * r1 --> to
  74. */
  75. .globl rt_hw_context_switch
  76. rt_hw_context_switch:
  77. stmfd sp!, {lr} @ push pc (lr should be pushed in place of PC)
  78. stmfd sp!, {r0-r12, lr} @ push lr & register file
  79. mrs r4, cpsr
  80. tst lr, #0x01
  81. orrne r4, r4, #0x20 @ it's thumb code
  82. stmfd sp!, {r4} @ push cpsr
  83. #ifdef RT_USING_LWP
  84. stmfd sp, {r13, r14}^ @ push usr_sp usr_lr
  85. sub sp, #8
  86. #endif
  87. str sp, [r0] @ store sp in preempted tasks TCB
  88. ldr sp, [r1] @ get new task stack pointer
  89. #ifdef RT_USING_SMP
  90. mov r0, r2
  91. bl rt_cpus_lock_status_restore
  92. #endif /*RT_USING_SMP*/
  93. #ifdef RT_USING_LWP
  94. ldmfd sp, {r13, r14}^ @ pop usr_sp usr_lr
  95. add sp, #8
  96. #endif
  97. ldmfd sp!, {r4} @ pop new task cpsr to spsr
  98. msr spsr_cxsf, r4
  99. ldmfd sp!, {r0-r12, lr, pc}^ @ pop new task r0-r12, lr & pc, copy spsr to cpsr
  100. /*
  101. * void rt_hw_context_switch_interrupt(rt_uint32 from, rt_uint32 to);
  102. */
  103. .equ Mode_USR, 0x10
  104. .equ Mode_FIQ, 0x11
  105. .equ Mode_IRQ, 0x12
  106. .equ Mode_SVC, 0x13
  107. .equ Mode_ABT, 0x17
  108. .equ Mode_UND, 0x1B
  109. .equ Mode_SYS, 0x1F
  110. .equ I_Bit, 0x80 @ when I bit is set, IRQ is disabled
  111. .equ F_Bit, 0x40 @ when F bit is set, FIQ is disabled
  112. .globl rt_thread_switch_interrupt_flag
  113. .globl rt_interrupt_from_thread
  114. .globl rt_interrupt_to_thread
  115. .globl rt_hw_context_switch_interrupt
  116. rt_hw_context_switch_interrupt:
  117. #ifdef RT_USING_SMP
  118. /* r0 :irq_mod context
  119. * r1 :addr of from_thread's sp
  120. * r2 :addr of to_thread's sp
  121. * r3 :to_thread's tcb
  122. */
  123. @ r0 point to {r0-r3} in stack
  124. push {r1 - r3}
  125. mov r1, r0
  126. add r0, r0, #4*4
  127. ldmfd r0!, {r4-r12,lr}@ reload saved registers
  128. mrs r3, spsr @ get cpsr of interrupt thread
  129. sub r2, lr, #4 @ save old task's pc to r2
  130. msr cpsr_c, #I_Bit|F_Bit|Mode_SVC
  131. stmfd sp!, {r2} @ push old task's pc
  132. stmfd sp!, {r4-r12,lr}@ push old task's lr,r12-r4
  133. ldmfd r1, {r4-r7} @ restore r0-r3 of the interrupt thread
  134. stmfd sp!, {r4-r7} @ push old task's r0-r3
  135. stmfd sp!, {r3} @ push old task's cpsr
  136. #ifdef RT_USING_LWP
  137. stmfd sp, {r13,r14}^ @push usr_sp usr_lr
  138. sub sp, #8
  139. #endif
  140. msr cpsr_c, #I_Bit|F_Bit|Mode_IRQ
  141. pop {r1 - r3}
  142. mov sp, r0
  143. msr cpsr_c, #I_Bit|F_Bit|Mode_SVC
  144. str sp, [r1]
  145. ldr sp, [r2]
  146. mov r0, r3
  147. bl rt_cpus_lock_status_restore
  148. #ifdef RT_USING_LWP
  149. ldmfd sp, {r13,r14}^ @pop usr_sp usr_lr
  150. add sp, #8
  151. #endif
  152. ldmfd sp!, {r4} @ pop new task's cpsr to spsr
  153. msr spsr_cxsf, r4
  154. ldmfd sp!, {r0-r12,lr,pc}^ @ pop new task's r0-r12,lr & pc, copy spsr to cpsr
  155. #else /*RT_USING_SMP*/
  156. ldr r2, =rt_thread_switch_interrupt_flag
  157. ldr r3, [r2]
  158. cmp r3, #1
  159. beq _reswitch
  160. ldr ip, =rt_interrupt_from_thread @ set rt_interrupt_from_thread
  161. mov r3, #1 @ set rt_thread_switch_interrupt_flag to 1
  162. str r0, [ip]
  163. str r3, [r2]
  164. _reswitch:
  165. ldr r2, =rt_interrupt_to_thread @ set rt_interrupt_to_thread
  166. str r1, [r2]
  167. bx lr
  168. #endif /*RT_USING_SMP*/