start_gcc.S 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * File : start_gcc.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. */
  24. .equ Mode_USR, 0x10
  25. .equ Mode_FIQ, 0x11
  26. .equ Mode_IRQ, 0x12
  27. .equ Mode_SVC, 0x13
  28. .equ Mode_ABT, 0x17
  29. .equ Mode_UND, 0x1B
  30. .equ Mode_SYS, 0x1F
  31. .equ I_Bit, 0x80 @ when I bit is set, IRQ is disabled
  32. .equ F_Bit, 0x40 @ when F bit is set, FIQ is disabled
  33. .equ UND_Stack_Size, 0x00000200
  34. .equ SVC_Stack_Size, 0x00000100
  35. .equ ABT_Stack_Size, 0x00000000
  36. .equ FIQ_Stack_Size, 0x00000000
  37. .equ IRQ_Stack_Size, 0x00000100
  38. .equ USR_Stack_Size, 0x00000100
  39. #define ISR_Stack_Size (UND_Stack_Size + SVC_Stack_Size + ABT_Stack_Size + \
  40. FIQ_Stack_Size + IRQ_Stack_Size)
  41. /* stack */
  42. .globl stack_start
  43. .globl stack_top
  44. stack_start:
  45. .rept ISR_Stack_Size
  46. .long 0
  47. .endr
  48. stack_top:
  49. /* reset entry */
  50. .globl _reset
  51. _reset:
  52. /* set the cpu to SVC32 mode and disable interrupt */
  53. mrs r0, cpsr
  54. bic r0, r0, #0x1f
  55. orr r0, r0, #0x13
  56. msr cpsr_c, r0
  57. /* setup stack */
  58. bl stack_setup
  59. /* clear .bss */
  60. mov r0,#0 /* get a zero */
  61. ldr r1,=__bss_start /* bss start */
  62. ldr r2,=__bss_end /* bss end */
  63. bss_loop:
  64. cmp r1,r2 /* check if data to clear */
  65. strlo r0,[r1],#4 /* clear 4 bytes */
  66. blo bss_loop /* loop until done */
  67. /* call C++ constructors of global objects */
  68. ldr r0, =__ctors_start__
  69. ldr r1, =__ctors_end__
  70. ctor_loop:
  71. cmp r0, r1
  72. beq ctor_end
  73. ldr r2, [r0], #4
  74. stmfd sp!, {r0-r1}
  75. mov lr, pc
  76. bx r2
  77. ldmfd sp!, {r0-r1}
  78. b ctor_loop
  79. ctor_end:
  80. /* start RT-Thread Kernel */
  81. ldr pc, _rtthread_startup
  82. _rtthread_startup:
  83. .word rtthread_startup
  84. stack_setup:
  85. ldr r0, =stack_top
  86. @ Enter Undefined Instruction Mode and set its Stack Pointer
  87. msr cpsr_c, #Mode_UND|I_Bit|F_Bit
  88. mov sp, r0
  89. sub r0, r0, #UND_Stack_Size
  90. @ Enter Abort Mode and set its Stack Pointer
  91. msr cpsr_c, #Mode_ABT|I_Bit|F_Bit
  92. mov sp, r0
  93. sub r0, r0, #ABT_Stack_Size
  94. @ Enter FIQ Mode and set its Stack Pointer
  95. msr cpsr_c, #Mode_FIQ|I_Bit|F_Bit
  96. mov sp, r0
  97. sub r0, r0, #FIQ_Stack_Size
  98. @ Enter IRQ Mode and set its Stack Pointer
  99. msr cpsr_c, #Mode_IRQ|I_Bit|F_Bit
  100. mov sp, r0
  101. sub r0, r0, #IRQ_Stack_Size
  102. @ Enter Supervisor Mode and set its Stack Pointer
  103. msr cpsr_c, #Mode_SVC|I_Bit|F_Bit
  104. mov sp, r0
  105. sub r0, r0, #SVC_Stack_Size
  106. @ Enter User Mode and set its Stack Pointer
  107. mov sp, r0
  108. sub sl, sp, #USR_Stack_Size
  109. bx lr
  110. /* exception handlers: undef, swi, padt, dabt, resv, irq, fiq */
  111. .align 5
  112. .globl vector_undef
  113. vector_undef:
  114. sub sp, sp, #72
  115. stmia sp, {r0 - r12} @/* Calling r0-r12 */
  116. add r8, sp, #60
  117. mrs r1, cpsr
  118. mrs r2, spsr
  119. orr r2,r2, #I_Bit|F_Bit
  120. msr cpsr_c, r2
  121. mov r0, r0
  122. stmdb r8, {sp, lr} @/* Calling SP, LR */
  123. msr cpsr_c, r1 @/* return to Undefined Instruction mode */
  124. str lr, [r8, #0] @/* Save calling PC */
  125. mrs r6, spsr
  126. str r6, [r8, #4] @/* Save CPSR */
  127. str r0, [r8, #8] @/* Save OLD_R0 */
  128. mov r0, sp
  129. bl rt_hw_trap_udef
  130. ldmia sp, {r0 - r12} @/* Calling r0 - r2 */
  131. mov r0, r0
  132. ldr lr, [sp, #60] @/* Get PC */
  133. add sp, sp, #72
  134. movs pc, lr @/* return & move spsr_svc into cpsr */
  135. .align 5
  136. .globl vector_swi
  137. vector_swi:
  138. bl rt_hw_trap_swi
  139. .align 5
  140. .globl vector_pabt
  141. vector_pabt:
  142. bl rt_hw_trap_pabt
  143. .align 5
  144. .globl vector_dabt
  145. vector_dabt:
  146. sub sp, sp, #72
  147. stmia sp, {r0 - r12} @/* Calling r0-r12 */
  148. add r8, sp, #60
  149. stmdb r8, {sp, lr} @/* Calling SP, LR */
  150. str lr, [r8, #0] @/* Save calling PC */
  151. mrs r6, spsr
  152. str r6, [r8, #4] @/* Save CPSR */
  153. str r0, [r8, #8] @/* Save OLD_R0 */
  154. mov r0, sp
  155. bl rt_hw_trap_dabt
  156. ldmia sp, {r0 - r12} @/* Calling r0 - r2 */
  157. mov r0, r0
  158. ldr lr, [sp, #60] @/* Get PC */
  159. add sp, sp, #72
  160. movs pc, lr @/* return & move spsr_svc into cpsr */
  161. .align 5
  162. .globl vector_resv
  163. vector_resv:
  164. b .
  165. .align 5
  166. .globl vector_fiq
  167. vector_fiq:
  168. stmfd sp!,{r0-r7,lr}
  169. bl rt_hw_trap_fiq
  170. ldmfd sp!,{r0-r7,lr}
  171. subs pc,lr,#4
  172. .globl rt_interrupt_enter
  173. .globl rt_interrupt_leave
  174. .globl rt_thread_switch_interrupt_flag
  175. .globl rt_interrupt_from_thread
  176. .globl rt_interrupt_to_thread
  177. .globl rt_current_thread
  178. .globl vmm_thread
  179. .globl vmm_virq_check
  180. .globl vector_irq
  181. vector_irq:
  182. stmfd sp!, {r0-r12,lr}
  183. bl rt_interrupt_enter
  184. bl rt_hw_trap_irq
  185. bl rt_interrupt_leave
  186. @ if rt_thread_switch_interrupt_flag set, jump to
  187. @ rt_hw_context_switch_interrupt_do and don't return
  188. ldr r0, =rt_thread_switch_interrupt_flag
  189. ldr r1, [r0]
  190. cmp r1, #1
  191. beq rt_hw_context_switch_interrupt_do
  192. ldmfd sp!, {r0-r12,lr}
  193. subs pc, lr, #4
  194. rt_hw_context_switch_interrupt_do:
  195. mov r1, #0 @ clear flag
  196. str r1, [r0]
  197. ldmfd sp!, {r0-r12,lr}@ reload saved registers
  198. stmfd sp, {r0-r2} @ save r0-r2
  199. mrs r0, spsr @ get cpsr of interrupt thread
  200. sub r1, sp, #4*3
  201. sub r2, lr, #4 @ save old task's pc to r2
  202. @ switch to SVC mode with no interrupt
  203. msr cpsr_c, #I_Bit|F_Bit|Mode_SVC
  204. stmfd sp!, {r2} @ push old task's pc
  205. stmfd sp!, {r3-r12,lr}@ push old task's lr,r12-r4
  206. ldmfd r1, {r1-r3} @ restore r0-r2 of the interrupt thread
  207. stmfd sp!, {r1-r3} @ push old task's r0-r2
  208. stmfd sp!, {r0} @ push old task's cpsr
  209. ldr r4, =rt_interrupt_from_thread
  210. ldr r5, [r4]
  211. str sp, [r5] @ store sp in preempted tasks's TCB
  212. ldr r6, =rt_interrupt_to_thread
  213. ldr r6, [r6]
  214. ldr sp, [r6] @ get new task's stack pointer
  215. ldmfd sp!, {r4} @ pop new task's cpsr to spsr
  216. msr spsr_cxsf, r4
  217. ldmfd sp!, {r0-r12,lr,pc}^ @ pop new task's r0-r12,lr & pc, copy spsr to cpsr