context_gcc.S 7.0 KB

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