context_gcc.S 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. #endif /*RT_USING_SMP*/
  43. b rt_hw_context_switch_exit
  44. .section .bss.share.isr
  45. _guest_switch_lvl:
  46. .word 0
  47. .globl vmm_virq_update
  48. .section .text.isr, "ax"
  49. /*
  50. * void rt_hw_context_switch(rt_uint32 from, rt_uint32 to, struct rt_thread *to_thread);
  51. * r0 --> from (from_thread stack)
  52. * r1 --> to (to_thread stack)
  53. * r2 --> to_thread
  54. */
  55. .globl rt_hw_context_switch
  56. rt_hw_context_switch:
  57. stmfd sp!, {lr} @ push pc (lr should be pushed in place of PC)
  58. stmfd sp!, {r0-r12, lr} @ push lr & register file
  59. mrs r4, cpsr
  60. tst lr, #0x01
  61. orrne r4, r4, #0x20 @ it's thumb code
  62. stmfd sp!, {r4} @ push cpsr
  63. #ifdef RT_USING_LWP
  64. stmfd sp, {r13, r14}^ @ push usr_sp usr_lr
  65. sub sp, #8
  66. #endif
  67. str sp, [r0] @ store sp in preempted tasks TCB
  68. ldr sp, [r1] @ get new task stack pointer
  69. #ifdef RT_USING_SMP
  70. mov r0, r2
  71. bl rt_cpus_lock_status_restore
  72. #endif /*RT_USING_SMP*/
  73. b rt_hw_context_switch_exit
  74. /*
  75. * void rt_hw_context_switch_interrupt(rt_uint32 from, rt_uint32 to);
  76. */
  77. .equ Mode_USR, 0x10
  78. .equ Mode_FIQ, 0x11
  79. .equ Mode_IRQ, 0x12
  80. .equ Mode_SVC, 0x13
  81. .equ Mode_ABT, 0x17
  82. .equ Mode_UND, 0x1B
  83. .equ Mode_SYS, 0x1F
  84. .equ I_Bit, 0x80 @ when I bit is set, IRQ is disabled
  85. .equ F_Bit, 0x40 @ when F bit is set, FIQ is disabled
  86. .globl rt_thread_switch_interrupt_flag
  87. .globl rt_interrupt_from_thread
  88. .globl rt_interrupt_to_thread
  89. .globl rt_hw_context_switch_interrupt
  90. rt_hw_context_switch_interrupt:
  91. #ifdef RT_USING_SMP
  92. /* r0 :svc_mod context
  93. * r1 :addr of from_thread's sp
  94. * r2 :addr of to_thread's sp
  95. * r3 :to_thread's tcb
  96. */
  97. str r0, [r1]
  98. ldr sp, [r2]
  99. mov r0, r3
  100. bl rt_cpus_lock_status_restore
  101. b rt_hw_context_switch_exit
  102. #else /*RT_USING_SMP*/
  103. ldr r2, =rt_thread_switch_interrupt_flag
  104. ldr r3, [r2]
  105. cmp r3, #1
  106. beq _reswitch
  107. ldr ip, =rt_interrupt_from_thread @ set rt_interrupt_from_thread
  108. mov r3, #1 @ set rt_thread_switch_interrupt_flag to 1
  109. str r0, [ip]
  110. str r3, [r2]
  111. _reswitch:
  112. ldr r2, =rt_interrupt_to_thread @ set rt_interrupt_to_thread
  113. str r1, [r2]
  114. bx lr
  115. #endif /*RT_USING_SMP*/
  116. .global rt_hw_context_switch_exit
  117. rt_hw_context_switch_exit:
  118. #ifdef RT_USING_SMP
  119. #ifdef RT_USING_SIGNALS
  120. mov r0, sp
  121. cps #Mode_IRQ
  122. bl rt_signal_check
  123. cps #Mode_SVC
  124. mov sp, r0
  125. #endif
  126. #endif
  127. #ifdef RT_USING_LWP
  128. ldmfd sp, {r13, r14}^ /* usr_sp, usr_lr */
  129. add sp, #8
  130. #endif
  131. ldmfd sp!, {r1}
  132. msr spsr_cxsf, r1 /* original mode */
  133. ldmfd sp!, {r0-r12,lr,pc}^ /* irq return */