start_gcc.S 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * File : start_gcc.S
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2013-2014, 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, 0x00000000
  34. .equ SVC_Stack_Size, 0x00000100
  35. .equ ABT_Stack_Size, 0x00000000
  36. .equ RT_FIQ_STACK_PGSZ, 0x00000000
  37. .equ RT_IRQ_STACK_PGSZ, 0x00000100
  38. .equ USR_Stack_Size, 0x00000100
  39. #define ISR_Stack_Size (UND_Stack_Size + SVC_Stack_Size + ABT_Stack_Size + \
  40. RT_FIQ_STACK_PGSZ + RT_IRQ_STACK_PGSZ)
  41. .section .data.share.isr
  42. /* stack */
  43. .globl stack_start
  44. .globl stack_top
  45. .align 3
  46. stack_start:
  47. .rept ISR_Stack_Size
  48. .byte 0
  49. .endr
  50. stack_top:
  51. .text
  52. /* reset entry */
  53. .globl _reset
  54. _reset:
  55. /* Disable IRQ & FIQ */
  56. cpsid if
  57. /* Check for HYP mode */
  58. mrs r0, cpsr_all
  59. and r0, r0, #0x1F
  60. mov r8, #0x1A
  61. cmp r0, r8
  62. beq overHyped
  63. b continue
  64. overHyped: /* Get out of HYP mode */
  65. ldr r1, =continue
  66. msr ELR_hyp, r1
  67. mrs r1, cpsr_all
  68. and r1, r1, #0x1f ;@ CPSR_MODE_MASK
  69. orr r1, r1, #0x13 ;@ CPSR_MODE_SUPERVISOR
  70. msr SPSR_hyp, r1
  71. eret
  72. continue:
  73. /* disable smp */
  74. bl arm_smp_disable
  75. /* disable mmu */
  76. bl rt_cpu_mmu_disable
  77. /* set the cpu to SVC32 mode and disable interrupt */
  78. mrs r0, cpsr
  79. bic r0, r0, #0x1f
  80. orr r0, r0, #0x13
  81. msr cpsr_c, r0
  82. /* setup stack */
  83. bl stack_setup
  84. /* clear .bss */
  85. mov r0,#0 /* get a zero */
  86. ldr r1,=__bss_start /* bss start */
  87. ldr r2,=__bss_end /* bss end */
  88. bss_loop:
  89. cmp r1,r2 /* check if data to clear */
  90. strlo r0,[r1],#4 /* clear 4 bytes */
  91. blo bss_loop /* loop until done */
  92. /* start RT-Thread Kernel */
  93. ldr pc, _rtthread_startup
  94. _rtthread_startup:
  95. .word rtthread_startup
  96. stack_setup:
  97. ldr r0, =stack_top
  98. @ Set the startup stack for svc
  99. mov sp, r0
  100. sub r0, r0, #SVC_Stack_Size
  101. @ Enter Undefined Instruction Mode and set its Stack Pointer
  102. msr cpsr_c, #Mode_UND|I_Bit|F_Bit
  103. mov sp, r0
  104. sub r0, r0, #UND_Stack_Size
  105. @ Enter Abort Mode and set its Stack Pointer
  106. msr cpsr_c, #Mode_ABT|I_Bit|F_Bit
  107. mov sp, r0
  108. sub r0, r0, #ABT_Stack_Size
  109. @ Enter FIQ Mode and set its Stack Pointer
  110. msr cpsr_c, #Mode_FIQ|I_Bit|F_Bit
  111. mov sp, r0
  112. sub r0, r0, #RT_FIQ_STACK_PGSZ
  113. @ Enter IRQ Mode and set its Stack Pointer
  114. msr cpsr_c, #Mode_IRQ|I_Bit|F_Bit
  115. mov sp, r0
  116. sub r0, r0, #RT_IRQ_STACK_PGSZ
  117. /* come back to SVC mode */
  118. msr cpsr_c, #Mode_SVC|I_Bit|F_Bit
  119. bx lr
  120. .text
  121. ;@ void arm_smp_enable(void);
  122. .globl arm_smp_enable
  123. arm_smp_enable:
  124. mrc p15, 0, r0, c1, c0, 1 ;@ set SMP bit in ACTLR
  125. orr r0, r0, #0x40
  126. mcr p15, 0, r0, c1, c0, 1
  127. bx lr
  128. .text
  129. ;@ void arm_smp_disable(void);
  130. .globl arm_smp_disable
  131. arm_smp_disable:
  132. mrc p15, 0, r0, c1, c0, 1 ;@ clear SMP bit in ACTLR
  133. bic r0, r0, #0x40
  134. mcr p15, 0, r0, c1, c0, 1
  135. bx lr
  136. /* exception handlers: undef, swi, padt, dabt, resv, irq, fiq */
  137. .section .text.isr, "ax"
  138. .align 5
  139. .globl vector_fiq
  140. vector_fiq:
  141. stmfd sp!,{r0-r7,lr}
  142. bl rt_hw_trap_fiq
  143. ldmfd sp!,{r0-r7,lr}
  144. subs pc, lr, #4
  145. .globl rt_interrupt_enter
  146. .globl rt_interrupt_leave
  147. .globl rt_thread_switch_interrupt_flag
  148. .globl rt_interrupt_from_thread
  149. .globl rt_interrupt_to_thread
  150. .globl rt_current_thread
  151. .globl vmm_thread
  152. .globl vmm_virq_check
  153. .align 5
  154. .globl vector_irq
  155. vector_irq:
  156. stmfd sp!, {r0-r12,lr}
  157. bl rt_interrupt_enter
  158. bl rt_hw_trap_irq
  159. bl rt_interrupt_leave
  160. @ if rt_thread_switch_interrupt_flag set, jump to
  161. @ rt_hw_context_switch_interrupt_do and don't return
  162. ldr r0, =rt_thread_switch_interrupt_flag
  163. ldr r1, [r0]
  164. cmp r1, #1
  165. beq rt_hw_context_switch_interrupt_do
  166. ldmfd sp!, {r0-r12,lr}
  167. subs pc, lr, #4
  168. rt_hw_context_switch_interrupt_do:
  169. mov r1, #0 @ clear flag
  170. str r1, [r0]
  171. mov r1, sp @ r1 point to {r0-r3} in stack
  172. add sp, sp, #4*4
  173. ldmfd sp!, {r4-r12,lr}@ reload saved registers
  174. mrs r0, spsr @ get cpsr of interrupt thread
  175. sub r2, lr, #4 @ save old task's pc to r2
  176. @ Switch to SVC mode with no interrupt. If the usr mode guest is
  177. @ interrupted, this will just switch to the stack of kernel space.
  178. @ save the registers in kernel space won't trigger data abort.
  179. msr cpsr_c, #I_Bit|F_Bit|Mode_SVC
  180. stmfd sp!, {r2} @ push old task's pc
  181. stmfd sp!, {r4-r12,lr}@ push old task's lr,r12-r4
  182. ldmfd r1, {r1-r4} @ restore r0-r3 of the interrupt thread
  183. stmfd sp!, {r1-r4} @ push old task's r0-r3
  184. stmfd sp!, {r0} @ push old task's cpsr
  185. ldr r4, =rt_interrupt_from_thread
  186. ldr r5, [r4]
  187. str sp, [r5] @ store sp in preempted tasks's TCB
  188. ldr r6, =rt_interrupt_to_thread
  189. ldr r6, [r6]
  190. ldr sp, [r6] @ get new task's stack pointer
  191. ldmfd sp!, {r4} @ pop new task's cpsr to spsr
  192. msr spsr_cxsf, r4
  193. ldmfd sp!, {r0-r12,lr,pc}^ @ pop new task's r0-r12,lr & pc, copy spsr to cpsr
  194. .macro push_svc_reg
  195. sub sp, sp, #17 * 4 @/* Sizeof(struct rt_hw_exp_stack) */
  196. stmia sp, {r0 - r12} @/* Calling r0-r12 */
  197. mov r0, sp
  198. mrs r6, spsr @/* Save CPSR */
  199. str lr, [r0, #15*4] @/* Push PC */
  200. str r6, [r0, #16*4] @/* Push CPSR */
  201. cps #Mode_SVC
  202. str sp, [r0, #13*4] @/* Save calling SP */
  203. str lr, [r0, #14*4] @/* Save calling PC */
  204. .endm
  205. .align 5
  206. .globl vector_swi
  207. vector_swi:
  208. push_svc_reg
  209. bl rt_hw_trap_swi
  210. b .
  211. .align 5
  212. .globl vector_undef
  213. vector_undef:
  214. push_svc_reg
  215. bl rt_hw_trap_undef
  216. b .
  217. .align 5
  218. .globl vector_pabt
  219. vector_pabt:
  220. push_svc_reg
  221. bl rt_hw_trap_pabt
  222. b .
  223. .align 5
  224. .globl vector_dabt
  225. vector_dabt:
  226. push_svc_reg
  227. bl rt_hw_trap_dabt
  228. b .
  229. .align 5
  230. .globl vector_resv
  231. vector_resv:
  232. push_svc_reg
  233. bl rt_hw_trap_resv
  234. b .