context_gcc.S 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. ldmfd sp!, {r4} @ pop new task spsr
  60. msr spsr_cxsf, r4
  61. ldmfd sp!, {r0-r12, lr, pc}^ @ pop new task r0-r12, lr & pc
  62. .section .bss.share.isr
  63. _guest_switch_lvl:
  64. .word 0
  65. .section .text.isr, "ax"
  66. /*
  67. * void rt_hw_context_switch(rt_uint32 from, rt_uint32 to);
  68. * r0 --> from
  69. * r1 --> to
  70. */
  71. .globl rt_hw_context_switch
  72. rt_hw_context_switch:
  73. stmfd sp!, {lr} @ push pc (lr should be pushed in place of PC)
  74. stmfd sp!, {r0-r12, lr} @ push lr & register file
  75. mrs r4, cpsr
  76. tst lr, #0x01
  77. orrne r4, r4, #0x20 @ it's thumb code
  78. stmfd sp!, {r4} @ push cpsr
  79. str sp, [r0] @ store sp in preempted tasks TCB
  80. ldr sp, [r1] @ get new task stack pointer
  81. #ifdef RT_USING_SMP
  82. mov r0, r2
  83. bl rt_cpus_lock_status_restore
  84. #endif /*RT_USING_SMP*/
  85. ldmfd sp!, {r4} @ pop new task cpsr to spsr
  86. msr spsr_cxsf, r4
  87. ldmfd sp!, {r0-r12, lr, pc}^ @ pop new task r0-r12, lr & pc, copy spsr to cpsr
  88. /*
  89. * void rt_hw_context_switch_interrupt(rt_uint32 from, rt_uint32 to);
  90. */
  91. .equ Mode_USR, 0x10
  92. .equ Mode_FIQ, 0x11
  93. .equ Mode_IRQ, 0x12
  94. .equ Mode_SVC, 0x13
  95. .equ Mode_ABT, 0x17
  96. .equ Mode_UND, 0x1B
  97. .equ Mode_SYS, 0x1F
  98. .equ I_Bit, 0x80 @ when I bit is set, IRQ is disabled
  99. .equ F_Bit, 0x40 @ when F bit is set, FIQ is disabled
  100. .globl rt_thread_switch_interrupt_flag
  101. .globl rt_interrupt_from_thread
  102. .globl rt_interrupt_to_thread
  103. .globl rt_hw_context_switch_interrupt
  104. rt_hw_context_switch_interrupt:
  105. #ifdef RT_USING_SMP
  106. /* r0 :irq_mod context
  107. * r1 :addr of from_thread's sp
  108. * r2 :addr of to_thread's sp
  109. * r3 :to_thread's tcb
  110. */
  111. @ r0 point to {r0-r3} in stack
  112. push {r1 - r3}
  113. mov r1, r0
  114. add r0, r0, #4*4
  115. ldmfd r0!, {r4-r12,lr}@ reload saved registers
  116. mrs r3, spsr @ get cpsr of interrupt thread
  117. sub r2, lr, #4 @ save old task's pc to r2
  118. msr cpsr_c, #I_Bit|F_Bit|Mode_SVC
  119. stmfd sp!, {r2} @ push old task's pc
  120. stmfd sp!, {r4-r12,lr}@ push old task's lr,r12-r4
  121. ldmfd r1, {r4-r7} @ restore r0-r3 of the interrupt thread
  122. stmfd sp!, {r4-r7} @ push old task's r0-r3
  123. stmfd sp!, {r3} @ push old task's cpsr
  124. msr cpsr_c, #I_Bit|F_Bit|Mode_IRQ
  125. pop {r1 - r3}
  126. mov sp, r0
  127. msr cpsr_c, #I_Bit|F_Bit|Mode_SVC
  128. str sp, [r1]
  129. ldr sp, [r2]
  130. mov r0, r3
  131. bl rt_cpus_lock_status_restore
  132. ldmfd sp!, {r4} @ pop new task's cpsr to spsr
  133. msr spsr_cxsf, r4
  134. ldmfd sp!, {r0-r12,lr,pc}^ @ pop new task's r0-r12,lr & pc, copy spsr to cpsr
  135. #else /*RT_USING_SMP*/
  136. ldr r2, =rt_thread_switch_interrupt_flag
  137. ldr r3, [r2]
  138. cmp r3, #1
  139. beq _reswitch
  140. ldr ip, =rt_interrupt_from_thread @ set rt_interrupt_from_thread
  141. mov r3, #1 @ set rt_thread_switch_interrupt_flag to 1
  142. str r0, [ip]
  143. str r3, [r2]
  144. _reswitch:
  145. ldr r2, =rt_interrupt_to_thread @ set rt_interrupt_to_thread
  146. str r1, [r2]
  147. bx lr
  148. #endif /*RT_USING_SMP*/