context_gcc.S 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * File : context_gcc.S
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2009, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2009-10-11 Bernard first version
  13. * 2012-01-01 aozima support context switch load/store FPU register.
  14. * 2013-06-18 aozima add restore MSP feature.
  15. * 2013-06-23 aozima support lazy stack optimized.
  16. */
  17. /**
  18. * @addtogroup cortex-m4
  19. */
  20. /*@{*/
  21. .cpu cortex-m4
  22. .syntax unified
  23. .thumb
  24. .text
  25. .equ SCB_VTOR, 0xE000ED08 /* Vector Table Offset Register */
  26. .equ NVIC_INT_CTRL, 0xE000ED04 /* interrupt control state register */
  27. .equ NVIC_SYSPRI2, 0xE000ED20 /* system priority register (2) */
  28. .equ NVIC_PENDSV_PRI, 0x00FF0000 /* PendSV priority value (lowest) */
  29. .equ NVIC_PENDSVSET, 0x10000000 /* value to trigger PendSV exception */
  30. /*
  31. * rt_base_t rt_hw_interrupt_disable();
  32. */
  33. .global rt_hw_interrupt_disable
  34. .type rt_hw_interrupt_disable, %function
  35. rt_hw_interrupt_disable:
  36. MRS r0, PRIMASK
  37. CPSID I
  38. BX LR
  39. /*
  40. * void rt_hw_interrupt_enable(rt_base_t level);
  41. */
  42. .global rt_hw_interrupt_enable
  43. .type rt_hw_interrupt_enable, %function
  44. rt_hw_interrupt_enable:
  45. MSR PRIMASK, r0
  46. BX LR
  47. /*
  48. * void rt_hw_context_switch(rt_uint32 from, rt_uint32 to);
  49. * r0 --> from
  50. * r1 --> to
  51. */
  52. .global rt_hw_context_switch_interrupt
  53. .type rt_hw_context_switch_interrupt, %function
  54. .global rt_hw_context_switch
  55. .type rt_hw_context_switch, %function
  56. rt_hw_context_switch_interrupt:
  57. rt_hw_context_switch:
  58. /* set rt_thread_switch_interrupt_flag to 1 */
  59. LDR r2, =rt_thread_switch_interrupt_flag
  60. LDR r3, [r2]
  61. CMP r3, #1
  62. BEQ _reswitch
  63. MOV r3, #1
  64. STR r3, [r2]
  65. LDR r2, =rt_interrupt_from_thread /* set rt_interrupt_from_thread */
  66. STR r0, [r2]
  67. _reswitch:
  68. LDR r2, =rt_interrupt_to_thread /* set rt_interrupt_to_thread */
  69. STR r1, [r2]
  70. LDR r0, =NVIC_INT_CTRL /* trigger the PendSV exception (causes context switch) */
  71. LDR r1, =NVIC_PENDSVSET
  72. STR r1, [r0]
  73. BX LR
  74. /* r0 --> switch from thread stack
  75. * r1 --> switch to thread stack
  76. * psr, pc, lr, r12, r3, r2, r1, r0 are pushed into [from] stack
  77. */
  78. .global PendSV_Handler
  79. .type PendSV_Handler, %function
  80. PendSV_Handler:
  81. /* disable interrupt to protect context switch */
  82. MRS r2, PRIMASK
  83. CPSID I
  84. /* get rt_thread_switch_interrupt_flag */
  85. LDR r0, =rt_thread_switch_interrupt_flag
  86. LDR r1, [r0]
  87. CBZ r1, pendsv_exit /* pendsv already handled */
  88. /* clear rt_thread_switch_interrupt_flag to 0 */
  89. MOV r1, #0x00
  90. STR r1, [r0]
  91. LDR r0, =rt_interrupt_from_thread
  92. LDR r1, [r0]
  93. CBZ r1, switch_to_thread /* skip register save at the first time */
  94. MRS r1, psp /* get from thread stack pointer */
  95. #if defined (__VFP_FP__) && !defined(__SOFTFP__)
  96. TST lr, #0x10 /* if(!EXC_RETURN[4]) */
  97. VSTMDBEQ r1!, {d8 - d15} /* push FPU register s16~s31 */
  98. #endif
  99. STMFD r1!, {r4 - r11} /* push r4 - r11 register */
  100. #if defined (__VFP_FP__) && !defined(__SOFTFP__)
  101. MOV r4, #0x00 /* flag = 0 */
  102. TST lr, #0x10 /* if(!EXC_RETURN[4]) */
  103. MOVEQ r4, #0x01 /* flag = 1 */
  104. STMFD r1!, {r4} /* push flag */
  105. #endif
  106. LDR r0, [r0]
  107. STR r1, [r0] /* update from thread stack pointer */
  108. switch_to_thread:
  109. LDR r1, =rt_interrupt_to_thread
  110. LDR r1, [r1]
  111. LDR r1, [r1] /* load thread stack pointer */
  112. #if defined (__VFP_FP__) && !defined(__SOFTFP__)
  113. LDMFD r1!, {r3} /* pop flag */
  114. #endif
  115. LDMFD r1!, {r4 - r11} /* pop r4 - r11 register */
  116. #if defined (__VFP_FP__) && !defined(__SOFTFP__)
  117. CMP r3, #0 /* if(flag_r3 != 0) */
  118. VLDMIANE r1!, {d8 - d15} /* pop FPU register s16~s31 */
  119. #endif
  120. MSR psp, r1 /* update stack pointer */
  121. pendsv_exit:
  122. /* restore interrupt */
  123. MSR PRIMASK, r2
  124. #if defined (__VFP_FP__) && !defined(__SOFTFP__)
  125. ORR lr, lr, #0x10 /* lr |= (1 << 4), clean FPCA. */
  126. CMP r3, #0 /* if(flag_r3 != 0) */
  127. BICNE lr, lr, #0x10 /* lr &= ~(1 << 4), set FPCA. */
  128. #endif
  129. ORR lr, lr, #0x04
  130. BX lr
  131. /*
  132. * void rt_hw_context_switch_to(rt_uint32 to);
  133. * r0 --> to
  134. */
  135. .global rt_hw_context_switch_to
  136. .type rt_hw_context_switch_to, %function
  137. rt_hw_context_switch_to:
  138. LDR r1, =rt_interrupt_to_thread
  139. STR r0, [r1]
  140. #if defined (__VFP_FP__) && !defined(__SOFTFP__)
  141. /* CLEAR CONTROL.FPCA */
  142. MRS r2, CONTROL /* read */
  143. BIC r2, #0x04 /* modify */
  144. MSR CONTROL, r2 /* write-back */
  145. #endif
  146. /* set from thread to 0 */
  147. LDR r1, =rt_interrupt_from_thread
  148. MOV r0, #0x0
  149. STR r0, [r1]
  150. /* set interrupt flag to 1 */
  151. LDR r1, =rt_thread_switch_interrupt_flag
  152. MOV r0, #1
  153. STR r0, [r1]
  154. /* set the PendSV exception priority */
  155. LDR r0, =NVIC_SYSPRI2
  156. LDR r1, =NVIC_PENDSV_PRI
  157. LDR.W r2, [r0,#0x00] /* read */
  158. ORR r1,r1,r2 /* modify */
  159. STR r1, [r0] /* write-back */
  160. LDR r0, =NVIC_INT_CTRL /* trigger the PendSV exception (causes context switch) */
  161. LDR r1, =NVIC_PENDSVSET
  162. STR r1, [r0]
  163. /* restore MSP */
  164. LDR r0, =SCB_VTOR
  165. LDR r0, [r0]
  166. LDR r0, [r0]
  167. NOP
  168. MSR msp, r0
  169. /* enable interrupts at processor level */
  170. CPSIE F
  171. CPSIE I
  172. /* never reach here! */
  173. /* compatible with old version */
  174. .global rt_hw_interrupt_thread_switch
  175. .type rt_hw_interrupt_thread_switch, %function
  176. rt_hw_interrupt_thread_switch:
  177. BX lr
  178. NOP
  179. .global HardFault_Handler
  180. .type HardFault_Handler, %function
  181. HardFault_Handler:
  182. /* get current context */
  183. MRS r0, psp /* get fault thread stack pointer */
  184. PUSH {lr}
  185. BL rt_hw_hard_fault_exception
  186. POP {lr}
  187. ORR lr, lr, #0x04
  188. BX lr