start_gcc.S 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * File : start_gcc.S
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2015, 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. * 2011-01-13 weety first version
  23. * 2015-04-15 ArdaFu Split from AT91SAM9260 BSP
  24. * 2015-04-21 ArdaFu Remove remap code. Using mmu to map vector table
  25. */
  26. #define S_FRAME_SIZE (18*4) //72
  27. @#define S_SPSR (17*4) //SPSR
  28. @#define S_CPSR (16*4) //CPSR
  29. #define S_PC (15*4) //R15
  30. @#define S_LR (14*4) //R14
  31. @#define S_SP (13*4) //R13
  32. @#define S_IP (12*4) //R12
  33. @#define S_FP (11*4) //R11
  34. @#define S_R10 (10*4)
  35. @#define S_R9 (9*4)
  36. @#define S_R8 (8*4)
  37. @#define S_R7 (7*4)
  38. @#define S_R6 (6*4)
  39. @#define S_R5 (5*4)
  40. @#define S_R4 (4*4)
  41. @#define S_R3 (3*4)
  42. @#define S_R2 (2*4)
  43. @#define S_R1 (1*4)
  44. @#define S_R0 (0*4)
  45. #define MODE_SYS 0x1F
  46. #define MODE_FIQ 0x11
  47. #define MODE_IRQ 0x12
  48. #define MODE_SVC 0x13
  49. #define MODE_ABT 0x17
  50. #define MODE_UND 0x1B
  51. #define MODEMASK 0x1F
  52. #define NOINT 0xC0
  53. .include "rt_low_level_gcc.inc"
  54. @;----------------------- Stack and Heap Definitions ---------------------------
  55. .section .nobss, "w"
  56. .space UND_STK_SIZE
  57. .global UND_STACK_START
  58. UND_STACK_START:
  59. .space ABT_STK_SIZE
  60. .align 2
  61. .global ABT_STACK_START
  62. ABT_STACK_START:
  63. .space FIQ_STK_SIZE
  64. .align 2
  65. .global FIQ_STACK_START
  66. FIQ_STACK_START:
  67. .space IRQ_STK_SIZE
  68. .align 2
  69. .global IRQ_STACK_START
  70. IRQ_STACK_START:
  71. .skip SYS_STK_SIZE
  72. .align 2
  73. .global SYS_STACK_START
  74. SYS_STACK_START:
  75. .space SVC_STK_SIZE
  76. .align 2
  77. .global SVC_STACK_START
  78. SVC_STACK_START:
  79. @;--------------Jump vector table-----------------------------------------------
  80. .section .init, "ax"
  81. .arm
  82. .global entry
  83. entry:
  84. LDR PC, vector_reset
  85. LDR PC, vector_undef
  86. LDR PC, vector_swi
  87. LDR PC, vector_pabt
  88. LDR PC, vector_dabt
  89. LDR PC, vector_resv
  90. LDR PC, vector_irq
  91. LDR PC, vector_fiq
  92. vector_reset:
  93. .word Reset_Handler
  94. vector_undef:
  95. .word Undef_Handler
  96. vector_swi:
  97. .word SWI_Handler
  98. vector_pabt:
  99. .word PAbt_Handler
  100. vector_dabt:
  101. .word DAbt_Handler
  102. vector_resv:
  103. .word Resv_Handler
  104. vector_irq:
  105. .word IRQ_Handler
  106. vector_fiq:
  107. .word FIQ_Handler
  108. .balignl 16,0xdeadbeef
  109. @;----------------- Reset Handler ---------------------------------------------
  110. .global rt_low_level_init
  111. .global main
  112. .global Reset_Handler
  113. Reset_Handler:
  114. @; Set the cpu to SVC32 mode
  115. MRS R0, CPSR
  116. BIC R0, R0, #MODEMASK
  117. ORR R0, R0, #MODE_SVC|NOINT
  118. MSR CPSR_cxsf, R0
  119. @; Set CO-Processor
  120. @; little-end,disbale I/D Cache MMU, vector table is 0x00000000
  121. MRC P15, 0, R0, C1, C0, 0 @; Read CP15
  122. LDR R1, =0x00003085 @; set clear bits
  123. BIC R0, R0, R1
  124. MCR P15, 0, R0, C1, C0, 0 @; Write CP15
  125. @; Call low level init function,
  126. @; disable and clear all IRQs, Init MMU, Init interrupt controller, etc.
  127. LDR SP, =SVC_STACK_START
  128. LDR R0, =rt_low_level_init
  129. BLX R0
  130. Setup_Stack:
  131. @; Setup Stack for each mode
  132. MRS R0, CPSR
  133. BIC R0, R0, #MODEMASK
  134. ORR R1, R0, #MODE_UND|NOINT
  135. MSR CPSR_cxsf, R1 @; Undef mode
  136. LDR SP, =UND_STACK_START
  137. ORR R1, R0, #MODE_ABT|NOINT
  138. MSR CPSR_cxsf, R1 @; Abort mode
  139. LDR SP, =ABT_STACK_START
  140. ORR R1, R0, #MODE_IRQ|NOINT
  141. MSR CPSR_cxsf, R1 @; IRQ mode
  142. LDR SP, =IRQ_STACK_START
  143. ORR R1, R0, #MODE_FIQ|NOINT
  144. MSR CPSR_cxsf, R1 @; FIQ mode
  145. LDR SP, =FIQ_STACK_START
  146. ORR R1, R0, #MODE_SYS|NOINT
  147. MSR CPSR_cxsf,R1 @; SYS/User mode
  148. LDR SP, =SYS_STACK_START
  149. ORR R1, R0, #MODE_SVC|NOINT
  150. MSR CPSR_cxsf, R1 @; SVC mode
  151. LDR SP, =SVC_STACK_START
  152. @; clear .bss
  153. MOV R0, #0 @; get a zero
  154. LDR R1, =__bss_start__ @; bss start
  155. LDR R2, =__bss_end__ @; bss end
  156. bss_clear_loop:
  157. CMP R1, R2 @; check if data to clear
  158. STRLO R0, [R1], #4 @; clear 4 bytes
  159. BLO bss_clear_loop @; loop until done
  160. @; call C++ constructors of global objects
  161. LDR R0, =__ctors_start__
  162. LDR R1, =__ctors_end__
  163. ctor_loop:
  164. CMP R0, R1
  165. BEQ ctor_end
  166. LDR R2, [R0], #4
  167. STMFD SP!, {R0-R1}
  168. MOV LR, PC
  169. BX R2
  170. LDMFD SP!, {R0-R1}
  171. B ctor_loop
  172. ctor_end:
  173. @; Enter the C code
  174. LDR R0, =main
  175. BLX R0
  176. @;----------------- Exception Handler -----------------------------------------
  177. .global rt_hw_trap_udef
  178. .global rt_hw_trap_swi
  179. .global rt_hw_trap_pabt
  180. .global rt_hw_trap_dabt
  181. .global rt_hw_trap_resv
  182. .global rt_hw_trap_irq
  183. .global rt_hw_trap_fiq
  184. .global rt_interrupt_enter
  185. .global rt_interrupt_leave
  186. .global rt_thread_switch_interrupt_flag
  187. .global rt_interrupt_from_thread
  188. .global rt_interrupt_to_thread
  189. .align 5
  190. Undef_Handler:
  191. SUB SP, SP, #S_FRAME_SIZE
  192. STMIA SP, {R0 - R12} @; Calling R0-R12
  193. ADD R8, SP, #S_PC
  194. STMDB R8, {SP, LR} @; Calling SP, LR
  195. STR LR, [R8, #0] @; Save calling PC
  196. MRS R6, SPSR
  197. STR R6, [R8, #4] @; Save CPSR
  198. STR R0, [R8, #8] @; Save SPSR
  199. MOV R0, SP
  200. BL rt_hw_trap_udef
  201. .align 5
  202. SWI_Handler:
  203. BL rt_hw_trap_swi
  204. .align 5
  205. PAbt_Handler:
  206. BL rt_hw_trap_pabt
  207. .align 5
  208. DAbt_Handler:
  209. SUB SP, SP, #S_FRAME_SIZE
  210. STMIA SP, {R0 - R12} @; Calling R0-R12
  211. ADD R8, SP, #S_PC
  212. STMDB R8, {SP, LR} @; Calling SP, LR
  213. STR LR, [R8, #0] @; Save calling PC
  214. MRS R6, SPSR
  215. STR R6, [R8, #4] @; Save CPSR
  216. STR R0, [R8, #8] @; Save SPSR
  217. MOV R0, SP
  218. BL rt_hw_trap_dabt
  219. .align 5
  220. Resv_Handler:
  221. BL rt_hw_trap_resv
  222. .align 5
  223. FIQ_Handler:
  224. STMFD SP!, {R0-R7,LR}
  225. BL rt_hw_trap_fiq
  226. LDMFD SP!, {R0-R7,LR}
  227. SUBS PC, LR, #4
  228. .align 5
  229. IRQ_Handler:
  230. STMFD SP!, {R0-R12,LR}
  231. BL rt_interrupt_enter
  232. BL rt_hw_trap_irq
  233. BL rt_interrupt_leave
  234. @; If rt_thread_switch_interrupt_flag set,
  235. @; jump to rt_hw_context_switch_interrupt_do and don't return
  236. LDR R0, =rt_thread_switch_interrupt_flag
  237. LDR R1, [R0]
  238. CMP R1, #1
  239. BEQ rt_hw_context_switch_interrupt_do
  240. LDMFD SP!, {R0-R12,LR}
  241. SUBS PC, LR, #4
  242. @;------ void rt_hw_context_switch_interrupt_do(rt_base_t flag) -----------------
  243. rt_hw_context_switch_interrupt_do:
  244. MOV R1, #0 @; Clear flag
  245. STR R1, [R0] @; Save to flag variable
  246. LDMFD SP!, {R0-R12,LR} @; Reload saved registers
  247. STMFD SP, {R0-R2} @; Save R0-R2
  248. SUB R1, SP, #4*3 @; Save old task's SP to R1
  249. SUB R2, LR, #4 @; Save old task's PC to R2
  250. MRS R0, SPSR @; Get CPSR of interrupt thread
  251. MSR CPSR_c, #MODE_SVC|NOINT @; Switch to SVC mode and no interrupt
  252. STMFD SP!, {R2} @; Push old task's PC
  253. STMFD SP!, {R3-R12,LR} @; Push old task's LR,R12-R3
  254. LDMFD R1, {R1-R3}
  255. STMFD SP!, {R1-R3} @; Push old task's R2-R0
  256. STMFD SP!, {R0} @; Push old task's CPSR
  257. LDR R4, =rt_interrupt_from_thread
  258. LDR R5, [R4] @; R5 = stack ptr in old tasks's TCB
  259. STR SP, [R5] @; Store SP in preempted tasks's TCB
  260. LDR R6, =rt_interrupt_to_thread
  261. LDR R6, [R6] @; R6 = stack ptr in new tasks's TCB
  262. LDR SP, [R6] @; Get new task's stack pointer
  263. LDMFD SP!, {R4} @; Pop new task's SPSR
  264. MSR SPSR_cxsf, R4
  265. LDMFD SP!, {R0-R12,LR,PC}^ @; pop new task's R0-R12,LR & PC SPSR 2 CPSR